blob: 769dcb8363020ca3cef4ac98f5b42f38b03a4c8f [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
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int new_cmdpos; // position set by set_cmdline_pos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
Bram Moolenaar217e1b82019-12-01 21:41:28 +010029static 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +010034static int cmd_hkmap = 0; // Hebrew mapping during command line
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#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
Bram Moolenaar217e1b82019-12-01 21:41:28 +010093 // remove trailing \v and the like
Bram Moolenaar66216052017-12-16 16:33:44 +010094 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 Moolenaar217e1b82019-12-01 21:41:28 +0100362 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;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 int gotesc = FALSE; // TRUE when <ESC> just typed
798 int do_abbr; // when TRUE check for abbr.
799 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 int did_wild_list = FALSE; // did wild_list() recently
806 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 int res;
808 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100809 int save_State = State; // remember State when called
810 int some_key_typed = FALSE; // one of the keys was typed
811 // mouse drag and release events are ignored, unless they are
812 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (firstc == ':' || firstc == '=' || firstc == '>')
843 cmd_hkmap = 0;
844#endif
845
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // alloc initial ccline.cmdbuff
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000859 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 Moolenaar217e1b82019-12-01 21:41:28 +0100866 // autoindent for :insert and :append
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000867 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (!cmd_silent)
889 {
890 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 gotocmdline(TRUE);
893 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100922 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100940 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#endif
942
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 // When inside an autocommand for writing "exiting" may be set and
944 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000945 settmode(TMODE_RAW);
946
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100947 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200948 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 Moolenaar217e1b82019-12-01 21:41:28 +0100952 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 Moolenaar217e1b82019-12-01 21:41:28 +0100956 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#endif
958
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100959 // 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.
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200961 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 Moolenaar217e1b82019-12-01 21:41:28 +0100971 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100975 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100979 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.
Bram Moolenaar72532d32018-04-07 19:09:09 +0200982
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200983 // Trigger SafeState if nothing is pending.
984 may_trigger_safestate(xpc.xp_numfiles <= 0);
985
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100986 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +0000987
Bram Moolenaar217e1b82019-12-01 21:41:28 +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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001002 // Invert horizontal movements and operations. Only when
1003 // typed by the user directly, not when the result of a
1004 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Special translations for 'wildmenu'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 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 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // Hitting CR after "emenu Name.": complete submenu
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001098 // Entered command line, move it up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 cmdline_row--;
1100 redrawcmd();
1101 }
1102 else if (save_p_ls != -1)
1103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001104 // restore 'laststatus' and 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 p_ls = save_p_ls;
1106 p_wmh = save_p_wmh;
1107 last_status(FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001108 update_screen(VALID); // redraw the screen NOW
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // Special translations for 'wildmenu'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001129 // 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135 // Hitting <Up>: Remove one submenu name in front of the
1136 // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 int found = FALSE;
1138
1139 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1140 i = 0;
1141 while (--j > 0)
1142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143 // check for start of menu name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (ccline.cmdbuff[j] == ' '
1145 && ccline.cmdbuff[j - 1] != '\\')
1146 {
1147 i = j + 1;
1148 break;
1149 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 // check for start of submenu name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001188 // go down a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001193 // If in a direct ancestor, strip off one ../ to go down
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001219 // go up a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 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 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001256 // TODO this is only for DOS/UNIX systems - need to put in
1257 // machine-specific stuff here and in upseg init
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001264 // 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
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001271#endif // FEAT_WILDMENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001273 // 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.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 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 Moolenaar217e1b82019-12-01 21:41:28 +01001282 // CTRL-\ e doesn't work when obtaining an expression, unless it
1283 // is in a mapping.
Bram Moolenaarb7356812012-10-11 04:04:37 +02001284 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)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001306 new_cmdpos = 99999; // keep it at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 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 Moolenaar217e1b82019-12-01 21:41:28 +01001313 // Need to save and restore ccline. And set "textlock"
1314 // 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 Moolenaar217e1b82019-12-01 21:41:28 +01001329 // Restore the cursor or use the position set with
1330 // set_cmdline_pos().
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001331 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 Moolenaar217e1b82019-12-01 21:41:28 +01001336 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001337 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 Moolenaar217e1b82019-12-01 21:41:28 +01001344 got_int = FALSE; // don't abandon the command line
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001345 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';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 gotesc = TRUE; // will free ccline.cmdbuff after putting it
1356 // in history
1357 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360
1361#ifdef FEAT_CMDWIN
1362 if (c == cedit_key || c == K_CMDWIN)
1363 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001364 // TODO: why is ex_normal_busy checked here?
1365 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001366 {
1367 /*
1368 * Open a window to edit the command line (and history).
1369 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001370 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001371 some_key_typed = TRUE;
1372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374# ifdef FEAT_DIGRAPHS
1375 else
1376# endif
1377#endif
1378#ifdef FEAT_DIGRAPHS
1379 c = do_digraph(c);
1380#endif
1381
1382 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1383 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1384 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001385 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001386 if (exmode_active
1387 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001388 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001389 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001390 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001392 if (c == K_KENTER)
1393 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001395 else
1396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001397 gotesc = FALSE; // Might have typed ESC previously, don't
1398 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001399 if (ccheck_abbr(c + ABBR_OFF))
1400 goto cmdline_changed;
1401 if (!cmd_silent)
1402 {
1403 windgoto(msg_row, 0);
1404 out_flush();
1405 }
1406 break;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409
1410 /*
1411 * Completion for 'wildchar' or 'wildcharm' key.
1412 * - hitting <ESC> twice means: abandon command line.
1413 * - wildcard expansion is only done when the 'wildchar' key is really
1414 * typed, not when it comes from a macro
1415 */
1416 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1417 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001418 int options = WILD_NO_BEEP;
1419 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1420 options |= WILD_BUFLASTUSED;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001421 if (xpc.xp_numfiles > 0) // typed p_wc at least twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001423 // if 'wildmode' contains "list" may still need to list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (xpc.xp_numfiles > 1
1425 && !did_wild_list
1426 && (wim_flags[wim_index] & WIM_LIST))
1427 {
1428 (void)showmatches(&xpc, FALSE);
1429 redrawcmd();
1430 did_wild_list = TRUE;
1431 }
1432 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001433 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001434 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001436 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001437 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001439 res = OK; // don't insert 'wildchar' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001441 else // typed p_wc first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
1443 wim_index = 0;
1444 j = ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001445 // if 'wildmode' first contains "longest", get longest
1446 // common part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001448 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001449 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001451 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001452 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001454 // if interrupted while completing, behave like it failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (got_int)
1456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001457 (void)vpeekc(); // remove <C-C> from input stream
1458 got_int = FALSE; // don't abandon the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1460#ifdef FEAT_WILDMENU
1461 xpc.xp_context = EXPAND_NOTHING;
1462#endif
1463 goto cmdline_changed;
1464 }
1465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001466 // when more than one match, and 'wildmode' first contains
1467 // "list", or no change and 'wildmode' contains "longest,list",
1468 // list all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (res == OK && xpc.xp_numfiles > 1)
1470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001471 // a "longest" that didn't do anything is skipped (but not
1472 // "list:longest")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1474 wim_index = 1;
1475 if ((wim_flags[wim_index] & WIM_LIST)
1476#ifdef FEAT_WILDMENU
1477 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1478#endif
1479 )
1480 {
1481 if (!(wim_flags[0] & WIM_LONGEST))
1482 {
1483#ifdef FEAT_WILDMENU
1484 int p_wmnu_save = p_wmnu;
1485 p_wmnu = 0;
1486#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001487 // remove match
Bram Moolenaarb3479632012-11-28 16:49:58 +01001488 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#ifdef FEAT_WILDMENU
1490 p_wmnu = p_wmnu_save;
1491#endif
1492 }
1493#ifdef FEAT_WILDMENU
1494 (void)showmatches(&xpc, p_wmnu
1495 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1496#else
1497 (void)showmatches(&xpc, FALSE);
1498#endif
1499 redrawcmd();
1500 did_wild_list = TRUE;
1501 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001502 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001503 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001505 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001506 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001509 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511#ifdef FEAT_WILDMENU
1512 else if (xpc.xp_numfiles == -1)
1513 xpc.xp_context = EXPAND_NOTHING;
1514#endif
1515 }
1516 if (wim_index < 3)
1517 ++wim_index;
1518 if (c == ESC)
1519 gotesc = TRUE;
1520 if (res == OK)
1521 goto cmdline_changed;
1522 }
1523
1524 gotesc = FALSE;
1525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (c == K_S_TAB && KeyTyped)
1528 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001529 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1530 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1531 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 goto cmdline_changed;
1533 }
1534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001535 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 c = NL;
1537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001538 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
1540 /*
1541 * Big switch for a typed command line character.
1542 */
1543 switch (c)
1544 {
1545 case K_BS:
1546 case Ctrl_H:
1547 case K_DEL:
1548 case K_KDEL:
1549 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (c == K_KDEL)
1551 c = K_DEL;
1552
1553 /*
1554 * delete current character is the same as backspace on next
1555 * character, except at end of line
1556 */
1557 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1558 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (has_mbyte && c == K_DEL)
1560 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1561 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (ccline.cmdpos > 0)
1563 {
1564 char_u *p;
1565
1566 j = ccline.cmdpos;
1567 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (has_mbyte)
1569 {
1570 p = mb_prevptr(ccline.cmdbuff, p);
1571 if (c == Ctrl_W)
1572 {
1573 while (p > ccline.cmdbuff && vim_isspace(*p))
1574 p = mb_prevptr(ccline.cmdbuff, p);
1575 i = mb_get_class(p);
1576 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1577 p = mb_prevptr(ccline.cmdbuff, p);
1578 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001579 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001582 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1585 --p;
1586 i = vim_iswordc(p[-1]);
1587 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1588 && vim_iswordc(p[-1]) == i)
1589 --p;
1590 }
1591 else
1592 --p;
1593 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1594 ccline.cmdlen -= j - ccline.cmdpos;
1595 i = ccline.cmdpos;
1596 while (i < ccline.cmdlen)
1597 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1598
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001599 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001601#ifdef FEAT_SEARCH_EXTRA
1602 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001603 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001604 is_state.search_start = is_state.save_cursor;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001605 // save view settings, so that the screen
1606 // won't be restored at the wrong position
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001607 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001608 }
1609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 redrawcmd();
1611 }
1612 else if (ccline.cmdlen == 0 && c != Ctrl_W
1613 && ccline.cmdprompt == NULL && indent == 0)
1614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001615 // In ex and debug mode it doesn't make sense to return.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (exmode_active
1617#ifdef FEAT_EVAL
1618 || ccline.cmdfirstc == '>'
1619#endif
1620 )
1621 goto cmdline_not_changed;
1622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001623 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (!cmd_silent)
1625 {
1626#ifdef FEAT_RIGHTLEFT
1627 if (cmdmsg_rl)
1628 msg_col = Columns;
1629 else
1630#endif
1631 msg_col = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001632 msg_putchar(' '); // delete ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001634#ifdef FEAT_SEARCH_EXTRA
1635 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001636 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 redraw_cmdline = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001639 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 goto cmdline_changed;
1642
1643 case K_INS:
1644 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 ccline.overstrike = !ccline.overstrike;
1646#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001647 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
1649 goto cmdline_not_changed;
1650
1651 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001652 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001654 // ":lmap" mappings exists, toggle use of mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001656#ifdef HAVE_INPUT_METHOD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001657 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658#endif
1659 if (b_im_ptr != NULL)
1660 {
1661 if (State & LANGMAP)
1662 *b_im_ptr = B_IMODE_LMAP;
1663 else
1664 *b_im_ptr = B_IMODE_NONE;
1665 }
1666 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001667#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 else
1669 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001670 // There are no ":lmap" mappings, toggle IM. When
1671 // 'imdisable' is set don't try getting the status, it's
1672 // always off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if ((p_imdisable && b_im_ptr != NULL)
1674 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (b_im_ptr != NULL)
1678 *b_im_ptr = B_IMODE_NONE;
1679 }
1680 else
1681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 im_set_active(TRUE); // Enable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (b_im_ptr != NULL)
1684 *b_im_ptr = B_IMODE_IM;
1685 }
1686 }
1687#endif
1688 if (b_im_ptr != NULL)
1689 {
1690 if (b_im_ptr == &curbuf->b_p_iminsert)
1691 set_iminsert_global();
1692 else
1693 set_imsearch_global();
1694 }
1695#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001696 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001698#if defined(FEAT_KEYMAP)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001699 // Show/unshow value of 'keymap' in status lines later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 status_redraw_curbuf();
1701#endif
1702 goto cmdline_not_changed;
1703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001704// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001706 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 j = ccline.cmdpos;
1708 ccline.cmdlen -= j;
1709 i = ccline.cmdpos = 0;
1710 while (i < ccline.cmdlen)
1711 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001712 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001714#ifdef FEAT_SEARCH_EXTRA
1715 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001716 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 redrawcmd();
1719 goto cmdline_changed;
1720
1721#ifdef FEAT_CLIPBOARD
1722 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001723 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (clip_star.state != SELECT_CLEARED)
1725 {
1726 if (clip_star.state == SELECT_DONE)
1727 clip_copy_modeless_selection(TRUE);
1728 goto cmdline_not_changed;
1729 }
1730 break;
1731#endif
1732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001735 // In exmode it doesn't make sense to return. Except when
1736 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001737 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001738 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 goto cmdline_not_changed;
1740
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001741 gotesc = TRUE; // will free ccline.cmdbuff after
1742 // putting it in history
1743 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001745 case Ctrl_R: // insert register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#endif
1749 putcmdline('"', TRUE);
1750 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001751 ++allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001752 i = c = plain_vgetc(); // CTRL-R <char>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (i == Ctrl_O)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (i == Ctrl_R)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001756 c = plain_vgetc(); // CTRL-R CTRL-R <char>
Bram Moolenaara92522f2017-07-15 15:21:38 +02001757 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001759 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#ifdef FEAT_EVAL
1761 /*
1762 * Insert the result of an expression.
1763 * Need to save the current command line, to be able to enter
1764 * a new one...
1765 */
1766 new_cmdpos = -1;
1767 if (c == '=')
1768 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001769 if (ccline.cmdfirstc == '=' // can't do this recursively
1770 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
1772 beep_flush();
1773 c = ESC;
1774 }
1775 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 }
1778#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001779 if (c != ESC) // use ESC to cancel inserting register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001781 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001782
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001783#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001784 // When there was a serious error abort getting the
1785 // command line.
Bram Moolenaaracf53452005-12-17 22:06:52 +00001786 if (aborting())
1787 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001788 gotesc = TRUE; // will free ccline.cmdbuff after
1789 // putting it in history
1790 goto returncmd; // back to cmd mode
Bram Moolenaaracf53452005-12-17 22:06:52 +00001791 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001792#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001793 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#ifdef FEAT_EVAL
1795 if (new_cmdpos >= 0)
1796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001797 // set_cmdline_pos() was used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (new_cmdpos > ccline.cmdlen)
1799 ccline.cmdpos = ccline.cmdlen;
1800 else
1801 ccline.cmdpos = new_cmdpos;
1802 }
1803#endif
1804 }
1805 redrawcmd();
1806 goto cmdline_changed;
1807
1808 case Ctrl_D:
1809 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001810 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001813 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 case K_RIGHT:
1816 case K_S_RIGHT:
1817 case K_C_RIGHT:
1818 do
1819 {
1820 if (ccline.cmdpos >= ccline.cmdlen)
1821 break;
1822 i = cmdline_charsize(ccline.cmdpos);
1823 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1824 break;
1825 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001827 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 + ccline.cmdpos);
1829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 ++ccline.cmdpos;
1831 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001832 while ((c == K_S_RIGHT || c == K_C_RIGHT
1833 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (has_mbyte)
1836 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 goto cmdline_not_changed;
1838
1839 case K_LEFT:
1840 case K_S_LEFT:
1841 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001842 if (ccline.cmdpos == 0)
1843 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 do
1845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001847 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1849 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1851 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001852 while (ccline.cmdpos > 0
1853 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001854 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (has_mbyte)
1857 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 goto cmdline_not_changed;
1859
1860 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001861 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001862 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar4f974752019-02-17 17:44:42 +01001864#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001865 // On MS-Windows ignore <M-F4>, we get it when closing the window
1866 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001867 case K_F4:
1868 if (mod_mask == MOD_MASK_ALT)
1869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001870 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00001871 goto cmdline_not_changed;
1872 }
1873 break;
1874#endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 case K_MIDDLEDRAG:
1877 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001878 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880 case K_MIDDLEMOUSE:
1881# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001882 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (!gui.in_use)
1884# endif
1885 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001887# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001889 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001891# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001892 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 redrawcmd();
1894 goto cmdline_changed;
1895
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001896# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001898 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 redrawcmd();
1900 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 case K_LEFTDRAG:
1904 case K_LEFTRELEASE:
1905 case K_RIGHTDRAG:
1906 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001907 // Ignore drag and release events when the button-down wasn't
1908 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (ignore_drag_release)
1910 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001911 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 case K_LEFTMOUSE:
1913 case K_RIGHTMOUSE:
1914 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1915 ignore_drag_release = TRUE;
1916 else
1917 ignore_drag_release = FALSE;
1918# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001919 // When GUI is active, also move when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (!gui.in_use)
1921# endif
1922 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001923 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924# ifdef FEAT_CLIPBOARD
1925 if (mouse_row < cmdline_row && clip_star.available)
1926 {
1927 int button, is_click, is_drag;
1928
1929 /*
1930 * Handle modeless selection.
1931 */
1932 button = get_mouse_button(KEY2TERMCAP1(c),
1933 &is_click, &is_drag);
1934 if (mouse_model_popup() && button == MOUSE_LEFT
1935 && (mod_mask & MOD_MASK_SHIFT))
1936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001937 // Translate shift-left to right button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 button = MOUSE_RIGHT;
1939 mod_mask &= ~MOD_MASK_SHIFT;
1940 }
1941 clip_modeless(button, is_click, is_drag);
1942 goto cmdline_not_changed;
1943 }
1944# endif
1945
1946 set_cmdspos();
1947 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1948 ++ccline.cmdpos)
1949 {
1950 i = cmdline_charsize(ccline.cmdpos);
1951 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1952 && mouse_col < ccline.cmdspos % Columns + i)
1953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (has_mbyte)
1955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001956 // Count ">" for double-wide char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001958 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 + ccline.cmdpos) - 1;
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 ccline.cmdspos += i;
1962 }
1963 goto cmdline_not_changed;
1964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 case K_MOUSEDOWN:
1967 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001968 case K_MOUSELEFT:
1969 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001970 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 case K_X1MOUSE:
1972 case K_X1DRAG:
1973 case K_X1RELEASE:
1974 case K_X2MOUSE:
1975 case K_X2DRAG:
1976 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001977 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto cmdline_not_changed;
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 case K_LEFTRELEASE_NM:
1983 goto cmdline_not_changed;
1984
1985 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001986 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 gui_do_scroll();
1989 redrawcmd();
1990 }
1991 goto cmdline_not_changed;
1992
1993 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001994 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001996 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 redrawcmd();
1998 }
1999 goto cmdline_not_changed;
2000#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002001#ifdef FEAT_GUI_TABLINE
2002 case K_TABLINE:
2003 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002004 // Don't want to change any tabs here. Make sure the same tab
2005 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002006 if (gui_use_tabline())
2007 gui_mch_set_curtab(tabpage_index(curtab));
2008 goto cmdline_not_changed;
2009#endif
2010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002011 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto cmdline_not_changed;
2013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002014 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 case K_HOME:
2016 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 case K_S_HOME:
2018 case K_C_HOME:
2019 ccline.cmdpos = 0;
2020 set_cmdspos();
2021 goto cmdline_not_changed;
2022
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002023 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 case K_END:
2025 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 case K_S_END:
2027 case K_C_END:
2028 ccline.cmdpos = ccline.cmdlen;
2029 set_cmdspos_cursor();
2030 goto cmdline_not_changed;
2031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01002033 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 break;
2035 goto cmdline_changed;
2036
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002037 case Ctrl_L:
2038#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002039 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002040 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002041#endif
2042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002044 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 break;
2046 goto cmdline_changed;
2047
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002048 case Ctrl_N: // next match
2049 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002050 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002052 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2053 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002055 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002057 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 case K_UP:
2059 case K_DOWN:
2060 case K_S_UP:
2061 case K_S_DOWN:
2062 case K_PAGEUP:
2063 case K_KPAGEUP:
2064 case K_PAGEDOWN:
2065 case K_KPAGEDOWN:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002066 if (get_hislen() == 0 || firstc == NUL) // no history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 goto cmdline_not_changed;
2068
2069 i = hiscnt;
2070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002071 // save current command string so it can be restored later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (lookfor == NULL)
2073 {
2074 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2075 goto cmdline_not_changed;
2076 lookfor[ccline.cmdpos] = NUL;
2077 }
2078
2079 j = (int)STRLEN(lookfor);
2080 for (;;)
2081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // one step backwards
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002083 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002084 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 if (hiscnt == get_hislen()) // first time
Bram Moolenaard7663c22019-08-06 21:59:57 +02002087 hiscnt = *get_hisidx(histype);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 else if (hiscnt == 0 && *get_hisidx(histype)
2089 != get_hislen() - 1)
Bram Moolenaard7663c22019-08-06 21:59:57 +02002090 hiscnt = get_hislen() - 1;
2091 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 --hiscnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 else // at top of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 hiscnt = i;
2096 break;
2097 }
2098 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 else // one step forwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002101 // on last entry, clear the line
Bram Moolenaard7663c22019-08-06 21:59:57 +02002102 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002104 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 break;
2106 }
2107
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 // not on a history line, nothing to do
Bram Moolenaard7663c22019-08-06 21:59:57 +02002109 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002111 if (hiscnt == get_hislen() - 1) // wrap around
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 hiscnt = 0;
2113 else
2114 ++hiscnt;
2115 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
2117 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 hiscnt = i;
2120 break;
2121 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002122 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002123 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002124 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 lookfor, (size_t)j) == 0)
2126 break;
2127 }
2128
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 if (hiscnt != i) // jumped to other entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 char_u *p;
2132 int len;
2133 int old_firstc;
2134
Bram Moolenaar438d1762018-09-30 17:11:48 +02002135 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002136 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002137 if (hiscnt == get_hislen())
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 p = lookfor; // back to the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002140 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 if (histype == HIST_SEARCH
2143 && p != lookfor
2144 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 // Correct for the separator character used when
2147 // adding the history entry vs the one used now.
2148 // First loop: count length.
2149 // Second loop: copy the characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 for (i = 0; i <= 1; ++i)
2151 {
2152 len = 0;
2153 for (j = 0; p[j] != NUL; ++j)
2154 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002155 // Replace old sep with new sep, unless it is
2156 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (p[j] == old_firstc
2158 && (j == 0 || p[j - 1] != '\\'))
2159 {
2160 if (i > 0)
2161 ccline.cmdbuff[len] = firstc;
2162 }
2163 else
2164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002165 // Escape new sep, unless it is already
2166 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (p[j] == firstc
2168 && (j == 0 || p[j - 1] != '\\'))
2169 {
2170 if (i > 0)
2171 ccline.cmdbuff[len] = '\\';
2172 ++len;
2173 }
2174 if (i > 0)
2175 ccline.cmdbuff[len] = p[j];
2176 }
2177 ++len;
2178 }
2179 if (i == 0)
2180 {
2181 alloc_cmdbuff(len);
2182 if (ccline.cmdbuff == NULL)
2183 goto returncmd;
2184 }
2185 }
2186 ccline.cmdbuff[len] = NUL;
2187 }
2188 else
2189 {
2190 alloc_cmdbuff((int)STRLEN(p));
2191 if (ccline.cmdbuff == NULL)
2192 goto returncmd;
2193 STRCPY(ccline.cmdbuff, p);
2194 }
2195
2196 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2197 redrawcmd();
2198 goto cmdline_changed;
2199 }
2200 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002201 goto cmdline_not_changed;
2202
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002203#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 case Ctrl_G: // next match
2205 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002206 if (may_adjust_incsearch_highlighting(
2207 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002208 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#endif
2211
2212 case Ctrl_V:
2213 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002215 int prev_mod_mask = mod_mask;
2216
2217 ignore_drag_release = TRUE;
2218 putcmdline('^', TRUE);
2219 c = get_literal(); // get next (two) character(s)
2220 do_abbr = FALSE; // don't do abbreviation now
2221 extra_char = NUL;
2222 // may need to remove ^ when composing char was typed
2223 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2224 {
2225 draw_cmdline(ccline.cmdpos,
2226 ccline.cmdlen - ccline.cmdpos);
2227 msg_putchar(' ');
2228 cursorcmd();
2229 }
2230
2231 if ((c == ESC || c == CSI)
2232 && !(prev_mod_mask & MOD_MASK_SHIFT))
2233 // Using CTRL-V: Change any modifyOtherKeys ESC
2234 // sequence to a normal key. Don't do this for
2235 // CTRL-SHIFT-V.
2236 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 break;
2240
2241#ifdef FEAT_DIGRAPHS
2242 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002245# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002246 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002249 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (c != NUL)
2251 break;
2252
2253 redrawcmd();
2254 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002255#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002258 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (!p_ari)
2260 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002261 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 goto cmdline_not_changed;
2263#endif
2264
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002265 case K_PS:
2266 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2267 goto cmdline_changed;
2268
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 default:
2270#ifdef UNIX
2271 if (c == intr_char)
2272 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002273 gotesc = TRUE; // will free ccline.cmdbuff after
2274 // putting it in history
2275 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277#endif
2278 /*
2279 * Normal character with no special meaning. Just set mod_mask
2280 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2281 * the string <S-Space>. This should only happen after ^V.
2282 */
2283 if (!IS_SPECIAL(c))
2284 mod_mask = 0x0;
2285 break;
2286 }
2287 /*
2288 * End of switch on command line character.
2289 * We come here if we have a normal character.
2290 */
2291
Bram Moolenaar13505972019-01-24 15:04:48 +01002292 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2293 && (ccheck_abbr(
2294 // Add ABBR_OFF for characters above 0x100, this is
2295 // what check_abbr() expects.
2296 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2297 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 goto cmdline_changed;
2299
2300 /*
2301 * put the character in the command line
2302 */
2303 if (IS_SPECIAL(c) || mod_mask != 0)
2304 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2305 else
2306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 if (has_mbyte)
2308 {
2309 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002310 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 put_on_cmdline(IObuff, j, TRUE);
2312 }
2313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 IObuff[0] = c;
2316 put_on_cmdline(IObuff, 1, TRUE);
2317 }
2318 }
2319 goto cmdline_changed;
2320
2321/*
2322 * This part implements incremental searches for "/" and "?"
2323 * Jump to cmdline_not_changed when a character has been read but the command
2324 * line did not change. Then we only search and redraw if something changed in
2325 * the past.
2326 * Jump to cmdline_changed when the command line did change.
2327 * (Sorry for the goto's, I know it is ugly).
2328 */
2329cmdline_not_changed:
2330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002331 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 continue;
2333#endif
2334
2335cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002336 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002337 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002340 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342
2343#ifdef FEAT_RIGHTLEFT
2344 if (cmdmsg_rl
2345# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002346 || (p_arshape && !p_tbidi
2347 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348# endif
2349 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 // Always redraw the whole command line to fix shaping and
2351 // right-left typing. Not efficient, but it works.
2352 // Do it only when there are no characters left to read
2353 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002354 if (vpeekc() == NUL)
2355 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357 }
2358
2359returncmd:
2360
2361#ifdef FEAT_RIGHTLEFT
2362 cmdmsg_rl = FALSE;
2363#endif
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002366 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002369 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#endif
2371
2372 if (ccline.cmdbuff != NULL)
2373 {
2374 /*
2375 * Put line in history buffer (":" and "=" only when it was typed).
2376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (ccline.cmdlen && firstc != NUL
2378 && (some_key_typed || histype == HIST_SEARCH))
2379 {
2380 add_to_history(histype, ccline.cmdbuff, TRUE,
2381 histype == HIST_SEARCH ? firstc : NUL);
2382 if (firstc == ':')
2383 {
2384 vim_free(new_last_cmdline);
2385 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2386 }
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002389 if (gotesc)
2390 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392
2393 /*
2394 * If the screen was shifted up, redraw the whole screen (later).
2395 * If the line is too long, clear it, so ruler and shown command do
2396 * not get printed in the middle of it.
2397 */
2398 msg_check();
2399 msg_scroll = save_msg_scroll;
2400 redir_off = FALSE;
2401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002402 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (some_key_typed)
2404 need_wait_return = FALSE;
2405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002407 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002410#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2412 im_save_status(b_im_ptr);
2413 im_set_active(FALSE);
2414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002417 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002419 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaar438d1762018-09-30 17:11:48 +02002421theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002422 {
2423 char_u *p = ccline.cmdbuff;
2424
Bram Moolenaar438d1762018-09-30 17:11:48 +02002425 if (did_save_ccline)
2426 restore_cmdline(&save_ccline);
2427 else
2428 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002429 return p;
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431}
2432
2433#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2434/*
2435 * Get a command line with a prompt.
2436 * This is prepared to be called recursively from getcmdline() (e.g. by
2437 * f_input() when evaluating an expression from CTRL-R =).
2438 * Returns the command line in allocated memory, or NULL.
2439 */
2440 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002441getcmdline_prompt(
2442 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002443 char_u *prompt, // command line prompt
2444 int attr, // attributes for prompt
2445 int xp_context, // type of expansion
2446 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002449 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002450 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002452 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaar438d1762018-09-30 17:11:48 +02002454 if (ccline.cmdbuff != NULL)
2455 {
2456 // Save the values of the current cmdline and restore them below.
2457 save_cmdline(&save_ccline);
2458 did_save_ccline = TRUE;
2459 }
2460
Bram Moolenaar66b51422019-08-18 21:44:12 +02002461 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 ccline.cmdprompt = prompt;
2463 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002464# ifdef FEAT_EVAL
2465 ccline.xp_context = xp_context;
2466 ccline.xp_arg = xp_arg;
2467 ccline.input_fn = (firstc == '@');
2468# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002469 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002470 s = getcmdline_int(firstc, 1L, 0, FALSE);
2471
2472 if (did_save_ccline)
2473 restore_cmdline(&save_ccline);
2474
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002475 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002476 // Restore msg_col, the prompt from input() may have changed it.
2477 // But only if called recursively and the commandline is therefore being
2478 // restored to an old one; if not, the input() prompt stays on the screen,
2479 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002480 if (ccline.cmdbuff != NULL)
2481 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 return s;
2484}
2485#endif
2486
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002487/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002488 * Read the 'wildmode' option, fill wim_flags[].
2489 */
2490 int
2491check_opt_wim(void)
2492{
2493 char_u new_wim_flags[4];
2494 char_u *p;
2495 int i;
2496 int idx = 0;
2497
2498 for (i = 0; i < 4; ++i)
2499 new_wim_flags[i] = 0;
2500
2501 for (p = p_wim; *p; ++p)
2502 {
2503 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2504 ;
2505 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2506 return FAIL;
2507 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2508 new_wim_flags[idx] |= WIM_LONGEST;
2509 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2510 new_wim_flags[idx] |= WIM_FULL;
2511 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2512 new_wim_flags[idx] |= WIM_LIST;
2513 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2514 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2515 else
2516 return FAIL;
2517 p += i;
2518 if (*p == NUL)
2519 break;
2520 if (*p == ',')
2521 {
2522 if (idx == 3)
2523 return FAIL;
2524 ++idx;
2525 }
2526 }
2527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002528 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002529 while (idx < 3)
2530 {
2531 new_wim_flags[idx + 1] = new_wim_flags[idx];
2532 ++idx;
2533 }
2534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002535 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002536 for (i = 0; i < 4; ++i)
2537 wim_flags[i] = new_wim_flags[i];
2538 return OK;
2539}
2540
2541/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002542 * Return TRUE when the text must not be changed and we can't switch to
2543 * another window or buffer. Used when editing the command line, evaluating
2544 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002545 */
2546 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002547text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002548{
2549#ifdef FEAT_CMDWIN
2550 if (cmdwin_type != 0)
2551 return TRUE;
2552#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002553 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002554}
2555
2556/*
2557 * Give an error message for a command that isn't allowed while the cmdline
2558 * window is open or editing the cmdline in another way.
2559 */
2560 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002561text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002563 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002564}
2565
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002567get_text_locked_msg(void)
2568{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569#ifdef FEAT_CMDWIN
2570 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002571 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002572#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002573 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002574}
2575
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002576/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002577 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2578 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002579 */
2580 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002581curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002582{
2583 if (curbuf_lock > 0)
2584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002585 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002586 return TRUE;
2587 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002588 return allbuf_locked();
2589}
2590
2591/*
2592 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2593 * message.
2594 */
2595 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002596allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002597{
2598 if (allbuf_lock > 0)
2599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002600 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002601 return TRUE;
2602 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002603 return FALSE;
2604}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002607cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002610 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return 1;
2612#endif
2613 return ptr2cells(ccline.cmdbuff + idx);
2614}
2615
2616/*
2617 * Compute the offset of the cursor on the command line for the prompt and
2618 * indent.
2619 */
2620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002621set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002623 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 ccline.cmdspos = 1 + ccline.cmdindent;
2625 else
2626 ccline.cmdspos = 0 + ccline.cmdindent;
2627}
2628
2629/*
2630 * Compute the screen position for the cursor on the command line.
2631 */
2632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002633set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 int i, m, c;
2636
2637 set_cmdspos();
2638 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002641 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002642 m = MAXCOL;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else
2645 m = MAXCOL;
2646 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2647 {
2648 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002649 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (has_mbyte)
2651 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002652 // If the cmdline doesn't fit, show cursor on last visible char.
2653 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if ((ccline.cmdspos += c) >= m)
2655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 ccline.cmdspos -= c;
2657 break;
2658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002660 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662}
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664/*
2665 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2666 * character that doesn't fit, so that a ">" must be displayed.
2667 */
2668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002669correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002671 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2673 && ccline.cmdspos % Columns + cells > Columns)
2674 ccline.cmdspos++;
2675}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677/*
2678 * Get an Ex command line for the ":" command.
2679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002681getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002682 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002683 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002684 int indent, // indent for inside conditionals
Bram Moolenaare96a2492019-06-25 04:12:16 +02002685 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002687 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (exec_from_reg && vpeekc() == ':')
2689 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002690 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691}
2692
2693/*
2694 * Get an Ex command line for Ex mode.
2695 * In Ex mode we only use the OS supplied line editing features and no
2696 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002700getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002701 int promptc, // normally ':', NUL for ":append" and '?' for
2702 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002703 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002704 int indent, // indent for inside conditionals
Bram Moolenaare96a2492019-06-25 04:12:16 +02002705 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 garray_T line_ga;
2708 char_u *pend;
2709 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002710 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002711 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002712 int vcol = 0;
2713 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002714 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002715 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002717 // Switch cursor on now. This avoids that it happens after the "\n", which
2718 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 cursor_on();
2720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002721 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002723 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002727 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 if (p_prompt)
2729 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 while (indent-- > 0)
2731 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734
2735 ga_init2(&line_ga, 1, 30);
2736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002739 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002740 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002741 while (indent >= 8)
2742 {
2743 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002744 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002745 indent -= 8;
2746 }
2747 while (indent-- > 0)
2748 {
2749 ga_append(&line_ga, ' ');
2750 msg_putchar(' ');
2751 }
2752 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 ++no_mapping;
2754 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 /*
2757 * Get the line, one character at a time.
2758 */
2759 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002762 long sw;
2763 char_u *s;
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (ga_grow(&line_ga, 40) == FAIL)
2766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaarba748c82017-02-26 14:00:07 +01002768 /*
2769 * Get one character at a time.
2770 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002771 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002773 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002774 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2775 c1 = '\n';
2776 else
2777 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 * Handle line editing.
2781 * Previously this was left to the system, putting the terminal in
2782 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002784 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 msg_putchar('\n');
2787 break;
2788 }
2789
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002790 if (c1 == K_PS)
2791 {
2792 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2793 goto redraw;
2794 }
2795
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002796 if (!escaped)
2797 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002798 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 if (c1 == '\r')
2800 c1 = '\n';
2801
2802 if (c1 == BS || c1 == K_BS
2803 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 if (line_ga.ga_len > 0)
2806 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002807 if (has_mbyte)
2808 {
2809 p = (char_u *)line_ga.ga_data;
2810 p[line_ga.ga_len] = NUL;
2811 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2812 line_ga.ga_len -= len;
2813 }
2814 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002815 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 goto redraw;
2817 }
2818 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002821 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 msg_col = startcol;
2824 msg_clr_eos();
2825 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002826 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 }
2828
2829 if (c1 == Ctrl_T)
2830 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002831 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 p = (char_u *)line_ga.ga_data;
2833 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002834 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002835 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002836add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002837 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002840 p = (char_u *)line_ga.ga_data;
2841 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002842 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2843 *s = ' ';
2844 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002847 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002850 p = (char_u *)line_ga.ga_data;
2851 p[line_ga.ga_len] = NUL;
2852 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002858 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002859 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002863 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002864 msg_outtrans_len(p, len);
2865 vcol += ptr2cells(p);
2866 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002870 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 continue;
2872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 if (c1 == Ctrl_D)
2875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002876 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 p = (char_u *)line_ga.ga_data;
2878 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 if (prev_char == '^')
2881 ex_keep_indent = TRUE;
2882 indent = 0;
2883 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 else
2886 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002887 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002888 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002889 if (indent > 0)
2890 {
2891 --indent;
2892 indent -= indent % get_sw_value(curbuf);
2893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002895 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002896 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002897 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2899 --line_ga.ga_len;
2900 }
2901 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903
2904 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2905 {
2906 escaped = TRUE;
2907 continue;
2908 }
2909
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002910 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911 if (IS_SPECIAL(c1))
2912 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002914
2915 if (IS_SPECIAL(c1))
2916 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002917 if (has_mbyte)
2918 len = (*mb_char2bytes)(c1,
2919 (char_u *)line_ga.ga_data + line_ga.ga_len);
2920 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002921 {
2922 len = 1;
2923 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2924 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002925 if (c1 == '\n')
2926 msg_putchar('\n');
2927 else if (c1 == TAB)
2928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002929 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002930 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002931 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002932 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002937 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002938 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002940 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 escaped = FALSE;
2942
2943 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002944 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002945
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002946 // We are done when a NL is entered, but not when it comes after an
2947 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002948 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002950 int bcount = 0;
2951
2952 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2953 ++bcount;
2954
2955 if (bcount > 0)
2956 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002957 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2958 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002959 line_ga.ga_len -= (bcount + 1) / 2;
2960 pend -= (bcount + 1) / 2;
2961 pend[-1] = '\n';
2962 }
2963
2964 if ((bcount & 1) == 0)
2965 {
2966 --line_ga.ga_len;
2967 --pend;
2968 *pend = NUL;
2969 break;
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 }
2973
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002974 --no_mapping;
2975 --allow_keys;
2976
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002977 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 msg_didout = FALSE;
2979 msg_col = 0;
2980 if (msg_row < Rows - 1)
2981 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002982 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984 if (got_int)
2985 ga_clear(&line_ga);
2986
2987 return (char_u *)line_ga.ga_data;
2988}
2989
Bram Moolenaare344bea2005-09-01 20:46:49 +00002990# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2991 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
2993 * Return TRUE if ccline.overstrike is on.
2994 */
2995 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002996cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 return ccline.overstrike;
2999}
3000
3001/*
3002 * Return TRUE if the cursor is at the end of the cmdline.
3003 */
3004 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003005cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 return (ccline.cmdpos >= ccline.cmdlen);
3008}
3009#endif
3010
Bram Moolenaar9372a112005-12-06 19:59:18 +00003011#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012/*
3013 * Return the virtual column number at the current cursor position.
3014 * This is used by the IM code to obtain the start of the preedit string.
3015 */
3016 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003017cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3020 return MAXCOL;
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (has_mbyte)
3023 {
3024 colnr_T col;
3025 int i = 0;
3026
3027 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003028 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 return col;
3031 }
3032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 return ccline.cmdpos;
3034}
3035#endif
3036
3037#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3038/*
3039 * If part of the command line is an IM preedit string, redraw it with
3040 * IM feedback attributes. The cursor position is restored after drawing.
3041 */
3042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003043redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 if ((State & CMDLINE)
3046 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003047 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 && !p_imdisable
3049 && im_is_preediting())
3050 {
3051 int cmdpos = 0;
3052 int cmdspos;
3053 int old_row;
3054 int old_col;
3055 colnr_T col;
3056
3057 old_row = msg_row;
3058 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003059 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 if (has_mbyte)
3062 {
3063 for (col = 0; col < preedit_start_col
3064 && cmdpos < ccline.cmdlen; ++col)
3065 {
3066 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003067 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
3069 }
3070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 cmdspos += preedit_start_col;
3073 cmdpos += preedit_start_col;
3074 }
3075
3076 msg_row = cmdline_row + (cmdspos / (int)Columns);
3077 msg_col = cmdspos % (int)Columns;
3078 if (msg_row >= Rows)
3079 msg_row = Rows - 1;
3080
3081 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3082 {
3083 int char_len;
3084 int char_attr;
3085
3086 char_attr = im_get_feedback_attr(col);
3087 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003088 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003091 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 char_len = 1;
3094
3095 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3096 cmdpos += char_len;
3097 }
3098
3099 msg_row = old_row;
3100 msg_col = old_col;
3101 }
3102}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003103#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
3105/*
3106 * Allocate a new command line buffer.
3107 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 /*
3113 * give some extra space to avoid having to allocate all the time
3114 */
3115 if (len < 80)
3116 len = 100;
3117 else
3118 len += 20;
3119
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003120 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 ccline.cmdbufflen = len;
3122}
3123
3124/*
3125 * Re-allocate the command line to length len + something extra.
3126 * return FAIL for failure, OK otherwise
3127 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003129realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 char_u *p;
3132
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003133 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003134 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003137 alloc_cmdbuff(len); // will get some more
3138 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003140 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return FAIL;
3142 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003143 // There isn't always a NUL after the command, but it may need to be
3144 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003145 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3146 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003148
3149 if (ccline.xpc != NULL
3150 && ccline.xpc->xp_pattern != NULL
3151 && ccline.xpc->xp_context != EXPAND_NOTHING
3152 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3153 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003154 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003156 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3157 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003158 if (i >= 0 && i <= ccline.cmdlen)
3159 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3160 }
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return OK;
3163}
3164
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003165#if defined(FEAT_ARABIC) || defined(PROTO)
3166static char_u *arshape_buf = NULL;
3167
3168# if defined(EXITFREE) || defined(PROTO)
3169 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003170free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003171{
3172 vim_free(arshape_buf);
3173}
3174# endif
3175#endif
3176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177/*
3178 * Draw part of the cmdline at the current cursor position. But draw stars
3179 * when cmdline_star is TRUE.
3180 */
3181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003182draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3185 int i;
3186
3187 if (cmdline_star > 0)
3188 for (i = 0; i < len; ++i)
3189 {
3190 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003192 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194 else
3195#endif
3196#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003197 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 static int buflen = 0;
3200 char_u *p;
3201 int j;
3202 int newlen = 0;
3203 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003204 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 int prev_c = 0;
3206 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003207 int u8c;
3208 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 /*
3212 * Do arabic shaping into a temporary buffer. This is very
3213 * inefficient!
3214 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003215 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003217 // Re-allocate the buffer. We keep it around to avoid a lot of
3218 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003219 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003220 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 arshape_buf = alloc(buflen);
3222 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003223 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003226 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003229 arshape_buf[0] = ' ';
3230 newlen = 1;
3231 }
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 for (j = start; j < start + len; j += mb_l)
3234 {
3235 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003236 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003237 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (ARABIC_CHAR(u8c))
3239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003240 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 if (cmdmsg_rl)
3242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003243 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 pc = prev_c;
3245 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003246 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (j + mb_l >= start + len)
3248 nc = NUL;
3249 else
3250 nc = utf_ptr2char(p + mb_l);
3251 }
3252 else
3253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003254 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (j + mb_l >= start + len)
3256 pc = NUL;
3257 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003258 {
3259 int pcc[MAX_MCO];
3260
3261 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003263 pc1 = pcc[0];
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 nc = prev_c;
3266 }
3267 prev_c = u8c;
3268
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003269 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003272 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003274 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3275 if (u8cc[1] != 0)
3276 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 }
3280 else
3281 {
3282 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003283 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 newlen += mb_l;
3285 }
3286 }
3287
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003288 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290 else
3291#endif
3292 msg_outtrans_len(ccline.cmdbuff + start, len);
3293}
3294
3295/*
3296 * Put a character on the command line. Shifts the following text to the
3297 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3298 * "c" must be printable (fit in one display cell)!
3299 */
3300 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003301putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 if (cmd_silent)
3304 return;
3305 msg_no_more = TRUE;
3306 msg_putchar(c);
3307 if (shift)
3308 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3309 msg_no_more = FALSE;
3310 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003311 extra_char = c;
3312 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314
3315/*
3316 * Undo a putcmdline(c, FALSE).
3317 */
3318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003319unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 if (cmd_silent)
3322 return;
3323 msg_no_more = TRUE;
3324 if (ccline.cmdlen == ccline.cmdpos)
3325 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003326 else if (has_mbyte)
3327 draw_cmdline(ccline.cmdpos,
3328 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 else
3330 draw_cmdline(ccline.cmdpos, 1);
3331 msg_no_more = FALSE;
3332 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003333 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334}
3335
3336/*
3337 * Put the given string, of the given length, onto the command line.
3338 * If len is -1, then STRLEN() is used to calculate the length.
3339 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3340 * part will be redrawn, otherwise it will not. If this function is called
3341 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3342 * called afterwards.
3343 */
3344 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003345put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 int retval;
3348 int i;
3349 int m;
3350 int c;
3351
3352 if (len < 0)
3353 len = (int)STRLEN(str);
3354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003355 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003357 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
3359 retval = OK;
3360 if (retval == OK)
3361 {
3362 if (!ccline.overstrike)
3363 {
3364 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3365 ccline.cmdbuff + ccline.cmdpos,
3366 (size_t)(ccline.cmdlen - ccline.cmdpos));
3367 ccline.cmdlen += len;
3368 }
3369 else
3370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (has_mbyte)
3372 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003373 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003375 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003377 // Count nr of bytes in cmdline that are overwritten by these
3378 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003380 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 --m;
3382 if (i < ccline.cmdlen)
3383 {
3384 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3385 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3386 ccline.cmdlen += ccline.cmdpos + len - i;
3387 }
3388 else
3389 ccline.cmdlen = ccline.cmdpos + len;
3390 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003391 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 ccline.cmdlen = ccline.cmdpos + len;
3393 }
3394 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3395 ccline.cmdbuff[ccline.cmdlen] = NUL;
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (enc_utf8)
3398 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003399 // When the inserted text starts with a composing character,
3400 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 i = 0;
3402 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3403 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3404 {
3405 i = (*mb_head_off)(ccline.cmdbuff,
3406 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3407 ccline.cmdpos -= i;
3408 len += i;
3409 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3410 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003411#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3413 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003414 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 i = (*mb_head_off)(ccline.cmdbuff,
3416 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3417 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3418 + ccline.cmdpos - i), c))
3419 {
3420 ccline.cmdpos -= i;
3421 len += i;
3422 }
3423 else
3424 i = 0;
3425 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (i != 0)
3428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003429 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3431 ccline.cmdspos -= i;
3432 msg_col -= i;
3433 if (msg_col < 0)
3434 {
3435 msg_col += Columns;
3436 --msg_row;
3437 }
3438 }
3439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 if (redraw && !cmd_silent)
3442 {
3443 msg_no_more = TRUE;
3444 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003445 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003447 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (cmdline_row != i || ccline.overstrike)
3449 msg_clr_eos();
3450 msg_no_more = FALSE;
3451 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003452 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003454 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003455 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003457 }
3458 else
3459 m = MAXCOL;
3460 for (i = 0; i < len; ++i)
3461 {
3462 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003463 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003464 if (has_mbyte)
3465 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003466 // Stop cursor at the end of the screen, but do increment the
3467 // insert position, so that entering a very long command
3468 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003469 if (ccline.cmdspos + c < m)
3470 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003471
Bram Moolenaar14184a32019-02-16 15:10:30 +01003472 if (has_mbyte)
3473 {
3474 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3475 if (c > len - i - 1)
3476 c = len - i - 1;
3477 ccline.cmdpos += c;
3478 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003480 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482 }
3483 if (redraw)
3484 msg_check();
3485 return retval;
3486}
3487
Bram Moolenaar66b51422019-08-18 21:44:12 +02003488static cmdline_info_T prev_ccline;
3489static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003490
3491/*
3492 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3493 * and overwrite it. But get_cmdline_str() may need it, thus make it
3494 * available globally in prev_ccline.
3495 */
3496 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003497save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003498{
3499 if (!prev_ccline_used)
3500 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003501 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003502 prev_ccline_used = TRUE;
3503 }
3504 *ccp = prev_ccline;
3505 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003506 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003507}
3508
3509/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003510 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003511 */
3512 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003513restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003514{
3515 ccline = prev_ccline;
3516 prev_ccline = *ccp;
3517}
3518
Bram Moolenaar8299df92004-07-10 09:47:34 +00003519/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003520 * Paste a yank register into the command line.
3521 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003522 * insert_reg() can't be used here, because special characters from the
3523 * register contents will be interpreted as commands.
3524 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003525 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003526 */
3527 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003528cmdline_paste(
3529 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003530 int literally, // Insert text literally instead of "as typed"
3531 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003532{
3533 long i;
3534 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003535 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003536 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003537
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003538 // check for valid regname; also accept special characters for CTRL-R in
3539 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003540 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003541 && regname != Ctrl_A && regname != Ctrl_L
3542 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543 return FAIL;
3544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003545 // A register containing CTRL-R can cause an endless loop. Allow using
3546 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547 line_breakcheck();
3548 if (got_int)
3549 return FAIL;
3550
3551#ifdef FEAT_CLIPBOARD
3552 regname = may_get_selection(regname);
3553#endif
3554
Bram Moolenaar438d1762018-09-30 17:11:48 +02003555 // Need to set "textlock" to avoid nasty things like going to another
3556 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003557 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003558 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003559 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003560
3561 if (i)
3562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003563 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003564 if (arg == NULL)
3565 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003567 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3568 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003569 p = arg;
3570 if (p_is && regname == Ctrl_W)
3571 {
3572 char_u *w;
3573 int len;
3574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003575 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003576 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003577 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003578 if (has_mbyte)
3579 {
3580 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3581 if (!vim_iswordc(mb_ptr2char(w - len)))
3582 break;
3583 w -= len;
3584 }
3585 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003586 {
3587 if (!vim_iswordc(w[-1]))
3588 break;
3589 --w;
3590 }
3591 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003592 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003593 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3594 p += len;
3595 }
3596
3597 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003598 if (allocated)
3599 vim_free(arg);
3600 return OK;
3601 }
3602
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003603 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604}
3605
3606/*
3607 * Put a string on the command line.
3608 * When "literally" is TRUE, insert literally.
3609 * When "literally" is FALSE, insert as typed, but don't leave the command
3610 * line.
3611 */
3612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003613cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003614{
3615 int c, cv;
3616
3617 if (literally)
3618 put_on_cmdline(s, -1, TRUE);
3619 else
3620 while (*s != NUL)
3621 {
3622 cv = *s;
3623 if (cv == Ctrl_V && s[1])
3624 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003625 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003626 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003627 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003628 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003629 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3630 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003631#ifdef UNIX
3632 || c == intr_char
3633#endif
3634 || (c == Ctrl_BSL && *s == Ctrl_N))
3635 stuffcharReadbuff(Ctrl_V);
3636 stuffcharReadbuff(c);
3637 }
3638}
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640#ifdef FEAT_WILDMENU
3641/*
3642 * Delete characters on the command line, from "from" to the current
3643 * position.
3644 */
3645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003646cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3649 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3650 ccline.cmdlen -= ccline.cmdpos - from;
3651 ccline.cmdpos = from;
3652}
3653#endif
3654
3655/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003656 * This function is called when the screen size changes and with incremental
3657 * search and in other situations where the command line may have been
3658 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003661redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003663 redrawcmdline_ex(TRUE);
3664}
3665
3666 void
3667redrawcmdline_ex(int do_compute_cmdrow)
3668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (cmd_silent)
3670 return;
3671 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003672 if (do_compute_cmdrow)
3673 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 redrawcmd();
3675 cursorcmd();
3676}
3677
3678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003679redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 int i;
3682
3683 if (cmd_silent)
3684 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003685 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 msg_putchar(ccline.cmdfirstc);
3687 if (ccline.cmdprompt != NULL)
3688 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003689 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003691 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003692 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 --ccline.cmdindent;
3694 }
3695 else
3696 for (i = ccline.cmdindent; i > 0; --i)
3697 msg_putchar(' ');
3698}
3699
3700/*
3701 * Redraw what is currently on the command line.
3702 */
3703 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003704redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 if (cmd_silent)
3707 return;
3708
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003709 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003710 if (ccline.cmdbuff == NULL)
3711 {
3712 windgoto(cmdline_row, 0);
3713 msg_clr_eos();
3714 return;
3715 }
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 msg_start();
3718 redrawcmdprompt();
3719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003720 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 msg_no_more = TRUE;
3722 draw_cmdline(0, ccline.cmdlen);
3723 msg_clr_eos();
3724 msg_no_more = FALSE;
3725
3726 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003727 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003728 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
3730 /*
3731 * An emsg() before may have set msg_scroll. This is used in normal mode,
3732 * in cmdline mode we can reset them now.
3733 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003734 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003736 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3737 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 skip_redraw = FALSE;
3739}
3740
3741 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003742compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003744 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 cmdline_row = Rows - 1;
3746 else
3747 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003748 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749}
3750
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003752cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
3754 if (cmd_silent)
3755 return;
3756
3757#ifdef FEAT_RIGHTLEFT
3758 if (cmdmsg_rl)
3759 {
3760 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3761 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3762 if (msg_row <= 0)
3763 msg_row = Rows - 1;
3764 }
3765 else
3766#endif
3767 {
3768 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3769 msg_col = ccline.cmdspos % (int)Columns;
3770 if (msg_row >= Rows)
3771 msg_row = Rows - 1;
3772 }
3773
3774 windgoto(msg_row, msg_col);
3775#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003776 if (p_imst == IM_ON_THE_SPOT)
3777 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#endif
3779#ifdef MCH_CURSOR_SHAPE
3780 mch_update_cursor();
3781#endif
3782}
3783
3784 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003785gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 msg_start();
3788#ifdef FEAT_RIGHTLEFT
3789 if (cmdmsg_rl)
3790 msg_col = Columns - 1;
3791 else
3792#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003793 msg_col = 0; // always start in column 0
3794 if (clr) // clear the bottom line(s)
3795 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 windgoto(cmdline_row, 0);
3797}
3798
3799/*
3800 * Check the word in front of the cursor for an abbreviation.
3801 * Called when the non-id character "c" has been entered.
3802 * When an abbreviation is recognized it is removed from the text with
3803 * backspaces and the replacement string is inserted, followed by "c".
3804 */
3805 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003806ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003808 int spos = 0;
3809
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003810 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 return FALSE;
3812
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003813 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3814 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003815 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3816 spos++;
3817 if (ccline.cmdlen - spos > 5
3818 && ccline.cmdbuff[spos] == '\''
3819 && ccline.cmdbuff[spos + 2] == ','
3820 && ccline.cmdbuff[spos + 3] == '\'')
3821 spos += 5;
3822 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003823 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003824 spos = 0;
3825
3826 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827}
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003830 * Escape special characters in "fname" for when used as a file name argument
3831 * after a Vim command, or, when "shell" is non-zero, a shell command.
3832 * Returns the result in allocated memory.
3833 */
3834 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003835vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003836{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003837 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003838#ifdef BACKSLASH_IN_FILENAME
3839 char_u buf[20];
3840 int j = 0;
3841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003842 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003843 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003844 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003845 buf[j++] = *p;
3846 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003847 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003848#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003849 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3850 if (shell && csh_like_shell() && p != NULL)
3851 {
3852 char_u *s;
3853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 // For csh and similar shells need to put two backslashes before '!'.
3855 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003856 s = vim_strsave_escaped(p, (char_u *)"!");
3857 vim_free(p);
3858 p = s;
3859 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003860#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003862 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3863 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003864 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003865 escape_fname(&p);
3866
3867 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003868}
3869
3870/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003871 * Put a backslash before the file name in "pp", which is in allocated memory.
3872 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003875{
3876 char_u *p;
3877
Bram Moolenaar964b3742019-05-24 18:54:09 +02003878 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003879 if (p != NULL)
3880 {
3881 p[0] = '\\';
3882 STRCPY(p + 1, *pp);
3883 vim_free(*pp);
3884 *pp = p;
3885 }
3886}
3887
3888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * For each file name in files[num_files]:
3890 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3891 */
3892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003893tilde_replace(
3894 char_u *orig_pat,
3895 int num_files,
3896 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 int i;
3899 char_u *p;
3900
3901 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3902 {
3903 for (i = 0; i < num_files; ++i)
3904 {
3905 p = home_replace_save(NULL, files[i]);
3906 if (p != NULL)
3907 {
3908 vim_free(files[i]);
3909 files[i] = p;
3910 }
3911 }
3912 }
3913}
3914
3915/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003916 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003918 cmdline_info_T *
3919get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003921 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922}
3923
Bram Moolenaar064154c2016-03-19 22:50:43 +01003924#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003925/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003926 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003927 * ccline and put the previous value in prev_ccline.
3928 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003929 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003930get_ccline_ptr(void)
3931{
3932 if ((State & CMDLINE) == 0)
3933 return NULL;
3934 if (ccline.cmdbuff != NULL)
3935 return &ccline;
3936 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3937 return &prev_ccline;
3938 return NULL;
3939}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003940#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003941
Bram Moolenaar064154c2016-03-19 22:50:43 +01003942#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003943/*
3944 * Get the current command line in allocated memory.
3945 * Only works when the command line is being edited.
3946 * Returns NULL when something is wrong.
3947 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003948 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003949get_cmdline_str(void)
3950{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003951 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003952
Bram Moolenaaree91c332018-09-25 22:27:35 +02003953 if (cmdline_star > 0)
3954 return NULL;
3955 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003956 if (p == NULL)
3957 return NULL;
3958 return vim_strnsave(p->cmdbuff, p->cmdlen);
3959}
3960
3961/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003962 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003963 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003964 void
3965f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3966{
3967 rettv->v_type = VAR_STRING;
3968 rettv->vval.v_string = get_cmdline_str();
3969}
3970
3971/*
3972 * "getcmdpos()" function
3973 */
3974 void
3975f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003976{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003977 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003978
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003979 rettv->vval.v_number = 0;
3980 if (p != NULL)
3981 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003982}
3983
3984/*
3985 * Set the command line byte position to "pos". Zero is the first position.
3986 * Only works when the command line is being edited.
3987 * Returns 1 when failed, 0 when OK.
3988 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003989 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003990set_cmdline_pos(
3991 int pos)
3992{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003993 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003994
3995 if (p == NULL)
3996 return 1;
3997
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003998 // The position is not set directly but after CTRL-\ e or CTRL-R = has
3999 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004000 if (pos < 0)
4001 new_cmdpos = 0;
4002 else
4003 new_cmdpos = pos;
4004 return 0;
4005}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004006
4007/*
4008 * "setcmdpos()" function
4009 */
4010 void
4011f_setcmdpos(typval_T *argvars, typval_T *rettv)
4012{
4013 int pos = (int)tv_get_number(&argvars[0]) - 1;
4014
4015 if (pos >= 0)
4016 rettv->vval.v_number = set_cmdline_pos(pos);
4017}
4018
4019/*
4020 * "getcmdtype()" function
4021 */
4022 void
4023f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4024{
4025 rettv->v_type = VAR_STRING;
4026 rettv->vval.v_string = alloc(2);
4027 if (rettv->vval.v_string != NULL)
4028 {
4029 rettv->vval.v_string[0] = get_cmdline_type();
4030 rettv->vval.v_string[1] = NUL;
4031 }
4032}
4033
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004034#endif
4035
4036#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4037/*
4038 * Get the current command-line type.
4039 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4040 * Only works when the command line is being edited.
4041 * Returns NUL when something is wrong.
4042 */
4043 int
4044get_cmdline_type(void)
4045{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004046 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004047
4048 if (p == NULL)
4049 return NUL;
4050 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004051 return
4052# ifdef FEAT_EVAL
4053 (p->input_fn) ? '@' :
4054# endif
4055 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004056 return p->cmdfirstc;
4057}
4058#endif
4059
Bram Moolenaard7663c22019-08-06 21:59:57 +02004060/*
4061 * Return the first character of the current command line.
4062 */
4063 int
4064get_cmdline_firstc(void)
4065{
4066 return ccline.cmdfirstc;
4067}
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069/*
4070 * Get indices "num1,num2" that specify a range within a list (not a range of
4071 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4072 * Returns OK if parsed successfully, otherwise FAIL.
4073 */
4074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004075get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 int len;
4078 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004079 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004082 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004084 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 *str += len;
4086 *num1 = (int)num;
4087 first = TRUE;
4088 }
4089 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004090 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
4092 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004093 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (len > 0)
4095 {
4096 *num2 = (int)num;
4097 *str = skipwhite(*str + len);
4098 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004099 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 return FAIL;
4101 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004102 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 *num2 = *num1;
4104 return OK;
4105}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107#if defined(FEAT_CMDWIN) || defined(PROTO)
4108/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004109 * Check value of 'cedit' and set cedit_key.
4110 * Returns NULL if value is OK, error message otherwise.
4111 */
4112 char *
4113check_cedit(void)
4114{
4115 int n;
4116
4117 if (*p_cedit == NUL)
4118 cedit_key = -1;
4119 else
4120 {
4121 n = string_to_key(p_cedit, FALSE);
4122 if (vim_isprintc(n))
4123 return e_invarg;
4124 cedit_key = n;
4125 }
4126 return NULL;
4127}
4128
4129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * Open a window on the current command line and history. Allow editing in
4131 * the window. Returns when the window is closed.
4132 * Returns:
4133 * CR if the command is to be executed
4134 * Ctrl_C if it is to be abandoned
4135 * K_IGNORE if editing continues
4136 */
4137 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004138open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004140 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004142 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 win_T *wp;
4144 int i;
4145 linenr_T lnum;
4146 int histtype;
4147 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int save_restart_edit = restart_edit;
4149 int save_State = State;
4150 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004151#ifdef FEAT_RIGHTLEFT
4152 int save_cmdmsg_rl = cmdmsg_rl;
4153#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004154#ifdef FEAT_FOLDING
4155 int save_KeyTyped;
4156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004158 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (cmdwin_type != 0
4160# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4161 || cmdline_star > 0
4162# endif
4163 )
4164 {
4165 beep_flush();
4166 return K_IGNORE;
4167 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004168 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004170 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 win_size_save(&winsizes);
4172
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004173 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4174 // command line window, but we don't want the popup menu then.
4175 pum_undisplay();
4176
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004177 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004178 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004179 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004180
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004181 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4183 {
4184 beep_flush();
4185 return K_IGNORE;
4186 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004187 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004189 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004190 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004191 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004194#ifdef FEAT_FOLDING
4195 curwin->w_p_fen = FALSE;
4196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004198 curwin->w_p_rl = cmdmsg_rl;
4199 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004201 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004203 // Don't allow switching to another buffer.
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 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004207 need_wait_return = FALSE;
4208
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004209 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4211 {
4212 if (p_wc == TAB)
4213 {
4214 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4215 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4216 }
4217 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4218 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004219 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004221 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4222 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004223 curbuf->b_p_tw = 0;
4224
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004225 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004227 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004229 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (i >= 0)
4231 {
4232 lnum = 0;
4233 do
4234 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004235 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004237 if (get_histentry(histtype)[i].hisstr != NULL)
4238 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 (colnr_T)0, FALSE);
4240 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004241 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004245 // Replace the empty last line with the current command-line and put the
4246 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4248 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4249 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004250 changed_line_abv_curs();
4251 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004252 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar23324a02019-10-01 17:39:04 +02004254 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 exmode_active = 0;
4256
4257 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaar23324a02019-10-01 17:39:04 +02004260 // Reset here so it can be set by a CmdWinEnter autocommand.
4261 cmdwin_result = 0;
4262
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004263 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004264 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004265 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004266 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 i = RedrawingDisabled;
4269 RedrawingDisabled = 0;
4270
4271 /*
4272 * Call the main loop until <CR> or CTRL-C is typed.
4273 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004274 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 RedrawingDisabled = i;
4277
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004278# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004279 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004280# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004281
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004282 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004283 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004284
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004285# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004286 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004287 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288# endif
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 exmode_active = save_exmode;
4292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004293 // Safety check: The old window or buffer was deleted: It's a bug when
4294 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004295 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 {
4297 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004298 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
4301 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004302# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (aborting() && cmdwin_result != K_IGNORE)
4305 cmdwin_result = Ctrl_C;
4306# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004311 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4312
4313 if (histtype == HIST_CMD)
4314 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004315 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004316 ccline.cmdbuff = vim_strsave((char_u *)p);
4317 cmdwin_result = CAR;
4318 }
4319 else
4320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004322 ccline.cmdbuff = NULL;
4323 stuffcharReadbuff(':');
4324 stuffReadbuff((char_u *)p);
4325 stuffcharReadbuff(CAR);
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4331 cmdwin_result = CAR;
4332 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004333 else if (cmdwin_result == Ctrl_C)
4334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004335 // :q or :close, don't execute any command
4336 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004337 ccline.cmdbuff = NULL;
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 else
4340 ccline.cmdbuff = vim_strsave(ml_get_curline());
4341 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004342 {
4343 ccline.cmdbuff = vim_strsave((char_u *)"");
4344 ccline.cmdlen = 0;
4345 ccline.cmdbufflen = 1;
4346 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else
4350 {
4351 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4352 ccline.cmdbufflen = ccline.cmdlen + 1;
4353 ccline.cmdpos = curwin->w_cursor.col;
4354 if (ccline.cmdpos > ccline.cmdlen)
4355 ccline.cmdpos = ccline.cmdlen;
4356 if (cmdwin_result == K_IGNORE)
4357 {
4358 set_cmdspos_cursor();
4359 redrawcmd();
4360 }
4361 }
4362
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004363# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004364 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004365 curwin->w_p_cole = 0;
4366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004368 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 win_goto(old_curwin);
4370 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004371
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004372 // win_close() may have already wiped the buffer when 'bh' is
4373 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004374 if (bufref_valid(&bufref))
4375 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004377 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380
4381 ga_clear(&winsizes);
4382 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004383# ifdef FEAT_RIGHTLEFT
4384 cmdmsg_rl = save_cmdmsg_rl;
4385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 return cmdwin_result;
4391}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004392#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394/*
4395 * Used for commands that either take a simple command string argument, or:
4396 * cmd << endmarker
4397 * {script}
4398 * endmarker
4399 * Returns a pointer to allocated memory with {script} or NULL.
4400 */
4401 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004402script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 char_u *theline;
4405 char *end_pattern = NULL;
4406 char dot[] = ".";
4407 garray_T ga;
4408
4409 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4410 return NULL;
4411
4412 ga_init2(&ga, 1, 0x400);
4413
4414 if (cmd[2] != NUL)
4415 end_pattern = (char *)skipwhite(cmd + 2);
4416 else
4417 end_pattern = dot;
4418
4419 for (;;)
4420 {
4421 theline = eap->getline(
4422#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004423 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004425 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004428 {
4429 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 ga_concat(&ga, theline);
4434 ga_append(&ga, '\n');
4435 vim_free(theline);
4436 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004437 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 return (char_u *)ga.ga_data;
4440}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004441
4442#if defined(FEAT_EVAL) || defined(PROTO)
4443/*
4444 * This function is used by f_input() and f_inputdialog() functions. The third
4445 * argument to f_input() specifies the type of completion to use at the
4446 * prompt. The third argument to f_inputdialog() specifies the value to return
4447 * when the user cancels the prompt.
4448 */
4449 void
4450get_user_input(
4451 typval_T *argvars,
4452 typval_T *rettv,
4453 int inputdialog,
4454 int secret)
4455{
4456 char_u *prompt = tv_get_string_chk(&argvars[0]);
4457 char_u *p = NULL;
4458 int c;
4459 char_u buf[NUMBUFLEN];
4460 int cmd_silent_save = cmd_silent;
4461 char_u *defstr = (char_u *)"";
4462 int xp_type = EXPAND_NOTHING;
4463 char_u *xp_arg = NULL;
4464
4465 rettv->v_type = VAR_STRING;
4466 rettv->vval.v_string = NULL;
4467
4468#ifdef NO_CONSOLE_INPUT
4469 // While starting up, there is no place to enter text. When running tests
4470 // with --not-a-term we assume feedkeys() will be used.
4471 if (no_console_input() && !is_not_a_term())
4472 return;
4473#endif
4474
4475 cmd_silent = FALSE; // Want to see the prompt.
4476 if (prompt != NULL)
4477 {
4478 // Only the part of the message after the last NL is considered as
4479 // prompt for the command line
4480 p = vim_strrchr(prompt, '\n');
4481 if (p == NULL)
4482 p = prompt;
4483 else
4484 {
4485 ++p;
4486 c = *p;
4487 *p = NUL;
4488 msg_start();
4489 msg_clr_eos();
4490 msg_puts_attr((char *)prompt, get_echo_attr());
4491 msg_didout = FALSE;
4492 msg_starthere();
4493 *p = c;
4494 }
4495 cmdline_row = msg_row;
4496
4497 if (argvars[1].v_type != VAR_UNKNOWN)
4498 {
4499 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4500 if (defstr != NULL)
4501 stuffReadbuffSpec(defstr);
4502
4503 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4504 {
4505 char_u *xp_name;
4506 int xp_namelen;
4507 long argt;
4508
4509 // input() with a third argument: completion
4510 rettv->vval.v_string = NULL;
4511
4512 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4513 if (xp_name == NULL)
4514 return;
4515
4516 xp_namelen = (int)STRLEN(xp_name);
4517
4518 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4519 &xp_arg) == FAIL)
4520 return;
4521 }
4522 }
4523
4524 if (defstr != NULL)
4525 {
4526 int save_ex_normal_busy = ex_normal_busy;
4527
4528 ex_normal_busy = 0;
4529 rettv->vval.v_string =
4530 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4531 xp_type, xp_arg);
4532 ex_normal_busy = save_ex_normal_busy;
4533 }
4534 if (inputdialog && rettv->vval.v_string == NULL
4535 && argvars[1].v_type != VAR_UNKNOWN
4536 && argvars[2].v_type != VAR_UNKNOWN)
4537 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4538 &argvars[2], buf));
4539
4540 vim_free(xp_arg);
4541
4542 // since the user typed this, no need to wait for return
4543 need_wait_return = FALSE;
4544 msg_didout = FALSE;
4545 }
4546 cmd_silent = cmd_silent_save;
4547}
4548#endif