blob: 6832158749c2d5008d101b1f6bf08f37aff06b92 [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 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 putcmdline('^', TRUE);
2213 c = get_literal(); /* get next (two) character(s) */
2214 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002215 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 /* may need to remove ^ when composing char was typed */
2217 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2218 {
2219 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2220 msg_putchar(' ');
2221 cursorcmd();
2222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 break;
2224
2225#ifdef FEAT_DIGRAPHS
2226 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002229# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 dont_scroll = TRUE; /* disallow scrolling here */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002233 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 if (c != NUL)
2235 break;
2236
2237 redrawcmd();
2238 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002239#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241#ifdef FEAT_RIGHTLEFT
2242 case Ctrl__: /* CTRL-_: switch language mode */
2243 if (!p_ari)
2244 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002245 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 goto cmdline_not_changed;
2247#endif
2248
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002249 case K_PS:
2250 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2251 goto cmdline_changed;
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 default:
2254#ifdef UNIX
2255 if (c == intr_char)
2256 {
2257 gotesc = TRUE; /* will free ccline.cmdbuff after
2258 putting it in history */
2259 goto returncmd; /* back to Normal mode */
2260 }
2261#endif
2262 /*
2263 * Normal character with no special meaning. Just set mod_mask
2264 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2265 * the string <S-Space>. This should only happen after ^V.
2266 */
2267 if (!IS_SPECIAL(c))
2268 mod_mask = 0x0;
2269 break;
2270 }
2271 /*
2272 * End of switch on command line character.
2273 * We come here if we have a normal character.
2274 */
2275
Bram Moolenaar13505972019-01-24 15:04:48 +01002276 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2277 && (ccheck_abbr(
2278 // Add ABBR_OFF for characters above 0x100, this is
2279 // what check_abbr() expects.
2280 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2281 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 goto cmdline_changed;
2283
2284 /*
2285 * put the character in the command line
2286 */
2287 if (IS_SPECIAL(c) || mod_mask != 0)
2288 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2289 else
2290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (has_mbyte)
2292 {
2293 j = (*mb_char2bytes)(c, IObuff);
2294 IObuff[j] = NUL; /* exclude composing chars */
2295 put_on_cmdline(IObuff, j, TRUE);
2296 }
2297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
2299 IObuff[0] = c;
2300 put_on_cmdline(IObuff, 1, TRUE);
2301 }
2302 }
2303 goto cmdline_changed;
2304
2305/*
2306 * This part implements incremental searches for "/" and "?"
2307 * Jump to cmdline_not_changed when a character has been read but the command
2308 * line did not change. Then we only search and redraw if something changed in
2309 * the past.
2310 * Jump to cmdline_changed when the command line did change.
2311 * (Sorry for the goto's, I know it is ugly).
2312 */
2313cmdline_not_changed:
2314#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002315 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 continue;
2317#endif
2318
2319cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002320 /* Trigger CmdlineChanged autocommands. */
2321 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002324 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#endif
2326
2327#ifdef FEAT_RIGHTLEFT
2328 if (cmdmsg_rl
2329# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002330 || (p_arshape && !p_tbidi
2331 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332# endif
2333 )
2334 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002335 * right-left typing. Not efficient, but it works.
2336 * Do it only when there are no characters left to read
2337 * to avoid useless intermediate redraws. */
2338 if (vpeekc() == NUL)
2339 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#endif
2341 }
2342
2343returncmd:
2344
2345#ifdef FEAT_RIGHTLEFT
2346 cmdmsg_rl = FALSE;
2347#endif
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002350 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002353 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354#endif
2355
2356 if (ccline.cmdbuff != NULL)
2357 {
2358 /*
2359 * Put line in history buffer (":" and "=" only when it was typed).
2360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (ccline.cmdlen && firstc != NUL
2362 && (some_key_typed || histype == HIST_SEARCH))
2363 {
2364 add_to_history(histype, ccline.cmdbuff, TRUE,
2365 histype == HIST_SEARCH ? firstc : NUL);
2366 if (firstc == ':')
2367 {
2368 vim_free(new_last_cmdline);
2369 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2370 }
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002373 if (gotesc)
2374 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376
2377 /*
2378 * If the screen was shifted up, redraw the whole screen (later).
2379 * If the line is too long, clear it, so ruler and shown command do
2380 * not get printed in the middle of it.
2381 */
2382 msg_check();
2383 msg_scroll = save_msg_scroll;
2384 redir_off = FALSE;
2385
2386 /* When the command line was typed, no need for a wait-return prompt. */
2387 if (some_key_typed)
2388 need_wait_return = FALSE;
2389
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002390 /* Trigger CmdlineLeave autocommands. */
2391 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002394#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2396 im_save_status(b_im_ptr);
2397 im_set_active(FALSE);
2398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400#ifdef CURSOR_SHAPE
2401 ui_cursor_shape(); /* may show different cursor shape */
2402#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002403 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
Bram Moolenaar438d1762018-09-30 17:11:48 +02002405theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002406 {
2407 char_u *p = ccline.cmdbuff;
2408
Bram Moolenaar438d1762018-09-30 17:11:48 +02002409 if (did_save_ccline)
2410 restore_cmdline(&save_ccline);
2411 else
2412 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002413 return p;
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415}
2416
2417#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2418/*
2419 * Get a command line with a prompt.
2420 * This is prepared to be called recursively from getcmdline() (e.g. by
2421 * f_input() when evaluating an expression from CTRL-R =).
2422 * Returns the command line in allocated memory, or NULL.
2423 */
2424 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002425getcmdline_prompt(
2426 int firstc,
2427 char_u *prompt, /* command line prompt */
2428 int attr, /* attributes for prompt */
2429 int xp_context, /* type of expansion */
2430 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002433 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002434 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002436 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
Bram Moolenaar438d1762018-09-30 17:11:48 +02002438 if (ccline.cmdbuff != NULL)
2439 {
2440 // Save the values of the current cmdline and restore them below.
2441 save_cmdline(&save_ccline);
2442 did_save_ccline = TRUE;
2443 }
2444
Bram Moolenaar66b51422019-08-18 21:44:12 +02002445 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 ccline.cmdprompt = prompt;
2447 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002448# ifdef FEAT_EVAL
2449 ccline.xp_context = xp_context;
2450 ccline.xp_arg = xp_arg;
2451 ccline.input_fn = (firstc == '@');
2452# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002453 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002454 s = getcmdline_int(firstc, 1L, 0, FALSE);
2455
2456 if (did_save_ccline)
2457 restore_cmdline(&save_ccline);
2458
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002459 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002460 /* Restore msg_col, the prompt from input() may have changed it.
2461 * But only if called recursively and the commandline is therefore being
2462 * restored to an old one; if not, the input() prompt stays on the screen,
2463 * so we need its modified msg_col left intact. */
2464 if (ccline.cmdbuff != NULL)
2465 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 return s;
2468}
2469#endif
2470
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002471/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002472 * Read the 'wildmode' option, fill wim_flags[].
2473 */
2474 int
2475check_opt_wim(void)
2476{
2477 char_u new_wim_flags[4];
2478 char_u *p;
2479 int i;
2480 int idx = 0;
2481
2482 for (i = 0; i < 4; ++i)
2483 new_wim_flags[i] = 0;
2484
2485 for (p = p_wim; *p; ++p)
2486 {
2487 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2488 ;
2489 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2490 return FAIL;
2491 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2492 new_wim_flags[idx] |= WIM_LONGEST;
2493 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2494 new_wim_flags[idx] |= WIM_FULL;
2495 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2496 new_wim_flags[idx] |= WIM_LIST;
2497 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2498 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2499 else
2500 return FAIL;
2501 p += i;
2502 if (*p == NUL)
2503 break;
2504 if (*p == ',')
2505 {
2506 if (idx == 3)
2507 return FAIL;
2508 ++idx;
2509 }
2510 }
2511
2512 /* fill remaining entries with last flag */
2513 while (idx < 3)
2514 {
2515 new_wim_flags[idx + 1] = new_wim_flags[idx];
2516 ++idx;
2517 }
2518
2519 /* only when there are no errors, wim_flags[] is changed */
2520 for (i = 0; i < 4; ++i)
2521 wim_flags[i] = new_wim_flags[i];
2522 return OK;
2523}
2524
2525/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002526 * Return TRUE when the text must not be changed and we can't switch to
2527 * another window or buffer. Used when editing the command line, evaluating
2528 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002529 */
2530 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002531text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002532{
2533#ifdef FEAT_CMDWIN
2534 if (cmdwin_type != 0)
2535 return TRUE;
2536#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002537 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002538}
2539
2540/*
2541 * Give an error message for a command that isn't allowed while the cmdline
2542 * window is open or editing the cmdline in another way.
2543 */
2544 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002546{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002547 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002548}
2549
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002550 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002551get_text_locked_msg(void)
2552{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002553#ifdef FEAT_CMDWIN
2554 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002555 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002556#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002557 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002558}
2559
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002560/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002561 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2562 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002563 */
2564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002565curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002566{
2567 if (curbuf_lock > 0)
2568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002569 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002570 return TRUE;
2571 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002572 return allbuf_locked();
2573}
2574
2575/*
2576 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2577 * message.
2578 */
2579 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002580allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002581{
2582 if (allbuf_lock > 0)
2583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002584 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002585 return TRUE;
2586 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002587 return FALSE;
2588}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002591cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2594 if (cmdline_star > 0) /* showing '*', always 1 position */
2595 return 1;
2596#endif
2597 return ptr2cells(ccline.cmdbuff + idx);
2598}
2599
2600/*
2601 * Compute the offset of the cursor on the command line for the prompt and
2602 * indent.
2603 */
2604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002605set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002607 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 ccline.cmdspos = 1 + ccline.cmdindent;
2609 else
2610 ccline.cmdspos = 0 + ccline.cmdindent;
2611}
2612
2613/*
2614 * Compute the screen position for the cursor on the command line.
2615 */
2616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002617set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 int i, m, c;
2620
2621 set_cmdspos();
2622 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002625 if (m < 0) /* overflow, Columns or Rows at weird value */
2626 m = MAXCOL;
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 else
2629 m = MAXCOL;
2630 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2631 {
2632 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2634 if (has_mbyte)
2635 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002636 /* If the cmdline doesn't fit, show cursor on last visible char.
2637 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if ((ccline.cmdspos += c) >= m)
2639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 ccline.cmdspos -= c;
2641 break;
2642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002644 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646}
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648/*
2649 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2650 * character that doesn't fit, so that a ">" must be displayed.
2651 */
2652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002653correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002655 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2657 && ccline.cmdspos % Columns + cells > Columns)
2658 ccline.cmdspos++;
2659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661/*
2662 * Get an Ex command line for the ":" command.
2663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002665getexline(
2666 int c, /* normally ':', NUL for ":append" */
2667 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002668 int indent, /* indent for inside conditionals */
2669 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 /* When executing a register, remove ':' that's in front of each line. */
2672 if (exec_from_reg && vpeekc() == ':')
2673 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002674 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675}
2676
2677/*
2678 * Get an Ex command line for Ex mode.
2679 * In Ex mode we only use the OS supplied line editing features and no
2680 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002684getexmodeline(
2685 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002687 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002688 int indent, /* indent for inside conditionals */
2689 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002691 garray_T line_ga;
2692 char_u *pend;
2693 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002694 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002695 int escaped = FALSE; /* CTRL-V typed */
2696 int vcol = 0;
2697 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002698 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002699 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 /* Switch cursor on now. This avoids that it happens after the "\n", which
2702 * confuses the system function that computes tabstops. */
2703 cursor_on();
2704
2705 /* always start in column 0; write a newline if necessary */
2706 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002709 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002711 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002712 if (p_prompt)
2713 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 while (indent-- > 0)
2715 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718
2719 ga_init2(&line_ga, 1, 30);
2720
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002721 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002723 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002724 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002725 while (indent >= 8)
2726 {
2727 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002728 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002729 indent -= 8;
2730 }
2731 while (indent-- > 0)
2732 {
2733 ga_append(&line_ga, ' ');
2734 msg_putchar(' ');
2735 }
2736 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 ++no_mapping;
2738 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 /*
2741 * Get the line, one character at a time.
2742 */
2743 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002746 long sw;
2747 char_u *s;
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (ga_grow(&line_ga, 40) == FAIL)
2750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaarba748c82017-02-26 14:00:07 +01002752 /*
2753 * Get one character at a time.
2754 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002755 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002756
2757 /* Check for a ":normal" command and no more characters left. */
2758 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2759 c1 = '\n';
2760 else
2761 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764 * Handle line editing.
2765 * Previously this was left to the system, putting the terminal in
2766 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 msg_putchar('\n');
2771 break;
2772 }
2773
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002774 if (c1 == K_PS)
2775 {
2776 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2777 goto redraw;
2778 }
2779
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 if (!escaped)
2781 {
2782 /* CR typed means "enter", which is NL */
2783 if (c1 == '\r')
2784 c1 = '\n';
2785
2786 if (c1 == BS || c1 == K_BS
2787 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002789 if (line_ga.ga_len > 0)
2790 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002791 if (has_mbyte)
2792 {
2793 p = (char_u *)line_ga.ga_data;
2794 p[line_ga.ga_len] = NUL;
2795 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2796 line_ga.ga_len -= len;
2797 }
2798 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002799 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 goto redraw;
2801 }
2802 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002807 msg_col = startcol;
2808 msg_clr_eos();
2809 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002810 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 }
2812
2813 if (c1 == Ctrl_T)
2814 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002815 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 p = (char_u *)line_ga.ga_data;
2817 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002818 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002819 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002821 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002823 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002824 p = (char_u *)line_ga.ga_data;
2825 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2827 *s = ' ';
2828 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830redraw:
2831 /* redraw the line */
2832 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002834 p = (char_u *)line_ga.ga_data;
2835 p[line_ga.ga_len] = NUL;
2836 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002840 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002842 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002843 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002847 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002848 msg_outtrans_len(p, len);
2849 vcol += ptr2cells(p);
2850 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002854 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 continue;
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 if (c1 == Ctrl_D)
2859 {
2860 /* Delete one shiftwidth. */
2861 p = (char_u *)line_ga.ga_data;
2862 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 if (prev_char == '^')
2865 ex_keep_indent = TRUE;
2866 indent = 0;
2867 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869 else
2870 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002872 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002873 if (indent > 0)
2874 {
2875 --indent;
2876 indent -= indent % get_sw_value(curbuf);
2877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002879 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002881 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002882 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2883 --line_ga.ga_len;
2884 }
2885 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002887
2888 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2889 {
2890 escaped = TRUE;
2891 continue;
2892 }
2893
2894 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2895 if (IS_SPECIAL(c1))
2896 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898
2899 if (IS_SPECIAL(c1))
2900 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002901 if (has_mbyte)
2902 len = (*mb_char2bytes)(c1,
2903 (char_u *)line_ga.ga_data + line_ga.ga_len);
2904 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002905 {
2906 len = 1;
2907 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2908 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909 if (c1 == '\n')
2910 msg_putchar('\n');
2911 else if (c1 == TAB)
2912 {
2913 /* Don't use chartabsize(), 'ts' can be different */
2914 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002916 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002921 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002924 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002925 escaped = FALSE;
2926
2927 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002928 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002929
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002930 /* We are done when a NL is entered, but not when it comes after an
2931 * odd number of backslashes, that results in a NUL. */
2932 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002934 int bcount = 0;
2935
2936 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2937 ++bcount;
2938
2939 if (bcount > 0)
2940 {
2941 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2942 * "\NL", etc. */
2943 line_ga.ga_len -= (bcount + 1) / 2;
2944 pend -= (bcount + 1) / 2;
2945 pend[-1] = '\n';
2946 }
2947
2948 if ((bcount & 1) == 0)
2949 {
2950 --line_ga.ga_len;
2951 --pend;
2952 *pend = NUL;
2953 break;
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956 }
2957
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002958 --no_mapping;
2959 --allow_keys;
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 /* make following messages go to the next line */
2962 msg_didout = FALSE;
2963 msg_col = 0;
2964 if (msg_row < Rows - 1)
2965 ++msg_row;
2966 emsg_on_display = FALSE; /* don't want ui_delay() */
2967
2968 if (got_int)
2969 ga_clear(&line_ga);
2970
2971 return (char_u *)line_ga.ga_data;
2972}
2973
Bram Moolenaare344bea2005-09-01 20:46:49 +00002974# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2975 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Return TRUE if ccline.overstrike is on.
2978 */
2979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002980cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 return ccline.overstrike;
2983}
2984
2985/*
2986 * Return TRUE if the cursor is at the end of the cmdline.
2987 */
2988 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002989cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 return (ccline.cmdpos >= ccline.cmdlen);
2992}
2993#endif
2994
Bram Moolenaar9372a112005-12-06 19:59:18 +00002995#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996/*
2997 * Return the virtual column number at the current cursor position.
2998 * This is used by the IM code to obtain the start of the preedit string.
2999 */
3000 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003001cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3004 return MAXCOL;
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (has_mbyte)
3007 {
3008 colnr_T col;
3009 int i = 0;
3010
3011 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003012 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014 return col;
3015 }
3016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 return ccline.cmdpos;
3018}
3019#endif
3020
3021#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3022/*
3023 * If part of the command line is an IM preedit string, redraw it with
3024 * IM feedback attributes. The cursor position is restored after drawing.
3025 */
3026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003027redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
3029 if ((State & CMDLINE)
3030 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003031 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 && !p_imdisable
3033 && im_is_preediting())
3034 {
3035 int cmdpos = 0;
3036 int cmdspos;
3037 int old_row;
3038 int old_col;
3039 colnr_T col;
3040
3041 old_row = msg_row;
3042 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003043 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (has_mbyte)
3046 {
3047 for (col = 0; col < preedit_start_col
3048 && cmdpos < ccline.cmdlen; ++col)
3049 {
3050 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003051 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 }
3054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 {
3056 cmdspos += preedit_start_col;
3057 cmdpos += preedit_start_col;
3058 }
3059
3060 msg_row = cmdline_row + (cmdspos / (int)Columns);
3061 msg_col = cmdspos % (int)Columns;
3062 if (msg_row >= Rows)
3063 msg_row = Rows - 1;
3064
3065 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3066 {
3067 int char_len;
3068 int char_attr;
3069
3070 char_attr = im_get_feedback_attr(col);
3071 if (char_attr < 0)
3072 break; /* end of preedit string */
3073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003075 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 char_len = 1;
3078
3079 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3080 cmdpos += char_len;
3081 }
3082
3083 msg_row = old_row;
3084 msg_col = old_col;
3085 }
3086}
3087#endif /* FEAT_XIM && FEAT_GUI_GTK */
3088
3089/*
3090 * Allocate a new command line buffer.
3091 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 */
3093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 /*
3097 * give some extra space to avoid having to allocate all the time
3098 */
3099 if (len < 80)
3100 len = 100;
3101 else
3102 len += 20;
3103
3104 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3105 ccline.cmdbufflen = len;
3106}
3107
3108/*
3109 * Re-allocate the command line to length len + something extra.
3110 * return FAIL for failure, OK otherwise
3111 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003113realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 char_u *p;
3116
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003117 if (len < ccline.cmdbufflen)
3118 return OK; /* no need to resize */
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 p = ccline.cmdbuff;
3121 alloc_cmdbuff(len); /* will get some more */
3122 if (ccline.cmdbuff == NULL) /* out of memory */
3123 {
3124 ccline.cmdbuff = p; /* keep the old one */
3125 return FAIL;
3126 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003127 /* There isn't always a NUL after the command, but it may need to be
3128 * there, thus copy up to the NUL and add a NUL. */
3129 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3130 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003132
3133 if (ccline.xpc != NULL
3134 && ccline.xpc->xp_pattern != NULL
3135 && ccline.xpc->xp_context != EXPAND_NOTHING
3136 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3137 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003138 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003139
3140 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3141 * to point into the newly allocated memory. */
3142 if (i >= 0 && i <= ccline.cmdlen)
3143 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3144 }
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 return OK;
3147}
3148
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003149#if defined(FEAT_ARABIC) || defined(PROTO)
3150static char_u *arshape_buf = NULL;
3151
3152# if defined(EXITFREE) || defined(PROTO)
3153 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003154free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003155{
3156 vim_free(arshape_buf);
3157}
3158# endif
3159#endif
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161/*
3162 * Draw part of the cmdline at the current cursor position. But draw stars
3163 * when cmdline_star is TRUE.
3164 */
3165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003166draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3169 int i;
3170
3171 if (cmdline_star > 0)
3172 for (i = 0; i < len; ++i)
3173 {
3174 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003176 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178 else
3179#endif
3180#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003181 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 static int buflen = 0;
3184 char_u *p;
3185 int j;
3186 int newlen = 0;
3187 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003188 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 int prev_c = 0;
3190 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003191 int u8c;
3192 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /*
3196 * Do arabic shaping into a temporary buffer. This is very
3197 * inefficient!
3198 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003199 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 /* Re-allocate the buffer. We keep it around to avoid a lot of
3202 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003204 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 arshape_buf = alloc(buflen);
3206 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 return; /* out of memory */
3208 }
3209
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003210 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3211 {
3212 /* Prepend a space to draw the leading composing char on. */
3213 arshape_buf[0] = ' ';
3214 newlen = 1;
3215 }
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 for (j = start; j < start + len; j += mb_l)
3218 {
3219 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003220 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003221 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (ARABIC_CHAR(u8c))
3223 {
3224 /* Do Arabic shaping. */
3225 if (cmdmsg_rl)
3226 {
3227 /* displaying from right to left */
3228 pc = prev_c;
3229 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003230 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (j + mb_l >= start + len)
3232 nc = NUL;
3233 else
3234 nc = utf_ptr2char(p + mb_l);
3235 }
3236 else
3237 {
3238 /* displaying from left to right */
3239 if (j + mb_l >= start + len)
3240 pc = NUL;
3241 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 {
3243 int pcc[MAX_MCO];
3244
3245 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003247 pc1 = pcc[0];
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 nc = prev_c;
3250 }
3251 prev_c = u8c;
3252
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003253 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003256 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003258 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3259 if (u8cc[1] != 0)
3260 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 }
3264 else
3265 {
3266 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 newlen += mb_l;
3269 }
3270 }
3271
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 else
3275#endif
3276 msg_outtrans_len(ccline.cmdbuff + start, len);
3277}
3278
3279/*
3280 * Put a character on the command line. Shifts the following text to the
3281 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3282 * "c" must be printable (fit in one display cell)!
3283 */
3284 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003285putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 if (cmd_silent)
3288 return;
3289 msg_no_more = TRUE;
3290 msg_putchar(c);
3291 if (shift)
3292 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3293 msg_no_more = FALSE;
3294 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003295 extra_char = c;
3296 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299/*
3300 * Undo a putcmdline(c, FALSE).
3301 */
3302 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003303unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 if (cmd_silent)
3306 return;
3307 msg_no_more = TRUE;
3308 if (ccline.cmdlen == ccline.cmdpos)
3309 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003310 else if (has_mbyte)
3311 draw_cmdline(ccline.cmdpos,
3312 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
3314 draw_cmdline(ccline.cmdpos, 1);
3315 msg_no_more = FALSE;
3316 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003317 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318}
3319
3320/*
3321 * Put the given string, of the given length, onto the command line.
3322 * If len is -1, then STRLEN() is used to calculate the length.
3323 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3324 * part will be redrawn, otherwise it will not. If this function is called
3325 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3326 * called afterwards.
3327 */
3328 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003329put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 int retval;
3332 int i;
3333 int m;
3334 int c;
3335
3336 if (len < 0)
3337 len = (int)STRLEN(str);
3338
3339 /* Check if ccline.cmdbuff needs to be longer */
3340 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003341 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
3343 retval = OK;
3344 if (retval == OK)
3345 {
3346 if (!ccline.overstrike)
3347 {
3348 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3349 ccline.cmdbuff + ccline.cmdpos,
3350 (size_t)(ccline.cmdlen - ccline.cmdpos));
3351 ccline.cmdlen += len;
3352 }
3353 else
3354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (has_mbyte)
3356 {
3357 /* Count nr of characters in the new string. */
3358 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003359 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 ++m;
3361 /* Count nr of bytes in cmdline that are overwritten by these
3362 * characters. */
3363 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003364 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 --m;
3366 if (i < ccline.cmdlen)
3367 {
3368 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3369 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3370 ccline.cmdlen += ccline.cmdpos + len - i;
3371 }
3372 else
3373 ccline.cmdlen = ccline.cmdpos + len;
3374 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003375 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 ccline.cmdlen = ccline.cmdpos + len;
3377 }
3378 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3379 ccline.cmdbuff[ccline.cmdlen] = NUL;
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (enc_utf8)
3382 {
3383 /* When the inserted text starts with a composing character,
3384 * backup to the character before it. There could be two of them.
3385 */
3386 i = 0;
3387 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3388 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3389 {
3390 i = (*mb_head_off)(ccline.cmdbuff,
3391 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3392 ccline.cmdpos -= i;
3393 len += i;
3394 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3395 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003396#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3398 {
3399 /* Check the previous character for Arabic combining pair. */
3400 i = (*mb_head_off)(ccline.cmdbuff,
3401 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3402 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3403 + ccline.cmdpos - i), c))
3404 {
3405 ccline.cmdpos -= i;
3406 len += i;
3407 }
3408 else
3409 i = 0;
3410 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (i != 0)
3413 {
3414 /* Also backup the cursor position. */
3415 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3416 ccline.cmdspos -= i;
3417 msg_col -= i;
3418 if (msg_col < 0)
3419 {
3420 msg_col += Columns;
3421 --msg_row;
3422 }
3423 }
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 if (redraw && !cmd_silent)
3427 {
3428 msg_no_more = TRUE;
3429 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003430 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3432 /* Avoid clearing the rest of the line too often. */
3433 if (cmdline_row != i || ccline.overstrike)
3434 msg_clr_eos();
3435 msg_no_more = FALSE;
3436 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003437 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003439 m = Columns * Rows;
3440 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003442 }
3443 else
3444 m = MAXCOL;
3445 for (i = 0; i < len; ++i)
3446 {
3447 c = cmdline_charsize(ccline.cmdpos);
3448 /* count ">" for a double-wide char that doesn't fit. */
3449 if (has_mbyte)
3450 correct_cmdspos(ccline.cmdpos, c);
3451 /* Stop cursor at the end of the screen, but do increment the
3452 * insert position, so that entering a very long command
3453 * works, even though you can't see it. */
3454 if (ccline.cmdspos + c < m)
3455 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003456
Bram Moolenaar14184a32019-02-16 15:10:30 +01003457 if (has_mbyte)
3458 {
3459 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3460 if (c > len - i - 1)
3461 c = len - i - 1;
3462 ccline.cmdpos += c;
3463 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003465 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467 }
3468 if (redraw)
3469 msg_check();
3470 return retval;
3471}
3472
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473static cmdline_info_T prev_ccline;
3474static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003475
3476/*
3477 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3478 * and overwrite it. But get_cmdline_str() may need it, thus make it
3479 * available globally in prev_ccline.
3480 */
3481 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003482save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003483{
3484 if (!prev_ccline_used)
3485 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003487 prev_ccline_used = TRUE;
3488 }
3489 *ccp = prev_ccline;
3490 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003491 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003492}
3493
3494/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003495 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003496 */
3497 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003498restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003499{
3500 ccline = prev_ccline;
3501 prev_ccline = *ccp;
3502}
3503
Bram Moolenaar8299df92004-07-10 09:47:34 +00003504/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003505 * Paste a yank register into the command line.
3506 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003507 * insert_reg() can't be used here, because special characters from the
3508 * register contents will be interpreted as commands.
3509 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003510 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003511 */
3512 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513cmdline_paste(
3514 int regname,
3515 int literally, /* Insert text literally instead of "as typed" */
3516 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003517{
3518 long i;
3519 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003520 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003521 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003522
3523 /* check for valid regname; also accept special characters for CTRL-R in
3524 * the command line */
3525 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003526 && regname != Ctrl_A && regname != Ctrl_L
3527 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003528 return FAIL;
3529
3530 /* A register containing CTRL-R can cause an endless loop. Allow using
3531 * CTRL-C to break the loop. */
3532 line_breakcheck();
3533 if (got_int)
3534 return FAIL;
3535
3536#ifdef FEAT_CLIPBOARD
3537 regname = may_get_selection(regname);
3538#endif
3539
Bram Moolenaar438d1762018-09-30 17:11:48 +02003540 // Need to set "textlock" to avoid nasty things like going to another
3541 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003542 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003544 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003545
3546 if (i)
3547 {
3548 /* Got the value of a special register in "arg". */
3549 if (arg == NULL)
3550 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003551
3552 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3553 * part of the word. */
3554 p = arg;
3555 if (p_is && regname == Ctrl_W)
3556 {
3557 char_u *w;
3558 int len;
3559
3560 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003561 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003562 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003563 if (has_mbyte)
3564 {
3565 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3566 if (!vim_iswordc(mb_ptr2char(w - len)))
3567 break;
3568 w -= len;
3569 }
3570 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003571 {
3572 if (!vim_iswordc(w[-1]))
3573 break;
3574 --w;
3575 }
3576 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003577 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003578 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3579 p += len;
3580 }
3581
3582 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003583 if (allocated)
3584 vim_free(arg);
3585 return OK;
3586 }
3587
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003588 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003589}
3590
3591/*
3592 * Put a string on the command line.
3593 * When "literally" is TRUE, insert literally.
3594 * When "literally" is FALSE, insert as typed, but don't leave the command
3595 * line.
3596 */
3597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003598cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003599{
3600 int c, cv;
3601
3602 if (literally)
3603 put_on_cmdline(s, -1, TRUE);
3604 else
3605 while (*s != NUL)
3606 {
3607 cv = *s;
3608 if (cv == Ctrl_V && s[1])
3609 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003611 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003612 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003614 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3615 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003616#ifdef UNIX
3617 || c == intr_char
3618#endif
3619 || (c == Ctrl_BSL && *s == Ctrl_N))
3620 stuffcharReadbuff(Ctrl_V);
3621 stuffcharReadbuff(c);
3622 }
3623}
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625#ifdef FEAT_WILDMENU
3626/*
3627 * Delete characters on the command line, from "from" to the current
3628 * position.
3629 */
3630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003631cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3634 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3635 ccline.cmdlen -= ccline.cmdpos - from;
3636 ccline.cmdpos = from;
3637}
3638#endif
3639
3640/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003641 * This function is called when the screen size changes and with incremental
3642 * search and in other situations where the command line may have been
3643 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
3645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003646redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003648 redrawcmdline_ex(TRUE);
3649}
3650
3651 void
3652redrawcmdline_ex(int do_compute_cmdrow)
3653{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (cmd_silent)
3655 return;
3656 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003657 if (do_compute_cmdrow)
3658 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 redrawcmd();
3660 cursorcmd();
3661}
3662
3663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003664redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 int i;
3667
3668 if (cmd_silent)
3669 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003670 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 msg_putchar(ccline.cmdfirstc);
3672 if (ccline.cmdprompt != NULL)
3673 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003674 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3676 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003677 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 --ccline.cmdindent;
3679 }
3680 else
3681 for (i = ccline.cmdindent; i > 0; --i)
3682 msg_putchar(' ');
3683}
3684
3685/*
3686 * Redraw what is currently on the command line.
3687 */
3688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003689redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 if (cmd_silent)
3692 return;
3693
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003694 /* when 'incsearch' is set there may be no command line while redrawing */
3695 if (ccline.cmdbuff == NULL)
3696 {
3697 windgoto(cmdline_row, 0);
3698 msg_clr_eos();
3699 return;
3700 }
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 msg_start();
3703 redrawcmdprompt();
3704
3705 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3706 msg_no_more = TRUE;
3707 draw_cmdline(0, ccline.cmdlen);
3708 msg_clr_eos();
3709 msg_no_more = FALSE;
3710
3711 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003712 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003713 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 /*
3716 * An emsg() before may have set msg_scroll. This is used in normal mode,
3717 * in cmdline mode we can reset them now.
3718 */
3719 msg_scroll = FALSE; /* next message overwrites cmdline */
3720
3721 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3722 * in cmdline mode */
3723 skip_redraw = FALSE;
3724}
3725
3726 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003727compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003729 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 cmdline_row = Rows - 1;
3731 else
3732 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003733 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734}
3735
Bram Moolenaar66b51422019-08-18 21:44:12 +02003736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003737cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 if (cmd_silent)
3740 return;
3741
3742#ifdef FEAT_RIGHTLEFT
3743 if (cmdmsg_rl)
3744 {
3745 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3746 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3747 if (msg_row <= 0)
3748 msg_row = Rows - 1;
3749 }
3750 else
3751#endif
3752 {
3753 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3754 msg_col = ccline.cmdspos % (int)Columns;
3755 if (msg_row >= Rows)
3756 msg_row = Rows - 1;
3757 }
3758
3759 windgoto(msg_row, msg_col);
3760#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003761 if (p_imst == IM_ON_THE_SPOT)
3762 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#endif
3764#ifdef MCH_CURSOR_SHAPE
3765 mch_update_cursor();
3766#endif
3767}
3768
3769 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003770gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 msg_start();
3773#ifdef FEAT_RIGHTLEFT
3774 if (cmdmsg_rl)
3775 msg_col = Columns - 1;
3776 else
3777#endif
3778 msg_col = 0; /* always start in column 0 */
3779 if (clr) /* clear the bottom line(s) */
3780 msg_clr_eos(); /* will reset clear_cmdline */
3781 windgoto(cmdline_row, 0);
3782}
3783
3784/*
3785 * Check the word in front of the cursor for an abbreviation.
3786 * Called when the non-id character "c" has been entered.
3787 * When an abbreviation is recognized it is removed from the text with
3788 * backspaces and the replacement string is inserted, followed by "c".
3789 */
3790 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003791ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003793 int spos = 0;
3794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3796 return FALSE;
3797
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003798 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3799 * Actually accepts any mark. */
3800 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3801 spos++;
3802 if (ccline.cmdlen - spos > 5
3803 && ccline.cmdbuff[spos] == '\''
3804 && ccline.cmdbuff[spos + 2] == ','
3805 && ccline.cmdbuff[spos + 3] == '\'')
3806 spos += 5;
3807 else
3808 /* check abbreviation from the beginning of the commandline */
3809 spos = 0;
3810
3811 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812}
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003815 * Escape special characters in "fname" for when used as a file name argument
3816 * after a Vim command, or, when "shell" is non-zero, a shell command.
3817 * Returns the result in allocated memory.
3818 */
3819 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003820vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003821{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003822 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003823#ifdef BACKSLASH_IN_FILENAME
3824 char_u buf[20];
3825 int j = 0;
3826
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003827 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003829 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003830 buf[j++] = *p;
3831 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003832 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003833#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003834 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3835 if (shell && csh_like_shell() && p != NULL)
3836 {
3837 char_u *s;
3838
3839 /* For csh and similar shells need to put two backslashes before '!'.
3840 * One is taken by Vim, one by the shell. */
3841 s = vim_strsave_escaped(p, (char_u *)"!");
3842 vim_free(p);
3843 p = s;
3844 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003845#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003846
3847 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3848 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003849 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003850 escape_fname(&p);
3851
3852 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003853}
3854
3855/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003856 * Put a backslash before the file name in "pp", which is in allocated memory.
3857 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003859escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003860{
3861 char_u *p;
3862
Bram Moolenaar964b3742019-05-24 18:54:09 +02003863 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003864 if (p != NULL)
3865 {
3866 p[0] = '\\';
3867 STRCPY(p + 1, *pp);
3868 vim_free(*pp);
3869 *pp = p;
3870 }
3871}
3872
3873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * For each file name in files[num_files]:
3875 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3876 */
3877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003878tilde_replace(
3879 char_u *orig_pat,
3880 int num_files,
3881 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
3883 int i;
3884 char_u *p;
3885
3886 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3887 {
3888 for (i = 0; i < num_files; ++i)
3889 {
3890 p = home_replace_save(NULL, files[i]);
3891 if (p != NULL)
3892 {
3893 vim_free(files[i]);
3894 files[i] = p;
3895 }
3896 }
3897 }
3898}
3899
3900/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003901 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003903 cmdline_info_T *
3904get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003906 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907}
3908
Bram Moolenaar064154c2016-03-19 22:50:43 +01003909#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003910/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003911 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003912 * ccline and put the previous value in prev_ccline.
3913 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003914 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003915get_ccline_ptr(void)
3916{
3917 if ((State & CMDLINE) == 0)
3918 return NULL;
3919 if (ccline.cmdbuff != NULL)
3920 return &ccline;
3921 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3922 return &prev_ccline;
3923 return NULL;
3924}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003925#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003926
Bram Moolenaar064154c2016-03-19 22:50:43 +01003927#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003928/*
3929 * Get the current command line in allocated memory.
3930 * Only works when the command line is being edited.
3931 * Returns NULL when something is wrong.
3932 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003933 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003934get_cmdline_str(void)
3935{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003936 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003937
Bram Moolenaaree91c332018-09-25 22:27:35 +02003938 if (cmdline_star > 0)
3939 return NULL;
3940 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003941 if (p == NULL)
3942 return NULL;
3943 return vim_strnsave(p->cmdbuff, p->cmdlen);
3944}
3945
3946/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003947 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003948 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003949 void
3950f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3951{
3952 rettv->v_type = VAR_STRING;
3953 rettv->vval.v_string = get_cmdline_str();
3954}
3955
3956/*
3957 * "getcmdpos()" function
3958 */
3959 void
3960f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003961{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003962 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003963
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003964 rettv->vval.v_number = 0;
3965 if (p != NULL)
3966 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003967}
3968
3969/*
3970 * Set the command line byte position to "pos". Zero is the first position.
3971 * Only works when the command line is being edited.
3972 * Returns 1 when failed, 0 when OK.
3973 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003974 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003975set_cmdline_pos(
3976 int pos)
3977{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003978 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003979
3980 if (p == NULL)
3981 return 1;
3982
3983 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3984 * changed the command line. */
3985 if (pos < 0)
3986 new_cmdpos = 0;
3987 else
3988 new_cmdpos = pos;
3989 return 0;
3990}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003991
3992/*
3993 * "setcmdpos()" function
3994 */
3995 void
3996f_setcmdpos(typval_T *argvars, typval_T *rettv)
3997{
3998 int pos = (int)tv_get_number(&argvars[0]) - 1;
3999
4000 if (pos >= 0)
4001 rettv->vval.v_number = set_cmdline_pos(pos);
4002}
4003
4004/*
4005 * "getcmdtype()" function
4006 */
4007 void
4008f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4009{
4010 rettv->v_type = VAR_STRING;
4011 rettv->vval.v_string = alloc(2);
4012 if (rettv->vval.v_string != NULL)
4013 {
4014 rettv->vval.v_string[0] = get_cmdline_type();
4015 rettv->vval.v_string[1] = NUL;
4016 }
4017}
4018
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004019#endif
4020
4021#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4022/*
4023 * Get the current command-line type.
4024 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4025 * Only works when the command line is being edited.
4026 * Returns NUL when something is wrong.
4027 */
4028 int
4029get_cmdline_type(void)
4030{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004031 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004032
4033 if (p == NULL)
4034 return NUL;
4035 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004036 return
4037# ifdef FEAT_EVAL
4038 (p->input_fn) ? '@' :
4039# endif
4040 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004041 return p->cmdfirstc;
4042}
4043#endif
4044
Bram Moolenaard7663c22019-08-06 21:59:57 +02004045/*
4046 * Return the first character of the current command line.
4047 */
4048 int
4049get_cmdline_firstc(void)
4050{
4051 return ccline.cmdfirstc;
4052}
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054/*
4055 * Get indices "num1,num2" that specify a range within a list (not a range of
4056 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4057 * Returns OK if parsed successfully, otherwise FAIL.
4058 */
4059 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004060get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 int len;
4063 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004064 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 *str = skipwhite(*str);
4067 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4068 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004069 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 *str += len;
4071 *num1 = (int)num;
4072 first = TRUE;
4073 }
4074 *str = skipwhite(*str);
4075 if (**str == ',') /* parse "to" part of range */
4076 {
4077 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004078 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (len > 0)
4080 {
4081 *num2 = (int)num;
4082 *str = skipwhite(*str + len);
4083 }
4084 else if (!first) /* no number given at all */
4085 return FAIL;
4086 }
4087 else if (first) /* only one number given */
4088 *num2 = *num1;
4089 return OK;
4090}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#if defined(FEAT_CMDWIN) || defined(PROTO)
4093/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004094 * Check value of 'cedit' and set cedit_key.
4095 * Returns NULL if value is OK, error message otherwise.
4096 */
4097 char *
4098check_cedit(void)
4099{
4100 int n;
4101
4102 if (*p_cedit == NUL)
4103 cedit_key = -1;
4104 else
4105 {
4106 n = string_to_key(p_cedit, FALSE);
4107 if (vim_isprintc(n))
4108 return e_invarg;
4109 cedit_key = n;
4110 }
4111 return NULL;
4112}
4113
4114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * Open a window on the current command line and history. Allow editing in
4116 * the window. Returns when the window is closed.
4117 * Returns:
4118 * CR if the command is to be executed
4119 * Ctrl_C if it is to be abandoned
4120 * K_IGNORE if editing continues
4121 */
4122 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004123open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004125 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004127 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 win_T *wp;
4129 int i;
4130 linenr_T lnum;
4131 int histtype;
4132 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 int save_restart_edit = restart_edit;
4134 int save_State = State;
4135 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004136#ifdef FEAT_RIGHTLEFT
4137 int save_cmdmsg_rl = cmdmsg_rl;
4138#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004139#ifdef FEAT_FOLDING
4140 int save_KeyTyped;
4141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 /* Can't do this recursively. Can't do it when typing a password. */
4144 if (cmdwin_type != 0
4145# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4146 || cmdline_star > 0
4147# endif
4148 )
4149 {
4150 beep_flush();
4151 return K_IGNORE;
4152 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004153 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004155 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 win_size_save(&winsizes);
4157
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004158 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4159 // command line window, but we don't want the popup menu then.
4160 pum_undisplay();
4161
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004162 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004163 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004164 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004165
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004166 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4168 {
4169 beep_flush();
4170 return K_IGNORE;
4171 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004172 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004174 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004175 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004176 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004179#ifdef FEAT_FOLDING
4180 curwin->w_p_fen = FALSE;
4181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004183 curwin->w_p_rl = cmdmsg_rl;
4184 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004186 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004188 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004189 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004191 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004192 need_wait_return = FALSE;
4193
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004194 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4196 {
4197 if (p_wc == TAB)
4198 {
4199 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4200 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4201 }
4202 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4203 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004204 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004206 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4207 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004208 curbuf->b_p_tw = 0;
4209
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004210 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004212 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004214 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (i >= 0)
4216 {
4217 lnum = 0;
4218 do
4219 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004220 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004222 if (get_histentry(histtype)[i].hisstr != NULL)
4223 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 (colnr_T)0, FALSE);
4225 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004226 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 }
4229
4230 /* Replace the empty last line with the current command-line and put the
4231 * cursor there. */
4232 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4233 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4234 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004235 changed_line_abv_curs();
4236 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004237 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar23324a02019-10-01 17:39:04 +02004239 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 exmode_active = 0;
4241
4242 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaar23324a02019-10-01 17:39:04 +02004245 // Reset here so it can be set by a CmdWinEnter autocommand.
4246 cmdwin_result = 0;
4247
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004248 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004249 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004250 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004251 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 i = RedrawingDisabled;
4254 RedrawingDisabled = 0;
4255
4256 /*
4257 * Call the main loop until <CR> or CTRL-C is typed.
4258 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004259 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 RedrawingDisabled = i;
4262
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004263# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004264 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004265# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004266
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004267 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004268 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004269
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004270# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004271 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004272 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273# endif
4274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 exmode_active = save_exmode;
4277
Bram Moolenaarf9821062008-06-20 16:31:07 +00004278 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004280 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004283 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 else
4286 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004287# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 /* autocmds may abort script processing */
4289 if (aborting() && cmdwin_result != K_IGNORE)
4290 cmdwin_result = Ctrl_C;
4291# endif
4292 /* Set the new command line from the cmdline buffer. */
4293 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004294 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004296 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4297
4298 if (histtype == HIST_CMD)
4299 {
4300 /* Execute the command directly. */
4301 ccline.cmdbuff = vim_strsave((char_u *)p);
4302 cmdwin_result = CAR;
4303 }
4304 else
4305 {
4306 /* First need to cancel what we were doing. */
4307 ccline.cmdbuff = NULL;
4308 stuffcharReadbuff(':');
4309 stuffReadbuff((char_u *)p);
4310 stuffcharReadbuff(CAR);
4311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 else if (cmdwin_result == K_XF2) /* :qa typed */
4314 {
4315 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4316 cmdwin_result = CAR;
4317 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004318 else if (cmdwin_result == Ctrl_C)
4319 {
4320 /* :q or :close, don't execute any command
4321 * and don't modify the cmd window. */
4322 ccline.cmdbuff = NULL;
4323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 else
4325 ccline.cmdbuff = vim_strsave(ml_get_curline());
4326 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004327 {
4328 ccline.cmdbuff = vim_strsave((char_u *)"");
4329 ccline.cmdlen = 0;
4330 ccline.cmdbufflen = 1;
4331 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 else
4335 {
4336 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4337 ccline.cmdbufflen = ccline.cmdlen + 1;
4338 ccline.cmdpos = curwin->w_cursor.col;
4339 if (ccline.cmdpos > ccline.cmdlen)
4340 ccline.cmdpos = ccline.cmdlen;
4341 if (cmdwin_result == K_IGNORE)
4342 {
4343 set_cmdspos_cursor();
4344 redrawcmd();
4345 }
4346 }
4347
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004348# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004349 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004350 curwin->w_p_cole = 0;
4351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004353 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 win_goto(old_curwin);
4355 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004356
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004357 // win_close() may have already wiped the buffer when 'bh' is
4358 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004359 if (bufref_valid(&bufref))
4360 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004362 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365
4366 ga_clear(&winsizes);
4367 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004368# ifdef FEAT_RIGHTLEFT
4369 cmdmsg_rl = save_cmdmsg_rl;
4370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
4375 return cmdwin_result;
4376}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004377#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379/*
4380 * Used for commands that either take a simple command string argument, or:
4381 * cmd << endmarker
4382 * {script}
4383 * endmarker
4384 * Returns a pointer to allocated memory with {script} or NULL.
4385 */
4386 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004387script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 char_u *theline;
4390 char *end_pattern = NULL;
4391 char dot[] = ".";
4392 garray_T ga;
4393
4394 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4395 return NULL;
4396
4397 ga_init2(&ga, 1, 0x400);
4398
4399 if (cmd[2] != NUL)
4400 end_pattern = (char *)skipwhite(cmd + 2);
4401 else
4402 end_pattern = dot;
4403
4404 for (;;)
4405 {
4406 theline = eap->getline(
4407#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004408 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004410 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004413 {
4414 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 ga_concat(&ga, theline);
4419 ga_append(&ga, '\n');
4420 vim_free(theline);
4421 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004422 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 return (char_u *)ga.ga_data;
4425}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004426
4427#if defined(FEAT_EVAL) || defined(PROTO)
4428/*
4429 * This function is used by f_input() and f_inputdialog() functions. The third
4430 * argument to f_input() specifies the type of completion to use at the
4431 * prompt. The third argument to f_inputdialog() specifies the value to return
4432 * when the user cancels the prompt.
4433 */
4434 void
4435get_user_input(
4436 typval_T *argvars,
4437 typval_T *rettv,
4438 int inputdialog,
4439 int secret)
4440{
4441 char_u *prompt = tv_get_string_chk(&argvars[0]);
4442 char_u *p = NULL;
4443 int c;
4444 char_u buf[NUMBUFLEN];
4445 int cmd_silent_save = cmd_silent;
4446 char_u *defstr = (char_u *)"";
4447 int xp_type = EXPAND_NOTHING;
4448 char_u *xp_arg = NULL;
4449
4450 rettv->v_type = VAR_STRING;
4451 rettv->vval.v_string = NULL;
4452
4453#ifdef NO_CONSOLE_INPUT
4454 // While starting up, there is no place to enter text. When running tests
4455 // with --not-a-term we assume feedkeys() will be used.
4456 if (no_console_input() && !is_not_a_term())
4457 return;
4458#endif
4459
4460 cmd_silent = FALSE; // Want to see the prompt.
4461 if (prompt != NULL)
4462 {
4463 // Only the part of the message after the last NL is considered as
4464 // prompt for the command line
4465 p = vim_strrchr(prompt, '\n');
4466 if (p == NULL)
4467 p = prompt;
4468 else
4469 {
4470 ++p;
4471 c = *p;
4472 *p = NUL;
4473 msg_start();
4474 msg_clr_eos();
4475 msg_puts_attr((char *)prompt, get_echo_attr());
4476 msg_didout = FALSE;
4477 msg_starthere();
4478 *p = c;
4479 }
4480 cmdline_row = msg_row;
4481
4482 if (argvars[1].v_type != VAR_UNKNOWN)
4483 {
4484 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4485 if (defstr != NULL)
4486 stuffReadbuffSpec(defstr);
4487
4488 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4489 {
4490 char_u *xp_name;
4491 int xp_namelen;
4492 long argt;
4493
4494 // input() with a third argument: completion
4495 rettv->vval.v_string = NULL;
4496
4497 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4498 if (xp_name == NULL)
4499 return;
4500
4501 xp_namelen = (int)STRLEN(xp_name);
4502
4503 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4504 &xp_arg) == FAIL)
4505 return;
4506 }
4507 }
4508
4509 if (defstr != NULL)
4510 {
4511 int save_ex_normal_busy = ex_normal_busy;
4512
4513 ex_normal_busy = 0;
4514 rettv->vval.v_string =
4515 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4516 xp_type, xp_arg);
4517 ex_normal_busy = save_ex_normal_busy;
4518 }
4519 if (inputdialog && rettv->vval.v_string == NULL
4520 && argvars[1].v_type != VAR_UNKNOWN
4521 && argvars[2].v_type != VAR_UNKNOWN)
4522 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4523 &argvars[2], buf));
4524
4525 vim_free(xp_arg);
4526
4527 // since the user typed this, no need to wait for return
4528 need_wait_return = FALSE;
4529 msg_didout = FALSE;
4530 }
4531 cmd_silent = cmd_silent_save;
4532}
4533#endif