blob: 7d2efcac42a2f474bce6a507fcea7fea4a596da3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar438d1762018-09-30 17:11:48 +020020// The current cmdline_info. It is initialized in getcmdline() and after that
21// used by other functions. When invoking getcmdline() recursively it needs
22// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#ifdef FEAT_EVAL
26static int new_cmdpos; /* position set by set_cmdline_pos() */
27#endif
28
Bram Moolenaara92522f2017-07-15 15:21:38 +020029static int extra_char = NUL; /* extra character to display when redrawing
30 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020031static int extra_char_shift;
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_RIGHTLEFT
34static int cmd_hkmap = 0; /* Hebrew mapping during command line */
35#endif
36
Bram Moolenaar438d1762018-09-30 17:11:48 +020037static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010038static int cmdline_charsize(int idx);
39static void set_cmdspos(void);
40static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020044static void save_cmdline(cmdline_info_T *ccp);
45static void restore_cmdline(cmdline_info_T *ccp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010050static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010051static int ccheck_abbr(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +020054static int open_cmdwin(void);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +010055
56static int cedit_key INIT(= -1); // key value of 'cedit' option
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020059
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020060 static void
61trigger_cmd_autocmd(int typechar, int evt)
62{
63 char_u typestr[2];
64
65 typestr[0] = typechar;
66 typestr[1] = NUL;
67 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
68}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020071 * Abandon the command line.
72 */
73 static void
74abandon_cmdline(void)
75{
Bram Moolenaard23a8232018-02-10 18:45:26 +010076 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020077 if (msg_scrolled == 0)
78 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010079 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020080 redraw_cmdline = TRUE;
81}
82
Bram Moolenaaree219b02017-12-17 14:55:01 +010083#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020084/*
Bram Moolenaar66216052017-12-16 16:33:44 +010085 * Guess that the pattern matches everything. Only finds specific cases, such
86 * as a trailing \|, which can happen while typing a pattern.
87 */
88 static int
89empty_pattern(char_u *p)
90{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010091 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010092
93 /* remove trailing \v and the like */
94 while (n >= 2 && p[n - 2] == '\\'
95 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
96 n -= 2;
97 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
98}
99
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200100// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200101typedef struct {
102 colnr_T vs_curswant;
103 colnr_T vs_leftcol;
104 linenr_T vs_topline;
105# ifdef FEAT_DIFF
106 int vs_topfill;
107# endif
108 linenr_T vs_botline;
109 linenr_T vs_empty_rows;
110} viewstate_T;
111
112 static void
113save_viewstate(viewstate_T *vs)
114{
115 vs->vs_curswant = curwin->w_curswant;
116 vs->vs_leftcol = curwin->w_leftcol;
117 vs->vs_topline = curwin->w_topline;
118# ifdef FEAT_DIFF
119 vs->vs_topfill = curwin->w_topfill;
120# endif
121 vs->vs_botline = curwin->w_botline;
122 vs->vs_empty_rows = curwin->w_empty_rows;
123}
124
125 static void
126restore_viewstate(viewstate_T *vs)
127{
128 curwin->w_curswant = vs->vs_curswant;
129 curwin->w_leftcol = vs->vs_leftcol;
130 curwin->w_topline = vs->vs_topline;
131# ifdef FEAT_DIFF
132 curwin->w_topfill = vs->vs_topfill;
133# endif
134 curwin->w_botline = vs->vs_botline;
135 curwin->w_empty_rows = vs->vs_empty_rows;
136}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200137
138// Struct to store the state of 'incsearch' highlighting.
139typedef struct {
140 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200141 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200142 viewstate_T init_viewstate;
143 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200144 pos_T match_start;
145 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200146 int did_incsearch;
147 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200148 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200149} incsearch_state_T;
150
151 static void
152init_incsearch_state(incsearch_state_T *is_state)
153{
154 is_state->match_start = curwin->w_cursor;
155 is_state->did_incsearch = FALSE;
156 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200157 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200158 CLEAR_POS(&is_state->match_end);
159 is_state->save_cursor = curwin->w_cursor; // may be restored later
160 is_state->search_start = curwin->w_cursor;
161 save_viewstate(&is_state->init_viewstate);
162 save_viewstate(&is_state->old_viewstate);
163}
164
165/*
166 * First move cursor to end of match, then to the start. This
167 * moves the whole match onto the screen when 'nowrap' is set.
168 */
169 static void
170set_search_match(pos_T *t)
171{
172 t->lnum += search_match_lines;
173 t->col = search_match_endcol;
174 if (t->lnum > curbuf->b_ml.ml_line_count)
175 {
176 t->lnum = curbuf->b_ml.ml_line_count;
177 coladvance((colnr_T)MAXCOL);
178 }
179}
180
181/*
182 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200183 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200184 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200185 */
186 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200187do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
188 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200189{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200190 char_u *cmd;
191 cmdmod_T save_cmdmod = cmdmod;
192 char_u *p;
193 int delim_optional = FALSE;
194 int delim;
195 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100196 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200197 exarg_T ea;
198 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200199 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200200
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200201 *skiplen = 0;
202 *patlen = ccline.cmdlen;
203
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200204 if (!p_is || cmd_silent)
205 return FALSE;
206
207 // by default search all lines
208 search_first_line = 0;
209 search_last_line = MAXLNUM;
210
211 if (firstc == '/' || firstc == '?')
212 return TRUE;
213 if (firstc != ':')
214 return FALSE;
215
216 vim_memset(&ea, 0, sizeof(ea));
217 ea.line1 = 1;
218 ea.line2 = 1;
219 ea.cmd = ccline.cmdbuff;
220 ea.addr_type = ADDR_LINES;
221
222 parse_command_modifiers(&ea, &dummy, TRUE);
223 cmdmod = save_cmdmod;
224
225 cmd = skip_range(ea.cmd, NULL);
226 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
227 return FALSE;
228
229 // Skip over "substitute" to find the pattern separator.
230 for (p = cmd; ASCII_ISALPHA(*p); ++p)
231 ;
232 if (*skipwhite(p) == NUL)
233 return FALSE;
234
235 if (STRNCMP(cmd, "substitute", p - cmd) == 0
236 || STRNCMP(cmd, "smagic", p - cmd) == 0
237 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
238 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200239 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200240 if (*cmd == 's' && cmd[1] == 'm')
241 p_magic = TRUE;
242 else if (*cmd == 's' && cmd[1] == 'n')
243 p_magic = FALSE;
244 }
245 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
246 {
247 // skip over flags
248 while (ASCII_ISALPHA(*(p = skipwhite(p))))
249 ++p;
250 if (*p == NUL)
251 return FALSE;
252 }
253 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
254 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
255 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
256 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
257 || STRNCMP(cmd, "global", p - cmd) == 0)
258 {
259 // skip over "!"
260 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200261 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200262 p++;
263 if (*skipwhite(p) == NUL)
264 return FALSE;
265 }
266 if (*cmd != 'g')
267 delim_optional = TRUE;
268 }
269 else
270 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200271
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272 p = skipwhite(p);
273 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
274 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200275
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200276 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200277
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200278 if (end == p && !use_last_pat)
279 return FALSE;
280
281 // Don't do 'hlsearch' highlighting if the pattern matches everything.
282 if (!use_last_pat)
283 {
284 char c = *end;
285 int empty;
286
287 *end = NUL;
288 empty = empty_pattern(p);
289 *end = c;
290 if (empty)
291 return FALSE;
292 }
293
294 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200295 *skiplen = (int)(p - ccline.cmdbuff);
296 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200297
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200298 // parse the address range
299 save_cursor = curwin->w_cursor;
300 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200301 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200302 if (ea.addr_count > 0)
303 {
304 // Allow for reverse match.
305 if (ea.line2 < ea.line1)
306 {
307 search_first_line = ea.line2;
308 search_last_line = ea.line1;
309 }
310 else
311 {
312 search_first_line = ea.line1;
313 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200314 }
315 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200316 else if (cmd[0] == 's' && cmd[1] != 'o')
317 {
318 // :s defaults to the current line
319 search_first_line = curwin->w_cursor.lnum;
320 search_last_line = curwin->w_cursor.lnum;
321 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200322
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200323 curwin->w_cursor = save_cursor;
324 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200325}
326
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200327 static void
328finish_incsearch_highlighting(
329 int gotesc,
330 incsearch_state_T *is_state,
331 int call_update_screen)
332{
333 if (is_state->did_incsearch)
334 {
335 is_state->did_incsearch = FALSE;
336 if (gotesc)
337 curwin->w_cursor = is_state->save_cursor;
338 else
339 {
340 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
341 {
342 // put the '" mark at the original position
343 curwin->w_cursor = is_state->save_cursor;
344 setpcmark();
345 }
346 curwin->w_cursor = is_state->search_start;
347 }
348 restore_viewstate(&is_state->old_viewstate);
349 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200350
351 // by default search all lines
352 search_first_line = 0;
353 search_last_line = MAXLNUM;
354
355 p_magic = is_state->magic_save;
356
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200357 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200358 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200359 if (call_update_screen)
360 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200361 }
362}
363
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200364/*
365 * Do 'incsearch' highlighting if desired.
366 */
367 static void
368may_do_incsearch_highlighting(
369 int firstc,
370 long count,
371 incsearch_state_T *is_state)
372{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200373 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200374 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200375 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200376#ifdef FEAT_RELTIME
377 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200378 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200379#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200380 int next_char;
381 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200382 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200383
Bram Moolenaar198cb662018-09-06 21:44:17 +0200384 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100385 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200386 save_last_search_pattern();
387
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200388 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200389 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200390 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200391 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200392 if (did_do_incsearch && vpeekc() == NUL)
393 // may have skipped a redraw, do it now
394 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200395 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200396 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200397
398 // If there is a character waiting, search and redraw later.
399 if (char_avail())
400 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200401 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402 is_state->incsearch_postponed = TRUE;
403 return;
404 }
405 is_state->incsearch_postponed = FALSE;
406
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200407 if (search_first_line == 0)
408 // start at the original cursor position
409 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100410 else if (search_first_line > curbuf->b_ml.ml_line_count)
411 {
412 // start after the last line
413 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
414 curwin->w_cursor.col = MAXCOL;
415 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200416 else
417 {
418 // start at the first line in the range
419 curwin->w_cursor.lnum = search_first_line;
420 curwin->w_cursor.col = 0;
421 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200423 // Use the previous pattern for ":s//".
424 next_char = ccline.cmdbuff[skiplen + patlen];
425 use_last_pat = patlen == 0 && skiplen > 0
426 && ccline.cmdbuff[skiplen - 1] == next_char;
427
428 // If there is no pattern, don't do anything.
429 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200430 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200431 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200432 set_no_hlsearch(TRUE); // turn off previous highlight
433 redraw_all_later(SOME_VALID);
434 }
435 else
436 {
437 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
438
439 cursor_off(); // so the user knows we're busy
440 out_flush();
441 ++emsg_off; // so it doesn't beep if bad expr
442#ifdef FEAT_RELTIME
443 // Set the time limit to half a second.
444 profile_setlimit(500L, &tm);
445#endif
446 if (!p_hls)
447 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200448 if (search_first_line != 0)
449 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200450 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200451#ifdef FEAT_RELTIME
452 vim_memset(&sia, 0, sizeof(sia));
453 sia.sa_tm = &tm;
454#endif
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200455 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200456 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200458 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200459#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200460 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200461#endif
462 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200463 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464 --emsg_off;
465
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466 if (curwin->w_cursor.lnum < search_first_line
467 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200468 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200469 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200470 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200471 curwin->w_cursor = is_state->search_start;
472 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200473
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200474 // if interrupted while searching, behave like it failed
475 if (got_int)
476 {
477 (void)vpeekc(); // remove <C-C> from input stream
478 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200479 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200480 }
481 else if (char_avail())
482 // cancelled searching because a char was typed
483 is_state->incsearch_postponed = TRUE;
484 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200485 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200486 highlight_match = TRUE; // highlight position
487 else
488 highlight_match = FALSE; // remove highlight
489
490 // First restore the old curwin values, so the screen is positioned in the
491 // same way as the actual search command.
492 restore_viewstate(&is_state->old_viewstate);
493 changed_cline_bef_curs();
494 update_topline();
495
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200496 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200497 {
498 pos_T save_pos = curwin->w_cursor;
499
500 is_state->match_start = curwin->w_cursor;
501 set_search_match(&curwin->w_cursor);
502 validate_cursor();
503 end_pos = curwin->w_cursor;
504 is_state->match_end = end_pos;
505 curwin->w_cursor = save_pos;
506 }
507 else
508 end_pos = curwin->w_cursor; // shutup gcc 4
509
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200510 // Disable 'hlsearch' highlighting if the pattern matches everything.
511 // Avoids a flash when typing "foo\|".
512 if (!use_last_pat)
513 {
514 next_char = ccline.cmdbuff[skiplen + patlen];
515 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200516 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
517 {
518 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200519 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200520 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200521 ccline.cmdbuff[skiplen + patlen] = next_char;
522 }
523
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200524 validate_cursor();
525 // May redraw the status line to show the cursor position.
526 if (p_ru && curwin->w_status_height > 0)
527 curwin->w_redr_status = TRUE;
528
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200530 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200531 restore_last_search_pattern();
532
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200533 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
534 // end of the pattern, e.g. for ":s/pat/".
535 if (ccline.cmdbuff[skiplen + patlen] != NUL)
536 curwin->w_cursor = is_state->search_start;
537 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200538 curwin->w_cursor = end_pos;
539
540 msg_starthere();
541 redrawcmdline();
542 is_state->did_incsearch = TRUE;
543}
544
545/*
546 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
547 * or previous match.
548 * Returns FAIL when jumping to cmdline_not_changed;
549 */
550 static int
551may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200552 int firstc,
553 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200554 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200555 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200556{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200557 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200558 pos_T t;
559 char_u *pat;
560 int search_flags = SEARCH_NOOF;
561 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563
Bram Moolenaar198cb662018-09-06 21:44:17 +0200564 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100565 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200566 save_last_search_pattern();
567
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200568 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200569 {
570 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200571 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200572 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200574 {
575 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200577 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200579 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200580 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200581 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200582 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200583 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200584 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200585 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200586 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200588 cursor_off();
589 out_flush();
590 if (c == Ctrl_G)
591 {
592 t = is_state->match_end;
593 if (LT_POS(is_state->match_start, is_state->match_end))
594 // Start searching at the end of the match not at the beginning of
595 // the next column.
596 (void)decl(&t);
597 search_flags += SEARCH_COL;
598 }
599 else
600 t = is_state->match_start;
601 if (!p_hls)
602 search_flags += SEARCH_KEEP;
603 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200604 save = pat[patlen];
605 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100606 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200608 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200610 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200611 if (i)
612 {
613 is_state->search_start = is_state->match_start;
614 is_state->match_end = t;
615 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200616 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200617 {
618 // Move just before 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)decl(&is_state->search_start);
622 }
623 else if (c == Ctrl_G && firstc == '?')
624 {
625 // Move just after the current match, so that when nv_search
626 // finishes the cursor will be put back on the match.
627 is_state->search_start = t;
628 (void)incl(&is_state->search_start);
629 }
630 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
631 {
632 // wrap around
633 is_state->search_start = t;
634 if (firstc == '?')
635 (void)incl(&is_state->search_start);
636 else
637 (void)decl(&is_state->search_start);
638 }
639
640 set_search_match(&is_state->match_end);
641 curwin->w_cursor = is_state->match_start;
642 changed_cline_bef_curs();
643 update_topline();
644 validate_cursor();
645 highlight_match = TRUE;
646 save_viewstate(&is_state->old_viewstate);
647 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200648 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200649 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200650 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 }
652 else
653 vim_beep(BO_ERROR);
654 restore_last_search_pattern();
655 return FAIL;
656}
657
658/*
659 * When CTRL-L typed: add character from the match to the pattern.
660 * May set "*c" to the added character.
661 * Return OK when jumping to cmdline_not_changed.
662 */
663 static int
664may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
665{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200666 int skiplen, patlen;
667
Bram Moolenaar198cb662018-09-06 21:44:17 +0200668 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100669 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200670 save_last_search_pattern();
671
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200672 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200673 {
674 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200675 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200676 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100677 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678
679 // Add a character from under the cursor for 'incsearch'.
680 if (is_state->did_incsearch)
681 {
682 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200683 *c = gchar_cursor();
684 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200685 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200686 // If 'ignorecase' and 'smartcase' are set and the
687 // command line has no uppercase characters, convert
688 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200689 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200691 if (*c == firstc || vim_strchr((char_u *)(
692 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200693 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200694 // put a backslash before special characters
695 stuffcharReadbuff(*c);
696 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200697 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200698 // add any composing characters
699 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
700 {
701 int save_c = *c;
702
703 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
704 {
705 curwin->w_cursor.col += mb_char2len(*c);
706 *c = gchar_cursor();
707 stuffcharReadbuff(*c);
708 }
709 *c = save_c;
710 }
711 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200712 }
713 }
714 return OK;
715}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200716#endif
717
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200718#ifdef FEAT_ARABIC
719/*
720 * Return TRUE if the command line has an Arabic character at or after "start"
721 * for "len" bytes.
722 */
723 static int
724cmdline_has_arabic(int start, int len)
725{
726 int j;
727 int mb_l;
728 int u8c;
729 char_u *p;
730 int u8cc[MAX_MCO];
731
732 if (!enc_utf8)
733 return FALSE;
734
735 for (j = start; j < start + len; j += mb_l)
736 {
737 p = ccline.cmdbuff + j;
738 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
739 mb_l = utfc_ptr2len_len(p, start + len - j);
740 if (ARABIC_CHAR(u8c))
741 return TRUE;
742 }
743 return FALSE;
744}
745#endif
746
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200747 void
748cmdline_init(void)
749{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200750 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200751}
752
Bram Moolenaar66216052017-12-16 16:33:44 +0100753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * getcmdline() - accept a command line starting with firstc.
755 *
756 * firstc == ':' get ":" command line.
757 * firstc == '/' or '?' get search pattern
758 * firstc == '=' get expression
759 * firstc == '@' get text for input() function
760 * firstc == '>' get text for debug mode
761 * firstc == NUL get text for :insert command
762 * firstc == -1 like NUL, and break on CTRL-C
763 *
764 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
765 * command line.
766 *
767 * Careful: getcmdline() can be called recursively!
768 *
769 * Return pointer to allocated string if there is a commandline, NULL
770 * otherwise.
771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100773getcmdline(
774 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200775 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200776 int indent, // indent for inside conditionals
777 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200778{
779 return getcmdline_int(firstc, count, indent, TRUE);
780}
781
782 static char_u *
783getcmdline_int(
784 int firstc,
785 long count UNUSED, // only used for incremental search
786 int indent, // indent for inside conditionals
787 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
789 int c;
790 int i;
791 int j;
792 int gotesc = FALSE; /* TRUE when <ESC> just typed */
793 int do_abbr; /* when TRUE check for abbr. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 char_u *lookfor = NULL; /* string to match */
795 int hiscnt; /* current history line in use */
796 int histype; /* history type to be used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200798 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799#endif
800 int did_wild_list = FALSE; /* did wild_list() recently */
801 int wim_index = 0; /* index in wim_flags[] */
802 int res;
803 int save_msg_scroll = msg_scroll;
804 int save_State = State; /* remember State when called */
805 int some_key_typed = FALSE; /* one of the keys was typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 /* mouse drag and release events are ignored, unless they are
807 * preceded with a mouse down event */
808 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#ifdef FEAT_EVAL
810 int break_ctrl_c = FALSE;
811#endif
812 expand_T xpc;
813 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200814 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200815 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200816 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
Bram Moolenaar438d1762018-09-30 17:11:48 +0200818 if (ccline.cmdbuff != NULL)
819 {
820 // Being called recursively. Since ccline is global, we need to save
821 // the current buffer and restore it when returning.
822 save_cmdline(&save_ccline);
823 did_save_ccline = TRUE;
824 }
825 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200826 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_EVAL
829 if (firstc == -1)
830 {
831 firstc = NUL;
832 break_ctrl_c = TRUE;
833 }
834#endif
835#ifdef FEAT_RIGHTLEFT
836 /* start without Hebrew mapping for a command line */
837 if (firstc == ':' || firstc == '=' || firstc == '>')
838 cmd_hkmap = 0;
839#endif
840
841 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200844 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif
846
847 /*
848 * set some variables for redrawcmd()
849 */
850 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000851 ccline.cmdindent = (firstc > 0 ? indent : 0);
852
853 /* alloc initial ccline.cmdbuff */
854 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200856 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 ccline.cmdlen = ccline.cmdpos = 0;
858 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100859 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000861 /* autoindent for :insert and :append */
862 if (firstc <= 0)
863 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200864 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000865 ccline.cmdbuff[indent] = NUL;
866 ccline.cmdpos = indent;
867 ccline.cmdspos = indent;
868 ccline.cmdlen = indent;
869 }
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000872 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874#ifdef FEAT_RIGHTLEFT
875 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
876 && (firstc == '/' || firstc == '?'))
877 cmdmsg_rl = TRUE;
878 else
879 cmdmsg_rl = FALSE;
880#endif
881
882 redir_off = TRUE; /* don't redirect the typed command */
883 if (!cmd_silent)
884 {
885 i = msg_scrolled;
886 msg_scrolled = 0; /* avoid wait_return message */
887 gotocmdline(TRUE);
888 msg_scrolled += i;
889 redrawcmdprompt(); /* draw prompt or indent */
890 set_cmdspos();
891 }
892 xpc.xp_context = EXPAND_NOTHING;
893 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000894#ifndef BACKSLASH_IN_FILENAME
895 xpc.xp_shell = FALSE;
896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000898#if defined(FEAT_EVAL)
899 if (ccline.input_fn)
900 {
901 xpc.xp_context = ccline.xp_context;
902 xpc.xp_pattern = ccline.cmdbuff;
903 xpc.xp_arg = ccline.xp_arg;
904 }
905#endif
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 /*
908 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
909 * doing ":@0" when register 0 doesn't contain a CR.
910 */
911 msg_scroll = FALSE;
912
913 State = CMDLINE;
914
915 if (firstc == '/' || firstc == '?' || firstc == '@')
916 {
917 /* Use ":lmap" mappings for search pattern and input(). */
918 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
919 b_im_ptr = &curbuf->b_p_iminsert;
920 else
921 b_im_ptr = &curbuf->b_p_imsearch;
922 if (*b_im_ptr == B_IMODE_LMAP)
923 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100924#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 im_set_active(*b_im_ptr == B_IMODE_IM);
926#endif
927 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100928#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else if (p_imcmdline)
930 im_set_active(TRUE);
931#endif
932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#ifdef CURSOR_SHAPE
935 ui_cursor_shape(); /* may show different cursor shape */
936#endif
937
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000938 /* When inside an autocommand for writing "exiting" may be set and
939 * terminal mode set to cooked. Need to set raw mode here then. */
940 settmode(TMODE_RAW);
941
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200942 /* Trigger CmdlineEnter autocommands. */
943 cmdline_type = firstc == NUL ? '-' : firstc;
944 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200945
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200947 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000951 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#endif
953
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200954 /* If something above caused an error, reset the flags, we do want to type
955 * and execute commands. Display may be messed up a bit. */
956 if (did_emsg)
957 redrawcmd();
958 did_emsg = FALSE;
959 got_int = FALSE;
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 /*
962 * Collect the command string, handling editing keys.
963 */
964 for (;;)
965 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000966 redir_off = TRUE; /* Don't redirect the typed command.
967 Repeated, because a ":redir" inside
968 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#ifdef USE_ON_FLY_SCROLL
970 dont_scroll = FALSE; /* allow scrolling here */
971#endif
972 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
973
Bram Moolenaar72532d32018-04-07 19:09:09 +0200974 did_emsg = FALSE; /* There can't really be a reason why an error
975 that occurs while typing a command should
976 cause the command not to be executed. */
977
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200978 // Trigger SafeState if nothing is pending.
979 may_trigger_safestate(xpc.xp_numfiles <= 0);
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000982
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100983 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
984 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000985 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000986 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100987 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (KeyTyped)
990 {
991 some_key_typed = TRUE;
992#ifdef FEAT_RIGHTLEFT
993 if (cmd_hkmap)
994 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (cmdmsg_rl && !KeyStuffed)
996 {
997 /* Invert horizontal movements and operations. Only when
998 * typed by the user directly, not when the result of a
999 * mapping. */
1000 switch (c)
1001 {
1002 case K_RIGHT: c = K_LEFT; break;
1003 case K_S_RIGHT: c = K_S_LEFT; break;
1004 case K_C_RIGHT: c = K_C_LEFT; break;
1005 case K_LEFT: c = K_RIGHT; break;
1006 case K_S_LEFT: c = K_S_RIGHT; break;
1007 case K_C_LEFT: c = K_C_RIGHT; break;
1008 }
1009 }
1010#endif
1011 }
1012
1013 /*
1014 * Ignore got_int when CTRL-C was typed here.
1015 * Don't ignore it in :global, we really need to break then, e.g., for
1016 * ":g/pat/normal /pat" (without the <CR>).
1017 * Don't ignore it for the input() function.
1018 */
1019 if ((c == Ctrl_C
1020#ifdef UNIX
1021 || c == intr_char
1022#endif
1023 )
1024#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1025 && firstc != '@'
1026#endif
1027#ifdef FEAT_EVAL
1028 && !break_ctrl_c
1029#endif
1030 && !global_busy)
1031 got_int = FALSE;
1032
Bram Moolenaard7663c22019-08-06 21:59:57 +02001033 // free old command line when finished moving around in the history
1034 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001036 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001037 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 && c != K_PAGEDOWN && c != K_PAGEUP
1039 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001040 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001042 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001045 * When there are matching completions to select <S-Tab> works like
1046 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001048 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 c = Ctrl_P;
1050
1051#ifdef FEAT_WILDMENU
1052 /* Special translations for 'wildmenu' */
1053 if (did_wild_list && p_wmnu)
1054 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001055 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001057 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 c = Ctrl_N;
1059 }
1060 /* Hitting CR after "emenu Name.": complete submenu */
1061 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1062 && ccline.cmdpos > 1
1063 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1064 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1065 && (c == '\n' || c == '\r' || c == K_KENTER))
1066 c = K_DOWN;
1067#endif
1068
1069 /* free expanded names when finished walking through matches */
1070 if (xpc.xp_numfiles != -1
1071 && !(c == p_wc && KeyTyped) && c != p_wcm
1072 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1073 && c != Ctrl_L)
1074 {
1075 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1076 did_wild_list = FALSE;
1077#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001078 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#endif
1080 xpc.xp_context = EXPAND_NOTHING;
1081 wim_index = 0;
1082#ifdef FEAT_WILDMENU
1083 if (p_wmnu && wild_menu_showing != 0)
1084 {
1085 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001086 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001087
1088 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001089 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 if (wild_menu_showing == WM_SCROLLED)
1092 {
1093 /* Entered command line, move it up */
1094 cmdline_row--;
1095 redrawcmd();
1096 }
1097 else if (save_p_ls != -1)
1098 {
1099 /* restore 'laststatus' and 'winminheight' */
1100 p_ls = save_p_ls;
1101 p_wmh = save_p_wmh;
1102 last_status(FALSE);
1103 update_screen(VALID); /* redraw the screen NOW */
1104 redrawcmd();
1105 save_p_ls = -1;
1106 }
1107 else
1108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 redraw_statuslines();
1111 }
1112 KeyTyped = skt;
1113 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001114 if (ccline.input_fn)
1115 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 }
1117#endif
1118 }
1119
1120#ifdef FEAT_WILDMENU
1121 /* Special translations for 'wildmenu' */
1122 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1123 {
1124 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001125 if (c == K_DOWN && ccline.cmdpos > 0
1126 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001128 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
1130 /* Hitting <Up>: Remove one submenu name in front of the
1131 * cursor */
1132 int found = FALSE;
1133
1134 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1135 i = 0;
1136 while (--j > 0)
1137 {
1138 /* check for start of menu name */
1139 if (ccline.cmdbuff[j] == ' '
1140 && ccline.cmdbuff[j - 1] != '\\')
1141 {
1142 i = j + 1;
1143 break;
1144 }
1145 /* check for start of submenu name */
1146 if (ccline.cmdbuff[j] == '.'
1147 && ccline.cmdbuff[j - 1] != '\\')
1148 {
1149 if (found)
1150 {
1151 i = j + 1;
1152 break;
1153 }
1154 else
1155 found = TRUE;
1156 }
1157 }
1158 if (i > 0)
1159 cmdline_del(i);
1160 c = p_wc;
1161 xpc.xp_context = EXPAND_NOTHING;
1162 }
1163 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001164 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001165 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001166 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168 char_u upseg[5];
1169
1170 upseg[0] = PATHSEP;
1171 upseg[1] = '.';
1172 upseg[2] = '.';
1173 upseg[3] = PATHSEP;
1174 upseg[4] = NUL;
1175
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001176 if (c == K_DOWN
1177 && ccline.cmdpos > 0
1178 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1179 && (ccline.cmdpos < 3
1180 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1182 {
1183 /* go down a directory */
1184 c = p_wc;
1185 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001186 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
1188 /* If in a direct ancestor, strip off one ../ to go down */
1189 int found = FALSE;
1190
1191 j = ccline.cmdpos;
1192 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1193 while (--j > i)
1194 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001195 if (has_mbyte)
1196 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (vim_ispathsep(ccline.cmdbuff[j]))
1198 {
1199 found = TRUE;
1200 break;
1201 }
1202 }
1203 if (found
1204 && ccline.cmdbuff[j - 1] == '.'
1205 && ccline.cmdbuff[j - 2] == '.'
1206 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1207 {
1208 cmdline_del(j - 2);
1209 c = p_wc;
1210 }
1211 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001212 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
1214 /* go up a directory */
1215 int found = FALSE;
1216
1217 j = ccline.cmdpos - 1;
1218 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1219 while (--j > i)
1220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (has_mbyte)
1222 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (vim_ispathsep(ccline.cmdbuff[j])
1224#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001225 && vim_strchr((char_u *)" *?[{`$%#",
1226 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#endif
1228 )
1229 {
1230 if (found)
1231 {
1232 i = j + 1;
1233 break;
1234 }
1235 else
1236 found = TRUE;
1237 }
1238 }
1239
1240 if (!found)
1241 j = i;
1242 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1243 j += 4;
1244 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1245 && j == i)
1246 j += 3;
1247 else
1248 j = 0;
1249 if (j > 0)
1250 {
1251 /* TODO this is only for DOS/UNIX systems - need to put in
1252 * machine-specific stuff here and in upseg init */
1253 cmdline_del(j);
1254 put_on_cmdline(upseg + 1, 3, FALSE);
1255 }
1256 else if (ccline.cmdpos > i)
1257 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001258
1259 /* Now complete in the new directory. Set KeyTyped in case the
1260 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001262 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266#endif /* FEAT_WILDMENU */
1267
1268 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1269 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1270 if (c == Ctrl_BSL)
1271 {
1272 ++no_mapping;
1273 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001274 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 --no_mapping;
1276 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001277 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1278 * is in a mapping. */
1279 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001280 || (ccline.cmdfirstc == '=' && KeyTyped)
1281#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001282 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001283#endif
1284 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
1286 vungetc(c);
1287 c = Ctrl_BSL;
1288 }
1289#ifdef FEAT_EVAL
1290 else if (c == 'e')
1291 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001292 char_u *p = NULL;
1293 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295 /*
1296 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001297 * Need to save and restore the current command line, to be
1298 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 */
1300 if (ccline.cmdpos == ccline.cmdlen)
1301 new_cmdpos = 99999; /* keep it at the end */
1302 else
1303 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (c == '=')
1307 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001308 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001309 * to avoid nasty things like going to another buffer when
1310 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001311 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001313 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001314
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001315 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001317 len = (int)STRLEN(p);
1318 if (realloc_cmdbuff(len + 1) == OK)
1319 {
1320 ccline.cmdlen = len;
1321 STRCPY(ccline.cmdbuff, p);
1322 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001324 /* Restore the cursor or use the position set with
1325 * set_cmdline_pos(). */
1326 if (new_cmdpos > ccline.cmdlen)
1327 ccline.cmdpos = ccline.cmdlen;
1328 else
1329 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001331 KeyTyped = FALSE; /* Don't do p_wc completion. */
1332 redrawcmd();
1333 goto cmdline_changed;
1334 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001335 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 }
1338 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001339 got_int = FALSE; /* don't abandon the command line */
1340 did_emsg = FALSE;
1341 emsg_on_display = FALSE;
1342 redrawcmd();
1343 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345#endif
1346 else
1347 {
1348 if (c == Ctrl_G && p_im && restart_edit == 0)
1349 restart_edit = 'a';
1350 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1351 in history */
1352 goto returncmd; /* back to Normal mode */
1353 }
1354 }
1355
1356#ifdef FEAT_CMDWIN
1357 if (c == cedit_key || c == K_CMDWIN)
1358 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001359 if (ex_normal_busy == 0 && got_int == FALSE)
1360 {
1361 /*
1362 * Open a window to edit the command line (and history).
1363 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001364 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001365 some_key_typed = TRUE;
1366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368# ifdef FEAT_DIGRAPHS
1369 else
1370# endif
1371#endif
1372#ifdef FEAT_DIGRAPHS
1373 c = do_digraph(c);
1374#endif
1375
1376 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1377 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1378 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001379 /* In Ex mode a backslash escapes a newline. */
1380 if (exmode_active
1381 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001382 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001383 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001386 if (c == K_KENTER)
1387 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001389 else
1390 {
1391 gotesc = FALSE; /* Might have typed ESC previously, don't
1392 truncate the cmdline now. */
1393 if (ccheck_abbr(c + ABBR_OFF))
1394 goto cmdline_changed;
1395 if (!cmd_silent)
1396 {
1397 windgoto(msg_row, 0);
1398 out_flush();
1399 }
1400 break;
1401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403
1404 /*
1405 * Completion for 'wildchar' or 'wildcharm' key.
1406 * - hitting <ESC> twice means: abandon command line.
1407 * - wildcard expansion is only done when the 'wildchar' key is really
1408 * typed, not when it comes from a macro
1409 */
1410 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1411 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001412 int options = WILD_NO_BEEP;
1413 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1414 options |= WILD_BUFLASTUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1416 {
1417 /* if 'wildmode' contains "list" may still need to list */
1418 if (xpc.xp_numfiles > 1
1419 && !did_wild_list
1420 && (wim_flags[wim_index] & WIM_LIST))
1421 {
1422 (void)showmatches(&xpc, FALSE);
1423 redrawcmd();
1424 did_wild_list = TRUE;
1425 }
1426 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001427 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001428 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001430 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001431 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else
1433 res = OK; /* don't insert 'wildchar' now */
1434 }
1435 else /* typed p_wc first time */
1436 {
1437 wim_index = 0;
1438 j = ccline.cmdpos;
1439 /* if 'wildmode' first contains "longest", get longest
1440 * common part */
1441 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001442 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001443 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001445 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001446 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448 /* if interrupted while completing, behave like it failed */
1449 if (got_int)
1450 {
1451 (void)vpeekc(); /* remove <C-C> from input stream */
1452 got_int = FALSE; /* don't abandon the command line */
1453 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1454#ifdef FEAT_WILDMENU
1455 xpc.xp_context = EXPAND_NOTHING;
1456#endif
1457 goto cmdline_changed;
1458 }
1459
1460 /* when more than one match, and 'wildmode' first contains
1461 * "list", or no change and 'wildmode' contains "longest,list",
1462 * list all matches */
1463 if (res == OK && xpc.xp_numfiles > 1)
1464 {
1465 /* a "longest" that didn't do anything is skipped (but not
1466 * "list:longest") */
1467 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1468 wim_index = 1;
1469 if ((wim_flags[wim_index] & WIM_LIST)
1470#ifdef FEAT_WILDMENU
1471 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1472#endif
1473 )
1474 {
1475 if (!(wim_flags[0] & WIM_LONGEST))
1476 {
1477#ifdef FEAT_WILDMENU
1478 int p_wmnu_save = p_wmnu;
1479 p_wmnu = 0;
1480#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001481 /* remove match */
1482 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483#ifdef FEAT_WILDMENU
1484 p_wmnu = p_wmnu_save;
1485#endif
1486 }
1487#ifdef FEAT_WILDMENU
1488 (void)showmatches(&xpc, p_wmnu
1489 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1490#else
1491 (void)showmatches(&xpc, FALSE);
1492#endif
1493 redrawcmd();
1494 did_wild_list = TRUE;
1495 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001496 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001497 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001499 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001500 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001503 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505#ifdef FEAT_WILDMENU
1506 else if (xpc.xp_numfiles == -1)
1507 xpc.xp_context = EXPAND_NOTHING;
1508#endif
1509 }
1510 if (wim_index < 3)
1511 ++wim_index;
1512 if (c == ESC)
1513 gotesc = TRUE;
1514 if (res == OK)
1515 goto cmdline_changed;
1516 }
1517
1518 gotesc = FALSE;
1519
1520 /* <S-Tab> goes to last match, in a clumsy way */
1521 if (c == K_S_TAB && KeyTyped)
1522 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001523 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1524 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1525 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 goto cmdline_changed;
1527 }
1528
1529 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1530 c = NL;
1531
1532 do_abbr = TRUE; /* default: check for abbreviation */
1533
1534 /*
1535 * Big switch for a typed command line character.
1536 */
1537 switch (c)
1538 {
1539 case K_BS:
1540 case Ctrl_H:
1541 case K_DEL:
1542 case K_KDEL:
1543 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (c == K_KDEL)
1545 c = K_DEL;
1546
1547 /*
1548 * delete current character is the same as backspace on next
1549 * character, except at end of line
1550 */
1551 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1552 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if (has_mbyte && c == K_DEL)
1554 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1555 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (ccline.cmdpos > 0)
1557 {
1558 char_u *p;
1559
1560 j = ccline.cmdpos;
1561 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (has_mbyte)
1563 {
1564 p = mb_prevptr(ccline.cmdbuff, p);
1565 if (c == Ctrl_W)
1566 {
1567 while (p > ccline.cmdbuff && vim_isspace(*p))
1568 p = mb_prevptr(ccline.cmdbuff, p);
1569 i = mb_get_class(p);
1570 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1571 p = mb_prevptr(ccline.cmdbuff, p);
1572 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001573 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001576 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1579 --p;
1580 i = vim_iswordc(p[-1]);
1581 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1582 && vim_iswordc(p[-1]) == i)
1583 --p;
1584 }
1585 else
1586 --p;
1587 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1588 ccline.cmdlen -= j - ccline.cmdpos;
1589 i = ccline.cmdpos;
1590 while (i < ccline.cmdlen)
1591 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1592
1593 /* Truncate at the end, required for multi-byte chars. */
1594 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001595#ifdef FEAT_SEARCH_EXTRA
1596 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001597 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001598 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001599 /* save view settings, so that the screen
1600 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001601 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001602 }
1603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 redrawcmd();
1605 }
1606 else if (ccline.cmdlen == 0 && c != Ctrl_W
1607 && ccline.cmdprompt == NULL && indent == 0)
1608 {
1609 /* In ex and debug mode it doesn't make sense to return. */
1610 if (exmode_active
1611#ifdef FEAT_EVAL
1612 || ccline.cmdfirstc == '>'
1613#endif
1614 )
1615 goto cmdline_not_changed;
1616
Bram Moolenaard23a8232018-02-10 18:45:26 +01001617 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (!cmd_silent)
1619 {
1620#ifdef FEAT_RIGHTLEFT
1621 if (cmdmsg_rl)
1622 msg_col = Columns;
1623 else
1624#endif
1625 msg_col = 0;
1626 msg_putchar(' '); /* delete ':' */
1627 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001628#ifdef FEAT_SEARCH_EXTRA
1629 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001630 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 redraw_cmdline = TRUE;
1633 goto returncmd; /* back to cmd mode */
1634 }
1635 goto cmdline_changed;
1636
1637 case K_INS:
1638 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 ccline.overstrike = !ccline.overstrike;
1640#ifdef CURSOR_SHAPE
1641 ui_cursor_shape(); /* may show different cursor shape */
1642#endif
1643 goto cmdline_not_changed;
1644
1645 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001646 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
1648 /* ":lmap" mappings exists, toggle use of mappings. */
1649 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001650#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 im_set_active(FALSE); /* Disable input method */
1652#endif
1653 if (b_im_ptr != NULL)
1654 {
1655 if (State & LANGMAP)
1656 *b_im_ptr = B_IMODE_LMAP;
1657 else
1658 *b_im_ptr = B_IMODE_NONE;
1659 }
1660 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 else
1663 {
1664 /* There are no ":lmap" mappings, toggle IM. When
1665 * 'imdisable' is set don't try getting the status, it's
1666 * always off. */
1667 if ((p_imdisable && b_im_ptr != NULL)
1668 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1669 {
1670 im_set_active(FALSE); /* Disable input method */
1671 if (b_im_ptr != NULL)
1672 *b_im_ptr = B_IMODE_NONE;
1673 }
1674 else
1675 {
1676 im_set_active(TRUE); /* Enable input method */
1677 if (b_im_ptr != NULL)
1678 *b_im_ptr = B_IMODE_IM;
1679 }
1680 }
1681#endif
1682 if (b_im_ptr != NULL)
1683 {
1684 if (b_im_ptr == &curbuf->b_p_iminsert)
1685 set_iminsert_global();
1686 else
1687 set_imsearch_global();
1688 }
1689#ifdef CURSOR_SHAPE
1690 ui_cursor_shape(); /* may show different cursor shape */
1691#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001692#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 /* Show/unshow value of 'keymap' in status lines later. */
1694 status_redraw_curbuf();
1695#endif
1696 goto cmdline_not_changed;
1697
1698/* case '@': only in very old vi */
1699 case Ctrl_U:
1700 /* delete all characters left of the cursor */
1701 j = ccline.cmdpos;
1702 ccline.cmdlen -= j;
1703 i = ccline.cmdpos = 0;
1704 while (i < ccline.cmdlen)
1705 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1706 /* Truncate at the end, required for multi-byte chars. */
1707 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001708#ifdef FEAT_SEARCH_EXTRA
1709 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001710 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 redrawcmd();
1713 goto cmdline_changed;
1714
1715#ifdef FEAT_CLIPBOARD
1716 case Ctrl_Y:
1717 /* Copy the modeless selection, if there is one. */
1718 if (clip_star.state != SELECT_CLEARED)
1719 {
1720 if (clip_star.state == SELECT_DONE)
1721 clip_copy_modeless_selection(TRUE);
1722 goto cmdline_not_changed;
1723 }
1724 break;
1725#endif
1726
1727 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1728 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001729 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001730 * ":normal" runs out of characters. */
1731 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001732 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 goto cmdline_not_changed;
1734
1735 gotesc = TRUE; /* will free ccline.cmdbuff after
1736 putting it in history */
1737 goto returncmd; /* back to cmd mode */
1738
1739 case Ctrl_R: /* insert register */
1740#ifdef USE_ON_FLY_SCROLL
1741 dont_scroll = TRUE; /* disallow scrolling here */
1742#endif
1743 putcmdline('"', TRUE);
1744 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001745 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (i == Ctrl_O)
1747 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1748 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001749 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001750 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --no_mapping;
1752#ifdef FEAT_EVAL
1753 /*
1754 * Insert the result of an expression.
1755 * Need to save the current command line, to be able to enter
1756 * a new one...
1757 */
1758 new_cmdpos = -1;
1759 if (c == '=')
1760 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001761 if (ccline.cmdfirstc == '=' // can't do this recursively
1762 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
1764 beep_flush();
1765 c = ESC;
1766 }
1767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770#endif
1771 if (c != ESC) /* use ESC to cancel inserting register */
1772 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001773 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001774
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001775#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001776 /* When there was a serious error abort getting the
1777 * command line. */
1778 if (aborting())
1779 {
1780 gotesc = TRUE; /* will free ccline.cmdbuff after
1781 putting it in history */
1782 goto returncmd; /* back to cmd mode */
1783 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 KeyTyped = FALSE; /* Don't do p_wc completion. */
1786#ifdef FEAT_EVAL
1787 if (new_cmdpos >= 0)
1788 {
1789 /* set_cmdline_pos() was used */
1790 if (new_cmdpos > ccline.cmdlen)
1791 ccline.cmdpos = ccline.cmdlen;
1792 else
1793 ccline.cmdpos = new_cmdpos;
1794 }
1795#endif
1796 }
1797 redrawcmd();
1798 goto cmdline_changed;
1799
1800 case Ctrl_D:
1801 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1802 break; /* Use ^D as normal char instead */
1803
1804 redrawcmd();
1805 continue; /* don't do incremental search now */
1806
1807 case K_RIGHT:
1808 case K_S_RIGHT:
1809 case K_C_RIGHT:
1810 do
1811 {
1812 if (ccline.cmdpos >= ccline.cmdlen)
1813 break;
1814 i = cmdline_charsize(ccline.cmdpos);
1815 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1816 break;
1817 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001819 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 + ccline.cmdpos);
1821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 ++ccline.cmdpos;
1823 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001824 while ((c == K_S_RIGHT || c == K_C_RIGHT
1825 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (has_mbyte)
1828 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 goto cmdline_not_changed;
1830
1831 case K_LEFT:
1832 case K_S_LEFT:
1833 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001834 if (ccline.cmdpos == 0)
1835 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 do
1837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (has_mbyte) /* move to first byte of char */
1840 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1841 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1843 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001844 while (ccline.cmdpos > 0
1845 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001846 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (has_mbyte)
1849 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 goto cmdline_not_changed;
1851
1852 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001853 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001854 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar4f974752019-02-17 17:44:42 +01001856#ifdef FEAT_GUI_MSWIN
1857 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1858 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001859 case K_F4:
1860 if (mod_mask == MOD_MASK_ALT)
1861 {
1862 redrawcmd(); /* somehow the cmdline is cleared */
1863 goto cmdline_not_changed;
1864 }
1865 break;
1866#endif
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 case K_MIDDLEDRAG:
1869 case K_MIDDLERELEASE:
1870 goto cmdline_not_changed; /* Ignore mouse */
1871
1872 case K_MIDDLEMOUSE:
1873# ifdef FEAT_GUI
1874 /* When GUI is active, also paste when 'mouse' is empty */
1875 if (!gui.in_use)
1876# endif
1877 if (!mouse_has(MOUSE_COMMAND))
1878 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001879# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001881 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001883# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001884 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 redrawcmd();
1886 goto cmdline_changed;
1887
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001888# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001890 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 redrawcmd();
1892 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895 case K_LEFTDRAG:
1896 case K_LEFTRELEASE:
1897 case K_RIGHTDRAG:
1898 case K_RIGHTRELEASE:
1899 /* Ignore drag and release events when the button-down wasn't
1900 * seen before. */
1901 if (ignore_drag_release)
1902 goto cmdline_not_changed;
1903 /* FALLTHROUGH */
1904 case K_LEFTMOUSE:
1905 case K_RIGHTMOUSE:
1906 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1907 ignore_drag_release = TRUE;
1908 else
1909 ignore_drag_release = FALSE;
1910# ifdef FEAT_GUI
1911 /* When GUI is active, also move when 'mouse' is empty */
1912 if (!gui.in_use)
1913# endif
1914 if (!mouse_has(MOUSE_COMMAND))
1915 goto cmdline_not_changed; /* Ignore mouse */
1916# ifdef FEAT_CLIPBOARD
1917 if (mouse_row < cmdline_row && clip_star.available)
1918 {
1919 int button, is_click, is_drag;
1920
1921 /*
1922 * Handle modeless selection.
1923 */
1924 button = get_mouse_button(KEY2TERMCAP1(c),
1925 &is_click, &is_drag);
1926 if (mouse_model_popup() && button == MOUSE_LEFT
1927 && (mod_mask & MOD_MASK_SHIFT))
1928 {
1929 /* Translate shift-left to right button. */
1930 button = MOUSE_RIGHT;
1931 mod_mask &= ~MOD_MASK_SHIFT;
1932 }
1933 clip_modeless(button, is_click, is_drag);
1934 goto cmdline_not_changed;
1935 }
1936# endif
1937
1938 set_cmdspos();
1939 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1940 ++ccline.cmdpos)
1941 {
1942 i = cmdline_charsize(ccline.cmdpos);
1943 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1944 && mouse_col < ccline.cmdspos % Columns + i)
1945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (has_mbyte)
1947 {
1948 /* Count ">" for double-wide char that doesn't fit. */
1949 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001950 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 + ccline.cmdpos) - 1;
1952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 ccline.cmdspos += i;
1954 }
1955 goto cmdline_not_changed;
1956
1957 /* Mouse scroll wheel: ignored here */
1958 case K_MOUSEDOWN:
1959 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001960 case K_MOUSELEFT:
1961 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 /* Alternate buttons ignored here */
1963 case K_X1MOUSE:
1964 case K_X1DRAG:
1965 case K_X1RELEASE:
1966 case K_X2MOUSE:
1967 case K_X2DRAG:
1968 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001969 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 goto cmdline_not_changed;
1971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#ifdef FEAT_GUI
1973 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1974 case K_LEFTRELEASE_NM:
1975 goto cmdline_not_changed;
1976
1977 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001978 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 gui_do_scroll();
1981 redrawcmd();
1982 }
1983 goto cmdline_not_changed;
1984
1985 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001986 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001988 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 redrawcmd();
1990 }
1991 goto cmdline_not_changed;
1992#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001993#ifdef FEAT_GUI_TABLINE
1994 case K_TABLINE:
1995 case K_TABMENU:
1996 /* Don't want to change any tabs here. Make sure the same tab
1997 * is still selected. */
1998 if (gui_use_tabline())
1999 gui_mch_set_curtab(tabpage_index(curtab));
2000 goto cmdline_not_changed;
2001#endif
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 case K_SELECT: /* end of Select mode mapping - ignore */
2004 goto cmdline_not_changed;
2005
2006 case Ctrl_B: /* begin of command line */
2007 case K_HOME:
2008 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 case K_S_HOME:
2010 case K_C_HOME:
2011 ccline.cmdpos = 0;
2012 set_cmdspos();
2013 goto cmdline_not_changed;
2014
2015 case Ctrl_E: /* end of command line */
2016 case K_END:
2017 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 case K_S_END:
2019 case K_C_END:
2020 ccline.cmdpos = ccline.cmdlen;
2021 set_cmdspos_cursor();
2022 goto cmdline_not_changed;
2023
2024 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002025 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 break;
2027 goto cmdline_changed;
2028
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002029 case Ctrl_L:
2030#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002031 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002032 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002033#endif
2034
2035 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002036 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 break;
2038 goto cmdline_changed;
2039
2040 case Ctrl_N: /* next match */
2041 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002042 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002044 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2045 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002047 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002049 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 case K_UP:
2051 case K_DOWN:
2052 case K_S_UP:
2053 case K_S_DOWN:
2054 case K_PAGEUP:
2055 case K_KPAGEUP:
2056 case K_PAGEDOWN:
2057 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002058 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 goto cmdline_not_changed;
2060
2061 i = hiscnt;
2062
2063 /* save current command string so it can be restored later */
2064 if (lookfor == NULL)
2065 {
2066 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2067 goto cmdline_not_changed;
2068 lookfor[ccline.cmdpos] = NUL;
2069 }
2070
2071 j = (int)STRLEN(lookfor);
2072 for (;;)
2073 {
2074 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002075 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002076 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002078 if (hiscnt == get_hislen()) /* first time */
2079 hiscnt = *get_hisidx(histype);
2080 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2081 hiscnt = get_hislen() - 1;
2082 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 --hiscnt;
2084 else /* at top of list */
2085 {
2086 hiscnt = i;
2087 break;
2088 }
2089 }
2090 else /* one step forwards */
2091 {
2092 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002093 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002095 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 break;
2097 }
2098
2099 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002100 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002102 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 hiscnt = 0;
2104 else
2105 ++hiscnt;
2106 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002107 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 hiscnt = i;
2110 break;
2111 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002112 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002113 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002114 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 lookfor, (size_t)j) == 0)
2116 break;
2117 }
2118
2119 if (hiscnt != i) /* jumped to other entry */
2120 {
2121 char_u *p;
2122 int len;
2123 int old_firstc;
2124
Bram Moolenaar438d1762018-09-30 17:11:48 +02002125 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002126 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002127 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 p = lookfor; /* back to the old one */
2129 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002130 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132 if (histype == HIST_SEARCH
2133 && p != lookfor
2134 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2135 {
2136 /* Correct for the separator character used when
2137 * adding the history entry vs the one used now.
2138 * First loop: count length.
2139 * Second loop: copy the characters. */
2140 for (i = 0; i <= 1; ++i)
2141 {
2142 len = 0;
2143 for (j = 0; p[j] != NUL; ++j)
2144 {
2145 /* Replace old sep with new sep, unless it is
2146 * escaped. */
2147 if (p[j] == old_firstc
2148 && (j == 0 || p[j - 1] != '\\'))
2149 {
2150 if (i > 0)
2151 ccline.cmdbuff[len] = firstc;
2152 }
2153 else
2154 {
2155 /* Escape new sep, unless it is already
2156 * escaped. */
2157 if (p[j] == firstc
2158 && (j == 0 || p[j - 1] != '\\'))
2159 {
2160 if (i > 0)
2161 ccline.cmdbuff[len] = '\\';
2162 ++len;
2163 }
2164 if (i > 0)
2165 ccline.cmdbuff[len] = p[j];
2166 }
2167 ++len;
2168 }
2169 if (i == 0)
2170 {
2171 alloc_cmdbuff(len);
2172 if (ccline.cmdbuff == NULL)
2173 goto returncmd;
2174 }
2175 }
2176 ccline.cmdbuff[len] = NUL;
2177 }
2178 else
2179 {
2180 alloc_cmdbuff((int)STRLEN(p));
2181 if (ccline.cmdbuff == NULL)
2182 goto returncmd;
2183 STRCPY(ccline.cmdbuff, p);
2184 }
2185
2186 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2187 redrawcmd();
2188 goto cmdline_changed;
2189 }
2190 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002191 goto cmdline_not_changed;
2192
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002193#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002194 case Ctrl_G: /* next match */
2195 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002196 if (may_adjust_incsearch_highlighting(
2197 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002198 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200#endif
2201
2202 case Ctrl_V:
2203 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 putcmdline('^', TRUE);
2206 c = get_literal(); /* get next (two) character(s) */
2207 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002208 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /* may need to remove ^ when composing char was typed */
2210 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2211 {
2212 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2213 msg_putchar(' ');
2214 cursorcmd();
2215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 break;
2217
2218#ifdef FEAT_DIGRAPHS
2219 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002222# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 dont_scroll = TRUE; /* disallow scrolling here */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002226 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (c != NUL)
2228 break;
2229
2230 redrawcmd();
2231 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002232#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234#ifdef FEAT_RIGHTLEFT
2235 case Ctrl__: /* CTRL-_: switch language mode */
2236 if (!p_ari)
2237 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002238 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 goto cmdline_not_changed;
2240#endif
2241
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002242 case K_PS:
2243 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2244 goto cmdline_changed;
2245
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 default:
2247#ifdef UNIX
2248 if (c == intr_char)
2249 {
2250 gotesc = TRUE; /* will free ccline.cmdbuff after
2251 putting it in history */
2252 goto returncmd; /* back to Normal mode */
2253 }
2254#endif
2255 /*
2256 * Normal character with no special meaning. Just set mod_mask
2257 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2258 * the string <S-Space>. This should only happen after ^V.
2259 */
2260 if (!IS_SPECIAL(c))
2261 mod_mask = 0x0;
2262 break;
2263 }
2264 /*
2265 * End of switch on command line character.
2266 * We come here if we have a normal character.
2267 */
2268
Bram Moolenaar13505972019-01-24 15:04:48 +01002269 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2270 && (ccheck_abbr(
2271 // Add ABBR_OFF for characters above 0x100, this is
2272 // what check_abbr() expects.
2273 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2274 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 goto cmdline_changed;
2276
2277 /*
2278 * put the character in the command line
2279 */
2280 if (IS_SPECIAL(c) || mod_mask != 0)
2281 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2282 else
2283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (has_mbyte)
2285 {
2286 j = (*mb_char2bytes)(c, IObuff);
2287 IObuff[j] = NUL; /* exclude composing chars */
2288 put_on_cmdline(IObuff, j, TRUE);
2289 }
2290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 IObuff[0] = c;
2293 put_on_cmdline(IObuff, 1, TRUE);
2294 }
2295 }
2296 goto cmdline_changed;
2297
2298/*
2299 * This part implements incremental searches for "/" and "?"
2300 * Jump to cmdline_not_changed when a character has been read but the command
2301 * line did not change. Then we only search and redraw if something changed in
2302 * the past.
2303 * Jump to cmdline_changed when the command line did change.
2304 * (Sorry for the goto's, I know it is ugly).
2305 */
2306cmdline_not_changed:
2307#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002308 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 continue;
2310#endif
2311
2312cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002313 /* Trigger CmdlineChanged autocommands. */
2314 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002317 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319
2320#ifdef FEAT_RIGHTLEFT
2321 if (cmdmsg_rl
2322# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002323 || (p_arshape && !p_tbidi
2324 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325# endif
2326 )
2327 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002328 * right-left typing. Not efficient, but it works.
2329 * Do it only when there are no characters left to read
2330 * to avoid useless intermediate redraws. */
2331 if (vpeekc() == NUL)
2332 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#endif
2334 }
2335
2336returncmd:
2337
2338#ifdef FEAT_RIGHTLEFT
2339 cmdmsg_rl = FALSE;
2340#endif
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002343 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002346 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348
2349 if (ccline.cmdbuff != NULL)
2350 {
2351 /*
2352 * Put line in history buffer (":" and "=" only when it was typed).
2353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (ccline.cmdlen && firstc != NUL
2355 && (some_key_typed || histype == HIST_SEARCH))
2356 {
2357 add_to_history(histype, ccline.cmdbuff, TRUE,
2358 histype == HIST_SEARCH ? firstc : NUL);
2359 if (firstc == ':')
2360 {
2361 vim_free(new_last_cmdline);
2362 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2363 }
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002366 if (gotesc)
2367 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370 /*
2371 * If the screen was shifted up, redraw the whole screen (later).
2372 * If the line is too long, clear it, so ruler and shown command do
2373 * not get printed in the middle of it.
2374 */
2375 msg_check();
2376 msg_scroll = save_msg_scroll;
2377 redir_off = FALSE;
2378
2379 /* When the command line was typed, no need for a wait-return prompt. */
2380 if (some_key_typed)
2381 need_wait_return = FALSE;
2382
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002383 /* Trigger CmdlineLeave autocommands. */
2384 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002387#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2389 im_save_status(b_im_ptr);
2390 im_set_active(FALSE);
2391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef CURSOR_SHAPE
2394 ui_cursor_shape(); /* may show different cursor shape */
2395#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002396 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaar438d1762018-09-30 17:11:48 +02002398theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002399 {
2400 char_u *p = ccline.cmdbuff;
2401
Bram Moolenaar438d1762018-09-30 17:11:48 +02002402 if (did_save_ccline)
2403 restore_cmdline(&save_ccline);
2404 else
2405 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002406 return p;
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408}
2409
2410#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2411/*
2412 * Get a command line with a prompt.
2413 * This is prepared to be called recursively from getcmdline() (e.g. by
2414 * f_input() when evaluating an expression from CTRL-R =).
2415 * Returns the command line in allocated memory, or NULL.
2416 */
2417 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002418getcmdline_prompt(
2419 int firstc,
2420 char_u *prompt, /* command line prompt */
2421 int attr, /* attributes for prompt */
2422 int xp_context, /* type of expansion */
2423 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002426 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002427 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002429 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
Bram Moolenaar438d1762018-09-30 17:11:48 +02002431 if (ccline.cmdbuff != NULL)
2432 {
2433 // Save the values of the current cmdline and restore them below.
2434 save_cmdline(&save_ccline);
2435 did_save_ccline = TRUE;
2436 }
2437
Bram Moolenaar66b51422019-08-18 21:44:12 +02002438 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 ccline.cmdprompt = prompt;
2440 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002441# ifdef FEAT_EVAL
2442 ccline.xp_context = xp_context;
2443 ccline.xp_arg = xp_arg;
2444 ccline.input_fn = (firstc == '@');
2445# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002446 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002447 s = getcmdline_int(firstc, 1L, 0, FALSE);
2448
2449 if (did_save_ccline)
2450 restore_cmdline(&save_ccline);
2451
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002452 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002453 /* Restore msg_col, the prompt from input() may have changed it.
2454 * But only if called recursively and the commandline is therefore being
2455 * restored to an old one; if not, the input() prompt stays on the screen,
2456 * so we need its modified msg_col left intact. */
2457 if (ccline.cmdbuff != NULL)
2458 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 return s;
2461}
2462#endif
2463
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002464/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002465 * Read the 'wildmode' option, fill wim_flags[].
2466 */
2467 int
2468check_opt_wim(void)
2469{
2470 char_u new_wim_flags[4];
2471 char_u *p;
2472 int i;
2473 int idx = 0;
2474
2475 for (i = 0; i < 4; ++i)
2476 new_wim_flags[i] = 0;
2477
2478 for (p = p_wim; *p; ++p)
2479 {
2480 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2481 ;
2482 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2483 return FAIL;
2484 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2485 new_wim_flags[idx] |= WIM_LONGEST;
2486 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2487 new_wim_flags[idx] |= WIM_FULL;
2488 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2489 new_wim_flags[idx] |= WIM_LIST;
2490 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2491 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2492 else
2493 return FAIL;
2494 p += i;
2495 if (*p == NUL)
2496 break;
2497 if (*p == ',')
2498 {
2499 if (idx == 3)
2500 return FAIL;
2501 ++idx;
2502 }
2503 }
2504
2505 /* fill remaining entries with last flag */
2506 while (idx < 3)
2507 {
2508 new_wim_flags[idx + 1] = new_wim_flags[idx];
2509 ++idx;
2510 }
2511
2512 /* only when there are no errors, wim_flags[] is changed */
2513 for (i = 0; i < 4; ++i)
2514 wim_flags[i] = new_wim_flags[i];
2515 return OK;
2516}
2517
2518/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002519 * Return TRUE when the text must not be changed and we can't switch to
2520 * another window or buffer. Used when editing the command line, evaluating
2521 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002522 */
2523 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002524text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002525{
2526#ifdef FEAT_CMDWIN
2527 if (cmdwin_type != 0)
2528 return TRUE;
2529#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002530 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002531}
2532
2533/*
2534 * Give an error message for a command that isn't allowed while the cmdline
2535 * window is open or editing the cmdline in another way.
2536 */
2537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002538text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002539{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002540 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002541}
2542
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002543 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002544get_text_locked_msg(void)
2545{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002546#ifdef FEAT_CMDWIN
2547 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002548 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002550 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002551}
2552
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002553/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002554 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2555 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002556 */
2557 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002558curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002559{
2560 if (curbuf_lock > 0)
2561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002563 return TRUE;
2564 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002565 return allbuf_locked();
2566}
2567
2568/*
2569 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2570 * message.
2571 */
2572 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002573allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002574{
2575 if (allbuf_lock > 0)
2576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002577 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002578 return TRUE;
2579 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002580 return FALSE;
2581}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002584cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585{
2586#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2587 if (cmdline_star > 0) /* showing '*', always 1 position */
2588 return 1;
2589#endif
2590 return ptr2cells(ccline.cmdbuff + idx);
2591}
2592
2593/*
2594 * Compute the offset of the cursor on the command line for the prompt and
2595 * indent.
2596 */
2597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002598set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002600 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 ccline.cmdspos = 1 + ccline.cmdindent;
2602 else
2603 ccline.cmdspos = 0 + ccline.cmdindent;
2604}
2605
2606/*
2607 * Compute the screen position for the cursor on the command line.
2608 */
2609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002610set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612 int i, m, c;
2613
2614 set_cmdspos();
2615 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002618 if (m < 0) /* overflow, Columns or Rows at weird value */
2619 m = MAXCOL;
2620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 else
2622 m = MAXCOL;
2623 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2624 {
2625 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2627 if (has_mbyte)
2628 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002629 /* If the cmdline doesn't fit, show cursor on last visible char.
2630 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if ((ccline.cmdspos += c) >= m)
2632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 ccline.cmdspos -= c;
2634 break;
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002637 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639}
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641/*
2642 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2643 * character that doesn't fit, so that a ">" must be displayed.
2644 */
2645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002646correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002648 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2650 && ccline.cmdspos % Columns + cells > Columns)
2651 ccline.cmdspos++;
2652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654/*
2655 * Get an Ex command line for the ":" command.
2656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002658getexline(
2659 int c, /* normally ':', NUL for ":append" */
2660 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002661 int indent, /* indent for inside conditionals */
2662 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 /* When executing a register, remove ':' that's in front of each line. */
2665 if (exec_from_reg && vpeekc() == ':')
2666 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002667 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668}
2669
2670/*
2671 * Get an Ex command line for Ex mode.
2672 * In Ex mode we only use the OS supplied line editing features and no
2673 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002674 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002677getexmodeline(
2678 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002680 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002681 int indent, /* indent for inside conditionals */
2682 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002684 garray_T line_ga;
2685 char_u *pend;
2686 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002687 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002688 int escaped = FALSE; /* CTRL-V typed */
2689 int vcol = 0;
2690 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002691 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002692 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 /* Switch cursor on now. This avoids that it happens after the "\n", which
2695 * confuses the system function that computes tabstops. */
2696 cursor_on();
2697
2698 /* always start in column 0; write a newline if necessary */
2699 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002704 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002705 if (p_prompt)
2706 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 while (indent-- > 0)
2708 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711
2712 ga_init2(&line_ga, 1, 30);
2713
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002714 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002716 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002717 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002718 while (indent >= 8)
2719 {
2720 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002721 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002722 indent -= 8;
2723 }
2724 while (indent-- > 0)
2725 {
2726 ga_append(&line_ga, ' ');
2727 msg_putchar(' ');
2728 }
2729 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 ++no_mapping;
2731 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 /*
2734 * Get the line, one character at a time.
2735 */
2736 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002739 long sw;
2740 char_u *s;
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (ga_grow(&line_ga, 40) == FAIL)
2743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaarba748c82017-02-26 14:00:07 +01002745 /*
2746 * Get one character at a time.
2747 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002748 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002749
2750 /* Check for a ":normal" command and no more characters left. */
2751 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2752 c1 = '\n';
2753 else
2754 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
2756 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 * Handle line editing.
2758 * Previously this was left to the system, putting the terminal in
2759 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002761 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 msg_putchar('\n');
2764 break;
2765 }
2766
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002767 if (c1 == K_PS)
2768 {
2769 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2770 goto redraw;
2771 }
2772
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002773 if (!escaped)
2774 {
2775 /* CR typed means "enter", which is NL */
2776 if (c1 == '\r')
2777 c1 = '\n';
2778
2779 if (c1 == BS || c1 == K_BS
2780 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002782 if (line_ga.ga_len > 0)
2783 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002784 if (has_mbyte)
2785 {
2786 p = (char_u *)line_ga.ga_data;
2787 p[line_ga.ga_len] = NUL;
2788 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2789 line_ga.ga_len -= len;
2790 }
2791 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002792 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 goto redraw;
2794 }
2795 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 msg_col = startcol;
2801 msg_clr_eos();
2802 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002803 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 }
2805
2806 if (c1 == Ctrl_T)
2807 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002808 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 p = (char_u *)line_ga.ga_data;
2810 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002811 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002812 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002814 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002816 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002817 p = (char_u *)line_ga.ga_data;
2818 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2820 *s = ' ';
2821 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823redraw:
2824 /* redraw the line */
2825 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002827 p = (char_u *)line_ga.ga_data;
2828 p[line_ga.ga_len] = NUL;
2829 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002834 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002835 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002836 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002840 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002841 msg_outtrans_len(p, len);
2842 vcol += ptr2cells(p);
2843 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
2845 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002847 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 continue;
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 if (c1 == Ctrl_D)
2852 {
2853 /* Delete one shiftwidth. */
2854 p = (char_u *)line_ga.ga_data;
2855 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 if (prev_char == '^')
2858 ex_keep_indent = TRUE;
2859 indent = 0;
2860 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 else
2863 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002865 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002866 if (indent > 0)
2867 {
2868 --indent;
2869 indent -= indent % get_sw_value(curbuf);
2870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002872 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002874 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2876 --line_ga.ga_len;
2877 }
2878 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880
2881 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2882 {
2883 escaped = TRUE;
2884 continue;
2885 }
2886
2887 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2888 if (IS_SPECIAL(c1))
2889 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002891
2892 if (IS_SPECIAL(c1))
2893 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002894 if (has_mbyte)
2895 len = (*mb_char2bytes)(c1,
2896 (char_u *)line_ga.ga_data + line_ga.ga_len);
2897 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002898 {
2899 len = 1;
2900 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2901 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 if (c1 == '\n')
2903 msg_putchar('\n');
2904 else if (c1 == TAB)
2905 {
2906 /* Don't use chartabsize(), 'ts' can be different */
2907 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002908 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002909 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002914 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002917 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002918 escaped = FALSE;
2919
2920 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002921 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002923 /* We are done when a NL is entered, but not when it comes after an
2924 * odd number of backslashes, that results in a NUL. */
2925 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002927 int bcount = 0;
2928
2929 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2930 ++bcount;
2931
2932 if (bcount > 0)
2933 {
2934 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2935 * "\NL", etc. */
2936 line_ga.ga_len -= (bcount + 1) / 2;
2937 pend -= (bcount + 1) / 2;
2938 pend[-1] = '\n';
2939 }
2940
2941 if ((bcount & 1) == 0)
2942 {
2943 --line_ga.ga_len;
2944 --pend;
2945 *pend = NUL;
2946 break;
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 }
2949 }
2950
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002951 --no_mapping;
2952 --allow_keys;
2953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 /* make following messages go to the next line */
2955 msg_didout = FALSE;
2956 msg_col = 0;
2957 if (msg_row < Rows - 1)
2958 ++msg_row;
2959 emsg_on_display = FALSE; /* don't want ui_delay() */
2960
2961 if (got_int)
2962 ga_clear(&line_ga);
2963
2964 return (char_u *)line_ga.ga_data;
2965}
2966
Bram Moolenaare344bea2005-09-01 20:46:49 +00002967# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2968 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969/*
2970 * Return TRUE if ccline.overstrike is on.
2971 */
2972 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002973cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
2975 return ccline.overstrike;
2976}
2977
2978/*
2979 * Return TRUE if the cursor is at the end of the cmdline.
2980 */
2981 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002982cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 return (ccline.cmdpos >= ccline.cmdlen);
2985}
2986#endif
2987
Bram Moolenaar9372a112005-12-06 19:59:18 +00002988#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989/*
2990 * Return the virtual column number at the current cursor position.
2991 * This is used by the IM code to obtain the start of the preedit string.
2992 */
2993 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002994cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2997 return MAXCOL;
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (has_mbyte)
3000 {
3001 colnr_T col;
3002 int i = 0;
3003
3004 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003005 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 return col;
3008 }
3009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 return ccline.cmdpos;
3011}
3012#endif
3013
3014#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3015/*
3016 * If part of the command line is an IM preedit string, redraw it with
3017 * IM feedback attributes. The cursor position is restored after drawing.
3018 */
3019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if ((State & CMDLINE)
3023 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003024 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 && !p_imdisable
3026 && im_is_preediting())
3027 {
3028 int cmdpos = 0;
3029 int cmdspos;
3030 int old_row;
3031 int old_col;
3032 colnr_T col;
3033
3034 old_row = msg_row;
3035 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003036 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (has_mbyte)
3039 {
3040 for (col = 0; col < preedit_start_col
3041 && cmdpos < ccline.cmdlen; ++col)
3042 {
3043 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003044 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046 }
3047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
3049 cmdspos += preedit_start_col;
3050 cmdpos += preedit_start_col;
3051 }
3052
3053 msg_row = cmdline_row + (cmdspos / (int)Columns);
3054 msg_col = cmdspos % (int)Columns;
3055 if (msg_row >= Rows)
3056 msg_row = Rows - 1;
3057
3058 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3059 {
3060 int char_len;
3061 int char_attr;
3062
3063 char_attr = im_get_feedback_attr(col);
3064 if (char_attr < 0)
3065 break; /* end of preedit string */
3066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003068 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 char_len = 1;
3071
3072 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3073 cmdpos += char_len;
3074 }
3075
3076 msg_row = old_row;
3077 msg_col = old_col;
3078 }
3079}
3080#endif /* FEAT_XIM && FEAT_GUI_GTK */
3081
3082/*
3083 * Allocate a new command line buffer.
3084 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
3086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 /*
3090 * give some extra space to avoid having to allocate all the time
3091 */
3092 if (len < 80)
3093 len = 100;
3094 else
3095 len += 20;
3096
3097 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3098 ccline.cmdbufflen = len;
3099}
3100
3101/*
3102 * Re-allocate the command line to length len + something extra.
3103 * return FAIL for failure, OK otherwise
3104 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003106realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 char_u *p;
3109
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003110 if (len < ccline.cmdbufflen)
3111 return OK; /* no need to resize */
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 p = ccline.cmdbuff;
3114 alloc_cmdbuff(len); /* will get some more */
3115 if (ccline.cmdbuff == NULL) /* out of memory */
3116 {
3117 ccline.cmdbuff = p; /* keep the old one */
3118 return FAIL;
3119 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003120 /* There isn't always a NUL after the command, but it may need to be
3121 * there, thus copy up to the NUL and add a NUL. */
3122 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3123 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003125
3126 if (ccline.xpc != NULL
3127 && ccline.xpc->xp_pattern != NULL
3128 && ccline.xpc->xp_context != EXPAND_NOTHING
3129 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3130 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003131 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003132
3133 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3134 * to point into the newly allocated memory. */
3135 if (i >= 0 && i <= ccline.cmdlen)
3136 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3137 }
3138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 return OK;
3140}
3141
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003142#if defined(FEAT_ARABIC) || defined(PROTO)
3143static char_u *arshape_buf = NULL;
3144
3145# if defined(EXITFREE) || defined(PROTO)
3146 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003147free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003148{
3149 vim_free(arshape_buf);
3150}
3151# endif
3152#endif
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154/*
3155 * Draw part of the cmdline at the current cursor position. But draw stars
3156 * when cmdline_star is TRUE.
3157 */
3158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003159draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3162 int i;
3163
3164 if (cmdline_star > 0)
3165 for (i = 0; i < len; ++i)
3166 {
3167 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003169 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171 else
3172#endif
3173#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003174 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 static int buflen = 0;
3177 char_u *p;
3178 int j;
3179 int newlen = 0;
3180 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003181 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 int prev_c = 0;
3183 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003184 int u8c;
3185 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 /*
3189 * Do arabic shaping into a temporary buffer. This is very
3190 * inefficient!
3191 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003192 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 /* Re-allocate the buffer. We keep it around to avoid a lot of
3195 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003197 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198 arshape_buf = alloc(buflen);
3199 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return; /* out of memory */
3201 }
3202
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003203 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3204 {
3205 /* Prepend a space to draw the leading composing char on. */
3206 arshape_buf[0] = ' ';
3207 newlen = 1;
3208 }
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 for (j = start; j < start + len; j += mb_l)
3211 {
3212 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003213 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003214 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (ARABIC_CHAR(u8c))
3216 {
3217 /* Do Arabic shaping. */
3218 if (cmdmsg_rl)
3219 {
3220 /* displaying from right to left */
3221 pc = prev_c;
3222 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003223 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (j + mb_l >= start + len)
3225 nc = NUL;
3226 else
3227 nc = utf_ptr2char(p + mb_l);
3228 }
3229 else
3230 {
3231 /* displaying from left to right */
3232 if (j + mb_l >= start + len)
3233 pc = NUL;
3234 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003235 {
3236 int pcc[MAX_MCO];
3237
3238 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003240 pc1 = pcc[0];
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 nc = prev_c;
3243 }
3244 prev_c = u8c;
3245
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003246 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003248 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003249 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003251 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3252 if (u8cc[1] != 0)
3253 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256 }
3257 else
3258 {
3259 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 newlen += mb_l;
3262 }
3263 }
3264
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267 else
3268#endif
3269 msg_outtrans_len(ccline.cmdbuff + start, len);
3270}
3271
3272/*
3273 * Put a character on the command line. Shifts the following text to the
3274 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3275 * "c" must be printable (fit in one display cell)!
3276 */
3277 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003278putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 if (cmd_silent)
3281 return;
3282 msg_no_more = TRUE;
3283 msg_putchar(c);
3284 if (shift)
3285 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3286 msg_no_more = FALSE;
3287 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003288 extra_char = c;
3289 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290}
3291
3292/*
3293 * Undo a putcmdline(c, FALSE).
3294 */
3295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003296unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 if (cmd_silent)
3299 return;
3300 msg_no_more = TRUE;
3301 if (ccline.cmdlen == ccline.cmdpos)
3302 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003303 else if (has_mbyte)
3304 draw_cmdline(ccline.cmdpos,
3305 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 else
3307 draw_cmdline(ccline.cmdpos, 1);
3308 msg_no_more = FALSE;
3309 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003310 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311}
3312
3313/*
3314 * Put the given string, of the given length, onto the command line.
3315 * If len is -1, then STRLEN() is used to calculate the length.
3316 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3317 * part will be redrawn, otherwise it will not. If this function is called
3318 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3319 * called afterwards.
3320 */
3321 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003322put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 int retval;
3325 int i;
3326 int m;
3327 int c;
3328
3329 if (len < 0)
3330 len = (int)STRLEN(str);
3331
3332 /* Check if ccline.cmdbuff needs to be longer */
3333 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003334 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 else
3336 retval = OK;
3337 if (retval == OK)
3338 {
3339 if (!ccline.overstrike)
3340 {
3341 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3342 ccline.cmdbuff + ccline.cmdpos,
3343 (size_t)(ccline.cmdlen - ccline.cmdpos));
3344 ccline.cmdlen += len;
3345 }
3346 else
3347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (has_mbyte)
3349 {
3350 /* Count nr of characters in the new string. */
3351 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003352 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 ++m;
3354 /* Count nr of bytes in cmdline that are overwritten by these
3355 * characters. */
3356 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003357 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 --m;
3359 if (i < ccline.cmdlen)
3360 {
3361 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3362 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3363 ccline.cmdlen += ccline.cmdpos + len - i;
3364 }
3365 else
3366 ccline.cmdlen = ccline.cmdpos + len;
3367 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003368 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 ccline.cmdlen = ccline.cmdpos + len;
3370 }
3371 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3372 ccline.cmdbuff[ccline.cmdlen] = NUL;
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (enc_utf8)
3375 {
3376 /* When the inserted text starts with a composing character,
3377 * backup to the character before it. There could be two of them.
3378 */
3379 i = 0;
3380 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3381 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3382 {
3383 i = (*mb_head_off)(ccline.cmdbuff,
3384 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3385 ccline.cmdpos -= i;
3386 len += i;
3387 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3388 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003389#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3391 {
3392 /* Check the previous character for Arabic combining pair. */
3393 i = (*mb_head_off)(ccline.cmdbuff,
3394 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3395 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3396 + ccline.cmdpos - i), c))
3397 {
3398 ccline.cmdpos -= i;
3399 len += i;
3400 }
3401 else
3402 i = 0;
3403 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (i != 0)
3406 {
3407 /* Also backup the cursor position. */
3408 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3409 ccline.cmdspos -= i;
3410 msg_col -= i;
3411 if (msg_col < 0)
3412 {
3413 msg_col += Columns;
3414 --msg_row;
3415 }
3416 }
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 if (redraw && !cmd_silent)
3420 {
3421 msg_no_more = TRUE;
3422 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003423 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3425 /* Avoid clearing the rest of the line too often. */
3426 if (cmdline_row != i || ccline.overstrike)
3427 msg_clr_eos();
3428 msg_no_more = FALSE;
3429 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003430 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003432 m = Columns * Rows;
3433 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003435 }
3436 else
3437 m = MAXCOL;
3438 for (i = 0; i < len; ++i)
3439 {
3440 c = cmdline_charsize(ccline.cmdpos);
3441 /* count ">" for a double-wide char that doesn't fit. */
3442 if (has_mbyte)
3443 correct_cmdspos(ccline.cmdpos, c);
3444 /* Stop cursor at the end of the screen, but do increment the
3445 * insert position, so that entering a very long command
3446 * works, even though you can't see it. */
3447 if (ccline.cmdspos + c < m)
3448 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003449
Bram Moolenaar14184a32019-02-16 15:10:30 +01003450 if (has_mbyte)
3451 {
3452 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3453 if (c > len - i - 1)
3454 c = len - i - 1;
3455 ccline.cmdpos += c;
3456 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003458 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 }
3461 if (redraw)
3462 msg_check();
3463 return retval;
3464}
3465
Bram Moolenaar66b51422019-08-18 21:44:12 +02003466static cmdline_info_T prev_ccline;
3467static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003468
3469/*
3470 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3471 * and overwrite it. But get_cmdline_str() may need it, thus make it
3472 * available globally in prev_ccline.
3473 */
3474 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003475save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003476{
3477 if (!prev_ccline_used)
3478 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003479 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003480 prev_ccline_used = TRUE;
3481 }
3482 *ccp = prev_ccline;
3483 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003484 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003485}
3486
3487/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003488 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003489 */
3490 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003492{
3493 ccline = prev_ccline;
3494 prev_ccline = *ccp;
3495}
3496
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003498 * Paste a yank register into the command line.
3499 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003500 * insert_reg() can't be used here, because special characters from the
3501 * register contents will be interpreted as commands.
3502 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003503 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003504 */
3505 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003506cmdline_paste(
3507 int regname,
3508 int literally, /* Insert text literally instead of "as typed" */
3509 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003510{
3511 long i;
3512 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003513 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003514 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003515
3516 /* check for valid regname; also accept special characters for CTRL-R in
3517 * the command line */
3518 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003519 && regname != Ctrl_A && regname != Ctrl_L
3520 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003521 return FAIL;
3522
3523 /* A register containing CTRL-R can cause an endless loop. Allow using
3524 * CTRL-C to break the loop. */
3525 line_breakcheck();
3526 if (got_int)
3527 return FAIL;
3528
3529#ifdef FEAT_CLIPBOARD
3530 regname = may_get_selection(regname);
3531#endif
3532
Bram Moolenaar438d1762018-09-30 17:11:48 +02003533 // Need to set "textlock" to avoid nasty things like going to another
3534 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003535 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003536 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003537 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003538
3539 if (i)
3540 {
3541 /* Got the value of a special register in "arg". */
3542 if (arg == NULL)
3543 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003544
3545 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3546 * part of the word. */
3547 p = arg;
3548 if (p_is && regname == Ctrl_W)
3549 {
3550 char_u *w;
3551 int len;
3552
3553 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003554 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003555 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003556 if (has_mbyte)
3557 {
3558 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3559 if (!vim_iswordc(mb_ptr2char(w - len)))
3560 break;
3561 w -= len;
3562 }
3563 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003564 {
3565 if (!vim_iswordc(w[-1]))
3566 break;
3567 --w;
3568 }
3569 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003570 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003571 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3572 p += len;
3573 }
3574
3575 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003576 if (allocated)
3577 vim_free(arg);
3578 return OK;
3579 }
3580
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003581 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003582}
3583
3584/*
3585 * Put a string on the command line.
3586 * When "literally" is TRUE, insert literally.
3587 * When "literally" is FALSE, insert as typed, but don't leave the command
3588 * line.
3589 */
3590 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003591cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003592{
3593 int c, cv;
3594
3595 if (literally)
3596 put_on_cmdline(s, -1, TRUE);
3597 else
3598 while (*s != NUL)
3599 {
3600 cv = *s;
3601 if (cv == Ctrl_V && s[1])
3602 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003604 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003605 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003607 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3608 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003609#ifdef UNIX
3610 || c == intr_char
3611#endif
3612 || (c == Ctrl_BSL && *s == Ctrl_N))
3613 stuffcharReadbuff(Ctrl_V);
3614 stuffcharReadbuff(c);
3615 }
3616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#ifdef FEAT_WILDMENU
3619/*
3620 * Delete characters on the command line, from "from" to the current
3621 * position.
3622 */
3623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003624cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3627 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3628 ccline.cmdlen -= ccline.cmdpos - from;
3629 ccline.cmdpos = from;
3630}
3631#endif
3632
3633/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003634 * This function is called when the screen size changes and with incremental
3635 * search and in other situations where the command line may have been
3636 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 */
3638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003639redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003641 redrawcmdline_ex(TRUE);
3642}
3643
3644 void
3645redrawcmdline_ex(int do_compute_cmdrow)
3646{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (cmd_silent)
3648 return;
3649 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003650 if (do_compute_cmdrow)
3651 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 redrawcmd();
3653 cursorcmd();
3654}
3655
3656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003657redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 int i;
3660
3661 if (cmd_silent)
3662 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003663 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 msg_putchar(ccline.cmdfirstc);
3665 if (ccline.cmdprompt != NULL)
3666 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003667 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3669 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003670 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 --ccline.cmdindent;
3672 }
3673 else
3674 for (i = ccline.cmdindent; i > 0; --i)
3675 msg_putchar(' ');
3676}
3677
3678/*
3679 * Redraw what is currently on the command line.
3680 */
3681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003682redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 if (cmd_silent)
3685 return;
3686
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003687 /* when 'incsearch' is set there may be no command line while redrawing */
3688 if (ccline.cmdbuff == NULL)
3689 {
3690 windgoto(cmdline_row, 0);
3691 msg_clr_eos();
3692 return;
3693 }
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 msg_start();
3696 redrawcmdprompt();
3697
3698 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3699 msg_no_more = TRUE;
3700 draw_cmdline(0, ccline.cmdlen);
3701 msg_clr_eos();
3702 msg_no_more = FALSE;
3703
3704 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003705 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003706 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /*
3709 * An emsg() before may have set msg_scroll. This is used in normal mode,
3710 * in cmdline mode we can reset them now.
3711 */
3712 msg_scroll = FALSE; /* next message overwrites cmdline */
3713
3714 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3715 * in cmdline mode */
3716 skip_redraw = FALSE;
3717}
3718
3719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003720compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003722 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 cmdline_row = Rows - 1;
3724 else
3725 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003726 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727}
3728
Bram Moolenaar66b51422019-08-18 21:44:12 +02003729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003730cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 if (cmd_silent)
3733 return;
3734
3735#ifdef FEAT_RIGHTLEFT
3736 if (cmdmsg_rl)
3737 {
3738 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3739 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3740 if (msg_row <= 0)
3741 msg_row = Rows - 1;
3742 }
3743 else
3744#endif
3745 {
3746 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3747 msg_col = ccline.cmdspos % (int)Columns;
3748 if (msg_row >= Rows)
3749 msg_row = Rows - 1;
3750 }
3751
3752 windgoto(msg_row, msg_col);
3753#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003754 if (p_imst == IM_ON_THE_SPOT)
3755 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#endif
3757#ifdef MCH_CURSOR_SHAPE
3758 mch_update_cursor();
3759#endif
3760}
3761
3762 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003763gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 msg_start();
3766#ifdef FEAT_RIGHTLEFT
3767 if (cmdmsg_rl)
3768 msg_col = Columns - 1;
3769 else
3770#endif
3771 msg_col = 0; /* always start in column 0 */
3772 if (clr) /* clear the bottom line(s) */
3773 msg_clr_eos(); /* will reset clear_cmdline */
3774 windgoto(cmdline_row, 0);
3775}
3776
3777/*
3778 * Check the word in front of the cursor for an abbreviation.
3779 * Called when the non-id character "c" has been entered.
3780 * When an abbreviation is recognized it is removed from the text with
3781 * backspaces and the replacement string is inserted, followed by "c".
3782 */
3783 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003784ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003786 int spos = 0;
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3789 return FALSE;
3790
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003791 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3792 * Actually accepts any mark. */
3793 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3794 spos++;
3795 if (ccline.cmdlen - spos > 5
3796 && ccline.cmdbuff[spos] == '\''
3797 && ccline.cmdbuff[spos + 2] == ','
3798 && ccline.cmdbuff[spos + 3] == '\'')
3799 spos += 5;
3800 else
3801 /* check abbreviation from the beginning of the commandline */
3802 spos = 0;
3803
3804 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805}
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003808 * Escape special characters in "fname" for when used as a file name argument
3809 * after a Vim command, or, when "shell" is non-zero, a shell command.
3810 * Returns the result in allocated memory.
3811 */
3812 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003813vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003814{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003815 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003816#ifdef BACKSLASH_IN_FILENAME
3817 char_u buf[20];
3818 int j = 0;
3819
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003820 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003821 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003822 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003823 buf[j++] = *p;
3824 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003825 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003826#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003827 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3828 if (shell && csh_like_shell() && p != NULL)
3829 {
3830 char_u *s;
3831
3832 /* For csh and similar shells need to put two backslashes before '!'.
3833 * One is taken by Vim, one by the shell. */
3834 s = vim_strsave_escaped(p, (char_u *)"!");
3835 vim_free(p);
3836 p = s;
3837 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003838#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003839
3840 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3841 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003842 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003843 escape_fname(&p);
3844
3845 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003846}
3847
3848/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003849 * Put a backslash before the file name in "pp", which is in allocated memory.
3850 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003851 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003852escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003853{
3854 char_u *p;
3855
Bram Moolenaar964b3742019-05-24 18:54:09 +02003856 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003857 if (p != NULL)
3858 {
3859 p[0] = '\\';
3860 STRCPY(p + 1, *pp);
3861 vim_free(*pp);
3862 *pp = p;
3863 }
3864}
3865
3866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * For each file name in files[num_files]:
3868 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3869 */
3870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003871tilde_replace(
3872 char_u *orig_pat,
3873 int num_files,
3874 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 int i;
3877 char_u *p;
3878
3879 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3880 {
3881 for (i = 0; i < num_files; ++i)
3882 {
3883 p = home_replace_save(NULL, files[i]);
3884 if (p != NULL)
3885 {
3886 vim_free(files[i]);
3887 files[i] = p;
3888 }
3889 }
3890 }
3891}
3892
3893/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003894 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003896 cmdline_info_T *
3897get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003899 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900}
3901
Bram Moolenaar064154c2016-03-19 22:50:43 +01003902#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003903/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003904 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003905 * ccline and put the previous value in prev_ccline.
3906 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003907 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003908get_ccline_ptr(void)
3909{
3910 if ((State & CMDLINE) == 0)
3911 return NULL;
3912 if (ccline.cmdbuff != NULL)
3913 return &ccline;
3914 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3915 return &prev_ccline;
3916 return NULL;
3917}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003918#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003919
Bram Moolenaar064154c2016-03-19 22:50:43 +01003920#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003921/*
3922 * Get the current command line in allocated memory.
3923 * Only works when the command line is being edited.
3924 * Returns NULL when something is wrong.
3925 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003926 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003927get_cmdline_str(void)
3928{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003929 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003930
Bram Moolenaaree91c332018-09-25 22:27:35 +02003931 if (cmdline_star > 0)
3932 return NULL;
3933 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003934 if (p == NULL)
3935 return NULL;
3936 return vim_strnsave(p->cmdbuff, p->cmdlen);
3937}
3938
3939/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003940 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003941 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003942 void
3943f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3944{
3945 rettv->v_type = VAR_STRING;
3946 rettv->vval.v_string = get_cmdline_str();
3947}
3948
3949/*
3950 * "getcmdpos()" function
3951 */
3952 void
3953f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003954{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003955 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003956
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003957 rettv->vval.v_number = 0;
3958 if (p != NULL)
3959 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003960}
3961
3962/*
3963 * Set the command line byte position to "pos". Zero is the first position.
3964 * Only works when the command line is being edited.
3965 * Returns 1 when failed, 0 when OK.
3966 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003967 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003968set_cmdline_pos(
3969 int pos)
3970{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003971 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003972
3973 if (p == NULL)
3974 return 1;
3975
3976 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3977 * changed the command line. */
3978 if (pos < 0)
3979 new_cmdpos = 0;
3980 else
3981 new_cmdpos = pos;
3982 return 0;
3983}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003984
3985/*
3986 * "setcmdpos()" function
3987 */
3988 void
3989f_setcmdpos(typval_T *argvars, typval_T *rettv)
3990{
3991 int pos = (int)tv_get_number(&argvars[0]) - 1;
3992
3993 if (pos >= 0)
3994 rettv->vval.v_number = set_cmdline_pos(pos);
3995}
3996
3997/*
3998 * "getcmdtype()" function
3999 */
4000 void
4001f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4002{
4003 rettv->v_type = VAR_STRING;
4004 rettv->vval.v_string = alloc(2);
4005 if (rettv->vval.v_string != NULL)
4006 {
4007 rettv->vval.v_string[0] = get_cmdline_type();
4008 rettv->vval.v_string[1] = NUL;
4009 }
4010}
4011
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004012#endif
4013
4014#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4015/*
4016 * Get the current command-line type.
4017 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4018 * Only works when the command line is being edited.
4019 * Returns NUL when something is wrong.
4020 */
4021 int
4022get_cmdline_type(void)
4023{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004024 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004025
4026 if (p == NULL)
4027 return NUL;
4028 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004029 return
4030# ifdef FEAT_EVAL
4031 (p->input_fn) ? '@' :
4032# endif
4033 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004034 return p->cmdfirstc;
4035}
4036#endif
4037
Bram Moolenaard7663c22019-08-06 21:59:57 +02004038/*
4039 * Return the first character of the current command line.
4040 */
4041 int
4042get_cmdline_firstc(void)
4043{
4044 return ccline.cmdfirstc;
4045}
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047/*
4048 * Get indices "num1,num2" that specify a range within a list (not a range of
4049 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4050 * Returns OK if parsed successfully, otherwise FAIL.
4051 */
4052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004053get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
4055 int len;
4056 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004057 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 *str = skipwhite(*str);
4060 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4061 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004062 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 *str += len;
4064 *num1 = (int)num;
4065 first = TRUE;
4066 }
4067 *str = skipwhite(*str);
4068 if (**str == ',') /* parse "to" part of range */
4069 {
4070 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004071 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 if (len > 0)
4073 {
4074 *num2 = (int)num;
4075 *str = skipwhite(*str + len);
4076 }
4077 else if (!first) /* no number given at all */
4078 return FAIL;
4079 }
4080 else if (first) /* only one number given */
4081 *num2 = *num1;
4082 return OK;
4083}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#if defined(FEAT_CMDWIN) || defined(PROTO)
4086/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004087 * Check value of 'cedit' and set cedit_key.
4088 * Returns NULL if value is OK, error message otherwise.
4089 */
4090 char *
4091check_cedit(void)
4092{
4093 int n;
4094
4095 if (*p_cedit == NUL)
4096 cedit_key = -1;
4097 else
4098 {
4099 n = string_to_key(p_cedit, FALSE);
4100 if (vim_isprintc(n))
4101 return e_invarg;
4102 cedit_key = n;
4103 }
4104 return NULL;
4105}
4106
4107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * Open a window on the current command line and history. Allow editing in
4109 * the window. Returns when the window is closed.
4110 * Returns:
4111 * CR if the command is to be executed
4112 * Ctrl_C if it is to be abandoned
4113 * K_IGNORE if editing continues
4114 */
4115 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004116open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004118 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004120 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 win_T *wp;
4122 int i;
4123 linenr_T lnum;
4124 int histtype;
4125 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 int save_restart_edit = restart_edit;
4127 int save_State = State;
4128 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004129#ifdef FEAT_RIGHTLEFT
4130 int save_cmdmsg_rl = cmdmsg_rl;
4131#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004132#ifdef FEAT_FOLDING
4133 int save_KeyTyped;
4134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 /* Can't do this recursively. Can't do it when typing a password. */
4137 if (cmdwin_type != 0
4138# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4139 || cmdline_star > 0
4140# endif
4141 )
4142 {
4143 beep_flush();
4144 return K_IGNORE;
4145 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004146 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004148 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 win_size_save(&winsizes);
4150
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004151 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4152 // command line window, but we don't want the popup menu then.
4153 pum_undisplay();
4154
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004155 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004156 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004157 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004158
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004159 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4161 {
4162 beep_flush();
4163 return K_IGNORE;
4164 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004165 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004167 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004168 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004169 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004172#ifdef FEAT_FOLDING
4173 curwin->w_p_fen = FALSE;
4174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004176 curwin->w_p_rl = cmdmsg_rl;
4177 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004179 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004181 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004182 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004184 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004185 need_wait_return = FALSE;
4186
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004187 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4189 {
4190 if (p_wc == TAB)
4191 {
4192 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4193 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4194 }
4195 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4196 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004197 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004199 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4200 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004201 curbuf->b_p_tw = 0;
4202
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004203 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004205 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004207 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (i >= 0)
4209 {
4210 lnum = 0;
4211 do
4212 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004213 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004215 if (get_histentry(histtype)[i].hisstr != NULL)
4216 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 (colnr_T)0, FALSE);
4218 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004219 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 }
4222
4223 /* Replace the empty last line with the current command-line and put the
4224 * cursor there. */
4225 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4226 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4227 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004228 changed_line_abv_curs();
4229 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004230 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaar23324a02019-10-01 17:39:04 +02004232 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 exmode_active = 0;
4234
4235 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar23324a02019-10-01 17:39:04 +02004238 // Reset here so it can be set by a CmdWinEnter autocommand.
4239 cmdwin_result = 0;
4240
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004241 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004242 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004243 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004244 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 i = RedrawingDisabled;
4247 RedrawingDisabled = 0;
4248
4249 /*
4250 * Call the main loop until <CR> or CTRL-C is typed.
4251 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004252 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 RedrawingDisabled = i;
4255
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004256# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004257 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004258# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004259
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004260 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004261 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004262
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004263# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004264 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004265 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266# endif
4267
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 exmode_active = save_exmode;
4270
Bram Moolenaarf9821062008-06-20 16:31:07 +00004271 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004273 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004276 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 else
4279 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004280# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 /* autocmds may abort script processing */
4282 if (aborting() && cmdwin_result != K_IGNORE)
4283 cmdwin_result = Ctrl_C;
4284# endif
4285 /* Set the new command line from the cmdline buffer. */
4286 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004287 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004289 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4290
4291 if (histtype == HIST_CMD)
4292 {
4293 /* Execute the command directly. */
4294 ccline.cmdbuff = vim_strsave((char_u *)p);
4295 cmdwin_result = CAR;
4296 }
4297 else
4298 {
4299 /* First need to cancel what we were doing. */
4300 ccline.cmdbuff = NULL;
4301 stuffcharReadbuff(':');
4302 stuffReadbuff((char_u *)p);
4303 stuffcharReadbuff(CAR);
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else if (cmdwin_result == K_XF2) /* :qa typed */
4307 {
4308 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4309 cmdwin_result = CAR;
4310 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004311 else if (cmdwin_result == Ctrl_C)
4312 {
4313 /* :q or :close, don't execute any command
4314 * and don't modify the cmd window. */
4315 ccline.cmdbuff = NULL;
4316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 else
4318 ccline.cmdbuff = vim_strsave(ml_get_curline());
4319 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004320 {
4321 ccline.cmdbuff = vim_strsave((char_u *)"");
4322 ccline.cmdlen = 0;
4323 ccline.cmdbufflen = 1;
4324 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 else
4328 {
4329 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4330 ccline.cmdbufflen = ccline.cmdlen + 1;
4331 ccline.cmdpos = curwin->w_cursor.col;
4332 if (ccline.cmdpos > ccline.cmdlen)
4333 ccline.cmdpos = ccline.cmdlen;
4334 if (cmdwin_result == K_IGNORE)
4335 {
4336 set_cmdspos_cursor();
4337 redrawcmd();
4338 }
4339 }
4340
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004341# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004342 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004343 curwin->w_p_cole = 0;
4344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004346 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 win_goto(old_curwin);
4348 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004349
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004350 // win_close() may have already wiped the buffer when 'bh' is
4351 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004352 if (bufref_valid(&bufref))
4353 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004355 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358
4359 ga_clear(&winsizes);
4360 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004361# ifdef FEAT_RIGHTLEFT
4362 cmdmsg_rl = save_cmdmsg_rl;
4363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 return cmdwin_result;
4369}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004370#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372/*
4373 * Used for commands that either take a simple command string argument, or:
4374 * cmd << endmarker
4375 * {script}
4376 * endmarker
4377 * Returns a pointer to allocated memory with {script} or NULL.
4378 */
4379 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004380script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 char_u *theline;
4383 char *end_pattern = NULL;
4384 char dot[] = ".";
4385 garray_T ga;
4386
4387 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4388 return NULL;
4389
4390 ga_init2(&ga, 1, 0x400);
4391
4392 if (cmd[2] != NUL)
4393 end_pattern = (char *)skipwhite(cmd + 2);
4394 else
4395 end_pattern = dot;
4396
4397 for (;;)
4398 {
4399 theline = eap->getline(
4400#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004401 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004403 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004406 {
4407 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 ga_concat(&ga, theline);
4412 ga_append(&ga, '\n');
4413 vim_free(theline);
4414 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004415 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 return (char_u *)ga.ga_data;
4418}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004419
4420#if defined(FEAT_EVAL) || defined(PROTO)
4421/*
4422 * This function is used by f_input() and f_inputdialog() functions. The third
4423 * argument to f_input() specifies the type of completion to use at the
4424 * prompt. The third argument to f_inputdialog() specifies the value to return
4425 * when the user cancels the prompt.
4426 */
4427 void
4428get_user_input(
4429 typval_T *argvars,
4430 typval_T *rettv,
4431 int inputdialog,
4432 int secret)
4433{
4434 char_u *prompt = tv_get_string_chk(&argvars[0]);
4435 char_u *p = NULL;
4436 int c;
4437 char_u buf[NUMBUFLEN];
4438 int cmd_silent_save = cmd_silent;
4439 char_u *defstr = (char_u *)"";
4440 int xp_type = EXPAND_NOTHING;
4441 char_u *xp_arg = NULL;
4442
4443 rettv->v_type = VAR_STRING;
4444 rettv->vval.v_string = NULL;
4445
4446#ifdef NO_CONSOLE_INPUT
4447 // While starting up, there is no place to enter text. When running tests
4448 // with --not-a-term we assume feedkeys() will be used.
4449 if (no_console_input() && !is_not_a_term())
4450 return;
4451#endif
4452
4453 cmd_silent = FALSE; // Want to see the prompt.
4454 if (prompt != NULL)
4455 {
4456 // Only the part of the message after the last NL is considered as
4457 // prompt for the command line
4458 p = vim_strrchr(prompt, '\n');
4459 if (p == NULL)
4460 p = prompt;
4461 else
4462 {
4463 ++p;
4464 c = *p;
4465 *p = NUL;
4466 msg_start();
4467 msg_clr_eos();
4468 msg_puts_attr((char *)prompt, get_echo_attr());
4469 msg_didout = FALSE;
4470 msg_starthere();
4471 *p = c;
4472 }
4473 cmdline_row = msg_row;
4474
4475 if (argvars[1].v_type != VAR_UNKNOWN)
4476 {
4477 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4478 if (defstr != NULL)
4479 stuffReadbuffSpec(defstr);
4480
4481 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4482 {
4483 char_u *xp_name;
4484 int xp_namelen;
4485 long argt;
4486
4487 // input() with a third argument: completion
4488 rettv->vval.v_string = NULL;
4489
4490 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4491 if (xp_name == NULL)
4492 return;
4493
4494 xp_namelen = (int)STRLEN(xp_name);
4495
4496 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4497 &xp_arg) == FAIL)
4498 return;
4499 }
4500 }
4501
4502 if (defstr != NULL)
4503 {
4504 int save_ex_normal_busy = ex_normal_busy;
4505
4506 ex_normal_busy = 0;
4507 rettv->vval.v_string =
4508 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4509 xp_type, xp_arg);
4510 ex_normal_busy = save_ex_normal_busy;
4511 }
4512 if (inputdialog && rettv->vval.v_string == NULL
4513 && argvars[1].v_type != VAR_UNKNOWN
4514 && argvars[2].v_type != VAR_UNKNOWN)
4515 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4516 &argvars[2], buf));
4517
4518 vim_free(xp_arg);
4519
4520 // since the user typed this, no need to wait for return
4521 need_wait_return = FALSE;
4522 msg_didout = FALSE;
4523 }
4524 cmd_silent = cmd_silent_save;
4525}
4526#endif