blob: 4a4d76ec9de65c9c18589fcce47d46b84ab6f0ec [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 Moolenaarc6725252019-11-23 21:56:46 +0100200 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200201
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200202 *skiplen = 0;
203 *patlen = ccline.cmdlen;
204
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200205 if (!p_is || cmd_silent)
206 return FALSE;
207
208 // by default search all lines
209 search_first_line = 0;
210 search_last_line = MAXLNUM;
211
212 if (firstc == '/' || firstc == '?')
213 return TRUE;
214 if (firstc != ':')
215 return FALSE;
216
Bram Moolenaarc6725252019-11-23 21:56:46 +0100217 ++emsg_off;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200218 vim_memset(&ea, 0, sizeof(ea));
219 ea.line1 = 1;
220 ea.line2 = 1;
221 ea.cmd = ccline.cmdbuff;
222 ea.addr_type = ADDR_LINES;
223
224 parse_command_modifiers(&ea, &dummy, TRUE);
225 cmdmod = save_cmdmod;
226
227 cmd = skip_range(ea.cmd, NULL);
228 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100229 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200230
231 // Skip over "substitute" to find the pattern separator.
232 for (p = cmd; ASCII_ISALPHA(*p); ++p)
233 ;
234 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100235 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
237 if (STRNCMP(cmd, "substitute", p - cmd) == 0
238 || STRNCMP(cmd, "smagic", p - cmd) == 0
239 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
240 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200241 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200242 if (*cmd == 's' && cmd[1] == 'm')
243 p_magic = TRUE;
244 else if (*cmd == 's' && cmd[1] == 'n')
245 p_magic = FALSE;
246 }
247 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
248 {
249 // skip over flags
250 while (ASCII_ISALPHA(*(p = skipwhite(p))))
251 ++p;
252 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100253 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200254 }
255 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
256 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
257 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
258 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
259 || STRNCMP(cmd, "global", p - cmd) == 0)
260 {
261 // skip over "!"
262 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200263 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200264 p++;
265 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100266 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200267 }
268 if (*cmd != 'g')
269 delim_optional = TRUE;
270 }
271 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100272 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200274 p = skipwhite(p);
275 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
276 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200277
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200278 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200279
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200280 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100281 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200282
283 // Don't do 'hlsearch' highlighting if the pattern matches everything.
284 if (!use_last_pat)
285 {
286 char c = *end;
287 int empty;
288
289 *end = NUL;
290 empty = empty_pattern(p);
291 *end = c;
292 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100293 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200294 }
295
296 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200297 *skiplen = (int)(p - ccline.cmdbuff);
298 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200299
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200300 // parse the address range
301 save_cursor = curwin->w_cursor;
302 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200303 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200304 if (ea.addr_count > 0)
305 {
306 // Allow for reverse match.
307 if (ea.line2 < ea.line1)
308 {
309 search_first_line = ea.line2;
310 search_last_line = ea.line1;
311 }
312 else
313 {
314 search_first_line = ea.line1;
315 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200316 }
317 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200318 else if (cmd[0] == 's' && cmd[1] != 'o')
319 {
320 // :s defaults to the current line
321 search_first_line = curwin->w_cursor.lnum;
322 search_last_line = curwin->w_cursor.lnum;
323 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200324
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200325 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100326 retval = TRUE;
327theend:
328 --emsg_off;
329 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200330}
331
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200332 static void
333finish_incsearch_highlighting(
334 int gotesc,
335 incsearch_state_T *is_state,
336 int call_update_screen)
337{
338 if (is_state->did_incsearch)
339 {
340 is_state->did_incsearch = FALSE;
341 if (gotesc)
342 curwin->w_cursor = is_state->save_cursor;
343 else
344 {
345 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
346 {
347 // put the '" mark at the original position
348 curwin->w_cursor = is_state->save_cursor;
349 setpcmark();
350 }
351 curwin->w_cursor = is_state->search_start;
352 }
353 restore_viewstate(&is_state->old_viewstate);
354 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200355
356 // by default search all lines
357 search_first_line = 0;
358 search_last_line = MAXLNUM;
359
360 p_magic = is_state->magic_save;
361
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200362 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200363 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200364 if (call_update_screen)
365 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200366 }
367}
368
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200369/*
370 * Do 'incsearch' highlighting if desired.
371 */
372 static void
373may_do_incsearch_highlighting(
374 int firstc,
375 long count,
376 incsearch_state_T *is_state)
377{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200378 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200379 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200380 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200381#ifdef FEAT_RELTIME
382 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200383 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200385 int next_char;
386 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200387 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200388
Bram Moolenaar198cb662018-09-06 21:44:17 +0200389 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100390 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200391 save_last_search_pattern();
392
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200393 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200394 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200395 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200396 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200397 if (did_do_incsearch && vpeekc() == NUL)
398 // may have skipped a redraw, do it now
399 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200400 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200401 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402
403 // If there is a character waiting, search and redraw later.
404 if (char_avail())
405 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200406 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407 is_state->incsearch_postponed = TRUE;
408 return;
409 }
410 is_state->incsearch_postponed = FALSE;
411
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200412 if (search_first_line == 0)
413 // start at the original cursor position
414 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100415 else if (search_first_line > curbuf->b_ml.ml_line_count)
416 {
417 // start after the last line
418 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
419 curwin->w_cursor.col = MAXCOL;
420 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200421 else
422 {
423 // start at the first line in the range
424 curwin->w_cursor.lnum = search_first_line;
425 curwin->w_cursor.col = 0;
426 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200427
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200428 // Use the previous pattern for ":s//".
429 next_char = ccline.cmdbuff[skiplen + patlen];
430 use_last_pat = patlen == 0 && skiplen > 0
431 && ccline.cmdbuff[skiplen - 1] == next_char;
432
433 // If there is no pattern, don't do anything.
434 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200435 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200436 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200437 set_no_hlsearch(TRUE); // turn off previous highlight
438 redraw_all_later(SOME_VALID);
439 }
440 else
441 {
442 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
443
444 cursor_off(); // so the user knows we're busy
445 out_flush();
446 ++emsg_off; // so it doesn't beep if bad expr
447#ifdef FEAT_RELTIME
448 // Set the time limit to half a second.
449 profile_setlimit(500L, &tm);
450#endif
451 if (!p_hls)
452 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200453 if (search_first_line != 0)
454 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200456#ifdef FEAT_RELTIME
457 vim_memset(&sia, 0, sizeof(sia));
458 sia.sa_tm = &tm;
459#endif
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200460 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200463 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200465 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466#endif
467 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469 --emsg_off;
470
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200471 if (curwin->w_cursor.lnum < search_first_line
472 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200473 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200474 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200475 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200476 curwin->w_cursor = is_state->search_start;
477 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200478
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 // if interrupted while searching, behave like it failed
480 if (got_int)
481 {
482 (void)vpeekc(); // remove <C-C> from input stream
483 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200484 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200485 }
486 else if (char_avail())
487 // cancelled searching because a char was typed
488 is_state->incsearch_postponed = TRUE;
489 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200490 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200491 highlight_match = TRUE; // highlight position
492 else
493 highlight_match = FALSE; // remove highlight
494
495 // First restore the old curwin values, so the screen is positioned in the
496 // same way as the actual search command.
497 restore_viewstate(&is_state->old_viewstate);
498 changed_cline_bef_curs();
499 update_topline();
500
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200501 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 {
503 pos_T save_pos = curwin->w_cursor;
504
505 is_state->match_start = curwin->w_cursor;
506 set_search_match(&curwin->w_cursor);
507 validate_cursor();
508 end_pos = curwin->w_cursor;
509 is_state->match_end = end_pos;
510 curwin->w_cursor = save_pos;
511 }
512 else
513 end_pos = curwin->w_cursor; // shutup gcc 4
514
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200515 // Disable 'hlsearch' highlighting if the pattern matches everything.
516 // Avoids a flash when typing "foo\|".
517 if (!use_last_pat)
518 {
519 next_char = ccline.cmdbuff[skiplen + patlen];
520 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200521 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
522 {
523 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200524 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200525 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200526 ccline.cmdbuff[skiplen + patlen] = next_char;
527 }
528
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 validate_cursor();
530 // May redraw the status line to show the cursor position.
531 if (p_ru && curwin->w_status_height > 0)
532 curwin->w_redr_status = TRUE;
533
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200535 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200536 restore_last_search_pattern();
537
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200538 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
539 // end of the pattern, e.g. for ":s/pat/".
540 if (ccline.cmdbuff[skiplen + patlen] != NUL)
541 curwin->w_cursor = is_state->search_start;
542 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200543 curwin->w_cursor = end_pos;
544
545 msg_starthere();
546 redrawcmdline();
547 is_state->did_incsearch = TRUE;
548}
549
550/*
551 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
552 * or previous match.
553 * Returns FAIL when jumping to cmdline_not_changed;
554 */
555 static int
556may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200557 int firstc,
558 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563 pos_T t;
564 char_u *pat;
565 int search_flags = SEARCH_NOOF;
566 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568
Bram Moolenaar198cb662018-09-06 21:44:17 +0200569 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100570 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200571 save_last_search_pattern();
572
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200574 {
575 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200577 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200578 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200579 {
580 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200581 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200582 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200583
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200585 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200586 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200587 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200588 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200589 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200590 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200591 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 cursor_off();
594 out_flush();
595 if (c == Ctrl_G)
596 {
597 t = is_state->match_end;
598 if (LT_POS(is_state->match_start, is_state->match_end))
599 // Start searching at the end of the match not at the beginning of
600 // the next column.
601 (void)decl(&t);
602 search_flags += SEARCH_COL;
603 }
604 else
605 t = is_state->match_start;
606 if (!p_hls)
607 search_flags += SEARCH_KEEP;
608 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 save = pat[patlen];
610 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100611 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200612 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200613 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616 if (i)
617 {
618 is_state->search_start = is_state->match_start;
619 is_state->match_end = t;
620 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622 {
623 // Move just before the current match, so that when nv_search
624 // finishes the cursor will be put back on the match.
625 is_state->search_start = t;
626 (void)decl(&is_state->search_start);
627 }
628 else if (c == Ctrl_G && firstc == '?')
629 {
630 // Move just after the current match, so that when nv_search
631 // finishes the cursor will be put back on the match.
632 is_state->search_start = t;
633 (void)incl(&is_state->search_start);
634 }
635 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
636 {
637 // wrap around
638 is_state->search_start = t;
639 if (firstc == '?')
640 (void)incl(&is_state->search_start);
641 else
642 (void)decl(&is_state->search_start);
643 }
644
645 set_search_match(&is_state->match_end);
646 curwin->w_cursor = is_state->match_start;
647 changed_cline_bef_curs();
648 update_topline();
649 validate_cursor();
650 highlight_match = TRUE;
651 save_viewstate(&is_state->old_viewstate);
652 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200653 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200655 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200656 }
657 else
658 vim_beep(BO_ERROR);
659 restore_last_search_pattern();
660 return FAIL;
661}
662
663/*
664 * When CTRL-L typed: add character from the match to the pattern.
665 * May set "*c" to the added character.
666 * Return OK when jumping to cmdline_not_changed.
667 */
668 static int
669may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
670{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200671 int skiplen, patlen;
672
Bram Moolenaar198cb662018-09-06 21:44:17 +0200673 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100674 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200675 save_last_search_pattern();
676
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200678 {
679 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200680 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200681 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100682 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200683
684 // Add a character from under the cursor for 'incsearch'.
685 if (is_state->did_incsearch)
686 {
687 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200688 *c = gchar_cursor();
689 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200691 // If 'ignorecase' and 'smartcase' are set and the
692 // command line has no uppercase characters, convert
693 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200694 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200695 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200696 if (*c == firstc || vim_strchr((char_u *)(
697 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200698 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200699 // put a backslash before special characters
700 stuffcharReadbuff(*c);
701 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200702 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200703 // add any composing characters
704 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
705 {
706 int save_c = *c;
707
708 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
709 {
710 curwin->w_cursor.col += mb_char2len(*c);
711 *c = gchar_cursor();
712 stuffcharReadbuff(*c);
713 }
714 *c = save_c;
715 }
716 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200717 }
718 }
719 return OK;
720}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200721#endif
722
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200723#ifdef FEAT_ARABIC
724/*
725 * Return TRUE if the command line has an Arabic character at or after "start"
726 * for "len" bytes.
727 */
728 static int
729cmdline_has_arabic(int start, int len)
730{
731 int j;
732 int mb_l;
733 int u8c;
734 char_u *p;
735 int u8cc[MAX_MCO];
736
737 if (!enc_utf8)
738 return FALSE;
739
740 for (j = start; j < start + len; j += mb_l)
741 {
742 p = ccline.cmdbuff + j;
743 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
744 mb_l = utfc_ptr2len_len(p, start + len - j);
745 if (ARABIC_CHAR(u8c))
746 return TRUE;
747 }
748 return FALSE;
749}
750#endif
751
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200752 void
753cmdline_init(void)
754{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200755 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200756}
757
Bram Moolenaar66216052017-12-16 16:33:44 +0100758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * getcmdline() - accept a command line starting with firstc.
760 *
761 * firstc == ':' get ":" command line.
762 * firstc == '/' or '?' get search pattern
763 * firstc == '=' get expression
764 * firstc == '@' get text for input() function
765 * firstc == '>' get text for debug mode
766 * firstc == NUL get text for :insert command
767 * firstc == -1 like NUL, and break on CTRL-C
768 *
769 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
770 * command line.
771 *
772 * Careful: getcmdline() can be called recursively!
773 *
774 * Return pointer to allocated string if there is a commandline, NULL
775 * otherwise.
776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100778getcmdline(
779 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200780 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200781 int indent, // indent for inside conditionals
782 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200783{
784 return getcmdline_int(firstc, count, indent, TRUE);
785}
786
787 static char_u *
788getcmdline_int(
789 int firstc,
790 long count UNUSED, // only used for incremental search
791 int indent, // indent for inside conditionals
792 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 int c;
795 int i;
796 int j;
797 int gotesc = FALSE; /* TRUE when <ESC> just typed */
798 int do_abbr; /* when TRUE check for abbr. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 char_u *lookfor = NULL; /* string to match */
800 int hiscnt; /* current history line in use */
801 int histype; /* history type to be used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200803 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#endif
805 int did_wild_list = FALSE; /* did wild_list() recently */
806 int wim_index = 0; /* index in wim_flags[] */
807 int res;
808 int save_msg_scroll = msg_scroll;
809 int save_State = State; /* remember State when called */
810 int some_key_typed = FALSE; /* one of the keys was typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 /* mouse drag and release events are ignored, unless they are
812 * preceded with a mouse down event */
813 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_EVAL
815 int break_ctrl_c = FALSE;
816#endif
817 expand_T xpc;
818 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200819 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200820 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200821 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar438d1762018-09-30 17:11:48 +0200823 if (ccline.cmdbuff != NULL)
824 {
825 // Being called recursively. Since ccline is global, we need to save
826 // the current buffer and restore it when returning.
827 save_cmdline(&save_ccline);
828 did_save_ccline = TRUE;
829 }
830 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200831 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#ifdef FEAT_EVAL
834 if (firstc == -1)
835 {
836 firstc = NUL;
837 break_ctrl_c = TRUE;
838 }
839#endif
840#ifdef FEAT_RIGHTLEFT
841 /* start without Hebrew mapping for a command line */
842 if (firstc == ':' || firstc == '=' || firstc == '>')
843 cmd_hkmap = 0;
844#endif
845
846 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200849 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif
851
852 /*
853 * set some variables for redrawcmd()
854 */
855 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000856 ccline.cmdindent = (firstc > 0 ? indent : 0);
857
858 /* alloc initial ccline.cmdbuff */
859 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200861 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 ccline.cmdlen = ccline.cmdpos = 0;
863 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100864 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000866 /* autoindent for :insert and :append */
867 if (firstc <= 0)
868 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200869 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000870 ccline.cmdbuff[indent] = NUL;
871 ccline.cmdpos = indent;
872 ccline.cmdspos = indent;
873 ccline.cmdlen = indent;
874 }
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000877 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879#ifdef FEAT_RIGHTLEFT
880 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
881 && (firstc == '/' || firstc == '?'))
882 cmdmsg_rl = TRUE;
883 else
884 cmdmsg_rl = FALSE;
885#endif
886
887 redir_off = TRUE; /* don't redirect the typed command */
888 if (!cmd_silent)
889 {
890 i = msg_scrolled;
891 msg_scrolled = 0; /* avoid wait_return message */
892 gotocmdline(TRUE);
893 msg_scrolled += i;
894 redrawcmdprompt(); /* draw prompt or indent */
895 set_cmdspos();
896 }
897 xpc.xp_context = EXPAND_NOTHING;
898 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000899#ifndef BACKSLASH_IN_FILENAME
900 xpc.xp_shell = FALSE;
901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000903#if defined(FEAT_EVAL)
904 if (ccline.input_fn)
905 {
906 xpc.xp_context = ccline.xp_context;
907 xpc.xp_pattern = ccline.cmdbuff;
908 xpc.xp_arg = ccline.xp_arg;
909 }
910#endif
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /*
913 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
914 * doing ":@0" when register 0 doesn't contain a CR.
915 */
916 msg_scroll = FALSE;
917
918 State = CMDLINE;
919
920 if (firstc == '/' || firstc == '?' || firstc == '@')
921 {
922 /* Use ":lmap" mappings for search pattern and input(). */
923 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
924 b_im_ptr = &curbuf->b_p_iminsert;
925 else
926 b_im_ptr = &curbuf->b_p_imsearch;
927 if (*b_im_ptr == B_IMODE_LMAP)
928 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 im_set_active(*b_im_ptr == B_IMODE_IM);
931#endif
932 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100933#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 else if (p_imcmdline)
935 im_set_active(TRUE);
936#endif
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#ifdef CURSOR_SHAPE
940 ui_cursor_shape(); /* may show different cursor shape */
941#endif
942
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000943 /* When inside an autocommand for writing "exiting" may be set and
944 * terminal mode set to cooked. Need to set raw mode here then. */
945 settmode(TMODE_RAW);
946
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200947 /* Trigger CmdlineEnter autocommands. */
948 cmdline_type = firstc == NUL ? '-' : firstc;
949 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200952 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000956 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#endif
958
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200959 /* If something above caused an error, reset the flags, we do want to type
960 * and execute commands. Display may be messed up a bit. */
961 if (did_emsg)
962 redrawcmd();
963 did_emsg = FALSE;
964 got_int = FALSE;
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /*
967 * Collect the command string, handling editing keys.
968 */
969 for (;;)
970 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000971 redir_off = TRUE; /* Don't redirect the typed command.
972 Repeated, because a ":redir" inside
973 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef USE_ON_FLY_SCROLL
975 dont_scroll = FALSE; /* allow scrolling here */
976#endif
977 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
978
Bram Moolenaar72532d32018-04-07 19:09:09 +0200979 did_emsg = FALSE; /* There can't really be a reason why an error
980 that occurs while typing a command should
981 cause the command not to be executed. */
982
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200983 // Trigger SafeState if nothing is pending.
984 may_trigger_safestate(xpc.xp_numfiles <= 0);
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000987
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100988 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
989 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000990 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000991 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100992 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (KeyTyped)
995 {
996 some_key_typed = TRUE;
997#ifdef FEAT_RIGHTLEFT
998 if (cmd_hkmap)
999 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (cmdmsg_rl && !KeyStuffed)
1001 {
1002 /* Invert horizontal movements and operations. Only when
1003 * typed by the user directly, not when the result of a
1004 * mapping. */
1005 switch (c)
1006 {
1007 case K_RIGHT: c = K_LEFT; break;
1008 case K_S_RIGHT: c = K_S_LEFT; break;
1009 case K_C_RIGHT: c = K_C_LEFT; break;
1010 case K_LEFT: c = K_RIGHT; break;
1011 case K_S_LEFT: c = K_S_RIGHT; break;
1012 case K_C_LEFT: c = K_C_RIGHT; break;
1013 }
1014 }
1015#endif
1016 }
1017
1018 /*
1019 * Ignore got_int when CTRL-C was typed here.
1020 * Don't ignore it in :global, we really need to break then, e.g., for
1021 * ":g/pat/normal /pat" (without the <CR>).
1022 * Don't ignore it for the input() function.
1023 */
1024 if ((c == Ctrl_C
1025#ifdef UNIX
1026 || c == intr_char
1027#endif
1028 )
1029#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1030 && firstc != '@'
1031#endif
1032#ifdef FEAT_EVAL
1033 && !break_ctrl_c
1034#endif
1035 && !global_busy)
1036 got_int = FALSE;
1037
Bram Moolenaard7663c22019-08-06 21:59:57 +02001038 // free old command line when finished moving around in the history
1039 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001041 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && c != K_PAGEDOWN && c != K_PAGEUP
1044 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001045 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001047 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001050 * When there are matching completions to select <S-Tab> works like
1051 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001053 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 c = Ctrl_P;
1055
1056#ifdef FEAT_WILDMENU
1057 /* Special translations for 'wildmenu' */
1058 if (did_wild_list && p_wmnu)
1059 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001060 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001062 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 c = Ctrl_N;
1064 }
1065 /* Hitting CR after "emenu Name.": complete submenu */
1066 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1067 && ccline.cmdpos > 1
1068 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1069 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1070 && (c == '\n' || c == '\r' || c == K_KENTER))
1071 c = K_DOWN;
1072#endif
1073
1074 /* free expanded names when finished walking through matches */
1075 if (xpc.xp_numfiles != -1
1076 && !(c == p_wc && KeyTyped) && c != p_wcm
1077 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1078 && c != Ctrl_L)
1079 {
1080 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1081 did_wild_list = FALSE;
1082#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001083 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085 xpc.xp_context = EXPAND_NOTHING;
1086 wim_index = 0;
1087#ifdef FEAT_WILDMENU
1088 if (p_wmnu && wild_menu_showing != 0)
1089 {
1090 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001091 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001092
1093 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001094 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 if (wild_menu_showing == WM_SCROLLED)
1097 {
1098 /* Entered command line, move it up */
1099 cmdline_row--;
1100 redrawcmd();
1101 }
1102 else if (save_p_ls != -1)
1103 {
1104 /* restore 'laststatus' and 'winminheight' */
1105 p_ls = save_p_ls;
1106 p_wmh = save_p_wmh;
1107 last_status(FALSE);
1108 update_screen(VALID); /* redraw the screen NOW */
1109 redrawcmd();
1110 save_p_ls = -1;
1111 }
1112 else
1113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 redraw_statuslines();
1116 }
1117 KeyTyped = skt;
1118 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001119 if (ccline.input_fn)
1120 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122#endif
1123 }
1124
1125#ifdef FEAT_WILDMENU
1126 /* Special translations for 'wildmenu' */
1127 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1128 {
1129 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001130 if (c == K_DOWN && ccline.cmdpos > 0
1131 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001133 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
1135 /* Hitting <Up>: Remove one submenu name in front of the
1136 * cursor */
1137 int found = FALSE;
1138
1139 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1140 i = 0;
1141 while (--j > 0)
1142 {
1143 /* check for start of menu name */
1144 if (ccline.cmdbuff[j] == ' '
1145 && ccline.cmdbuff[j - 1] != '\\')
1146 {
1147 i = j + 1;
1148 break;
1149 }
1150 /* check for start of submenu name */
1151 if (ccline.cmdbuff[j] == '.'
1152 && ccline.cmdbuff[j - 1] != '\\')
1153 {
1154 if (found)
1155 {
1156 i = j + 1;
1157 break;
1158 }
1159 else
1160 found = TRUE;
1161 }
1162 }
1163 if (i > 0)
1164 cmdline_del(i);
1165 c = p_wc;
1166 xpc.xp_context = EXPAND_NOTHING;
1167 }
1168 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001169 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001170 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001171 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 char_u upseg[5];
1174
1175 upseg[0] = PATHSEP;
1176 upseg[1] = '.';
1177 upseg[2] = '.';
1178 upseg[3] = PATHSEP;
1179 upseg[4] = NUL;
1180
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001181 if (c == K_DOWN
1182 && ccline.cmdpos > 0
1183 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1184 && (ccline.cmdpos < 3
1185 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1187 {
1188 /* go down a directory */
1189 c = p_wc;
1190 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001191 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
1193 /* If in a direct ancestor, strip off one ../ to go down */
1194 int found = FALSE;
1195
1196 j = ccline.cmdpos;
1197 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1198 while (--j > i)
1199 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001200 if (has_mbyte)
1201 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (vim_ispathsep(ccline.cmdbuff[j]))
1203 {
1204 found = TRUE;
1205 break;
1206 }
1207 }
1208 if (found
1209 && ccline.cmdbuff[j - 1] == '.'
1210 && ccline.cmdbuff[j - 2] == '.'
1211 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1212 {
1213 cmdline_del(j - 2);
1214 c = p_wc;
1215 }
1216 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001217 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 /* go up a directory */
1220 int found = FALSE;
1221
1222 j = ccline.cmdpos - 1;
1223 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1224 while (--j > i)
1225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (has_mbyte)
1227 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (vim_ispathsep(ccline.cmdbuff[j])
1229#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001230 && vim_strchr((char_u *)" *?[{`$%#",
1231 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233 )
1234 {
1235 if (found)
1236 {
1237 i = j + 1;
1238 break;
1239 }
1240 else
1241 found = TRUE;
1242 }
1243 }
1244
1245 if (!found)
1246 j = i;
1247 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1248 j += 4;
1249 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1250 && j == i)
1251 j += 3;
1252 else
1253 j = 0;
1254 if (j > 0)
1255 {
1256 /* TODO this is only for DOS/UNIX systems - need to put in
1257 * machine-specific stuff here and in upseg init */
1258 cmdline_del(j);
1259 put_on_cmdline(upseg + 1, 3, FALSE);
1260 }
1261 else if (ccline.cmdpos > i)
1262 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001263
1264 /* Now complete in the new directory. Set KeyTyped in case the
1265 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001267 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271#endif /* FEAT_WILDMENU */
1272
1273 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1274 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1275 if (c == Ctrl_BSL)
1276 {
1277 ++no_mapping;
1278 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001279 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 --no_mapping;
1281 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001282 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1283 * is in a mapping. */
1284 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001285 || (ccline.cmdfirstc == '=' && KeyTyped)
1286#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001287 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001288#endif
1289 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 vungetc(c);
1292 c = Ctrl_BSL;
1293 }
1294#ifdef FEAT_EVAL
1295 else if (c == 'e')
1296 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001297 char_u *p = NULL;
1298 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 /*
1301 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001302 * Need to save and restore the current command line, to be
1303 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 */
1305 if (ccline.cmdpos == ccline.cmdlen)
1306 new_cmdpos = 99999; /* keep it at the end */
1307 else
1308 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (c == '=')
1312 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001313 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001314 * to avoid nasty things like going to another buffer when
1315 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001316 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001318 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001319
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001320 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001322 len = (int)STRLEN(p);
1323 if (realloc_cmdbuff(len + 1) == OK)
1324 {
1325 ccline.cmdlen = len;
1326 STRCPY(ccline.cmdbuff, p);
1327 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001329 /* Restore the cursor or use the position set with
1330 * set_cmdline_pos(). */
1331 if (new_cmdpos > ccline.cmdlen)
1332 ccline.cmdpos = ccline.cmdlen;
1333 else
1334 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001336 KeyTyped = FALSE; /* Don't do p_wc completion. */
1337 redrawcmd();
1338 goto cmdline_changed;
1339 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001340 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342 }
1343 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001344 got_int = FALSE; /* don't abandon the command line */
1345 did_emsg = FALSE;
1346 emsg_on_display = FALSE;
1347 redrawcmd();
1348 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350#endif
1351 else
1352 {
1353 if (c == Ctrl_G && p_im && restart_edit == 0)
1354 restart_edit = 'a';
1355 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1356 in history */
1357 goto returncmd; /* back to Normal mode */
1358 }
1359 }
1360
1361#ifdef FEAT_CMDWIN
1362 if (c == cedit_key || c == K_CMDWIN)
1363 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001364 if (ex_normal_busy == 0 && got_int == FALSE)
1365 {
1366 /*
1367 * Open a window to edit the command line (and history).
1368 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001369 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001370 some_key_typed = TRUE;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373# ifdef FEAT_DIGRAPHS
1374 else
1375# endif
1376#endif
1377#ifdef FEAT_DIGRAPHS
1378 c = do_digraph(c);
1379#endif
1380
1381 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1382 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1383 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 /* In Ex mode a backslash escapes a newline. */
1385 if (exmode_active
1386 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001387 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001388 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001389 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001391 if (c == K_KENTER)
1392 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001394 else
1395 {
1396 gotesc = FALSE; /* Might have typed ESC previously, don't
1397 truncate the cmdline now. */
1398 if (ccheck_abbr(c + ABBR_OFF))
1399 goto cmdline_changed;
1400 if (!cmd_silent)
1401 {
1402 windgoto(msg_row, 0);
1403 out_flush();
1404 }
1405 break;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408
1409 /*
1410 * Completion for 'wildchar' or 'wildcharm' key.
1411 * - hitting <ESC> twice means: abandon command line.
1412 * - wildcard expansion is only done when the 'wildchar' key is really
1413 * typed, not when it comes from a macro
1414 */
1415 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1416 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001417 int options = WILD_NO_BEEP;
1418 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1419 options |= WILD_BUFLASTUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1421 {
1422 /* if 'wildmode' contains "list" may still need to list */
1423 if (xpc.xp_numfiles > 1
1424 && !did_wild_list
1425 && (wim_flags[wim_index] & WIM_LIST))
1426 {
1427 (void)showmatches(&xpc, FALSE);
1428 redrawcmd();
1429 did_wild_list = TRUE;
1430 }
1431 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001432 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001433 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001435 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001436 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
1438 res = OK; /* don't insert 'wildchar' now */
1439 }
1440 else /* typed p_wc first time */
1441 {
1442 wim_index = 0;
1443 j = ccline.cmdpos;
1444 /* if 'wildmode' first contains "longest", get longest
1445 * common part */
1446 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001447 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001448 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001450 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001451 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 /* if interrupted while completing, behave like it failed */
1454 if (got_int)
1455 {
1456 (void)vpeekc(); /* remove <C-C> from input stream */
1457 got_int = FALSE; /* don't abandon the command line */
1458 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1459#ifdef FEAT_WILDMENU
1460 xpc.xp_context = EXPAND_NOTHING;
1461#endif
1462 goto cmdline_changed;
1463 }
1464
1465 /* when more than one match, and 'wildmode' first contains
1466 * "list", or no change and 'wildmode' contains "longest,list",
1467 * list all matches */
1468 if (res == OK && xpc.xp_numfiles > 1)
1469 {
1470 /* a "longest" that didn't do anything is skipped (but not
1471 * "list:longest") */
1472 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1473 wim_index = 1;
1474 if ((wim_flags[wim_index] & WIM_LIST)
1475#ifdef FEAT_WILDMENU
1476 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1477#endif
1478 )
1479 {
1480 if (!(wim_flags[0] & WIM_LONGEST))
1481 {
1482#ifdef FEAT_WILDMENU
1483 int p_wmnu_save = p_wmnu;
1484 p_wmnu = 0;
1485#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001486 /* remove match */
1487 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#ifdef FEAT_WILDMENU
1489 p_wmnu = p_wmnu_save;
1490#endif
1491 }
1492#ifdef FEAT_WILDMENU
1493 (void)showmatches(&xpc, p_wmnu
1494 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1495#else
1496 (void)showmatches(&xpc, FALSE);
1497#endif
1498 redrawcmd();
1499 did_wild_list = TRUE;
1500 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001501 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001502 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001504 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001505 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001508 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510#ifdef FEAT_WILDMENU
1511 else if (xpc.xp_numfiles == -1)
1512 xpc.xp_context = EXPAND_NOTHING;
1513#endif
1514 }
1515 if (wim_index < 3)
1516 ++wim_index;
1517 if (c == ESC)
1518 gotesc = TRUE;
1519 if (res == OK)
1520 goto cmdline_changed;
1521 }
1522
1523 gotesc = FALSE;
1524
1525 /* <S-Tab> goes to last match, in a clumsy way */
1526 if (c == K_S_TAB && KeyTyped)
1527 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001528 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1529 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1530 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 goto cmdline_changed;
1532 }
1533
1534 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1535 c = NL;
1536
1537 do_abbr = TRUE; /* default: check for abbreviation */
1538
1539 /*
1540 * Big switch for a typed command line character.
1541 */
1542 switch (c)
1543 {
1544 case K_BS:
1545 case Ctrl_H:
1546 case K_DEL:
1547 case K_KDEL:
1548 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (c == K_KDEL)
1550 c = K_DEL;
1551
1552 /*
1553 * delete current character is the same as backspace on next
1554 * character, except at end of line
1555 */
1556 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1557 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (has_mbyte && c == K_DEL)
1559 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1560 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (ccline.cmdpos > 0)
1562 {
1563 char_u *p;
1564
1565 j = ccline.cmdpos;
1566 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (has_mbyte)
1568 {
1569 p = mb_prevptr(ccline.cmdbuff, p);
1570 if (c == Ctrl_W)
1571 {
1572 while (p > ccline.cmdbuff && vim_isspace(*p))
1573 p = mb_prevptr(ccline.cmdbuff, p);
1574 i = mb_get_class(p);
1575 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1576 p = mb_prevptr(ccline.cmdbuff, p);
1577 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001578 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001581 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1584 --p;
1585 i = vim_iswordc(p[-1]);
1586 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1587 && vim_iswordc(p[-1]) == i)
1588 --p;
1589 }
1590 else
1591 --p;
1592 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1593 ccline.cmdlen -= j - ccline.cmdpos;
1594 i = ccline.cmdpos;
1595 while (i < ccline.cmdlen)
1596 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1597
1598 /* Truncate at the end, required for multi-byte chars. */
1599 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001600#ifdef FEAT_SEARCH_EXTRA
1601 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001602 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001603 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001604 /* save view settings, so that the screen
1605 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001606 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001607 }
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 redrawcmd();
1610 }
1611 else if (ccline.cmdlen == 0 && c != Ctrl_W
1612 && ccline.cmdprompt == NULL && indent == 0)
1613 {
1614 /* In ex and debug mode it doesn't make sense to return. */
1615 if (exmode_active
1616#ifdef FEAT_EVAL
1617 || ccline.cmdfirstc == '>'
1618#endif
1619 )
1620 goto cmdline_not_changed;
1621
Bram Moolenaard23a8232018-02-10 18:45:26 +01001622 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (!cmd_silent)
1624 {
1625#ifdef FEAT_RIGHTLEFT
1626 if (cmdmsg_rl)
1627 msg_col = Columns;
1628 else
1629#endif
1630 msg_col = 0;
1631 msg_putchar(' '); /* delete ':' */
1632 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001633#ifdef FEAT_SEARCH_EXTRA
1634 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001635 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 redraw_cmdline = TRUE;
1638 goto returncmd; /* back to cmd mode */
1639 }
1640 goto cmdline_changed;
1641
1642 case K_INS:
1643 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 ccline.overstrike = !ccline.overstrike;
1645#ifdef CURSOR_SHAPE
1646 ui_cursor_shape(); /* may show different cursor shape */
1647#endif
1648 goto cmdline_not_changed;
1649
1650 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001651 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 /* ":lmap" mappings exists, toggle use of mappings. */
1654 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001655#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 im_set_active(FALSE); /* Disable input method */
1657#endif
1658 if (b_im_ptr != NULL)
1659 {
1660 if (State & LANGMAP)
1661 *b_im_ptr = B_IMODE_LMAP;
1662 else
1663 *b_im_ptr = B_IMODE_NONE;
1664 }
1665 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 else
1668 {
1669 /* There are no ":lmap" mappings, toggle IM. When
1670 * 'imdisable' is set don't try getting the status, it's
1671 * always off. */
1672 if ((p_imdisable && b_im_ptr != NULL)
1673 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1674 {
1675 im_set_active(FALSE); /* Disable input method */
1676 if (b_im_ptr != NULL)
1677 *b_im_ptr = B_IMODE_NONE;
1678 }
1679 else
1680 {
1681 im_set_active(TRUE); /* Enable input method */
1682 if (b_im_ptr != NULL)
1683 *b_im_ptr = B_IMODE_IM;
1684 }
1685 }
1686#endif
1687 if (b_im_ptr != NULL)
1688 {
1689 if (b_im_ptr == &curbuf->b_p_iminsert)
1690 set_iminsert_global();
1691 else
1692 set_imsearch_global();
1693 }
1694#ifdef CURSOR_SHAPE
1695 ui_cursor_shape(); /* may show different cursor shape */
1696#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001697#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 /* Show/unshow value of 'keymap' in status lines later. */
1699 status_redraw_curbuf();
1700#endif
1701 goto cmdline_not_changed;
1702
1703/* case '@': only in very old vi */
1704 case Ctrl_U:
1705 /* delete all characters left of the cursor */
1706 j = ccline.cmdpos;
1707 ccline.cmdlen -= j;
1708 i = ccline.cmdpos = 0;
1709 while (i < ccline.cmdlen)
1710 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1711 /* Truncate at the end, required for multi-byte chars. */
1712 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001713#ifdef FEAT_SEARCH_EXTRA
1714 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001715 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 redrawcmd();
1718 goto cmdline_changed;
1719
1720#ifdef FEAT_CLIPBOARD
1721 case Ctrl_Y:
1722 /* Copy the modeless selection, if there is one. */
1723 if (clip_star.state != SELECT_CLEARED)
1724 {
1725 if (clip_star.state == SELECT_DONE)
1726 clip_copy_modeless_selection(TRUE);
1727 goto cmdline_not_changed;
1728 }
1729 break;
1730#endif
1731
1732 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1733 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001734 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001735 * ":normal" runs out of characters. */
1736 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001737 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 goto cmdline_not_changed;
1739
1740 gotesc = TRUE; /* will free ccline.cmdbuff after
1741 putting it in history */
1742 goto returncmd; /* back to cmd mode */
1743
1744 case Ctrl_R: /* insert register */
1745#ifdef USE_ON_FLY_SCROLL
1746 dont_scroll = TRUE; /* disallow scrolling here */
1747#endif
1748 putcmdline('"', TRUE);
1749 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001750 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001751 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (i == Ctrl_O)
1753 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1754 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001755 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001756 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001758 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#ifdef FEAT_EVAL
1760 /*
1761 * Insert the result of an expression.
1762 * Need to save the current command line, to be able to enter
1763 * a new one...
1764 */
1765 new_cmdpos = -1;
1766 if (c == '=')
1767 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001768 if (ccline.cmdfirstc == '=' // can't do this recursively
1769 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 beep_flush();
1772 c = ESC;
1773 }
1774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
1777#endif
1778 if (c != ESC) /* use ESC to cancel inserting register */
1779 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001780 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001781
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001782#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001783 /* When there was a serious error abort getting the
1784 * command line. */
1785 if (aborting())
1786 {
1787 gotesc = TRUE; /* will free ccline.cmdbuff after
1788 putting it in history */
1789 goto returncmd; /* back to cmd mode */
1790 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 KeyTyped = FALSE; /* Don't do p_wc completion. */
1793#ifdef FEAT_EVAL
1794 if (new_cmdpos >= 0)
1795 {
1796 /* set_cmdline_pos() was used */
1797 if (new_cmdpos > ccline.cmdlen)
1798 ccline.cmdpos = ccline.cmdlen;
1799 else
1800 ccline.cmdpos = new_cmdpos;
1801 }
1802#endif
1803 }
1804 redrawcmd();
1805 goto cmdline_changed;
1806
1807 case Ctrl_D:
1808 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1809 break; /* Use ^D as normal char instead */
1810
1811 redrawcmd();
1812 continue; /* don't do incremental search now */
1813
1814 case K_RIGHT:
1815 case K_S_RIGHT:
1816 case K_C_RIGHT:
1817 do
1818 {
1819 if (ccline.cmdpos >= ccline.cmdlen)
1820 break;
1821 i = cmdline_charsize(ccline.cmdpos);
1822 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1823 break;
1824 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001826 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 + ccline.cmdpos);
1828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 ++ccline.cmdpos;
1830 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001831 while ((c == K_S_RIGHT || c == K_C_RIGHT
1832 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (has_mbyte)
1835 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 goto cmdline_not_changed;
1837
1838 case K_LEFT:
1839 case K_S_LEFT:
1840 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001841 if (ccline.cmdpos == 0)
1842 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 do
1844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (has_mbyte) /* move to first byte of char */
1847 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1848 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1850 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001851 while (ccline.cmdpos > 0
1852 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001853 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (has_mbyte)
1856 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 goto cmdline_not_changed;
1858
1859 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001860 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001861 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar4f974752019-02-17 17:44:42 +01001863#ifdef FEAT_GUI_MSWIN
1864 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1865 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001866 case K_F4:
1867 if (mod_mask == MOD_MASK_ALT)
1868 {
1869 redrawcmd(); /* somehow the cmdline is cleared */
1870 goto cmdline_not_changed;
1871 }
1872 break;
1873#endif
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 case K_MIDDLEDRAG:
1876 case K_MIDDLERELEASE:
1877 goto cmdline_not_changed; /* Ignore mouse */
1878
1879 case K_MIDDLEMOUSE:
1880# ifdef FEAT_GUI
1881 /* When GUI is active, also paste when 'mouse' is empty */
1882 if (!gui.in_use)
1883# endif
1884 if (!mouse_has(MOUSE_COMMAND))
1885 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001886# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001888 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001890# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001891 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 redrawcmd();
1893 goto cmdline_changed;
1894
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001895# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001897 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 redrawcmd();
1899 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 case K_LEFTDRAG:
1903 case K_LEFTRELEASE:
1904 case K_RIGHTDRAG:
1905 case K_RIGHTRELEASE:
1906 /* Ignore drag and release events when the button-down wasn't
1907 * seen before. */
1908 if (ignore_drag_release)
1909 goto cmdline_not_changed;
1910 /* FALLTHROUGH */
1911 case K_LEFTMOUSE:
1912 case K_RIGHTMOUSE:
1913 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1914 ignore_drag_release = TRUE;
1915 else
1916 ignore_drag_release = FALSE;
1917# ifdef FEAT_GUI
1918 /* When GUI is active, also move when 'mouse' is empty */
1919 if (!gui.in_use)
1920# endif
1921 if (!mouse_has(MOUSE_COMMAND))
1922 goto cmdline_not_changed; /* Ignore mouse */
1923# ifdef FEAT_CLIPBOARD
1924 if (mouse_row < cmdline_row && clip_star.available)
1925 {
1926 int button, is_click, is_drag;
1927
1928 /*
1929 * Handle modeless selection.
1930 */
1931 button = get_mouse_button(KEY2TERMCAP1(c),
1932 &is_click, &is_drag);
1933 if (mouse_model_popup() && button == MOUSE_LEFT
1934 && (mod_mask & MOD_MASK_SHIFT))
1935 {
1936 /* Translate shift-left to right button. */
1937 button = MOUSE_RIGHT;
1938 mod_mask &= ~MOD_MASK_SHIFT;
1939 }
1940 clip_modeless(button, is_click, is_drag);
1941 goto cmdline_not_changed;
1942 }
1943# endif
1944
1945 set_cmdspos();
1946 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1947 ++ccline.cmdpos)
1948 {
1949 i = cmdline_charsize(ccline.cmdpos);
1950 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1951 && mouse_col < ccline.cmdspos % Columns + i)
1952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (has_mbyte)
1954 {
1955 /* Count ">" for double-wide char that doesn't fit. */
1956 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001957 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 + ccline.cmdpos) - 1;
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 ccline.cmdspos += i;
1961 }
1962 goto cmdline_not_changed;
1963
1964 /* Mouse scroll wheel: ignored here */
1965 case K_MOUSEDOWN:
1966 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001967 case K_MOUSELEFT:
1968 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /* Alternate buttons ignored here */
1970 case K_X1MOUSE:
1971 case K_X1DRAG:
1972 case K_X1RELEASE:
1973 case K_X2MOUSE:
1974 case K_X2DRAG:
1975 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001976 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto cmdline_not_changed;
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef FEAT_GUI
1980 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1981 case K_LEFTRELEASE_NM:
1982 goto cmdline_not_changed;
1983
1984 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001985 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 gui_do_scroll();
1988 redrawcmd();
1989 }
1990 goto cmdline_not_changed;
1991
1992 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001993 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001995 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 redrawcmd();
1997 }
1998 goto cmdline_not_changed;
1999#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002000#ifdef FEAT_GUI_TABLINE
2001 case K_TABLINE:
2002 case K_TABMENU:
2003 /* Don't want to change any tabs here. Make sure the same tab
2004 * is still selected. */
2005 if (gui_use_tabline())
2006 gui_mch_set_curtab(tabpage_index(curtab));
2007 goto cmdline_not_changed;
2008#endif
2009
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 case K_SELECT: /* end of Select mode mapping - ignore */
2011 goto cmdline_not_changed;
2012
2013 case Ctrl_B: /* begin of command line */
2014 case K_HOME:
2015 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 case K_S_HOME:
2017 case K_C_HOME:
2018 ccline.cmdpos = 0;
2019 set_cmdspos();
2020 goto cmdline_not_changed;
2021
2022 case Ctrl_E: /* end of command line */
2023 case K_END:
2024 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 case K_S_END:
2026 case K_C_END:
2027 ccline.cmdpos = ccline.cmdlen;
2028 set_cmdspos_cursor();
2029 goto cmdline_not_changed;
2030
2031 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002032 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 break;
2034 goto cmdline_changed;
2035
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002036 case Ctrl_L:
2037#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002038 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002039 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002040#endif
2041
2042 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002043 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 break;
2045 goto cmdline_changed;
2046
2047 case Ctrl_N: /* next match */
2048 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002049 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002051 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2052 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002054 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002056 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 case K_UP:
2058 case K_DOWN:
2059 case K_S_UP:
2060 case K_S_DOWN:
2061 case K_PAGEUP:
2062 case K_KPAGEUP:
2063 case K_PAGEDOWN:
2064 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002065 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 goto cmdline_not_changed;
2067
2068 i = hiscnt;
2069
2070 /* save current command string so it can be restored later */
2071 if (lookfor == NULL)
2072 {
2073 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2074 goto cmdline_not_changed;
2075 lookfor[ccline.cmdpos] = NUL;
2076 }
2077
2078 j = (int)STRLEN(lookfor);
2079 for (;;)
2080 {
2081 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002082 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002083 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002085 if (hiscnt == get_hislen()) /* first time */
2086 hiscnt = *get_hisidx(histype);
2087 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2088 hiscnt = get_hislen() - 1;
2089 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 --hiscnt;
2091 else /* at top of list */
2092 {
2093 hiscnt = i;
2094 break;
2095 }
2096 }
2097 else /* one step forwards */
2098 {
2099 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002100 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002102 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 break;
2104 }
2105
2106 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002107 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002109 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 hiscnt = 0;
2111 else
2112 ++hiscnt;
2113 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002114 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
2116 hiscnt = i;
2117 break;
2118 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002119 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002120 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002121 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 lookfor, (size_t)j) == 0)
2123 break;
2124 }
2125
2126 if (hiscnt != i) /* jumped to other entry */
2127 {
2128 char_u *p;
2129 int len;
2130 int old_firstc;
2131
Bram Moolenaar438d1762018-09-30 17:11:48 +02002132 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002133 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002134 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 p = lookfor; /* back to the old one */
2136 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002137 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139 if (histype == HIST_SEARCH
2140 && p != lookfor
2141 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2142 {
2143 /* Correct for the separator character used when
2144 * adding the history entry vs the one used now.
2145 * First loop: count length.
2146 * Second loop: copy the characters. */
2147 for (i = 0; i <= 1; ++i)
2148 {
2149 len = 0;
2150 for (j = 0; p[j] != NUL; ++j)
2151 {
2152 /* Replace old sep with new sep, unless it is
2153 * escaped. */
2154 if (p[j] == old_firstc
2155 && (j == 0 || p[j - 1] != '\\'))
2156 {
2157 if (i > 0)
2158 ccline.cmdbuff[len] = firstc;
2159 }
2160 else
2161 {
2162 /* Escape new sep, unless it is already
2163 * escaped. */
2164 if (p[j] == firstc
2165 && (j == 0 || p[j - 1] != '\\'))
2166 {
2167 if (i > 0)
2168 ccline.cmdbuff[len] = '\\';
2169 ++len;
2170 }
2171 if (i > 0)
2172 ccline.cmdbuff[len] = p[j];
2173 }
2174 ++len;
2175 }
2176 if (i == 0)
2177 {
2178 alloc_cmdbuff(len);
2179 if (ccline.cmdbuff == NULL)
2180 goto returncmd;
2181 }
2182 }
2183 ccline.cmdbuff[len] = NUL;
2184 }
2185 else
2186 {
2187 alloc_cmdbuff((int)STRLEN(p));
2188 if (ccline.cmdbuff == NULL)
2189 goto returncmd;
2190 STRCPY(ccline.cmdbuff, p);
2191 }
2192
2193 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2194 redrawcmd();
2195 goto cmdline_changed;
2196 }
2197 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002198 goto cmdline_not_changed;
2199
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002200#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002201 case Ctrl_G: /* next match */
2202 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002203 if (may_adjust_incsearch_highlighting(
2204 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002205 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#endif
2208
2209 case Ctrl_V:
2210 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002212 int prev_mod_mask = mod_mask;
2213
2214 ignore_drag_release = TRUE;
2215 putcmdline('^', TRUE);
2216 c = get_literal(); // get next (two) character(s)
2217 do_abbr = FALSE; // don't do abbreviation now
2218 extra_char = NUL;
2219 // may need to remove ^ when composing char was typed
2220 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2221 {
2222 draw_cmdline(ccline.cmdpos,
2223 ccline.cmdlen - ccline.cmdpos);
2224 msg_putchar(' ');
2225 cursorcmd();
2226 }
2227
2228 if ((c == ESC || c == CSI)
2229 && !(prev_mod_mask & MOD_MASK_SHIFT))
2230 // Using CTRL-V: Change any modifyOtherKeys ESC
2231 // sequence to a normal key. Don't do this for
2232 // CTRL-SHIFT-V.
2233 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 break;
2237
2238#ifdef FEAT_DIGRAPHS
2239 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002242# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 dont_scroll = TRUE; /* disallow scrolling here */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002246 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (c != NUL)
2248 break;
2249
2250 redrawcmd();
2251 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002252#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254#ifdef FEAT_RIGHTLEFT
2255 case Ctrl__: /* CTRL-_: switch language mode */
2256 if (!p_ari)
2257 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002258 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 goto cmdline_not_changed;
2260#endif
2261
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002262 case K_PS:
2263 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2264 goto cmdline_changed;
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 default:
2267#ifdef UNIX
2268 if (c == intr_char)
2269 {
2270 gotesc = TRUE; /* will free ccline.cmdbuff after
2271 putting it in history */
2272 goto returncmd; /* back to Normal mode */
2273 }
2274#endif
2275 /*
2276 * Normal character with no special meaning. Just set mod_mask
2277 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2278 * the string <S-Space>. This should only happen after ^V.
2279 */
2280 if (!IS_SPECIAL(c))
2281 mod_mask = 0x0;
2282 break;
2283 }
2284 /*
2285 * End of switch on command line character.
2286 * We come here if we have a normal character.
2287 */
2288
Bram Moolenaar13505972019-01-24 15:04:48 +01002289 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2290 && (ccheck_abbr(
2291 // Add ABBR_OFF for characters above 0x100, this is
2292 // what check_abbr() expects.
2293 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2294 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 goto cmdline_changed;
2296
2297 /*
2298 * put the character in the command line
2299 */
2300 if (IS_SPECIAL(c) || mod_mask != 0)
2301 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2302 else
2303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (has_mbyte)
2305 {
2306 j = (*mb_char2bytes)(c, IObuff);
2307 IObuff[j] = NUL; /* exclude composing chars */
2308 put_on_cmdline(IObuff, j, TRUE);
2309 }
2310 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 IObuff[0] = c;
2313 put_on_cmdline(IObuff, 1, TRUE);
2314 }
2315 }
2316 goto cmdline_changed;
2317
2318/*
2319 * This part implements incremental searches for "/" and "?"
2320 * Jump to cmdline_not_changed when a character has been read but the command
2321 * line did not change. Then we only search and redraw if something changed in
2322 * the past.
2323 * Jump to cmdline_changed when the command line did change.
2324 * (Sorry for the goto's, I know it is ugly).
2325 */
2326cmdline_not_changed:
2327#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002328 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 continue;
2330#endif
2331
2332cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002333 /* Trigger CmdlineChanged autocommands. */
2334 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002337 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338#endif
2339
2340#ifdef FEAT_RIGHTLEFT
2341 if (cmdmsg_rl
2342# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002343 || (p_arshape && !p_tbidi
2344 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345# endif
2346 )
2347 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002348 * right-left typing. Not efficient, but it works.
2349 * Do it only when there are no characters left to read
2350 * to avoid useless intermediate redraws. */
2351 if (vpeekc() == NUL)
2352 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#endif
2354 }
2355
2356returncmd:
2357
2358#ifdef FEAT_RIGHTLEFT
2359 cmdmsg_rl = FALSE;
2360#endif
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002363 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002366 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#endif
2368
2369 if (ccline.cmdbuff != NULL)
2370 {
2371 /*
2372 * Put line in history buffer (":" and "=" only when it was typed).
2373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (ccline.cmdlen && firstc != NUL
2375 && (some_key_typed || histype == HIST_SEARCH))
2376 {
2377 add_to_history(histype, ccline.cmdbuff, TRUE,
2378 histype == HIST_SEARCH ? firstc : NUL);
2379 if (firstc == ':')
2380 {
2381 vim_free(new_last_cmdline);
2382 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2383 }
2384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002386 if (gotesc)
2387 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389
2390 /*
2391 * If the screen was shifted up, redraw the whole screen (later).
2392 * If the line is too long, clear it, so ruler and shown command do
2393 * not get printed in the middle of it.
2394 */
2395 msg_check();
2396 msg_scroll = save_msg_scroll;
2397 redir_off = FALSE;
2398
2399 /* When the command line was typed, no need for a wait-return prompt. */
2400 if (some_key_typed)
2401 need_wait_return = FALSE;
2402
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002403 /* Trigger CmdlineLeave autocommands. */
2404 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002405
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002407#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2409 im_save_status(b_im_ptr);
2410 im_set_active(FALSE);
2411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#ifdef CURSOR_SHAPE
2414 ui_cursor_shape(); /* may show different cursor shape */
2415#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002416 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaar438d1762018-09-30 17:11:48 +02002418theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002419 {
2420 char_u *p = ccline.cmdbuff;
2421
Bram Moolenaar438d1762018-09-30 17:11:48 +02002422 if (did_save_ccline)
2423 restore_cmdline(&save_ccline);
2424 else
2425 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002426 return p;
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
2430#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2431/*
2432 * Get a command line with a prompt.
2433 * This is prepared to be called recursively from getcmdline() (e.g. by
2434 * f_input() when evaluating an expression from CTRL-R =).
2435 * Returns the command line in allocated memory, or NULL.
2436 */
2437 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002438getcmdline_prompt(
2439 int firstc,
2440 char_u *prompt, /* command line prompt */
2441 int attr, /* attributes for prompt */
2442 int xp_context, /* type of expansion */
2443 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002446 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002447 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002449 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar438d1762018-09-30 17:11:48 +02002451 if (ccline.cmdbuff != NULL)
2452 {
2453 // Save the values of the current cmdline and restore them below.
2454 save_cmdline(&save_ccline);
2455 did_save_ccline = TRUE;
2456 }
2457
Bram Moolenaar66b51422019-08-18 21:44:12 +02002458 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 ccline.cmdprompt = prompt;
2460 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002461# ifdef FEAT_EVAL
2462 ccline.xp_context = xp_context;
2463 ccline.xp_arg = xp_arg;
2464 ccline.input_fn = (firstc == '@');
2465# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002466 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002467 s = getcmdline_int(firstc, 1L, 0, FALSE);
2468
2469 if (did_save_ccline)
2470 restore_cmdline(&save_ccline);
2471
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002472 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002473 /* Restore msg_col, the prompt from input() may have changed it.
2474 * But only if called recursively and the commandline is therefore being
2475 * restored to an old one; if not, the input() prompt stays on the screen,
2476 * so we need its modified msg_col left intact. */
2477 if (ccline.cmdbuff != NULL)
2478 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 return s;
2481}
2482#endif
2483
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002484/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002485 * Read the 'wildmode' option, fill wim_flags[].
2486 */
2487 int
2488check_opt_wim(void)
2489{
2490 char_u new_wim_flags[4];
2491 char_u *p;
2492 int i;
2493 int idx = 0;
2494
2495 for (i = 0; i < 4; ++i)
2496 new_wim_flags[i] = 0;
2497
2498 for (p = p_wim; *p; ++p)
2499 {
2500 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2501 ;
2502 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2503 return FAIL;
2504 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2505 new_wim_flags[idx] |= WIM_LONGEST;
2506 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2507 new_wim_flags[idx] |= WIM_FULL;
2508 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2509 new_wim_flags[idx] |= WIM_LIST;
2510 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2511 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2512 else
2513 return FAIL;
2514 p += i;
2515 if (*p == NUL)
2516 break;
2517 if (*p == ',')
2518 {
2519 if (idx == 3)
2520 return FAIL;
2521 ++idx;
2522 }
2523 }
2524
2525 /* fill remaining entries with last flag */
2526 while (idx < 3)
2527 {
2528 new_wim_flags[idx + 1] = new_wim_flags[idx];
2529 ++idx;
2530 }
2531
2532 /* only when there are no errors, wim_flags[] is changed */
2533 for (i = 0; i < 4; ++i)
2534 wim_flags[i] = new_wim_flags[i];
2535 return OK;
2536}
2537
2538/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002539 * Return TRUE when the text must not be changed and we can't switch to
2540 * another window or buffer. Used when editing the command line, evaluating
2541 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002542 */
2543 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002544text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002545{
2546#ifdef FEAT_CMDWIN
2547 if (cmdwin_type != 0)
2548 return TRUE;
2549#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002550 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002551}
2552
2553/*
2554 * Give an error message for a command that isn't allowed while the cmdline
2555 * window is open or editing the cmdline in another way.
2556 */
2557 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002558text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002559{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002561}
2562
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002563 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002564get_text_locked_msg(void)
2565{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002566#ifdef FEAT_CMDWIN
2567 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002568 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002570 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002571}
2572
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002573/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002574 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2575 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002576 */
2577 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002578curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002579{
2580 if (curbuf_lock > 0)
2581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002582 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002583 return TRUE;
2584 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002585 return allbuf_locked();
2586}
2587
2588/*
2589 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2590 * message.
2591 */
2592 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002593allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002594{
2595 if (allbuf_lock > 0)
2596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002597 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002598 return TRUE;
2599 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002600 return FALSE;
2601}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002604cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2607 if (cmdline_star > 0) /* showing '*', always 1 position */
2608 return 1;
2609#endif
2610 return ptr2cells(ccline.cmdbuff + idx);
2611}
2612
2613/*
2614 * Compute the offset of the cursor on the command line for the prompt and
2615 * indent.
2616 */
2617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002618set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002620 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 ccline.cmdspos = 1 + ccline.cmdindent;
2622 else
2623 ccline.cmdspos = 0 + ccline.cmdindent;
2624}
2625
2626/*
2627 * Compute the screen position for the cursor on the command line.
2628 */
2629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002630set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 int i, m, c;
2633
2634 set_cmdspos();
2635 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002638 if (m < 0) /* overflow, Columns or Rows at weird value */
2639 m = MAXCOL;
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 else
2642 m = MAXCOL;
2643 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2644 {
2645 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2647 if (has_mbyte)
2648 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002649 /* If the cmdline doesn't fit, show cursor on last visible char.
2650 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if ((ccline.cmdspos += c) >= m)
2652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 ccline.cmdspos -= c;
2654 break;
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002657 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659}
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661/*
2662 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2663 * character that doesn't fit, so that a ">" must be displayed.
2664 */
2665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002666correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002668 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2670 && ccline.cmdspos % Columns + cells > Columns)
2671 ccline.cmdspos++;
2672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674/*
2675 * Get an Ex command line for the ":" command.
2676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002678getexline(
2679 int c, /* normally ':', NUL for ":append" */
2680 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002681 int indent, /* indent for inside conditionals */
2682 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 /* When executing a register, remove ':' that's in front of each line. */
2685 if (exec_from_reg && vpeekc() == ':')
2686 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002687 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688}
2689
2690/*
2691 * Get an Ex command line for Ex mode.
2692 * In Ex mode we only use the OS supplied line editing features and no
2693 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002694 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002697getexmodeline(
2698 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002699 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002700 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002701 int indent, /* indent for inside conditionals */
2702 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 garray_T line_ga;
2705 char_u *pend;
2706 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002707 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 int escaped = FALSE; /* CTRL-V typed */
2709 int vcol = 0;
2710 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002711 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002712 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 /* Switch cursor on now. This avoids that it happens after the "\n", which
2715 * confuses the system function that computes tabstops. */
2716 cursor_on();
2717
2718 /* always start in column 0; write a newline if necessary */
2719 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002720 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002724 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 if (p_prompt)
2726 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 while (indent-- > 0)
2728 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731
2732 ga_init2(&line_ga, 1, 30);
2733
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002734 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002736 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002737 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002738 while (indent >= 8)
2739 {
2740 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002741 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002742 indent -= 8;
2743 }
2744 while (indent-- > 0)
2745 {
2746 ga_append(&line_ga, ' ');
2747 msg_putchar(' ');
2748 }
2749 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 ++no_mapping;
2751 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 /*
2754 * Get the line, one character at a time.
2755 */
2756 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002759 long sw;
2760 char_u *s;
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (ga_grow(&line_ga, 40) == FAIL)
2763 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaarba748c82017-02-26 14:00:07 +01002765 /*
2766 * Get one character at a time.
2767 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002768 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002769
2770 /* Check for a ":normal" command and no more characters left. */
2771 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2772 c1 = '\n';
2773 else
2774 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777 * Handle line editing.
2778 * Previously this was left to the system, putting the terminal in
2779 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 msg_putchar('\n');
2784 break;
2785 }
2786
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002787 if (c1 == K_PS)
2788 {
2789 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2790 goto redraw;
2791 }
2792
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 if (!escaped)
2794 {
2795 /* CR typed means "enter", which is NL */
2796 if (c1 == '\r')
2797 c1 = '\n';
2798
2799 if (c1 == BS || c1 == K_BS
2800 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 if (line_ga.ga_len > 0)
2803 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002804 if (has_mbyte)
2805 {
2806 p = (char_u *)line_ga.ga_data;
2807 p[line_ga.ga_len] = NUL;
2808 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2809 line_ga.ga_len -= len;
2810 }
2811 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002812 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813 goto redraw;
2814 }
2815 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 msg_col = startcol;
2821 msg_clr_eos();
2822 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002823 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 }
2825
2826 if (c1 == Ctrl_T)
2827 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002828 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002829 p = (char_u *)line_ga.ga_data;
2830 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002831 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002832 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002834 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002836 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002837 p = (char_u *)line_ga.ga_data;
2838 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2840 *s = ' ';
2841 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843redraw:
2844 /* redraw the line */
2845 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002847 p = (char_u *)line_ga.ga_data;
2848 p[line_ga.ga_len] = NUL;
2849 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002855 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002856 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002860 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002861 msg_outtrans_len(p, len);
2862 vcol += ptr2cells(p);
2863 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002866 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002867 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 continue;
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 if (c1 == Ctrl_D)
2872 {
2873 /* Delete one shiftwidth. */
2874 p = (char_u *)line_ga.ga_data;
2875 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 if (prev_char == '^')
2878 ex_keep_indent = TRUE;
2879 indent = 0;
2880 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 else
2883 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002885 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002886 if (indent > 0)
2887 {
2888 --indent;
2889 indent -= indent % get_sw_value(curbuf);
2890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002892 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002893 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002894 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2896 --line_ga.ga_len;
2897 }
2898 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900
2901 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2902 {
2903 escaped = TRUE;
2904 continue;
2905 }
2906
2907 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2908 if (IS_SPECIAL(c1))
2909 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911
2912 if (IS_SPECIAL(c1))
2913 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002914 if (has_mbyte)
2915 len = (*mb_char2bytes)(c1,
2916 (char_u *)line_ga.ga_data + line_ga.ga_len);
2917 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002918 {
2919 len = 1;
2920 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2921 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922 if (c1 == '\n')
2923 msg_putchar('\n');
2924 else if (c1 == TAB)
2925 {
2926 /* Don't use chartabsize(), 'ts' can be different */
2927 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002928 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002929 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002934 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002935 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002937 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002938 escaped = FALSE;
2939
2940 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002941 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002942
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002943 /* We are done when a NL is entered, but not when it comes after an
2944 * odd number of backslashes, that results in a NUL. */
2945 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002947 int bcount = 0;
2948
2949 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2950 ++bcount;
2951
2952 if (bcount > 0)
2953 {
2954 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2955 * "\NL", etc. */
2956 line_ga.ga_len -= (bcount + 1) / 2;
2957 pend -= (bcount + 1) / 2;
2958 pend[-1] = '\n';
2959 }
2960
2961 if ((bcount & 1) == 0)
2962 {
2963 --line_ga.ga_len;
2964 --pend;
2965 *pend = NUL;
2966 break;
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 }
2970
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002971 --no_mapping;
2972 --allow_keys;
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 /* make following messages go to the next line */
2975 msg_didout = FALSE;
2976 msg_col = 0;
2977 if (msg_row < Rows - 1)
2978 ++msg_row;
2979 emsg_on_display = FALSE; /* don't want ui_delay() */
2980
2981 if (got_int)
2982 ga_clear(&line_ga);
2983
2984 return (char_u *)line_ga.ga_data;
2985}
2986
Bram Moolenaare344bea2005-09-01 20:46:49 +00002987# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2988 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989/*
2990 * Return TRUE if ccline.overstrike is on.
2991 */
2992 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002993cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 return ccline.overstrike;
2996}
2997
2998/*
2999 * Return TRUE if the cursor is at the end of the cmdline.
3000 */
3001 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003002cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 return (ccline.cmdpos >= ccline.cmdlen);
3005}
3006#endif
3007
Bram Moolenaar9372a112005-12-06 19:59:18 +00003008#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009/*
3010 * Return the virtual column number at the current cursor position.
3011 * This is used by the IM code to obtain the start of the preedit string.
3012 */
3013 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003014cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3017 return MAXCOL;
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (has_mbyte)
3020 {
3021 colnr_T col;
3022 int i = 0;
3023
3024 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003025 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 return col;
3028 }
3029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 return ccline.cmdpos;
3031}
3032#endif
3033
3034#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3035/*
3036 * If part of the command line is an IM preedit string, redraw it with
3037 * IM feedback attributes. The cursor position is restored after drawing.
3038 */
3039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003040redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 if ((State & CMDLINE)
3043 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003044 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 && !p_imdisable
3046 && im_is_preediting())
3047 {
3048 int cmdpos = 0;
3049 int cmdspos;
3050 int old_row;
3051 int old_col;
3052 colnr_T col;
3053
3054 old_row = msg_row;
3055 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003056 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 if (has_mbyte)
3059 {
3060 for (col = 0; col < preedit_start_col
3061 && cmdpos < ccline.cmdlen; ++col)
3062 {
3063 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003064 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 }
3066 }
3067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
3069 cmdspos += preedit_start_col;
3070 cmdpos += preedit_start_col;
3071 }
3072
3073 msg_row = cmdline_row + (cmdspos / (int)Columns);
3074 msg_col = cmdspos % (int)Columns;
3075 if (msg_row >= Rows)
3076 msg_row = Rows - 1;
3077
3078 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3079 {
3080 int char_len;
3081 int char_attr;
3082
3083 char_attr = im_get_feedback_attr(col);
3084 if (char_attr < 0)
3085 break; /* end of preedit string */
3086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003088 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 char_len = 1;
3091
3092 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3093 cmdpos += char_len;
3094 }
3095
3096 msg_row = old_row;
3097 msg_col = old_col;
3098 }
3099}
3100#endif /* FEAT_XIM && FEAT_GUI_GTK */
3101
3102/*
3103 * Allocate a new command line buffer.
3104 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 */
3106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 /*
3110 * give some extra space to avoid having to allocate all the time
3111 */
3112 if (len < 80)
3113 len = 100;
3114 else
3115 len += 20;
3116
3117 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3118 ccline.cmdbufflen = len;
3119}
3120
3121/*
3122 * Re-allocate the command line to length len + something extra.
3123 * return FAIL for failure, OK otherwise
3124 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003126realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 char_u *p;
3129
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003130 if (len < ccline.cmdbufflen)
3131 return OK; /* no need to resize */
3132
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 p = ccline.cmdbuff;
3134 alloc_cmdbuff(len); /* will get some more */
3135 if (ccline.cmdbuff == NULL) /* out of memory */
3136 {
3137 ccline.cmdbuff = p; /* keep the old one */
3138 return FAIL;
3139 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003140 /* There isn't always a NUL after the command, but it may need to be
3141 * there, thus copy up to the NUL and add a NUL. */
3142 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3143 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003145
3146 if (ccline.xpc != NULL
3147 && ccline.xpc->xp_pattern != NULL
3148 && ccline.xpc->xp_context != EXPAND_NOTHING
3149 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3150 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003151 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003152
3153 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3154 * to point into the newly allocated memory. */
3155 if (i >= 0 && i <= ccline.cmdlen)
3156 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3157 }
3158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return OK;
3160}
3161
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003162#if defined(FEAT_ARABIC) || defined(PROTO)
3163static char_u *arshape_buf = NULL;
3164
3165# if defined(EXITFREE) || defined(PROTO)
3166 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003167free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003168{
3169 vim_free(arshape_buf);
3170}
3171# endif
3172#endif
3173
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174/*
3175 * Draw part of the cmdline at the current cursor position. But draw stars
3176 * when cmdline_star is TRUE.
3177 */
3178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003179draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3182 int i;
3183
3184 if (cmdline_star > 0)
3185 for (i = 0; i < len; ++i)
3186 {
3187 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003189 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
3191 else
3192#endif
3193#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003194 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 static int buflen = 0;
3197 char_u *p;
3198 int j;
3199 int newlen = 0;
3200 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003201 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 int prev_c = 0;
3203 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003204 int u8c;
3205 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
3208 /*
3209 * Do arabic shaping into a temporary buffer. This is very
3210 * inefficient!
3211 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003212 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 /* Re-allocate the buffer. We keep it around to avoid a lot of
3215 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003216 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003217 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003218 arshape_buf = alloc(buflen);
3219 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 return; /* out of memory */
3221 }
3222
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003223 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3224 {
3225 /* Prepend a space to draw the leading composing char on. */
3226 arshape_buf[0] = ' ';
3227 newlen = 1;
3228 }
3229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 for (j = start; j < start + len; j += mb_l)
3231 {
3232 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003233 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003234 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (ARABIC_CHAR(u8c))
3236 {
3237 /* Do Arabic shaping. */
3238 if (cmdmsg_rl)
3239 {
3240 /* displaying from right to left */
3241 pc = prev_c;
3242 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003243 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (j + mb_l >= start + len)
3245 nc = NUL;
3246 else
3247 nc = utf_ptr2char(p + mb_l);
3248 }
3249 else
3250 {
3251 /* displaying from left to right */
3252 if (j + mb_l >= start + len)
3253 pc = NUL;
3254 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 {
3256 int pcc[MAX_MCO];
3257
3258 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003260 pc1 = pcc[0];
3261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 nc = prev_c;
3263 }
3264 prev_c = u8c;
3265
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003266 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003269 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003271 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3272 if (u8cc[1] != 0)
3273 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276 }
3277 else
3278 {
3279 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 newlen += mb_l;
3282 }
3283 }
3284
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287 else
3288#endif
3289 msg_outtrans_len(ccline.cmdbuff + start, len);
3290}
3291
3292/*
3293 * Put a character on the command line. Shifts the following text to the
3294 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3295 * "c" must be printable (fit in one display cell)!
3296 */
3297 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003298putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 if (cmd_silent)
3301 return;
3302 msg_no_more = TRUE;
3303 msg_putchar(c);
3304 if (shift)
3305 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3306 msg_no_more = FALSE;
3307 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003308 extra_char = c;
3309 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310}
3311
3312/*
3313 * Undo a putcmdline(c, FALSE).
3314 */
3315 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003316unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if (cmd_silent)
3319 return;
3320 msg_no_more = TRUE;
3321 if (ccline.cmdlen == ccline.cmdpos)
3322 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003323 else if (has_mbyte)
3324 draw_cmdline(ccline.cmdpos,
3325 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
3327 draw_cmdline(ccline.cmdpos, 1);
3328 msg_no_more = FALSE;
3329 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003330 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331}
3332
3333/*
3334 * Put the given string, of the given length, onto the command line.
3335 * If len is -1, then STRLEN() is used to calculate the length.
3336 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3337 * part will be redrawn, otherwise it will not. If this function is called
3338 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3339 * called afterwards.
3340 */
3341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003342put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int retval;
3345 int i;
3346 int m;
3347 int c;
3348
3349 if (len < 0)
3350 len = (int)STRLEN(str);
3351
3352 /* Check if ccline.cmdbuff needs to be longer */
3353 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003354 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 else
3356 retval = OK;
3357 if (retval == OK)
3358 {
3359 if (!ccline.overstrike)
3360 {
3361 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3362 ccline.cmdbuff + ccline.cmdpos,
3363 (size_t)(ccline.cmdlen - ccline.cmdpos));
3364 ccline.cmdlen += len;
3365 }
3366 else
3367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (has_mbyte)
3369 {
3370 /* Count nr of characters in the new string. */
3371 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003372 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 ++m;
3374 /* Count nr of bytes in cmdline that are overwritten by these
3375 * characters. */
3376 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003377 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 --m;
3379 if (i < ccline.cmdlen)
3380 {
3381 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3382 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3383 ccline.cmdlen += ccline.cmdpos + len - i;
3384 }
3385 else
3386 ccline.cmdlen = ccline.cmdpos + len;
3387 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003388 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 ccline.cmdlen = ccline.cmdpos + len;
3390 }
3391 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3392 ccline.cmdbuff[ccline.cmdlen] = NUL;
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (enc_utf8)
3395 {
3396 /* When the inserted text starts with a composing character,
3397 * backup to the character before it. There could be two of them.
3398 */
3399 i = 0;
3400 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3401 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3402 {
3403 i = (*mb_head_off)(ccline.cmdbuff,
3404 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3405 ccline.cmdpos -= i;
3406 len += i;
3407 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3408 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003409#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3411 {
3412 /* Check the previous character for Arabic combining pair. */
3413 i = (*mb_head_off)(ccline.cmdbuff,
3414 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3415 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3416 + ccline.cmdpos - i), c))
3417 {
3418 ccline.cmdpos -= i;
3419 len += i;
3420 }
3421 else
3422 i = 0;
3423 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (i != 0)
3426 {
3427 /* Also backup the cursor position. */
3428 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3429 ccline.cmdspos -= i;
3430 msg_col -= i;
3431 if (msg_col < 0)
3432 {
3433 msg_col += Columns;
3434 --msg_row;
3435 }
3436 }
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439 if (redraw && !cmd_silent)
3440 {
3441 msg_no_more = TRUE;
3442 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003443 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3445 /* Avoid clearing the rest of the line too often. */
3446 if (cmdline_row != i || ccline.overstrike)
3447 msg_clr_eos();
3448 msg_no_more = FALSE;
3449 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003450 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003452 m = Columns * Rows;
3453 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003455 }
3456 else
3457 m = MAXCOL;
3458 for (i = 0; i < len; ++i)
3459 {
3460 c = cmdline_charsize(ccline.cmdpos);
3461 /* count ">" for a double-wide char that doesn't fit. */
3462 if (has_mbyte)
3463 correct_cmdspos(ccline.cmdpos, c);
3464 /* Stop cursor at the end of the screen, but do increment the
3465 * insert position, so that entering a very long command
3466 * works, even though you can't see it. */
3467 if (ccline.cmdspos + c < m)
3468 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003469
Bram Moolenaar14184a32019-02-16 15:10:30 +01003470 if (has_mbyte)
3471 {
3472 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3473 if (c > len - i - 1)
3474 c = len - i - 1;
3475 ccline.cmdpos += c;
3476 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003478 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 }
3481 if (redraw)
3482 msg_check();
3483 return retval;
3484}
3485
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486static cmdline_info_T prev_ccline;
3487static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003488
3489/*
3490 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3491 * and overwrite it. But get_cmdline_str() may need it, thus make it
3492 * available globally in prev_ccline.
3493 */
3494 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003496{
3497 if (!prev_ccline_used)
3498 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003500 prev_ccline_used = TRUE;
3501 }
3502 *ccp = prev_ccline;
3503 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003504 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003505}
3506
3507/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003508 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003509 */
3510 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003511restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003512{
3513 ccline = prev_ccline;
3514 prev_ccline = *ccp;
3515}
3516
Bram Moolenaar8299df92004-07-10 09:47:34 +00003517/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003518 * Paste a yank register into the command line.
3519 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003520 * insert_reg() can't be used here, because special characters from the
3521 * register contents will be interpreted as commands.
3522 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003523 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003524 */
3525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003526cmdline_paste(
3527 int regname,
3528 int literally, /* Insert text literally instead of "as typed" */
3529 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003530{
3531 long i;
3532 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003533 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003534 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535
3536 /* check for valid regname; also accept special characters for CTRL-R in
3537 * the command line */
3538 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003539 && regname != Ctrl_A && regname != Ctrl_L
3540 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541 return FAIL;
3542
3543 /* A register containing CTRL-R can cause an endless loop. Allow using
3544 * CTRL-C to break the loop. */
3545 line_breakcheck();
3546 if (got_int)
3547 return FAIL;
3548
3549#ifdef FEAT_CLIPBOARD
3550 regname = may_get_selection(regname);
3551#endif
3552
Bram Moolenaar438d1762018-09-30 17:11:48 +02003553 // Need to set "textlock" to avoid nasty things like going to another
3554 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003555 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003556 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003557 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003558
3559 if (i)
3560 {
3561 /* Got the value of a special register in "arg". */
3562 if (arg == NULL)
3563 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003564
3565 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3566 * part of the word. */
3567 p = arg;
3568 if (p_is && regname == Ctrl_W)
3569 {
3570 char_u *w;
3571 int len;
3572
3573 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003574 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003575 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003576 if (has_mbyte)
3577 {
3578 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3579 if (!vim_iswordc(mb_ptr2char(w - len)))
3580 break;
3581 w -= len;
3582 }
3583 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003584 {
3585 if (!vim_iswordc(w[-1]))
3586 break;
3587 --w;
3588 }
3589 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003590 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003591 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3592 p += len;
3593 }
3594
3595 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596 if (allocated)
3597 vim_free(arg);
3598 return OK;
3599 }
3600
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003601 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003602}
3603
3604/*
3605 * Put a string on the command line.
3606 * When "literally" is TRUE, insert literally.
3607 * When "literally" is FALSE, insert as typed, but don't leave the command
3608 * line.
3609 */
3610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003611cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003612{
3613 int c, cv;
3614
3615 if (literally)
3616 put_on_cmdline(s, -1, TRUE);
3617 else
3618 while (*s != NUL)
3619 {
3620 cv = *s;
3621 if (cv == Ctrl_V && s[1])
3622 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003623 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003624 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003625 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003626 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003627 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3628 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003629#ifdef UNIX
3630 || c == intr_char
3631#endif
3632 || (c == Ctrl_BSL && *s == Ctrl_N))
3633 stuffcharReadbuff(Ctrl_V);
3634 stuffcharReadbuff(c);
3635 }
3636}
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638#ifdef FEAT_WILDMENU
3639/*
3640 * Delete characters on the command line, from "from" to the current
3641 * position.
3642 */
3643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003644cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3647 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3648 ccline.cmdlen -= ccline.cmdpos - from;
3649 ccline.cmdpos = from;
3650}
3651#endif
3652
3653/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003654 * This function is called when the screen size changes and with incremental
3655 * search and in other situations where the command line may have been
3656 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
3658 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003659redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003661 redrawcmdline_ex(TRUE);
3662}
3663
3664 void
3665redrawcmdline_ex(int do_compute_cmdrow)
3666{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (cmd_silent)
3668 return;
3669 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003670 if (do_compute_cmdrow)
3671 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 redrawcmd();
3673 cursorcmd();
3674}
3675
3676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003677redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 int i;
3680
3681 if (cmd_silent)
3682 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003683 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 msg_putchar(ccline.cmdfirstc);
3685 if (ccline.cmdprompt != NULL)
3686 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003687 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3689 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003690 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 --ccline.cmdindent;
3692 }
3693 else
3694 for (i = ccline.cmdindent; i > 0; --i)
3695 msg_putchar(' ');
3696}
3697
3698/*
3699 * Redraw what is currently on the command line.
3700 */
3701 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003702redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 if (cmd_silent)
3705 return;
3706
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003707 /* when 'incsearch' is set there may be no command line while redrawing */
3708 if (ccline.cmdbuff == NULL)
3709 {
3710 windgoto(cmdline_row, 0);
3711 msg_clr_eos();
3712 return;
3713 }
3714
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 msg_start();
3716 redrawcmdprompt();
3717
3718 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3719 msg_no_more = TRUE;
3720 draw_cmdline(0, ccline.cmdlen);
3721 msg_clr_eos();
3722 msg_no_more = FALSE;
3723
3724 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003725 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003726 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728 /*
3729 * An emsg() before may have set msg_scroll. This is used in normal mode,
3730 * in cmdline mode we can reset them now.
3731 */
3732 msg_scroll = FALSE; /* next message overwrites cmdline */
3733
3734 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3735 * in cmdline mode */
3736 skip_redraw = FALSE;
3737}
3738
3739 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003740compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003742 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 cmdline_row = Rows - 1;
3744 else
3745 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003746 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747}
3748
Bram Moolenaar66b51422019-08-18 21:44:12 +02003749 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003750cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752 if (cmd_silent)
3753 return;
3754
3755#ifdef FEAT_RIGHTLEFT
3756 if (cmdmsg_rl)
3757 {
3758 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3759 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3760 if (msg_row <= 0)
3761 msg_row = Rows - 1;
3762 }
3763 else
3764#endif
3765 {
3766 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3767 msg_col = ccline.cmdspos % (int)Columns;
3768 if (msg_row >= Rows)
3769 msg_row = Rows - 1;
3770 }
3771
3772 windgoto(msg_row, msg_col);
3773#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003774 if (p_imst == IM_ON_THE_SPOT)
3775 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776#endif
3777#ifdef MCH_CURSOR_SHAPE
3778 mch_update_cursor();
3779#endif
3780}
3781
3782 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003783gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 msg_start();
3786#ifdef FEAT_RIGHTLEFT
3787 if (cmdmsg_rl)
3788 msg_col = Columns - 1;
3789 else
3790#endif
3791 msg_col = 0; /* always start in column 0 */
3792 if (clr) /* clear the bottom line(s) */
3793 msg_clr_eos(); /* will reset clear_cmdline */
3794 windgoto(cmdline_row, 0);
3795}
3796
3797/*
3798 * Check the word in front of the cursor for an abbreviation.
3799 * Called when the non-id character "c" has been entered.
3800 * When an abbreviation is recognized it is removed from the text with
3801 * backspaces and the replacement string is inserted, followed by "c".
3802 */
3803 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003804ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003806 int spos = 0;
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3809 return FALSE;
3810
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003811 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3812 * Actually accepts any mark. */
3813 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3814 spos++;
3815 if (ccline.cmdlen - spos > 5
3816 && ccline.cmdbuff[spos] == '\''
3817 && ccline.cmdbuff[spos + 2] == ','
3818 && ccline.cmdbuff[spos + 3] == '\'')
3819 spos += 5;
3820 else
3821 /* check abbreviation from the beginning of the commandline */
3822 spos = 0;
3823
3824 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825}
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828 * Escape special characters in "fname" for when used as a file name argument
3829 * after a Vim command, or, when "shell" is non-zero, a shell command.
3830 * Returns the result in allocated memory.
3831 */
3832 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003833vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003834{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003835 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003836#ifdef BACKSLASH_IN_FILENAME
3837 char_u buf[20];
3838 int j = 0;
3839
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003840 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003841 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003842 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003843 buf[j++] = *p;
3844 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003845 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003846#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003847 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3848 if (shell && csh_like_shell() && p != NULL)
3849 {
3850 char_u *s;
3851
3852 /* For csh and similar shells need to put two backslashes before '!'.
3853 * One is taken by Vim, one by the shell. */
3854 s = vim_strsave_escaped(p, (char_u *)"!");
3855 vim_free(p);
3856 p = s;
3857 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003858#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003859
3860 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3861 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003862 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003863 escape_fname(&p);
3864
3865 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003866}
3867
3868/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003869 * Put a backslash before the file name in "pp", which is in allocated memory.
3870 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003872escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003873{
3874 char_u *p;
3875
Bram Moolenaar964b3742019-05-24 18:54:09 +02003876 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003877 if (p != NULL)
3878 {
3879 p[0] = '\\';
3880 STRCPY(p + 1, *pp);
3881 vim_free(*pp);
3882 *pp = p;
3883 }
3884}
3885
3886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 * For each file name in files[num_files]:
3888 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3889 */
3890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003891tilde_replace(
3892 char_u *orig_pat,
3893 int num_files,
3894 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 int i;
3897 char_u *p;
3898
3899 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3900 {
3901 for (i = 0; i < num_files; ++i)
3902 {
3903 p = home_replace_save(NULL, files[i]);
3904 if (p != NULL)
3905 {
3906 vim_free(files[i]);
3907 files[i] = p;
3908 }
3909 }
3910 }
3911}
3912
3913/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003914 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003916 cmdline_info_T *
3917get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003919 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920}
3921
Bram Moolenaar064154c2016-03-19 22:50:43 +01003922#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003923/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003924 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003925 * ccline and put the previous value in prev_ccline.
3926 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003927 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003928get_ccline_ptr(void)
3929{
3930 if ((State & CMDLINE) == 0)
3931 return NULL;
3932 if (ccline.cmdbuff != NULL)
3933 return &ccline;
3934 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3935 return &prev_ccline;
3936 return NULL;
3937}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003938#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003939
Bram Moolenaar064154c2016-03-19 22:50:43 +01003940#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003941/*
3942 * Get the current command line in allocated memory.
3943 * Only works when the command line is being edited.
3944 * Returns NULL when something is wrong.
3945 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003946 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003947get_cmdline_str(void)
3948{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003949 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003950
Bram Moolenaaree91c332018-09-25 22:27:35 +02003951 if (cmdline_star > 0)
3952 return NULL;
3953 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003954 if (p == NULL)
3955 return NULL;
3956 return vim_strnsave(p->cmdbuff, p->cmdlen);
3957}
3958
3959/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003960 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003961 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003962 void
3963f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3964{
3965 rettv->v_type = VAR_STRING;
3966 rettv->vval.v_string = get_cmdline_str();
3967}
3968
3969/*
3970 * "getcmdpos()" function
3971 */
3972 void
3973f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003974{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003975 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003976
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003977 rettv->vval.v_number = 0;
3978 if (p != NULL)
3979 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003980}
3981
3982/*
3983 * Set the command line byte position to "pos". Zero is the first position.
3984 * Only works when the command line is being edited.
3985 * Returns 1 when failed, 0 when OK.
3986 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003987 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003988set_cmdline_pos(
3989 int pos)
3990{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003991 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003992
3993 if (p == NULL)
3994 return 1;
3995
3996 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3997 * changed the command line. */
3998 if (pos < 0)
3999 new_cmdpos = 0;
4000 else
4001 new_cmdpos = pos;
4002 return 0;
4003}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004004
4005/*
4006 * "setcmdpos()" function
4007 */
4008 void
4009f_setcmdpos(typval_T *argvars, typval_T *rettv)
4010{
4011 int pos = (int)tv_get_number(&argvars[0]) - 1;
4012
4013 if (pos >= 0)
4014 rettv->vval.v_number = set_cmdline_pos(pos);
4015}
4016
4017/*
4018 * "getcmdtype()" function
4019 */
4020 void
4021f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4022{
4023 rettv->v_type = VAR_STRING;
4024 rettv->vval.v_string = alloc(2);
4025 if (rettv->vval.v_string != NULL)
4026 {
4027 rettv->vval.v_string[0] = get_cmdline_type();
4028 rettv->vval.v_string[1] = NUL;
4029 }
4030}
4031
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004032#endif
4033
4034#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4035/*
4036 * Get the current command-line type.
4037 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4038 * Only works when the command line is being edited.
4039 * Returns NUL when something is wrong.
4040 */
4041 int
4042get_cmdline_type(void)
4043{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004044 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004045
4046 if (p == NULL)
4047 return NUL;
4048 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004049 return
4050# ifdef FEAT_EVAL
4051 (p->input_fn) ? '@' :
4052# endif
4053 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004054 return p->cmdfirstc;
4055}
4056#endif
4057
Bram Moolenaard7663c22019-08-06 21:59:57 +02004058/*
4059 * Return the first character of the current command line.
4060 */
4061 int
4062get_cmdline_firstc(void)
4063{
4064 return ccline.cmdfirstc;
4065}
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067/*
4068 * Get indices "num1,num2" that specify a range within a list (not a range of
4069 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4070 * Returns OK if parsed successfully, otherwise FAIL.
4071 */
4072 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004073get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 int len;
4076 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004077 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 *str = skipwhite(*str);
4080 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4081 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004082 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 *str += len;
4084 *num1 = (int)num;
4085 first = TRUE;
4086 }
4087 *str = skipwhite(*str);
4088 if (**str == ',') /* parse "to" part of range */
4089 {
4090 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004091 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (len > 0)
4093 {
4094 *num2 = (int)num;
4095 *str = skipwhite(*str + len);
4096 }
4097 else if (!first) /* no number given at all */
4098 return FAIL;
4099 }
4100 else if (first) /* only one number given */
4101 *num2 = *num1;
4102 return OK;
4103}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#if defined(FEAT_CMDWIN) || defined(PROTO)
4106/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004107 * Check value of 'cedit' and set cedit_key.
4108 * Returns NULL if value is OK, error message otherwise.
4109 */
4110 char *
4111check_cedit(void)
4112{
4113 int n;
4114
4115 if (*p_cedit == NUL)
4116 cedit_key = -1;
4117 else
4118 {
4119 n = string_to_key(p_cedit, FALSE);
4120 if (vim_isprintc(n))
4121 return e_invarg;
4122 cedit_key = n;
4123 }
4124 return NULL;
4125}
4126
4127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * Open a window on the current command line and history. Allow editing in
4129 * the window. Returns when the window is closed.
4130 * Returns:
4131 * CR if the command is to be executed
4132 * Ctrl_C if it is to be abandoned
4133 * K_IGNORE if editing continues
4134 */
4135 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004136open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004138 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004140 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 win_T *wp;
4142 int i;
4143 linenr_T lnum;
4144 int histtype;
4145 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int save_restart_edit = restart_edit;
4147 int save_State = State;
4148 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004149#ifdef FEAT_RIGHTLEFT
4150 int save_cmdmsg_rl = cmdmsg_rl;
4151#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004152#ifdef FEAT_FOLDING
4153 int save_KeyTyped;
4154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /* Can't do this recursively. Can't do it when typing a password. */
4157 if (cmdwin_type != 0
4158# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4159 || cmdline_star > 0
4160# endif
4161 )
4162 {
4163 beep_flush();
4164 return K_IGNORE;
4165 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004166 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004168 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 win_size_save(&winsizes);
4170
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004171 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4172 // command line window, but we don't want the popup menu then.
4173 pum_undisplay();
4174
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004175 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004176 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004177 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004178
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004179 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4181 {
4182 beep_flush();
4183 return K_IGNORE;
4184 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004185 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004187 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004188 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004189 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004192#ifdef FEAT_FOLDING
4193 curwin->w_p_fen = FALSE;
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004196 curwin->w_p_rl = cmdmsg_rl;
4197 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004199 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004201 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004202 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004204 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004205 need_wait_return = FALSE;
4206
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004207 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4209 {
4210 if (p_wc == TAB)
4211 {
4212 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4213 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4214 }
4215 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4216 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004217 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004219 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4220 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004221 curbuf->b_p_tw = 0;
4222
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004223 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004225 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004227 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (i >= 0)
4229 {
4230 lnum = 0;
4231 do
4232 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004233 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004235 if (get_histentry(histtype)[i].hisstr != NULL)
4236 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 (colnr_T)0, FALSE);
4238 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004239 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 }
4242
4243 /* Replace the empty last line with the current command-line and put the
4244 * cursor there. */
4245 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4246 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4247 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004248 changed_line_abv_curs();
4249 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004250 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar23324a02019-10-01 17:39:04 +02004252 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 exmode_active = 0;
4254
4255 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaar23324a02019-10-01 17:39:04 +02004258 // Reset here so it can be set by a CmdWinEnter autocommand.
4259 cmdwin_result = 0;
4260
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004261 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004262 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004263 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004264 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 i = RedrawingDisabled;
4267 RedrawingDisabled = 0;
4268
4269 /*
4270 * Call the main loop until <CR> or CTRL-C is typed.
4271 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004272 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 RedrawingDisabled = i;
4275
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004276# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004277 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004278# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004279
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004280 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004281 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004282
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004283# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004284 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004285 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286# endif
4287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 exmode_active = save_exmode;
4290
Bram Moolenaarf9821062008-06-20 16:31:07 +00004291 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004293 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 {
4295 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004296 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 else
4299 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004300# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 /* autocmds may abort script processing */
4302 if (aborting() && cmdwin_result != K_IGNORE)
4303 cmdwin_result = Ctrl_C;
4304# endif
4305 /* Set the new command line from the cmdline buffer. */
4306 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004307 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004309 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4310
4311 if (histtype == HIST_CMD)
4312 {
4313 /* Execute the command directly. */
4314 ccline.cmdbuff = vim_strsave((char_u *)p);
4315 cmdwin_result = CAR;
4316 }
4317 else
4318 {
4319 /* First need to cancel what we were doing. */
4320 ccline.cmdbuff = NULL;
4321 stuffcharReadbuff(':');
4322 stuffReadbuff((char_u *)p);
4323 stuffcharReadbuff(CAR);
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 else if (cmdwin_result == K_XF2) /* :qa typed */
4327 {
4328 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4329 cmdwin_result = CAR;
4330 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004331 else if (cmdwin_result == Ctrl_C)
4332 {
4333 /* :q or :close, don't execute any command
4334 * and don't modify the cmd window. */
4335 ccline.cmdbuff = NULL;
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 else
4338 ccline.cmdbuff = vim_strsave(ml_get_curline());
4339 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004340 {
4341 ccline.cmdbuff = vim_strsave((char_u *)"");
4342 ccline.cmdlen = 0;
4343 ccline.cmdbufflen = 1;
4344 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 else
4348 {
4349 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4350 ccline.cmdbufflen = ccline.cmdlen + 1;
4351 ccline.cmdpos = curwin->w_cursor.col;
4352 if (ccline.cmdpos > ccline.cmdlen)
4353 ccline.cmdpos = ccline.cmdlen;
4354 if (cmdwin_result == K_IGNORE)
4355 {
4356 set_cmdspos_cursor();
4357 redrawcmd();
4358 }
4359 }
4360
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004361# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004362 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004363 curwin->w_p_cole = 0;
4364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004366 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 win_goto(old_curwin);
4368 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004369
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004370 // win_close() may have already wiped the buffer when 'bh' is
4371 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004372 if (bufref_valid(&bufref))
4373 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004375 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378
4379 ga_clear(&winsizes);
4380 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004381# ifdef FEAT_RIGHTLEFT
4382 cmdmsg_rl = save_cmdmsg_rl;
4383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 return cmdwin_result;
4389}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004390#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392/*
4393 * Used for commands that either take a simple command string argument, or:
4394 * cmd << endmarker
4395 * {script}
4396 * endmarker
4397 * Returns a pointer to allocated memory with {script} or NULL.
4398 */
4399 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004400script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 char_u *theline;
4403 char *end_pattern = NULL;
4404 char dot[] = ".";
4405 garray_T ga;
4406
4407 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4408 return NULL;
4409
4410 ga_init2(&ga, 1, 0x400);
4411
4412 if (cmd[2] != NUL)
4413 end_pattern = (char *)skipwhite(cmd + 2);
4414 else
4415 end_pattern = dot;
4416
4417 for (;;)
4418 {
4419 theline = eap->getline(
4420#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004421 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004423 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004426 {
4427 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 ga_concat(&ga, theline);
4432 ga_append(&ga, '\n');
4433 vim_free(theline);
4434 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004435 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437 return (char_u *)ga.ga_data;
4438}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004439
4440#if defined(FEAT_EVAL) || defined(PROTO)
4441/*
4442 * This function is used by f_input() and f_inputdialog() functions. The third
4443 * argument to f_input() specifies the type of completion to use at the
4444 * prompt. The third argument to f_inputdialog() specifies the value to return
4445 * when the user cancels the prompt.
4446 */
4447 void
4448get_user_input(
4449 typval_T *argvars,
4450 typval_T *rettv,
4451 int inputdialog,
4452 int secret)
4453{
4454 char_u *prompt = tv_get_string_chk(&argvars[0]);
4455 char_u *p = NULL;
4456 int c;
4457 char_u buf[NUMBUFLEN];
4458 int cmd_silent_save = cmd_silent;
4459 char_u *defstr = (char_u *)"";
4460 int xp_type = EXPAND_NOTHING;
4461 char_u *xp_arg = NULL;
4462
4463 rettv->v_type = VAR_STRING;
4464 rettv->vval.v_string = NULL;
4465
4466#ifdef NO_CONSOLE_INPUT
4467 // While starting up, there is no place to enter text. When running tests
4468 // with --not-a-term we assume feedkeys() will be used.
4469 if (no_console_input() && !is_not_a_term())
4470 return;
4471#endif
4472
4473 cmd_silent = FALSE; // Want to see the prompt.
4474 if (prompt != NULL)
4475 {
4476 // Only the part of the message after the last NL is considered as
4477 // prompt for the command line
4478 p = vim_strrchr(prompt, '\n');
4479 if (p == NULL)
4480 p = prompt;
4481 else
4482 {
4483 ++p;
4484 c = *p;
4485 *p = NUL;
4486 msg_start();
4487 msg_clr_eos();
4488 msg_puts_attr((char *)prompt, get_echo_attr());
4489 msg_didout = FALSE;
4490 msg_starthere();
4491 *p = c;
4492 }
4493 cmdline_row = msg_row;
4494
4495 if (argvars[1].v_type != VAR_UNKNOWN)
4496 {
4497 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4498 if (defstr != NULL)
4499 stuffReadbuffSpec(defstr);
4500
4501 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4502 {
4503 char_u *xp_name;
4504 int xp_namelen;
4505 long argt;
4506
4507 // input() with a third argument: completion
4508 rettv->vval.v_string = NULL;
4509
4510 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4511 if (xp_name == NULL)
4512 return;
4513
4514 xp_namelen = (int)STRLEN(xp_name);
4515
4516 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4517 &xp_arg) == FAIL)
4518 return;
4519 }
4520 }
4521
4522 if (defstr != NULL)
4523 {
4524 int save_ex_normal_busy = ex_normal_busy;
4525
4526 ex_normal_busy = 0;
4527 rettv->vval.v_string =
4528 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4529 xp_type, xp_arg);
4530 ex_normal_busy = save_ex_normal_busy;
4531 }
4532 if (inputdialog && rettv->vval.v_string == NULL
4533 && argvars[1].v_type != VAR_UNKNOWN
4534 && argvars[2].v_type != VAR_UNKNOWN)
4535 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4536 &argvars[2], buf));
4537
4538 vim_free(xp_arg);
4539
4540 // since the user typed this, no need to wait for return
4541 need_wait_return = FALSE;
4542 msg_didout = FALSE;
4543 }
4544 cmd_silent = cmd_silent_save;
4545}
4546#endif