blob: bc3cccc632a8d7f4de57b4a47c2c4fd129fa71ef [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 Moolenaarc036e872020-02-21 21:30:52 +0100187do_incsearch_highlighting(int firstc, int *search_delim, incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200188 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 == '?')
Bram Moolenaarc036e872020-02-21 21:30:52 +0100213 {
214 *search_delim = firstc;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200215 return TRUE;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100216 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200217 if (firstc != ':')
218 return FALSE;
219
Bram Moolenaarc6725252019-11-23 21:56:46 +0100220 ++emsg_off;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200221 vim_memset(&ea, 0, sizeof(ea));
222 ea.line1 = 1;
223 ea.line2 = 1;
224 ea.cmd = ccline.cmdbuff;
225 ea.addr_type = ADDR_LINES;
226
227 parse_command_modifiers(&ea, &dummy, TRUE);
228 cmdmod = save_cmdmod;
229
230 cmd = skip_range(ea.cmd, NULL);
231 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100232 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200233
234 // Skip over "substitute" to find the pattern separator.
235 for (p = cmd; ASCII_ISALPHA(*p); ++p)
236 ;
237 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100238 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200239
240 if (STRNCMP(cmd, "substitute", p - cmd) == 0
241 || STRNCMP(cmd, "smagic", p - cmd) == 0
242 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
243 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200244 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200245 if (*cmd == 's' && cmd[1] == 'm')
246 p_magic = TRUE;
247 else if (*cmd == 's' && cmd[1] == 'n')
248 p_magic = FALSE;
249 }
250 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
251 {
252 // skip over flags
253 while (ASCII_ISALPHA(*(p = skipwhite(p))))
254 ++p;
255 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100256 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200257 }
258 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
259 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
260 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
261 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
262 || STRNCMP(cmd, "global", p - cmd) == 0)
263 {
264 // skip over "!"
265 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200266 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200267 p++;
268 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100269 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200270 }
271 if (*cmd != 'g')
272 delim_optional = TRUE;
273 }
274 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100275 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200276
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200277 p = skipwhite(p);
278 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100279 *search_delim = delim;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200281
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200282 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200283
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200284 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100285 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200286
287 // Don't do 'hlsearch' highlighting if the pattern matches everything.
288 if (!use_last_pat)
289 {
290 char c = *end;
291 int empty;
292
293 *end = NUL;
294 empty = empty_pattern(p);
295 *end = c;
296 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100297 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200298 }
299
300 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200301 *skiplen = (int)(p - ccline.cmdbuff);
302 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200303
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200304 // parse the address range
305 save_cursor = curwin->w_cursor;
306 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200307 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200308 if (ea.addr_count > 0)
309 {
310 // Allow for reverse match.
311 if (ea.line2 < ea.line1)
312 {
313 search_first_line = ea.line2;
314 search_last_line = ea.line1;
315 }
316 else
317 {
318 search_first_line = ea.line1;
319 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200320 }
321 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200322 else if (cmd[0] == 's' && cmd[1] != 'o')
323 {
324 // :s defaults to the current line
325 search_first_line = curwin->w_cursor.lnum;
326 search_last_line = curwin->w_cursor.lnum;
327 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200328
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200329 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100330 retval = TRUE;
331theend:
332 --emsg_off;
333 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200334}
335
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200336 static void
337finish_incsearch_highlighting(
338 int gotesc,
339 incsearch_state_T *is_state,
340 int call_update_screen)
341{
342 if (is_state->did_incsearch)
343 {
344 is_state->did_incsearch = FALSE;
345 if (gotesc)
346 curwin->w_cursor = is_state->save_cursor;
347 else
348 {
349 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
350 {
351 // put the '" mark at the original position
352 curwin->w_cursor = is_state->save_cursor;
353 setpcmark();
354 }
355 curwin->w_cursor = is_state->search_start;
356 }
357 restore_viewstate(&is_state->old_viewstate);
358 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200359
360 // by default search all lines
361 search_first_line = 0;
362 search_last_line = MAXLNUM;
363
364 p_magic = is_state->magic_save;
365
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100366 validate_cursor(); // needed for TAB
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200367 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200368 if (call_update_screen)
369 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200370 }
371}
372
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200373/*
374 * Do 'incsearch' highlighting if desired.
375 */
376 static void
377may_do_incsearch_highlighting(
378 int firstc,
379 long count,
380 incsearch_state_T *is_state)
381{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200382 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200383 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200385#ifdef FEAT_RELTIME
386 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200387 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200388#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200389 int next_char;
390 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200391 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100392 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200393
Bram Moolenaar198cb662018-09-06 21:44:17 +0200394 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100395 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200396 save_last_search_pattern();
397
Bram Moolenaarc036e872020-02-21 21:30:52 +0100398 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200399 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200400 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200401 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200402 if (did_do_incsearch && vpeekc() == NUL)
403 // may have skipped a redraw, do it now
404 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200405 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200406 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407
408 // If there is a character waiting, search and redraw later.
409 if (char_avail())
410 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200411 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200412 is_state->incsearch_postponed = TRUE;
413 return;
414 }
415 is_state->incsearch_postponed = FALSE;
416
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 if (search_first_line == 0)
418 // start at the original cursor position
419 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100420 else if (search_first_line > curbuf->b_ml.ml_line_count)
421 {
422 // start after the last line
423 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
424 curwin->w_cursor.col = MAXCOL;
425 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200426 else
427 {
428 // start at the first line in the range
429 curwin->w_cursor.lnum = search_first_line;
430 curwin->w_cursor.col = 0;
431 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200432
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200433 // Use the previous pattern for ":s//".
434 next_char = ccline.cmdbuff[skiplen + patlen];
435 use_last_pat = patlen == 0 && skiplen > 0
436 && ccline.cmdbuff[skiplen - 1] == next_char;
437
438 // If there is no pattern, don't do anything.
439 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200440 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200441 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200442 set_no_hlsearch(TRUE); // turn off previous highlight
443 redraw_all_later(SOME_VALID);
444 }
445 else
446 {
447 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
448
449 cursor_off(); // so the user knows we're busy
450 out_flush();
451 ++emsg_off; // so it doesn't beep if bad expr
452#ifdef FEAT_RELTIME
453 // Set the time limit to half a second.
454 profile_setlimit(500L, &tm);
455#endif
456 if (!p_hls)
457 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200458 if (search_first_line != 0)
459 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200460 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200461#ifdef FEAT_RELTIME
462 vim_memset(&sia, 0, sizeof(sia));
463 sia.sa_tm = &tm;
464#endif
Bram Moolenaarc036e872020-02-21 21:30:52 +0100465 found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200467#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200468 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200470 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200471#endif
472 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200473 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200474 --emsg_off;
475
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200476 if (curwin->w_cursor.lnum < search_first_line
477 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200478 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200479 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200480 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200481 curwin->w_cursor = is_state->search_start;
482 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200484 // if interrupted while searching, behave like it failed
485 if (got_int)
486 {
487 (void)vpeekc(); // remove <C-C> from input stream
488 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200489 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 }
491 else if (char_avail())
492 // cancelled searching because a char was typed
493 is_state->incsearch_postponed = TRUE;
494 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200495 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200496 highlight_match = TRUE; // highlight position
497 else
498 highlight_match = FALSE; // remove highlight
499
500 // First restore the old curwin values, so the screen is positioned in the
501 // same way as the actual search command.
502 restore_viewstate(&is_state->old_viewstate);
503 changed_cline_bef_curs();
504 update_topline();
505
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200506 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200507 {
508 pos_T save_pos = curwin->w_cursor;
509
510 is_state->match_start = curwin->w_cursor;
511 set_search_match(&curwin->w_cursor);
512 validate_cursor();
513 end_pos = curwin->w_cursor;
514 is_state->match_end = end_pos;
515 curwin->w_cursor = save_pos;
516 }
517 else
518 end_pos = curwin->w_cursor; // shutup gcc 4
519
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200520 // Disable 'hlsearch' highlighting if the pattern matches everything.
521 // Avoids a flash when typing "foo\|".
522 if (!use_last_pat)
523 {
524 next_char = ccline.cmdbuff[skiplen + patlen];
525 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200526 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
527 {
528 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200529 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200530 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200531 ccline.cmdbuff[skiplen + patlen] = next_char;
532 }
533
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 validate_cursor();
535 // May redraw the status line to show the cursor position.
536 if (p_ru && curwin->w_status_height > 0)
537 curwin->w_redr_status = TRUE;
538
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200540 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200541 restore_last_search_pattern();
542
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200543 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
544 // end of the pattern, e.g. for ":s/pat/".
545 if (ccline.cmdbuff[skiplen + patlen] != NUL)
546 curwin->w_cursor = is_state->search_start;
547 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200548 curwin->w_cursor = end_pos;
549
550 msg_starthere();
551 redrawcmdline();
552 is_state->did_incsearch = TRUE;
553}
554
555/*
556 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
557 * or previous match.
558 * Returns FAIL when jumping to cmdline_not_changed;
559 */
560 static int
561may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int firstc,
563 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200565 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200566{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568 pos_T t;
569 char_u *pat;
570 int search_flags = SEARCH_NOOF;
571 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200572 int save;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100573 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574
Bram Moolenaar198cb662018-09-06 21:44:17 +0200575 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100576 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200577 save_last_search_pattern();
578
Bram Moolenaarc036e872020-02-21 21:30:52 +0100579 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200580 {
581 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200582 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200583 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200585 {
586 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200588 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200589
Bram Moolenaarc036e872020-02-21 21:30:52 +0100590 if (search_delim == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200591 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200593 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200594 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200595 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200597 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 cursor_off();
600 out_flush();
601 if (c == Ctrl_G)
602 {
603 t = is_state->match_end;
604 if (LT_POS(is_state->match_start, is_state->match_end))
605 // Start searching at the end of the match not at the beginning of
606 // the next column.
607 (void)decl(&t);
608 search_flags += SEARCH_COL;
609 }
610 else
611 t = is_state->match_start;
612 if (!p_hls)
613 search_flags += SEARCH_KEEP;
614 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 save = pat[patlen];
616 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100617 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200618 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200619 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622 if (i)
623 {
624 is_state->search_start = is_state->match_start;
625 is_state->match_end = t;
626 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200627 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628 {
629 // Move just before the current match, so that when nv_search
630 // finishes the cursor will be put back on the match.
631 is_state->search_start = t;
632 (void)decl(&is_state->search_start);
633 }
634 else if (c == Ctrl_G && firstc == '?')
635 {
636 // Move just after the current match, so that when nv_search
637 // finishes the cursor will be put back on the match.
638 is_state->search_start = t;
639 (void)incl(&is_state->search_start);
640 }
641 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
642 {
643 // wrap around
644 is_state->search_start = t;
645 if (firstc == '?')
646 (void)incl(&is_state->search_start);
647 else
648 (void)decl(&is_state->search_start);
649 }
650
651 set_search_match(&is_state->match_end);
652 curwin->w_cursor = is_state->match_start;
653 changed_cline_bef_curs();
654 update_topline();
655 validate_cursor();
656 highlight_match = TRUE;
657 save_viewstate(&is_state->old_viewstate);
658 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200659 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200661 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200662 }
663 else
664 vim_beep(BO_ERROR);
665 restore_last_search_pattern();
666 return FAIL;
667}
668
669/*
670 * When CTRL-L typed: add character from the match to the pattern.
671 * May set "*c" to the added character.
672 * Return OK when jumping to cmdline_not_changed.
673 */
674 static int
675may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
676{
Bram Moolenaarc036e872020-02-21 21:30:52 +0100677 int skiplen, patlen, search_delim;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200678
Bram Moolenaar198cb662018-09-06 21:44:17 +0200679 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100680 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200681 save_last_search_pattern();
682
Bram Moolenaarc036e872020-02-21 21:30:52 +0100683 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200684 {
685 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200686 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200687 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100688 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200689
690 // Add a character from under the cursor for 'incsearch'.
691 if (is_state->did_incsearch)
692 {
693 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200694 *c = gchar_cursor();
695 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200696 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200697 // If 'ignorecase' and 'smartcase' are set and the
698 // command line has no uppercase characters, convert
699 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200700 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200701 *c = MB_TOLOWER(*c);
Bram Moolenaarc036e872020-02-21 21:30:52 +0100702 if (*c == search_delim || vim_strchr((char_u *)(
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200703 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200704 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200705 // put a backslash before special characters
706 stuffcharReadbuff(*c);
707 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200708 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200709 // add any composing characters
710 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
711 {
712 int save_c = *c;
713
714 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
715 {
716 curwin->w_cursor.col += mb_char2len(*c);
717 *c = gchar_cursor();
718 stuffcharReadbuff(*c);
719 }
720 *c = save_c;
721 }
722 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200723 }
724 }
725 return OK;
726}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200727#endif
728
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200729#ifdef FEAT_ARABIC
730/*
731 * Return TRUE if the command line has an Arabic character at or after "start"
732 * for "len" bytes.
733 */
734 static int
735cmdline_has_arabic(int start, int len)
736{
737 int j;
738 int mb_l;
739 int u8c;
740 char_u *p;
741 int u8cc[MAX_MCO];
742
743 if (!enc_utf8)
744 return FALSE;
745
746 for (j = start; j < start + len; j += mb_l)
747 {
748 p = ccline.cmdbuff + j;
749 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
750 mb_l = utfc_ptr2len_len(p, start + len - j);
751 if (ARABIC_CHAR(u8c))
752 return TRUE;
753 }
754 return FALSE;
755}
756#endif
757
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200758 void
759cmdline_init(void)
760{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200761 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200762}
763
Bram Moolenaar66216052017-12-16 16:33:44 +0100764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * getcmdline() - accept a command line starting with firstc.
766 *
767 * firstc == ':' get ":" command line.
768 * firstc == '/' or '?' get search pattern
769 * firstc == '=' get expression
770 * firstc == '@' get text for input() function
771 * firstc == '>' get text for debug mode
772 * firstc == NUL get text for :insert command
773 * firstc == -1 like NUL, and break on CTRL-C
774 *
775 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
776 * command line.
777 *
778 * Careful: getcmdline() can be called recursively!
779 *
780 * Return pointer to allocated string if there is a commandline, NULL
781 * otherwise.
782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100784getcmdline(
785 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200786 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200787 int indent, // indent for inside conditionals
788 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200789{
790 return getcmdline_int(firstc, count, indent, TRUE);
791}
792
793 static char_u *
794getcmdline_int(
795 int firstc,
796 long count UNUSED, // only used for incremental search
797 int indent, // indent for inside conditionals
798 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
800 int c;
801 int i;
802 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100803 int gotesc = FALSE; // TRUE when <ESC> just typed
804 int do_abbr; // when TRUE check for abbr.
805 char_u *lookfor = NULL; // string to match
806 int hiscnt; // current history line in use
807 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200809 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 int did_wild_list = FALSE; // did wild_list() recently
812 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 int res;
814 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 int save_State = State; // remember State when called
816 int some_key_typed = FALSE; // one of the keys was typed
817 // mouse drag and release events are ignored, unless they are
818 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#ifdef FEAT_EVAL
821 int break_ctrl_c = FALSE;
822#endif
823 expand_T xpc;
824 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200825 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200826 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200827 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaar438d1762018-09-30 17:11:48 +0200829 if (ccline.cmdbuff != NULL)
830 {
831 // Being called recursively. Since ccline is global, we need to save
832 // the current buffer and restore it when returning.
833 save_cmdline(&save_ccline);
834 did_save_ccline = TRUE;
835 }
836 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200837 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200838
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_EVAL
840 if (firstc == -1)
841 {
842 firstc = NUL;
843 break_ctrl_c = TRUE;
844 }
845#endif
846#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100847 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (firstc == ':' || firstc == '=' || firstc == '>')
849 cmd_hkmap = 0;
850#endif
851
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100852 ccline.overstrike = FALSE; // always start in insert mode
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200855 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857
858 /*
859 * set some variables for redrawcmd()
860 */
861 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000862 ccline.cmdindent = (firstc > 0 ? indent : 0);
863
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100864 // alloc initial ccline.cmdbuff
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000865 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200867 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 ccline.cmdlen = ccline.cmdpos = 0;
869 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100870 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100872 // autoindent for :insert and :append
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000873 if (firstc <= 0)
874 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200875 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000876 ccline.cmdbuff[indent] = NUL;
877 ccline.cmdpos = indent;
878 ccline.cmdspos = indent;
879 ccline.cmdlen = indent;
880 }
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000883 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
885#ifdef FEAT_RIGHTLEFT
886 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
887 && (firstc == '/' || firstc == '?'))
888 cmdmsg_rl = TRUE;
889 else
890 cmdmsg_rl = FALSE;
891#endif
892
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (!cmd_silent)
895 {
896 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100897 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 gotocmdline(TRUE);
899 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100900 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 set_cmdspos();
902 }
903 xpc.xp_context = EXPAND_NOTHING;
904 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000905#ifndef BACKSLASH_IN_FILENAME
906 xpc.xp_shell = FALSE;
907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000909#if defined(FEAT_EVAL)
910 if (ccline.input_fn)
911 {
912 xpc.xp_context = ccline.xp_context;
913 xpc.xp_pattern = ccline.cmdbuff;
914 xpc.xp_arg = ccline.xp_arg;
915 }
916#endif
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 /*
919 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
920 * doing ":@0" when register 0 doesn't contain a CR.
921 */
922 msg_scroll = FALSE;
923
924 State = CMDLINE;
925
926 if (firstc == '/' || firstc == '?' || firstc == '@')
927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100928 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
930 b_im_ptr = &curbuf->b_p_iminsert;
931 else
932 b_im_ptr = &curbuf->b_p_imsearch;
933 if (*b_im_ptr == B_IMODE_LMAP)
934 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100935#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 im_set_active(*b_im_ptr == B_IMODE_IM);
937#endif
938 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100939#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 else if (p_imcmdline)
941 im_set_active(TRUE);
942#endif
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100946 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947#endif
948
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100949 // When inside an autocommand for writing "exiting" may be set and
950 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000951 settmode(TMODE_RAW);
952
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100953 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200954 cmdline_type = firstc == NUL ? '-' : firstc;
955 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100958 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
964
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 // If something above caused an error, reset the flags, we do want to type
966 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200967 if (did_emsg)
968 redrawcmd();
969 did_emsg = FALSE;
970 got_int = FALSE;
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 /*
973 * Collect the command string, handling editing keys.
974 */
975 for (;;)
976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 redir_off = TRUE; // Don't redirect the typed command.
978 // Repeated, because a ":redir" inside
979 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100981 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100983 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 did_emsg = FALSE; // There can't really be a reason why an error
986 // that occurs while typing a command should
987 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +0200988
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200989 // Trigger SafeState if nothing is pending.
990 may_trigger_safestate(xpc.xp_numfiles <= 0);
991
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100992 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +0000993
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100994 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
995 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +0000996 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000997 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100998 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (KeyTyped)
1001 {
1002 some_key_typed = TRUE;
1003#ifdef FEAT_RIGHTLEFT
1004 if (cmd_hkmap)
1005 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (cmdmsg_rl && !KeyStuffed)
1007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001008 // Invert horizontal movements and operations. Only when
1009 // typed by the user directly, not when the result of a
1010 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 switch (c)
1012 {
1013 case K_RIGHT: c = K_LEFT; break;
1014 case K_S_RIGHT: c = K_S_LEFT; break;
1015 case K_C_RIGHT: c = K_C_LEFT; break;
1016 case K_LEFT: c = K_RIGHT; break;
1017 case K_S_LEFT: c = K_S_RIGHT; break;
1018 case K_C_LEFT: c = K_C_RIGHT; break;
1019 }
1020 }
1021#endif
1022 }
1023
1024 /*
1025 * Ignore got_int when CTRL-C was typed here.
1026 * Don't ignore it in :global, we really need to break then, e.g., for
1027 * ":g/pat/normal /pat" (without the <CR>).
1028 * Don't ignore it for the input() function.
1029 */
1030 if ((c == Ctrl_C
1031#ifdef UNIX
1032 || c == intr_char
1033#endif
1034 )
1035#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1036 && firstc != '@'
1037#endif
1038#ifdef FEAT_EVAL
1039 && !break_ctrl_c
1040#endif
1041 && !global_busy)
1042 got_int = FALSE;
1043
Bram Moolenaard7663c22019-08-06 21:59:57 +02001044 // free old command line when finished moving around in the history
1045 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001047 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001048 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 && c != K_PAGEDOWN && c != K_PAGEUP
1050 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001051 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001053 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001056 * When there are matching completions to select <S-Tab> works like
1057 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001059 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 c = Ctrl_P;
1061
1062#ifdef FEAT_WILDMENU
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001063 // Special translations for 'wildmenu'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (did_wild_list && p_wmnu)
1065 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001066 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001068 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 c = Ctrl_N;
1070 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // Hitting CR after "emenu Name.": complete submenu
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1073 && ccline.cmdpos > 1
1074 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1075 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1076 && (c == '\n' || c == '\r' || c == K_KENTER))
1077 c = K_DOWN;
1078#endif
1079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (xpc.xp_numfiles != -1
1082 && !(c == p_wc && KeyTyped) && c != p_wcm
1083 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1084 && c != Ctrl_L)
1085 {
1086 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1087 did_wild_list = FALSE;
1088#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001089 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
1091 xpc.xp_context = EXPAND_NOTHING;
1092 wim_index = 0;
1093#ifdef FEAT_WILDMENU
1094 if (p_wmnu && wild_menu_showing != 0)
1095 {
1096 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001097 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001098
1099 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001100 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102 if (wild_menu_showing == WM_SCROLLED)
1103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001104 // Entered command line, move it up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 cmdline_row--;
1106 redrawcmd();
1107 }
1108 else if (save_p_ls != -1)
1109 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001110 // restore 'laststatus' and 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 p_ls = save_p_ls;
1112 p_wmh = save_p_wmh;
1113 last_status(FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001114 update_screen(VALID); // redraw the screen NOW
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 redrawcmd();
1116 save_p_ls = -1;
1117 }
1118 else
1119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 redraw_statuslines();
1122 }
1123 KeyTyped = skt;
1124 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001125 if (ccline.input_fn)
1126 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128#endif
1129 }
1130
1131#ifdef FEAT_WILDMENU
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001132 // Special translations for 'wildmenu'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135 // Hitting <Down> after "emenu Name.": complete submenu
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001136 if (c == K_DOWN && ccline.cmdpos > 0
1137 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001139 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Hitting <Up>: Remove one submenu name in front of the
1142 // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 int found = FALSE;
1144
1145 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1146 i = 0;
1147 while (--j > 0)
1148 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001149 // check for start of menu name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (ccline.cmdbuff[j] == ' '
1151 && ccline.cmdbuff[j - 1] != '\\')
1152 {
1153 i = j + 1;
1154 break;
1155 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001156 // check for start of submenu name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (ccline.cmdbuff[j] == '.'
1158 && ccline.cmdbuff[j - 1] != '\\')
1159 {
1160 if (found)
1161 {
1162 i = j + 1;
1163 break;
1164 }
1165 else
1166 found = TRUE;
1167 }
1168 }
1169 if (i > 0)
1170 cmdline_del(i);
1171 c = p_wc;
1172 xpc.xp_context = EXPAND_NOTHING;
1173 }
1174 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001175 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001176 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001177 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 char_u upseg[5];
1180
1181 upseg[0] = PATHSEP;
1182 upseg[1] = '.';
1183 upseg[2] = '.';
1184 upseg[3] = PATHSEP;
1185 upseg[4] = NUL;
1186
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001187 if (c == K_DOWN
1188 && ccline.cmdpos > 0
1189 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1190 && (ccline.cmdpos < 3
1191 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1193 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001194 // go down a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 c = p_wc;
1196 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001197 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001199 // If in a direct ancestor, strip off one ../ to go down
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 int found = FALSE;
1201
1202 j = ccline.cmdpos;
1203 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1204 while (--j > i)
1205 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001206 if (has_mbyte)
1207 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 if (vim_ispathsep(ccline.cmdbuff[j]))
1209 {
1210 found = TRUE;
1211 break;
1212 }
1213 }
1214 if (found
1215 && ccline.cmdbuff[j - 1] == '.'
1216 && ccline.cmdbuff[j - 2] == '.'
1217 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1218 {
1219 cmdline_del(j - 2);
1220 c = p_wc;
1221 }
1222 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001223 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001225 // go up a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 int found = FALSE;
1227
1228 j = ccline.cmdpos - 1;
1229 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1230 while (--j > i)
1231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (has_mbyte)
1233 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (vim_ispathsep(ccline.cmdbuff[j])
1235#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001236 && vim_strchr((char_u *)" *?[{`$%#",
1237 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238#endif
1239 )
1240 {
1241 if (found)
1242 {
1243 i = j + 1;
1244 break;
1245 }
1246 else
1247 found = TRUE;
1248 }
1249 }
1250
1251 if (!found)
1252 j = i;
1253 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1254 j += 4;
1255 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1256 && j == i)
1257 j += 3;
1258 else
1259 j = 0;
1260 if (j > 0)
1261 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001262 // TODO this is only for DOS/UNIX systems - need to put in
1263 // machine-specific stuff here and in upseg init
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 cmdline_del(j);
1265 put_on_cmdline(upseg + 1, 3, FALSE);
1266 }
1267 else if (ccline.cmdpos > i)
1268 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001270 // Now complete in the new directory. Set KeyTyped in case the
1271 // Up key came from a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001273 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001277#endif // FEAT_WILDMENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001279 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1280 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (c == Ctrl_BSL)
1282 {
1283 ++no_mapping;
1284 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001285 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 --no_mapping;
1287 --allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001288 // CTRL-\ e doesn't work when obtaining an expression, unless it
1289 // is in a mapping.
Bram Moolenaarb7356812012-10-11 04:04:37 +02001290 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001291 || (ccline.cmdfirstc == '=' && KeyTyped)
1292#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001293 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001294#endif
1295 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 vungetc(c);
1298 c = Ctrl_BSL;
1299 }
1300#ifdef FEAT_EVAL
1301 else if (c == 'e')
1302 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001303 char_u *p = NULL;
1304 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 /*
1307 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001308 * Need to save and restore the current command line, to be
1309 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 */
1311 if (ccline.cmdpos == ccline.cmdlen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001312 new_cmdpos = 99999; // keep it at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 else
1314 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (c == '=')
1318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001319 // Need to save and restore ccline. And set "textlock"
1320 // to avoid nasty things like going to another buffer when
1321 // evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001322 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001324 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001325
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001326 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001328 len = (int)STRLEN(p);
1329 if (realloc_cmdbuff(len + 1) == OK)
1330 {
1331 ccline.cmdlen = len;
1332 STRCPY(ccline.cmdbuff, p);
1333 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001335 // Restore the cursor or use the position set with
1336 // set_cmdline_pos().
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001337 if (new_cmdpos > ccline.cmdlen)
1338 ccline.cmdpos = ccline.cmdlen;
1339 else
1340 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001342 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001343 redrawcmd();
1344 goto cmdline_changed;
1345 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001346 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 }
1349 beep_flush();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001350 got_int = FALSE; // don't abandon the command line
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001351 did_emsg = FALSE;
1352 emsg_on_display = FALSE;
1353 redrawcmd();
1354 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356#endif
1357 else
1358 {
1359 if (c == Ctrl_G && p_im && restart_edit == 0)
1360 restart_edit = 'a';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001361 gotesc = TRUE; // will free ccline.cmdbuff after putting it
1362 // in history
1363 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 }
1366
1367#ifdef FEAT_CMDWIN
1368 if (c == cedit_key || c == K_CMDWIN)
1369 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001370 // TODO: why is ex_normal_busy checked here?
1371 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001372 {
1373 /*
1374 * Open a window to edit the command line (and history).
1375 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001376 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001377 some_key_typed = TRUE;
1378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380# ifdef FEAT_DIGRAPHS
1381 else
1382# endif
1383#endif
1384#ifdef FEAT_DIGRAPHS
1385 c = do_digraph(c);
1386#endif
1387
1388 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1389 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001391 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001392 if (exmode_active
1393 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001394 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001395 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001396 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001398 if (c == K_KENTER)
1399 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001401 else
1402 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001403 gotesc = FALSE; // Might have typed ESC previously, don't
1404 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001405 if (ccheck_abbr(c + ABBR_OFF))
1406 goto cmdline_changed;
1407 if (!cmd_silent)
1408 {
1409 windgoto(msg_row, 0);
1410 out_flush();
1411 }
1412 break;
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415
1416 /*
1417 * Completion for 'wildchar' or 'wildcharm' key.
1418 * - hitting <ESC> twice means: abandon command line.
1419 * - wildcard expansion is only done when the 'wildchar' key is really
1420 * typed, not when it comes from a macro
1421 */
1422 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1423 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001424 int options = WILD_NO_BEEP;
1425 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1426 options |= WILD_BUFLASTUSED;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001427 if (xpc.xp_numfiles > 0) // typed p_wc at least twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 // if 'wildmode' contains "list" may still need to list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (xpc.xp_numfiles > 1
1431 && !did_wild_list
1432 && (wim_flags[wim_index] & WIM_LIST))
1433 {
1434 (void)showmatches(&xpc, FALSE);
1435 redrawcmd();
1436 did_wild_list = TRUE;
1437 }
1438 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001439 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001440 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001442 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001443 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001445 res = OK; // don't insert 'wildchar' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001447 else // typed p_wc first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
1449 wim_index = 0;
1450 j = ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001451 // if 'wildmode' first contains "longest", get longest
1452 // common part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001454 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001455 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001457 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001458 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001460 // if interrupted while completing, behave like it failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (got_int)
1462 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001463 (void)vpeekc(); // remove <C-C> from input stream
1464 got_int = FALSE; // don't abandon the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1466#ifdef FEAT_WILDMENU
1467 xpc.xp_context = EXPAND_NOTHING;
1468#endif
1469 goto cmdline_changed;
1470 }
1471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001472 // when more than one match, and 'wildmode' first contains
1473 // "list", or no change and 'wildmode' contains "longest,list",
1474 // list all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (res == OK && xpc.xp_numfiles > 1)
1476 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001477 // a "longest" that didn't do anything is skipped (but not
1478 // "list:longest")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1480 wim_index = 1;
1481 if ((wim_flags[wim_index] & WIM_LIST)
1482#ifdef FEAT_WILDMENU
1483 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1484#endif
1485 )
1486 {
1487 if (!(wim_flags[0] & WIM_LONGEST))
1488 {
1489#ifdef FEAT_WILDMENU
1490 int p_wmnu_save = p_wmnu;
1491 p_wmnu = 0;
1492#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 // remove match
Bram Moolenaarb3479632012-11-28 16:49:58 +01001494 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#ifdef FEAT_WILDMENU
1496 p_wmnu = p_wmnu_save;
1497#endif
1498 }
1499#ifdef FEAT_WILDMENU
1500 (void)showmatches(&xpc, p_wmnu
1501 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1502#else
1503 (void)showmatches(&xpc, FALSE);
1504#endif
1505 redrawcmd();
1506 did_wild_list = TRUE;
1507 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001508 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001509 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001511 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001512 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001515 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517#ifdef FEAT_WILDMENU
1518 else if (xpc.xp_numfiles == -1)
1519 xpc.xp_context = EXPAND_NOTHING;
1520#endif
1521 }
1522 if (wim_index < 3)
1523 ++wim_index;
1524 if (c == ESC)
1525 gotesc = TRUE;
1526 if (res == OK)
1527 goto cmdline_changed;
1528 }
1529
1530 gotesc = FALSE;
1531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001532 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (c == K_S_TAB && KeyTyped)
1534 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001535 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1536 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1537 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 goto cmdline_changed;
1539 }
1540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001541 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 c = NL;
1543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001544 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546 /*
1547 * Big switch for a typed command line character.
1548 */
1549 switch (c)
1550 {
1551 case K_BS:
1552 case Ctrl_H:
1553 case K_DEL:
1554 case K_KDEL:
1555 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (c == K_KDEL)
1557 c = K_DEL;
1558
1559 /*
1560 * delete current character is the same as backspace on next
1561 * character, except at end of line
1562 */
1563 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1564 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (has_mbyte && c == K_DEL)
1566 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1567 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (ccline.cmdpos > 0)
1569 {
1570 char_u *p;
1571
1572 j = ccline.cmdpos;
1573 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (has_mbyte)
1575 {
1576 p = mb_prevptr(ccline.cmdbuff, p);
1577 if (c == Ctrl_W)
1578 {
1579 while (p > ccline.cmdbuff && vim_isspace(*p))
1580 p = mb_prevptr(ccline.cmdbuff, p);
1581 i = mb_get_class(p);
1582 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1583 p = mb_prevptr(ccline.cmdbuff, p);
1584 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001585 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001588 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1591 --p;
1592 i = vim_iswordc(p[-1]);
1593 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1594 && vim_iswordc(p[-1]) == i)
1595 --p;
1596 }
1597 else
1598 --p;
1599 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1600 ccline.cmdlen -= j - ccline.cmdpos;
1601 i = ccline.cmdpos;
1602 while (i < ccline.cmdlen)
1603 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001605 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001607#ifdef FEAT_SEARCH_EXTRA
1608 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001609 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001610 is_state.search_start = is_state.save_cursor;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001611 // save view settings, so that the screen
1612 // won't be restored at the wrong position
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001613 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001614 }
1615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 redrawcmd();
1617 }
1618 else if (ccline.cmdlen == 0 && c != Ctrl_W
1619 && ccline.cmdprompt == NULL && indent == 0)
1620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001621 // In ex and debug mode it doesn't make sense to return.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (exmode_active
1623#ifdef FEAT_EVAL
1624 || ccline.cmdfirstc == '>'
1625#endif
1626 )
1627 goto cmdline_not_changed;
1628
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001629 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!cmd_silent)
1631 {
1632#ifdef FEAT_RIGHTLEFT
1633 if (cmdmsg_rl)
1634 msg_col = Columns;
1635 else
1636#endif
1637 msg_col = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 msg_putchar(' '); // delete ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001640#ifdef FEAT_SEARCH_EXTRA
1641 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001642 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 redraw_cmdline = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001645 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647 goto cmdline_changed;
1648
1649 case K_INS:
1650 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 ccline.overstrike = !ccline.overstrike;
1652#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001653 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
1655 goto cmdline_not_changed;
1656
1657 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001658 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001660 // ":lmap" mappings exists, toggle use of mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001662#ifdef HAVE_INPUT_METHOD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001663 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664#endif
1665 if (b_im_ptr != NULL)
1666 {
1667 if (State & LANGMAP)
1668 *b_im_ptr = B_IMODE_LMAP;
1669 else
1670 *b_im_ptr = B_IMODE_NONE;
1671 }
1672 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001673#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 else
1675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 // There are no ":lmap" mappings, toggle IM. When
1677 // 'imdisable' is set don't try getting the status, it's
1678 // always off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if ((p_imdisable && b_im_ptr != NULL)
1680 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001682 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (b_im_ptr != NULL)
1684 *b_im_ptr = B_IMODE_NONE;
1685 }
1686 else
1687 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001688 im_set_active(TRUE); // Enable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (b_im_ptr != NULL)
1690 *b_im_ptr = B_IMODE_IM;
1691 }
1692 }
1693#endif
1694 if (b_im_ptr != NULL)
1695 {
1696 if (b_im_ptr == &curbuf->b_p_iminsert)
1697 set_iminsert_global();
1698 else
1699 set_imsearch_global();
1700 }
1701#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001702 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001704#if defined(FEAT_KEYMAP)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001705 // Show/unshow value of 'keymap' in status lines later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 status_redraw_curbuf();
1707#endif
1708 goto cmdline_not_changed;
1709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001710// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001712 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 j = ccline.cmdpos;
1714 ccline.cmdlen -= j;
1715 i = ccline.cmdpos = 0;
1716 while (i < ccline.cmdlen)
1717 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001718 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001720#ifdef FEAT_SEARCH_EXTRA
1721 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001722 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 redrawcmd();
1725 goto cmdline_changed;
1726
1727#ifdef FEAT_CLIPBOARD
1728 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (clip_star.state != SELECT_CLEARED)
1731 {
1732 if (clip_star.state == SELECT_DONE)
1733 clip_copy_modeless_selection(TRUE);
1734 goto cmdline_not_changed;
1735 }
1736 break;
1737#endif
1738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001739 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001741 // In exmode it doesn't make sense to return. Except when
1742 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001743 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001744 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 goto cmdline_not_changed;
1746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 gotesc = TRUE; // will free ccline.cmdbuff after
1748 // putting it in history
1749 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 case Ctrl_R: // insert register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#endif
1755 putcmdline('"', TRUE);
1756 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001757 ++allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 i = c = plain_vgetc(); // CTRL-R <char>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (i == Ctrl_O)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (i == Ctrl_R)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001762 c = plain_vgetc(); // CTRL-R CTRL-R <char>
Bram Moolenaara92522f2017-07-15 15:21:38 +02001763 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001765 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_EVAL
1767 /*
1768 * Insert the result of an expression.
1769 * Need to save the current command line, to be able to enter
1770 * a new one...
1771 */
1772 new_cmdpos = -1;
1773 if (c == '=')
1774 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001775 if (ccline.cmdfirstc == '=' // can't do this recursively
1776 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
1778 beep_flush();
1779 c = ESC;
1780 }
1781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
1784#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001785 if (c != ESC) // use ESC to cancel inserting register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001787 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001788
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001789#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001790 // When there was a serious error abort getting the
1791 // command line.
Bram Moolenaaracf53452005-12-17 22:06:52 +00001792 if (aborting())
1793 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001794 gotesc = TRUE; // will free ccline.cmdbuff after
1795 // putting it in history
1796 goto returncmd; // back to cmd mode
Bram Moolenaaracf53452005-12-17 22:06:52 +00001797 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001798#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001799 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#ifdef FEAT_EVAL
1801 if (new_cmdpos >= 0)
1802 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001803 // set_cmdline_pos() was used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (new_cmdpos > ccline.cmdlen)
1805 ccline.cmdpos = ccline.cmdlen;
1806 else
1807 ccline.cmdpos = new_cmdpos;
1808 }
1809#endif
1810 }
1811 redrawcmd();
1812 goto cmdline_changed;
1813
1814 case Ctrl_D:
1815 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001816 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001819 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 case K_RIGHT:
1822 case K_S_RIGHT:
1823 case K_C_RIGHT:
1824 do
1825 {
1826 if (ccline.cmdpos >= ccline.cmdlen)
1827 break;
1828 i = cmdline_charsize(ccline.cmdpos);
1829 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1830 break;
1831 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001833 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 + ccline.cmdpos);
1835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 ++ccline.cmdpos;
1837 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001838 while ((c == K_S_RIGHT || c == K_C_RIGHT
1839 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (has_mbyte)
1842 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 goto cmdline_not_changed;
1844
1845 case K_LEFT:
1846 case K_S_LEFT:
1847 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001848 if (ccline.cmdpos == 0)
1849 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 do
1851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001853 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1855 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1857 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001858 while (ccline.cmdpos > 0
1859 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001860 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (has_mbyte)
1863 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 goto cmdline_not_changed;
1865
1866 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001867 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001868 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaar4f974752019-02-17 17:44:42 +01001870#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 // On MS-Windows ignore <M-F4>, we get it when closing the window
1872 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001873 case K_F4:
1874 if (mod_mask == MOD_MASK_ALT)
1875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001876 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00001877 goto cmdline_not_changed;
1878 }
1879 break;
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 case K_MIDDLEDRAG:
1883 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001884 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 case K_MIDDLEMOUSE:
1887# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001888 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (!gui.in_use)
1890# endif
1891 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001892 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001893# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001895 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001897# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001898 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 redrawcmd();
1900 goto cmdline_changed;
1901
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001902# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001904 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 redrawcmd();
1906 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909 case K_LEFTDRAG:
1910 case K_LEFTRELEASE:
1911 case K_RIGHTDRAG:
1912 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001913 // Ignore drag and release events when the button-down wasn't
1914 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (ignore_drag_release)
1916 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001917 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 case K_LEFTMOUSE:
1919 case K_RIGHTMOUSE:
1920 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1921 ignore_drag_release = TRUE;
1922 else
1923 ignore_drag_release = FALSE;
1924# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001925 // When GUI is active, also move when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (!gui.in_use)
1927# endif
1928 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001929 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930# ifdef FEAT_CLIPBOARD
1931 if (mouse_row < cmdline_row && clip_star.available)
1932 {
1933 int button, is_click, is_drag;
1934
1935 /*
1936 * Handle modeless selection.
1937 */
1938 button = get_mouse_button(KEY2TERMCAP1(c),
1939 &is_click, &is_drag);
1940 if (mouse_model_popup() && button == MOUSE_LEFT
1941 && (mod_mask & MOD_MASK_SHIFT))
1942 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 // Translate shift-left to right button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 button = MOUSE_RIGHT;
1945 mod_mask &= ~MOD_MASK_SHIFT;
1946 }
1947 clip_modeless(button, is_click, is_drag);
1948 goto cmdline_not_changed;
1949 }
1950# endif
1951
1952 set_cmdspos();
1953 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1954 ++ccline.cmdpos)
1955 {
1956 i = cmdline_charsize(ccline.cmdpos);
1957 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1958 && mouse_col < ccline.cmdspos % Columns + i)
1959 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (has_mbyte)
1961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001962 // Count ">" for double-wide char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001964 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 + ccline.cmdpos) - 1;
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 ccline.cmdspos += i;
1968 }
1969 goto cmdline_not_changed;
1970
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 case K_MOUSEDOWN:
1973 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001974 case K_MOUSELEFT:
1975 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001976 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 case K_X1MOUSE:
1978 case K_X1DRAG:
1979 case K_X1RELEASE:
1980 case K_X2MOUSE:
1981 case K_X2DRAG:
1982 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001983 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 goto cmdline_not_changed;
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001987 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 case K_LEFTRELEASE_NM:
1989 goto cmdline_not_changed;
1990
1991 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001992 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 gui_do_scroll();
1995 redrawcmd();
1996 }
1997 goto cmdline_not_changed;
1998
1999 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002000 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002002 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 redrawcmd();
2004 }
2005 goto cmdline_not_changed;
2006#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002007#ifdef FEAT_GUI_TABLINE
2008 case K_TABLINE:
2009 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002010 // Don't want to change any tabs here. Make sure the same tab
2011 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002012 if (gui_use_tabline())
2013 gui_mch_set_curtab(tabpage_index(curtab));
2014 goto cmdline_not_changed;
2015#endif
2016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002017 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 goto cmdline_not_changed;
2019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 case K_HOME:
2022 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 case K_S_HOME:
2024 case K_C_HOME:
2025 ccline.cmdpos = 0;
2026 set_cmdspos();
2027 goto cmdline_not_changed;
2028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002029 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 case K_END:
2031 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 case K_S_END:
2033 case K_C_END:
2034 ccline.cmdpos = ccline.cmdlen;
2035 set_cmdspos_cursor();
2036 goto cmdline_not_changed;
2037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002038 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01002039 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 break;
2041 goto cmdline_changed;
2042
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002043 case Ctrl_L:
2044#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002045 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002046 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002047#endif
2048
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002049 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002050 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 break;
2052 goto cmdline_changed;
2053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002054 case Ctrl_N: // next match
2055 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002056 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002058 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2059 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002061 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002063 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 case K_UP:
2065 case K_DOWN:
2066 case K_S_UP:
2067 case K_S_DOWN:
2068 case K_PAGEUP:
2069 case K_KPAGEUP:
2070 case K_PAGEDOWN:
2071 case K_KPAGEDOWN:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 if (get_hislen() == 0 || firstc == NUL) // no history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 goto cmdline_not_changed;
2074
2075 i = hiscnt;
2076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002077 // save current command string so it can be restored later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (lookfor == NULL)
2079 {
2080 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2081 goto cmdline_not_changed;
2082 lookfor[ccline.cmdpos] = NUL;
2083 }
2084
2085 j = (int)STRLEN(lookfor);
2086 for (;;)
2087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // one step backwards
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002089 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002090 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002092 if (hiscnt == get_hislen()) // first time
Bram Moolenaard7663c22019-08-06 21:59:57 +02002093 hiscnt = *get_hisidx(histype);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002094 else if (hiscnt == 0 && *get_hisidx(histype)
2095 != get_hislen() - 1)
Bram Moolenaard7663c22019-08-06 21:59:57 +02002096 hiscnt = get_hislen() - 1;
2097 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 --hiscnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 else // at top of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101 hiscnt = i;
2102 break;
2103 }
2104 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 else // one step forwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // on last entry, clear the line
Bram Moolenaard7663c22019-08-06 21:59:57 +02002108 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002110 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 break;
2112 }
2113
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002114 // not on a history line, nothing to do
Bram Moolenaard7663c22019-08-06 21:59:57 +02002115 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002117 if (hiscnt == get_hislen() - 1) // wrap around
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 hiscnt = 0;
2119 else
2120 ++hiscnt;
2121 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
2123 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
2125 hiscnt = i;
2126 break;
2127 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002128 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002129 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002130 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 lookfor, (size_t)j) == 0)
2132 break;
2133 }
2134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 if (hiscnt != i) // jumped to other entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 char_u *p;
2138 int len;
2139 int old_firstc;
2140
Bram Moolenaar438d1762018-09-30 17:11:48 +02002141 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002142 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002143 if (hiscnt == get_hislen())
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002144 p = lookfor; // back to the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002146 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148 if (histype == HIST_SEARCH
2149 && p != lookfor
2150 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 // Correct for the separator character used when
2153 // adding the history entry vs the one used now.
2154 // First loop: count length.
2155 // Second loop: copy the characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 for (i = 0; i <= 1; ++i)
2157 {
2158 len = 0;
2159 for (j = 0; p[j] != NUL; ++j)
2160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 // Replace old sep with new sep, unless it is
2162 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (p[j] == old_firstc
2164 && (j == 0 || p[j - 1] != '\\'))
2165 {
2166 if (i > 0)
2167 ccline.cmdbuff[len] = firstc;
2168 }
2169 else
2170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002171 // Escape new sep, unless it is already
2172 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (p[j] == firstc
2174 && (j == 0 || p[j - 1] != '\\'))
2175 {
2176 if (i > 0)
2177 ccline.cmdbuff[len] = '\\';
2178 ++len;
2179 }
2180 if (i > 0)
2181 ccline.cmdbuff[len] = p[j];
2182 }
2183 ++len;
2184 }
2185 if (i == 0)
2186 {
2187 alloc_cmdbuff(len);
2188 if (ccline.cmdbuff == NULL)
2189 goto returncmd;
2190 }
2191 }
2192 ccline.cmdbuff[len] = NUL;
2193 }
2194 else
2195 {
2196 alloc_cmdbuff((int)STRLEN(p));
2197 if (ccline.cmdbuff == NULL)
2198 goto returncmd;
2199 STRCPY(ccline.cmdbuff, p);
2200 }
2201
2202 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2203 redrawcmd();
2204 goto cmdline_changed;
2205 }
2206 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002207 goto cmdline_not_changed;
2208
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002209#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002210 case Ctrl_G: // next match
2211 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002212 if (may_adjust_incsearch_highlighting(
2213 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002214 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216#endif
2217
2218 case Ctrl_V:
2219 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002221 int prev_mod_mask = mod_mask;
2222
2223 ignore_drag_release = TRUE;
2224 putcmdline('^', TRUE);
2225 c = get_literal(); // get next (two) character(s)
2226 do_abbr = FALSE; // don't do abbreviation now
2227 extra_char = NUL;
2228 // may need to remove ^ when composing char was typed
2229 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2230 {
2231 draw_cmdline(ccline.cmdpos,
2232 ccline.cmdlen - ccline.cmdpos);
2233 msg_putchar(' ');
2234 cursorcmd();
2235 }
2236
2237 if ((c == ESC || c == CSI)
2238 && !(prev_mod_mask & MOD_MASK_SHIFT))
2239 // Using CTRL-V: Change any modifyOtherKeys ESC
2240 // sequence to a normal key. Don't do this for
2241 // CTRL-SHIFT-V.
2242 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 break;
2246
2247#ifdef FEAT_DIGRAPHS
2248 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002251# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002255 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (c != NUL)
2257 break;
2258
2259 redrawcmd();
2260 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002261#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002264 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (!p_ari)
2266 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002267 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 goto cmdline_not_changed;
2269#endif
2270
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002271 case K_PS:
2272 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2273 goto cmdline_changed;
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 default:
2276#ifdef UNIX
2277 if (c == intr_char)
2278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002279 gotesc = TRUE; // will free ccline.cmdbuff after
2280 // putting it in history
2281 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283#endif
2284 /*
2285 * Normal character with no special meaning. Just set mod_mask
2286 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2287 * the string <S-Space>. This should only happen after ^V.
2288 */
2289 if (!IS_SPECIAL(c))
2290 mod_mask = 0x0;
2291 break;
2292 }
2293 /*
2294 * End of switch on command line character.
2295 * We come here if we have a normal character.
2296 */
2297
Bram Moolenaar13505972019-01-24 15:04:48 +01002298 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2299 && (ccheck_abbr(
2300 // Add ABBR_OFF for characters above 0x100, this is
2301 // what check_abbr() expects.
2302 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2303 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 goto cmdline_changed;
2305
2306 /*
2307 * put the character in the command line
2308 */
2309 if (IS_SPECIAL(c) || mod_mask != 0)
2310 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2311 else
2312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (has_mbyte)
2314 {
2315 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 put_on_cmdline(IObuff, j, TRUE);
2318 }
2319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 IObuff[0] = c;
2322 put_on_cmdline(IObuff, 1, TRUE);
2323 }
2324 }
2325 goto cmdline_changed;
2326
2327/*
2328 * This part implements incremental searches for "/" and "?"
2329 * Jump to cmdline_not_changed when a character has been read but the command
2330 * line did not change. Then we only search and redraw if something changed in
2331 * the past.
2332 * Jump to cmdline_changed when the command line did change.
2333 * (Sorry for the goto's, I know it is ugly).
2334 */
2335cmdline_not_changed:
2336#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002337 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 continue;
2339#endif
2340
2341cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002343 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002346 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348
2349#ifdef FEAT_RIGHTLEFT
2350 if (cmdmsg_rl
2351# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002352 || (p_arshape && !p_tbidi
2353 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354# endif
2355 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002356 // Always redraw the whole command line to fix shaping and
2357 // right-left typing. Not efficient, but it works.
2358 // Do it only when there are no characters left to read
2359 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002360 if (vpeekc() == NUL)
2361 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#endif
2363 }
2364
2365returncmd:
2366
2367#ifdef FEAT_RIGHTLEFT
2368 cmdmsg_rl = FALSE;
2369#endif
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002372 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002375 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#endif
2377
2378 if (ccline.cmdbuff != NULL)
2379 {
2380 /*
2381 * Put line in history buffer (":" and "=" only when it was typed).
2382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (ccline.cmdlen && firstc != NUL
2384 && (some_key_typed || histype == HIST_SEARCH))
2385 {
2386 add_to_history(histype, ccline.cmdbuff, TRUE,
2387 histype == HIST_SEARCH ? firstc : NUL);
2388 if (firstc == ':')
2389 {
2390 vim_free(new_last_cmdline);
2391 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2392 }
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002395 if (gotesc)
2396 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398
2399 /*
2400 * If the screen was shifted up, redraw the whole screen (later).
2401 * If the line is too long, clear it, so ruler and shown command do
2402 * not get printed in the middle of it.
2403 */
2404 msg_check();
2405 msg_scroll = save_msg_scroll;
2406 redir_off = FALSE;
2407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002408 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (some_key_typed)
2410 need_wait_return = FALSE;
2411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002412 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002413 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002414
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002416#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2418 im_save_status(b_im_ptr);
2419 im_set_active(FALSE);
2420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002425 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
Bram Moolenaar438d1762018-09-30 17:11:48 +02002427theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002428 {
2429 char_u *p = ccline.cmdbuff;
2430
Bram Moolenaar438d1762018-09-30 17:11:48 +02002431 if (did_save_ccline)
2432 restore_cmdline(&save_ccline);
2433 else
2434 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002435 return p;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437}
2438
2439#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2440/*
2441 * Get a command line with a prompt.
2442 * This is prepared to be called recursively from getcmdline() (e.g. by
2443 * f_input() when evaluating an expression from CTRL-R =).
2444 * Returns the command line in allocated memory, or NULL.
2445 */
2446 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002447getcmdline_prompt(
2448 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002449 char_u *prompt, // command line prompt
2450 int attr, // attributes for prompt
2451 int xp_context, // type of expansion
2452 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002455 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002456 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002458 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
Bram Moolenaar438d1762018-09-30 17:11:48 +02002460 if (ccline.cmdbuff != NULL)
2461 {
2462 // Save the values of the current cmdline and restore them below.
2463 save_cmdline(&save_ccline);
2464 did_save_ccline = TRUE;
2465 }
2466
Bram Moolenaar66b51422019-08-18 21:44:12 +02002467 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 ccline.cmdprompt = prompt;
2469 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002470# ifdef FEAT_EVAL
2471 ccline.xp_context = xp_context;
2472 ccline.xp_arg = xp_arg;
2473 ccline.input_fn = (firstc == '@');
2474# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002475 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002476 s = getcmdline_int(firstc, 1L, 0, FALSE);
2477
2478 if (did_save_ccline)
2479 restore_cmdline(&save_ccline);
2480
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002481 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002482 // Restore msg_col, the prompt from input() may have changed it.
2483 // But only if called recursively and the commandline is therefore being
2484 // restored to an old one; if not, the input() prompt stays on the screen,
2485 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002486 if (ccline.cmdbuff != NULL)
2487 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 return s;
2490}
2491#endif
2492
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002493/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002494 * Read the 'wildmode' option, fill wim_flags[].
2495 */
2496 int
2497check_opt_wim(void)
2498{
2499 char_u new_wim_flags[4];
2500 char_u *p;
2501 int i;
2502 int idx = 0;
2503
2504 for (i = 0; i < 4; ++i)
2505 new_wim_flags[i] = 0;
2506
2507 for (p = p_wim; *p; ++p)
2508 {
2509 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2510 ;
2511 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2512 return FAIL;
2513 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2514 new_wim_flags[idx] |= WIM_LONGEST;
2515 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2516 new_wim_flags[idx] |= WIM_FULL;
2517 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2518 new_wim_flags[idx] |= WIM_LIST;
2519 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2520 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2521 else
2522 return FAIL;
2523 p += i;
2524 if (*p == NUL)
2525 break;
2526 if (*p == ',')
2527 {
2528 if (idx == 3)
2529 return FAIL;
2530 ++idx;
2531 }
2532 }
2533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002534 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002535 while (idx < 3)
2536 {
2537 new_wim_flags[idx + 1] = new_wim_flags[idx];
2538 ++idx;
2539 }
2540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002541 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002542 for (i = 0; i < 4; ++i)
2543 wim_flags[i] = new_wim_flags[i];
2544 return OK;
2545}
2546
2547/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002548 * Return TRUE when the text must not be changed and we can't switch to
2549 * another window or buffer. Used when editing the command line, evaluating
2550 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002551 */
2552 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002553text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002554{
2555#ifdef FEAT_CMDWIN
2556 if (cmdwin_type != 0)
2557 return TRUE;
2558#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002559 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002560}
2561
2562/*
2563 * Give an error message for a command that isn't allowed while the cmdline
2564 * window is open or editing the cmdline in another way.
2565 */
2566 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002567text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002568{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002569 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002570}
2571
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002572 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002573get_text_locked_msg(void)
2574{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002575#ifdef FEAT_CMDWIN
2576 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002577 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002578#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002579 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002580}
2581
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002582/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002583 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2584 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585 */
2586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002587curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002588{
2589 if (curbuf_lock > 0)
2590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002591 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002592 return TRUE;
2593 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002594 return allbuf_locked();
2595}
2596
2597/*
2598 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2599 * message.
2600 */
2601 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002603{
2604 if (allbuf_lock > 0)
2605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002606 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002607 return TRUE;
2608 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002609 return FALSE;
2610}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002613cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002616 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return 1;
2618#endif
2619 return ptr2cells(ccline.cmdbuff + idx);
2620}
2621
2622/*
2623 * Compute the offset of the cursor on the command line for the prompt and
2624 * indent.
2625 */
2626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002627set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002629 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 ccline.cmdspos = 1 + ccline.cmdindent;
2631 else
2632 ccline.cmdspos = 0 + ccline.cmdindent;
2633}
2634
2635/*
2636 * Compute the screen position for the cursor on the command line.
2637 */
2638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002639set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 int i, m, c;
2642
2643 set_cmdspos();
2644 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002647 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002648 m = MAXCOL;
2649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 else
2651 m = MAXCOL;
2652 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2653 {
2654 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002655 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (has_mbyte)
2657 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002658 // If the cmdline doesn't fit, show cursor on last visible char.
2659 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if ((ccline.cmdspos += c) >= m)
2661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ccline.cmdspos -= c;
2663 break;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002666 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668}
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2672 * character that doesn't fit, so that a ">" must be displayed.
2673 */
2674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002675correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002677 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2679 && ccline.cmdspos % Columns + cells > Columns)
2680 ccline.cmdspos++;
2681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683/*
2684 * Get an Ex command line for the ":" command.
2685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002687getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002688 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002689 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002690 int indent, // indent for inside conditionals
Bram Moolenaare96a2492019-06-25 04:12:16 +02002691 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002693 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (exec_from_reg && vpeekc() == ':')
2695 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002696 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697}
2698
2699/*
2700 * Get an Ex command line for Ex mode.
2701 * In Ex mode we only use the OS supplied line editing features and no
2702 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002703 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002706getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002707 int promptc, // normally ':', NUL for ":append" and '?' for
2708 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002709 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002710 int indent, // indent for inside conditionals
Bram Moolenaare96a2492019-06-25 04:12:16 +02002711 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 garray_T line_ga;
2714 char_u *pend;
2715 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002716 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002717 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 int vcol = 0;
2719 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002720 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002721 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002723 // Switch cursor on now. This avoids that it happens after the "\n", which
2724 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 cursor_on();
2726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002727 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002733 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002734 if (p_prompt)
2735 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 while (indent-- > 0)
2737 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740
2741 ga_init2(&line_ga, 1, 30);
2742
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002743 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002745 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002746 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002747 while (indent >= 8)
2748 {
2749 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002750 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002751 indent -= 8;
2752 }
2753 while (indent-- > 0)
2754 {
2755 ga_append(&line_ga, ' ');
2756 msg_putchar(' ');
2757 }
2758 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 ++no_mapping;
2760 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 /*
2763 * Get the line, one character at a time.
2764 */
2765 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002768 long sw;
2769 char_u *s;
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (ga_grow(&line_ga, 40) == FAIL)
2772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
Bram Moolenaarba748c82017-02-26 14:00:07 +01002774 /*
2775 * Get one character at a time.
2776 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002777 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002779 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002780 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2781 c1 = '\n';
2782 else
2783 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 * Handle line editing.
2787 * Previously this was left to the system, putting the terminal in
2788 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 msg_putchar('\n');
2793 break;
2794 }
2795
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002796 if (c1 == K_PS)
2797 {
2798 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2799 goto redraw;
2800 }
2801
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 if (!escaped)
2803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002804 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 if (c1 == '\r')
2806 c1 = '\n';
2807
2808 if (c1 == BS || c1 == K_BS
2809 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 if (line_ga.ga_len > 0)
2812 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002813 if (has_mbyte)
2814 {
2815 p = (char_u *)line_ga.ga_data;
2816 p[line_ga.ga_len] = NUL;
2817 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2818 line_ga.ga_len -= len;
2819 }
2820 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002821 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 goto redraw;
2823 }
2824 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002829 msg_col = startcol;
2830 msg_clr_eos();
2831 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002832 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 }
2834
2835 if (c1 == Ctrl_T)
2836 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002837 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838 p = (char_u *)line_ga.ga_data;
2839 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002840 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002841 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002842add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002843 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002845 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002846 p = (char_u *)line_ga.ga_data;
2847 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2849 *s = ' ';
2850 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002853 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002856 p = (char_u *)line_ga.ga_data;
2857 p[line_ga.ga_len] = NUL;
2858 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002862 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002864 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002865 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002867 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002869 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002870 msg_outtrans_len(p, len);
2871 vcol += ptr2cells(p);
2872 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002876 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 continue;
2878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 if (c1 == Ctrl_D)
2881 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002882 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 p = (char_u *)line_ga.ga_data;
2884 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 if (prev_char == '^')
2887 ex_keep_indent = TRUE;
2888 indent = 0;
2889 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891 else
2892 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002893 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002894 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002895 if (indent > 0)
2896 {
2897 --indent;
2898 indent -= indent % get_sw_value(curbuf);
2899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002901 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002903 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002904 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2905 --line_ga.ga_len;
2906 }
2907 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909
2910 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2911 {
2912 escaped = TRUE;
2913 continue;
2914 }
2915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002916 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002917 if (IS_SPECIAL(c1))
2918 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920
2921 if (IS_SPECIAL(c1))
2922 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002923 if (has_mbyte)
2924 len = (*mb_char2bytes)(c1,
2925 (char_u *)line_ga.ga_data + line_ga.ga_len);
2926 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002927 {
2928 len = 1;
2929 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2930 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002931 if (c1 == '\n')
2932 msg_putchar('\n');
2933 else if (c1 == TAB)
2934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002935 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002937 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002938 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002942 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002943 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002946 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002947 escaped = FALSE;
2948
2949 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002950 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002952 // We are done when a NL is entered, but not when it comes after an
2953 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002954 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002956 int bcount = 0;
2957
2958 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2959 ++bcount;
2960
2961 if (bcount > 0)
2962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2964 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002965 line_ga.ga_len -= (bcount + 1) / 2;
2966 pend -= (bcount + 1) / 2;
2967 pend[-1] = '\n';
2968 }
2969
2970 if ((bcount & 1) == 0)
2971 {
2972 --line_ga.ga_len;
2973 --pend;
2974 *pend = NUL;
2975 break;
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978 }
2979
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002980 --no_mapping;
2981 --allow_keys;
2982
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002983 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 msg_didout = FALSE;
2985 msg_col = 0;
2986 if (msg_row < Rows - 1)
2987 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002988 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 if (got_int)
2991 ga_clear(&line_ga);
2992
2993 return (char_u *)line_ga.ga_data;
2994}
2995
Bram Moolenaare344bea2005-09-01 20:46:49 +00002996# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2997 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
2999 * Return TRUE if ccline.overstrike is on.
3000 */
3001 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003002cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 return ccline.overstrike;
3005}
3006
3007/*
3008 * Return TRUE if the cursor is at the end of the cmdline.
3009 */
3010 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003011cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 return (ccline.cmdpos >= ccline.cmdlen);
3014}
3015#endif
3016
Bram Moolenaar9372a112005-12-06 19:59:18 +00003017#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
3019 * Return the virtual column number at the current cursor position.
3020 * This is used by the IM code to obtain the start of the preedit string.
3021 */
3022 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003023cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3026 return MAXCOL;
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (has_mbyte)
3029 {
3030 colnr_T col;
3031 int i = 0;
3032
3033 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003034 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036 return col;
3037 }
3038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 return ccline.cmdpos;
3040}
3041#endif
3042
3043#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3044/*
3045 * If part of the command line is an IM preedit string, redraw it with
3046 * IM feedback attributes. The cursor position is restored after drawing.
3047 */
3048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 if ((State & CMDLINE)
3052 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003053 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 && !p_imdisable
3055 && im_is_preediting())
3056 {
3057 int cmdpos = 0;
3058 int cmdspos;
3059 int old_row;
3060 int old_col;
3061 colnr_T col;
3062
3063 old_row = msg_row;
3064 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003065 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (has_mbyte)
3068 {
3069 for (col = 0; col < preedit_start_col
3070 && cmdpos < ccline.cmdlen; ++col)
3071 {
3072 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003073 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075 }
3076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 cmdspos += preedit_start_col;
3079 cmdpos += preedit_start_col;
3080 }
3081
3082 msg_row = cmdline_row + (cmdspos / (int)Columns);
3083 msg_col = cmdspos % (int)Columns;
3084 if (msg_row >= Rows)
3085 msg_row = Rows - 1;
3086
3087 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3088 {
3089 int char_len;
3090 int char_attr;
3091
3092 char_attr = im_get_feedback_attr(col);
3093 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003094 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003097 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 char_len = 1;
3100
3101 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3102 cmdpos += char_len;
3103 }
3104
3105 msg_row = old_row;
3106 msg_col = old_col;
3107 }
3108}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003109#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111/*
3112 * Allocate a new command line buffer.
3113 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
3115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003116alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 /*
3119 * give some extra space to avoid having to allocate all the time
3120 */
3121 if (len < 80)
3122 len = 100;
3123 else
3124 len += 20;
3125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003126 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 ccline.cmdbufflen = len;
3128}
3129
3130/*
3131 * Re-allocate the command line to length len + something extra.
3132 * return FAIL for failure, OK otherwise
3133 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003135realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 char_u *p;
3138
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003139 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003140 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003143 alloc_cmdbuff(len); // will get some more
3144 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003146 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 return FAIL;
3148 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003149 // There isn't always a NUL after the command, but it may need to be
3150 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003151 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3152 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003154
3155 if (ccline.xpc != NULL
3156 && ccline.xpc->xp_pattern != NULL
3157 && ccline.xpc->xp_context != EXPAND_NOTHING
3158 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3159 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003160 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003162 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3163 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003164 if (i >= 0 && i <= ccline.cmdlen)
3165 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3166 }
3167
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return OK;
3169}
3170
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003171#if defined(FEAT_ARABIC) || defined(PROTO)
3172static char_u *arshape_buf = NULL;
3173
3174# if defined(EXITFREE) || defined(PROTO)
3175 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003176free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003177{
3178 vim_free(arshape_buf);
3179}
3180# endif
3181#endif
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183/*
3184 * Draw part of the cmdline at the current cursor position. But draw stars
3185 * when cmdline_star is TRUE.
3186 */
3187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003188draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3191 int i;
3192
3193 if (cmdline_star > 0)
3194 for (i = 0; i < len; ++i)
3195 {
3196 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003198 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200 else
3201#endif
3202#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003203 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 static int buflen = 0;
3206 char_u *p;
3207 int j;
3208 int newlen = 0;
3209 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003210 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 int prev_c = 0;
3212 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003213 int u8c;
3214 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
3217 /*
3218 * Do arabic shaping into a temporary buffer. This is very
3219 * inefficient!
3220 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003221 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003223 // Re-allocate the buffer. We keep it around to avoid a lot of
3224 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003225 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003226 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003227 arshape_buf = alloc(buflen);
3228 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003232 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003234 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003235 arshape_buf[0] = ' ';
3236 newlen = 1;
3237 }
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 for (j = start; j < start + len; j += mb_l)
3240 {
3241 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003243 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (ARABIC_CHAR(u8c))
3245 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003246 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (cmdmsg_rl)
3248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003249 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 pc = prev_c;
3251 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003252 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (j + mb_l >= start + len)
3254 nc = NUL;
3255 else
3256 nc = utf_ptr2char(p + mb_l);
3257 }
3258 else
3259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003260 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (j + mb_l >= start + len)
3262 pc = NUL;
3263 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003264 {
3265 int pcc[MAX_MCO];
3266
3267 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003269 pc1 = pcc[0];
3270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 nc = prev_c;
3272 }
3273 prev_c = u8c;
3274
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003275 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003278 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003280 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3281 if (u8cc[1] != 0)
3282 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003283 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285 }
3286 else
3287 {
3288 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003289 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 newlen += mb_l;
3291 }
3292 }
3293
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003294 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else
3297#endif
3298 msg_outtrans_len(ccline.cmdbuff + start, len);
3299}
3300
3301/*
3302 * Put a character on the command line. Shifts the following text to the
3303 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3304 * "c" must be printable (fit in one display cell)!
3305 */
3306 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003307putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 if (cmd_silent)
3310 return;
3311 msg_no_more = TRUE;
3312 msg_putchar(c);
3313 if (shift)
3314 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3315 msg_no_more = FALSE;
3316 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003317 extra_char = c;
3318 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319}
3320
3321/*
3322 * Undo a putcmdline(c, FALSE).
3323 */
3324 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003325unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 if (cmd_silent)
3328 return;
3329 msg_no_more = TRUE;
3330 if (ccline.cmdlen == ccline.cmdpos)
3331 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003332 else if (has_mbyte)
3333 draw_cmdline(ccline.cmdpos,
3334 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 else
3336 draw_cmdline(ccline.cmdpos, 1);
3337 msg_no_more = FALSE;
3338 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003339 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340}
3341
3342/*
3343 * Put the given string, of the given length, onto the command line.
3344 * If len is -1, then STRLEN() is used to calculate the length.
3345 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3346 * part will be redrawn, otherwise it will not. If this function is called
3347 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3348 * called afterwards.
3349 */
3350 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003351put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 int retval;
3354 int i;
3355 int m;
3356 int c;
3357
3358 if (len < 0)
3359 len = (int)STRLEN(str);
3360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003361 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003363 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else
3365 retval = OK;
3366 if (retval == OK)
3367 {
3368 if (!ccline.overstrike)
3369 {
3370 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3371 ccline.cmdbuff + ccline.cmdpos,
3372 (size_t)(ccline.cmdlen - ccline.cmdpos));
3373 ccline.cmdlen += len;
3374 }
3375 else
3376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (has_mbyte)
3378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003379 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003381 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003383 // Count nr of bytes in cmdline that are overwritten by these
3384 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003386 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 --m;
3388 if (i < ccline.cmdlen)
3389 {
3390 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3391 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3392 ccline.cmdlen += ccline.cmdpos + len - i;
3393 }
3394 else
3395 ccline.cmdlen = ccline.cmdpos + len;
3396 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003397 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 ccline.cmdlen = ccline.cmdpos + len;
3399 }
3400 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3401 ccline.cmdbuff[ccline.cmdlen] = NUL;
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (enc_utf8)
3404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003405 // When the inserted text starts with a composing character,
3406 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 i = 0;
3408 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3409 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3410 {
3411 i = (*mb_head_off)(ccline.cmdbuff,
3412 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3413 ccline.cmdpos -= i;
3414 len += i;
3415 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3416 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003417#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003420 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 i = (*mb_head_off)(ccline.cmdbuff,
3422 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3423 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3424 + ccline.cmdpos - i), c))
3425 {
3426 ccline.cmdpos -= i;
3427 len += i;
3428 }
3429 else
3430 i = 0;
3431 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (i != 0)
3434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003435 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3437 ccline.cmdspos -= i;
3438 msg_col -= i;
3439 if (msg_col < 0)
3440 {
3441 msg_col += Columns;
3442 --msg_row;
3443 }
3444 }
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 if (redraw && !cmd_silent)
3448 {
3449 msg_no_more = TRUE;
3450 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003451 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003453 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (cmdline_row != i || ccline.overstrike)
3455 msg_clr_eos();
3456 msg_no_more = FALSE;
3457 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003458 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003460 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003461 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003463 }
3464 else
3465 m = MAXCOL;
3466 for (i = 0; i < len; ++i)
3467 {
3468 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003469 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003470 if (has_mbyte)
3471 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003472 // Stop cursor at the end of the screen, but do increment the
3473 // insert position, so that entering a very long command
3474 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003475 if (ccline.cmdspos + c < m)
3476 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003477
Bram Moolenaar14184a32019-02-16 15:10:30 +01003478 if (has_mbyte)
3479 {
3480 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3481 if (c > len - i - 1)
3482 c = len - i - 1;
3483 ccline.cmdpos += c;
3484 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003486 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 }
3489 if (redraw)
3490 msg_check();
3491 return retval;
3492}
3493
Bram Moolenaar66b51422019-08-18 21:44:12 +02003494static cmdline_info_T prev_ccline;
3495static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003496
3497/*
3498 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3499 * and overwrite it. But get_cmdline_str() may need it, thus make it
3500 * available globally in prev_ccline.
3501 */
3502 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003503save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003504{
3505 if (!prev_ccline_used)
3506 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003508 prev_ccline_used = TRUE;
3509 }
3510 *ccp = prev_ccline;
3511 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003512 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003513}
3514
3515/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003516 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003517 */
3518 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003520{
3521 ccline = prev_ccline;
3522 prev_ccline = *ccp;
3523}
3524
Bram Moolenaar8299df92004-07-10 09:47:34 +00003525/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003526 * Paste a yank register into the command line.
3527 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003528 * insert_reg() can't be used here, because special characters from the
3529 * register contents will be interpreted as commands.
3530 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003531 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003532 */
3533 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003534cmdline_paste(
3535 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003536 int literally, // Insert text literally instead of "as typed"
3537 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003538{
3539 long i;
3540 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003541 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003542 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003544 // check for valid regname; also accept special characters for CTRL-R in
3545 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003546 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003547 && regname != Ctrl_A && regname != Ctrl_L
3548 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003549 return FAIL;
3550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003551 // A register containing CTRL-R can cause an endless loop. Allow using
3552 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003553 line_breakcheck();
3554 if (got_int)
3555 return FAIL;
3556
3557#ifdef FEAT_CLIPBOARD
3558 regname = may_get_selection(regname);
3559#endif
3560
Bram Moolenaar438d1762018-09-30 17:11:48 +02003561 // Need to set "textlock" to avoid nasty things like going to another
3562 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003563 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003564 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003565 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003566
3567 if (i)
3568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003569 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003570 if (arg == NULL)
3571 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003573 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3574 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003575 p = arg;
3576 if (p_is && regname == Ctrl_W)
3577 {
3578 char_u *w;
3579 int len;
3580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003581 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003582 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003583 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003584 if (has_mbyte)
3585 {
3586 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3587 if (!vim_iswordc(mb_ptr2char(w - len)))
3588 break;
3589 w -= len;
3590 }
3591 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003592 {
3593 if (!vim_iswordc(w[-1]))
3594 break;
3595 --w;
3596 }
3597 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003598 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003599 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3600 p += len;
3601 }
3602
3603 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604 if (allocated)
3605 vim_free(arg);
3606 return OK;
3607 }
3608
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003609 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610}
3611
3612/*
3613 * Put a string on the command line.
3614 * When "literally" is TRUE, insert literally.
3615 * When "literally" is FALSE, insert as typed, but don't leave the command
3616 * line.
3617 */
3618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003619cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003620{
3621 int c, cv;
3622
3623 if (literally)
3624 put_on_cmdline(s, -1, TRUE);
3625 else
3626 while (*s != NUL)
3627 {
3628 cv = *s;
3629 if (cv == Ctrl_V && s[1])
3630 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003631 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003632 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003633 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003634 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003635 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3636 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003637#ifdef UNIX
3638 || c == intr_char
3639#endif
3640 || (c == Ctrl_BSL && *s == Ctrl_N))
3641 stuffcharReadbuff(Ctrl_V);
3642 stuffcharReadbuff(c);
3643 }
3644}
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646#ifdef FEAT_WILDMENU
3647/*
3648 * Delete characters on the command line, from "from" to the current
3649 * position.
3650 */
3651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003652cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3655 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3656 ccline.cmdlen -= ccline.cmdpos - from;
3657 ccline.cmdpos = from;
3658}
3659#endif
3660
3661/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003662 * This function is called when the screen size changes and with incremental
3663 * search and in other situations where the command line may have been
3664 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003667redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003669 redrawcmdline_ex(TRUE);
3670}
3671
3672 void
3673redrawcmdline_ex(int do_compute_cmdrow)
3674{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (cmd_silent)
3676 return;
3677 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003678 if (do_compute_cmdrow)
3679 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 redrawcmd();
3681 cursorcmd();
3682}
3683
3684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003685redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 int i;
3688
3689 if (cmd_silent)
3690 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003691 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 msg_putchar(ccline.cmdfirstc);
3693 if (ccline.cmdprompt != NULL)
3694 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003695 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003698 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 --ccline.cmdindent;
3700 }
3701 else
3702 for (i = ccline.cmdindent; i > 0; --i)
3703 msg_putchar(' ');
3704}
3705
3706/*
3707 * Redraw what is currently on the command line.
3708 */
3709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003710redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 if (cmd_silent)
3713 return;
3714
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003715 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003716 if (ccline.cmdbuff == NULL)
3717 {
3718 windgoto(cmdline_row, 0);
3719 msg_clr_eos();
3720 return;
3721 }
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 msg_start();
3724 redrawcmdprompt();
3725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003726 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 msg_no_more = TRUE;
3728 draw_cmdline(0, ccline.cmdlen);
3729 msg_clr_eos();
3730 msg_no_more = FALSE;
3731
3732 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003733 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003734 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 /*
3737 * An emsg() before may have set msg_scroll. This is used in normal mode,
3738 * in cmdline mode we can reset them now.
3739 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003740 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003742 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3743 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 skip_redraw = FALSE;
3745}
3746
3747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003748compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003750 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 cmdline_row = Rows - 1;
3752 else
3753 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003754 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755}
3756
Bram Moolenaar66b51422019-08-18 21:44:12 +02003757 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003758cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
3760 if (cmd_silent)
3761 return;
3762
3763#ifdef FEAT_RIGHTLEFT
3764 if (cmdmsg_rl)
3765 {
3766 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3767 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3768 if (msg_row <= 0)
3769 msg_row = Rows - 1;
3770 }
3771 else
3772#endif
3773 {
3774 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3775 msg_col = ccline.cmdspos % (int)Columns;
3776 if (msg_row >= Rows)
3777 msg_row = Rows - 1;
3778 }
3779
3780 windgoto(msg_row, msg_col);
3781#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003782 if (p_imst == IM_ON_THE_SPOT)
3783 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784#endif
3785#ifdef MCH_CURSOR_SHAPE
3786 mch_update_cursor();
3787#endif
3788}
3789
3790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003791gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
3793 msg_start();
3794#ifdef FEAT_RIGHTLEFT
3795 if (cmdmsg_rl)
3796 msg_col = Columns - 1;
3797 else
3798#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003799 msg_col = 0; // always start in column 0
3800 if (clr) // clear the bottom line(s)
3801 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 windgoto(cmdline_row, 0);
3803}
3804
3805/*
3806 * Check the word in front of the cursor for an abbreviation.
3807 * Called when the non-id character "c" has been entered.
3808 * When an abbreviation is recognized it is removed from the text with
3809 * backspaces and the replacement string is inserted, followed by "c".
3810 */
3811 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003812ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003814 int spos = 0;
3815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003816 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 return FALSE;
3818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003819 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3820 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003821 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3822 spos++;
3823 if (ccline.cmdlen - spos > 5
3824 && ccline.cmdbuff[spos] == '\''
3825 && ccline.cmdbuff[spos + 2] == ','
3826 && ccline.cmdbuff[spos + 3] == '\'')
3827 spos += 5;
3828 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003829 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003830 spos = 0;
3831
3832 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833}
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003836 * Escape special characters in "fname" for when used as a file name argument
3837 * after a Vim command, or, when "shell" is non-zero, a shell command.
3838 * Returns the result in allocated memory.
3839 */
3840 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003841vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003842{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003843 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003844#ifdef BACKSLASH_IN_FILENAME
3845 char_u buf[20];
3846 int j = 0;
3847
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003848 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003849 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003850 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003851 buf[j++] = *p;
3852 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003853 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003854#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003855 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3856 if (shell && csh_like_shell() && p != NULL)
3857 {
3858 char_u *s;
3859
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003860 // For csh and similar shells need to put two backslashes before '!'.
3861 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003862 s = vim_strsave_escaped(p, (char_u *)"!");
3863 vim_free(p);
3864 p = s;
3865 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003866#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003868 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3869 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003870 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003871 escape_fname(&p);
3872
3873 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003874}
3875
3876/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003877 * Put a backslash before the file name in "pp", which is in allocated memory.
3878 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003880escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003881{
3882 char_u *p;
3883
Bram Moolenaar964b3742019-05-24 18:54:09 +02003884 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003885 if (p != NULL)
3886 {
3887 p[0] = '\\';
3888 STRCPY(p + 1, *pp);
3889 vim_free(*pp);
3890 *pp = p;
3891 }
3892}
3893
3894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 * For each file name in files[num_files]:
3896 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3897 */
3898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003899tilde_replace(
3900 char_u *orig_pat,
3901 int num_files,
3902 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 int i;
3905 char_u *p;
3906
3907 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3908 {
3909 for (i = 0; i < num_files; ++i)
3910 {
3911 p = home_replace_save(NULL, files[i]);
3912 if (p != NULL)
3913 {
3914 vim_free(files[i]);
3915 files[i] = p;
3916 }
3917 }
3918 }
3919}
3920
3921/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003922 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003924 cmdline_info_T *
3925get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003927 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928}
3929
Bram Moolenaar064154c2016-03-19 22:50:43 +01003930#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003931/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003932 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003933 * ccline and put the previous value in prev_ccline.
3934 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003935 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003936get_ccline_ptr(void)
3937{
3938 if ((State & CMDLINE) == 0)
3939 return NULL;
3940 if (ccline.cmdbuff != NULL)
3941 return &ccline;
3942 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3943 return &prev_ccline;
3944 return NULL;
3945}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003946#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003947
Bram Moolenaar064154c2016-03-19 22:50:43 +01003948#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003949/*
3950 * Get the current command line in allocated memory.
3951 * Only works when the command line is being edited.
3952 * Returns NULL when something is wrong.
3953 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003954 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003955get_cmdline_str(void)
3956{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003957 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003958
Bram Moolenaaree91c332018-09-25 22:27:35 +02003959 if (cmdline_star > 0)
3960 return NULL;
3961 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003962 if (p == NULL)
3963 return NULL;
3964 return vim_strnsave(p->cmdbuff, p->cmdlen);
3965}
3966
3967/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003968 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003969 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003970 void
3971f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3972{
3973 rettv->v_type = VAR_STRING;
3974 rettv->vval.v_string = get_cmdline_str();
3975}
3976
3977/*
3978 * "getcmdpos()" function
3979 */
3980 void
3981f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003982{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003983 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003984
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003985 rettv->vval.v_number = 0;
3986 if (p != NULL)
3987 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003988}
3989
3990/*
3991 * Set the command line byte position to "pos". Zero is the first position.
3992 * Only works when the command line is being edited.
3993 * Returns 1 when failed, 0 when OK.
3994 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003995 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003996set_cmdline_pos(
3997 int pos)
3998{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003999 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004000
4001 if (p == NULL)
4002 return 1;
4003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004004 // The position is not set directly but after CTRL-\ e or CTRL-R = has
4005 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004006 if (pos < 0)
4007 new_cmdpos = 0;
4008 else
4009 new_cmdpos = pos;
4010 return 0;
4011}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004012
4013/*
4014 * "setcmdpos()" function
4015 */
4016 void
4017f_setcmdpos(typval_T *argvars, typval_T *rettv)
4018{
4019 int pos = (int)tv_get_number(&argvars[0]) - 1;
4020
4021 if (pos >= 0)
4022 rettv->vval.v_number = set_cmdline_pos(pos);
4023}
4024
4025/*
4026 * "getcmdtype()" function
4027 */
4028 void
4029f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4030{
4031 rettv->v_type = VAR_STRING;
4032 rettv->vval.v_string = alloc(2);
4033 if (rettv->vval.v_string != NULL)
4034 {
4035 rettv->vval.v_string[0] = get_cmdline_type();
4036 rettv->vval.v_string[1] = NUL;
4037 }
4038}
4039
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004040#endif
4041
4042#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4043/*
4044 * Get the current command-line type.
4045 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4046 * Only works when the command line is being edited.
4047 * Returns NUL when something is wrong.
4048 */
4049 int
4050get_cmdline_type(void)
4051{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004052 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004053
4054 if (p == NULL)
4055 return NUL;
4056 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004057 return
4058# ifdef FEAT_EVAL
4059 (p->input_fn) ? '@' :
4060# endif
4061 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004062 return p->cmdfirstc;
4063}
4064#endif
4065
Bram Moolenaard7663c22019-08-06 21:59:57 +02004066/*
4067 * Return the first character of the current command line.
4068 */
4069 int
4070get_cmdline_firstc(void)
4071{
4072 return ccline.cmdfirstc;
4073}
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075/*
4076 * Get indices "num1,num2" that specify a range within a list (not a range of
4077 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4078 * Returns OK if parsed successfully, otherwise FAIL.
4079 */
4080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004081get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 int len;
4084 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004085 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004088 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004090 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 *str += len;
4092 *num1 = (int)num;
4093 first = TRUE;
4094 }
4095 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004096 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
4098 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004099 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (len > 0)
4101 {
4102 *num2 = (int)num;
4103 *str = skipwhite(*str + len);
4104 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004105 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 return FAIL;
4107 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004108 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 *num2 = *num1;
4110 return OK;
4111}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#if defined(FEAT_CMDWIN) || defined(PROTO)
4114/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004115 * Check value of 'cedit' and set cedit_key.
4116 * Returns NULL if value is OK, error message otherwise.
4117 */
4118 char *
4119check_cedit(void)
4120{
4121 int n;
4122
4123 if (*p_cedit == NUL)
4124 cedit_key = -1;
4125 else
4126 {
4127 n = string_to_key(p_cedit, FALSE);
4128 if (vim_isprintc(n))
4129 return e_invarg;
4130 cedit_key = n;
4131 }
4132 return NULL;
4133}
4134
4135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 * Open a window on the current command line and history. Allow editing in
4137 * the window. Returns when the window is closed.
4138 * Returns:
4139 * CR if the command is to be executed
4140 * Ctrl_C if it is to be abandoned
4141 * K_IGNORE if editing continues
4142 */
4143 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004144open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004146 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004148 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 win_T *wp;
4150 int i;
4151 linenr_T lnum;
4152 int histtype;
4153 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int save_restart_edit = restart_edit;
4155 int save_State = State;
4156 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004157#ifdef FEAT_RIGHTLEFT
4158 int save_cmdmsg_rl = cmdmsg_rl;
4159#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004160#ifdef FEAT_FOLDING
4161 int save_KeyTyped;
4162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004164 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (cmdwin_type != 0
4166# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4167 || cmdline_star > 0
4168# endif
4169 )
4170 {
4171 beep_flush();
4172 return K_IGNORE;
4173 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004174 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004176 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 win_size_save(&winsizes);
4178
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004179 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4180 // command line window, but we don't want the popup menu then.
4181 pum_undisplay();
4182
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004183 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004184 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004185 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004186
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004187 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4189 {
4190 beep_flush();
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004191 ga_clear(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return K_IGNORE;
4193 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004194 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004196 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004197 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004198 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004199 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004200 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004203#ifdef FEAT_FOLDING
4204 curwin->w_p_fen = FALSE;
4205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004207 curwin->w_p_rl = cmdmsg_rl;
4208 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004210 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004212 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004213 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004215 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004216 need_wait_return = FALSE;
4217
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004218 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4220 {
4221 if (p_wc == TAB)
4222 {
4223 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4224 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4225 }
4226 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4227 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004228 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004230 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4231 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004232 curbuf->b_p_tw = 0;
4233
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004234 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004236 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004238 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (i >= 0)
4240 {
4241 lnum = 0;
4242 do
4243 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004244 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004246 if (get_histentry(histtype)[i].hisstr != NULL)
4247 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 (colnr_T)0, FALSE);
4249 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004250 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 }
4253
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004254 // Replace the empty last line with the current command-line and put the
4255 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4257 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4258 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004259 changed_line_abv_curs();
4260 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004261 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaar23324a02019-10-01 17:39:04 +02004263 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 exmode_active = 0;
4265
4266 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaar23324a02019-10-01 17:39:04 +02004269 // Reset here so it can be set by a CmdWinEnter autocommand.
4270 cmdwin_result = 0;
4271
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004272 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004273 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004274 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004275 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 i = RedrawingDisabled;
4278 RedrawingDisabled = 0;
4279
4280 /*
4281 * Call the main loop until <CR> or CTRL-C is typed.
4282 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004283 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
4285 RedrawingDisabled = i;
4286
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004287# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004288 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004289# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004290
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004291 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004292 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004293
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004294# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004295 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004296 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297# endif
4298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 exmode_active = save_exmode;
4301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004302 // Safety check: The old window or buffer was deleted: It's a bug when
4303 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004304 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
4306 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004307 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 else
4310 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004311# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004312 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (aborting() && cmdwin_result != K_IGNORE)
4314 cmdwin_result = Ctrl_C;
4315# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004320 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4321
4322 if (histtype == HIST_CMD)
4323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004325 ccline.cmdbuff = vim_strsave((char_u *)p);
4326 cmdwin_result = CAR;
4327 }
4328 else
4329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004330 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004331 ccline.cmdbuff = NULL;
4332 stuffcharReadbuff(':');
4333 stuffReadbuff((char_u *)p);
4334 stuffcharReadbuff(CAR);
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
4339 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4340 cmdwin_result = CAR;
4341 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004342 else if (cmdwin_result == Ctrl_C)
4343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004344 // :q or :close, don't execute any command
4345 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004346 ccline.cmdbuff = NULL;
4347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 else
4349 ccline.cmdbuff = vim_strsave(ml_get_curline());
4350 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004351 {
4352 ccline.cmdbuff = vim_strsave((char_u *)"");
4353 ccline.cmdlen = 0;
4354 ccline.cmdbufflen = 1;
4355 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 else
4359 {
4360 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4361 ccline.cmdbufflen = ccline.cmdlen + 1;
4362 ccline.cmdpos = curwin->w_cursor.col;
4363 if (ccline.cmdpos > ccline.cmdlen)
4364 ccline.cmdpos = ccline.cmdlen;
4365 if (cmdwin_result == K_IGNORE)
4366 {
4367 set_cmdspos_cursor();
4368 redrawcmd();
4369 }
4370 }
4371
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004372# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004373 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004374 curwin->w_p_cole = 0;
4375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004377 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 win_goto(old_curwin);
4379 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004380
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004381 // win_close() may have already wiped the buffer when 'bh' is
4382 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004383 if (bufref_valid(&bufref))
Bram Moolenaara6e8f882019-12-14 16:18:15 +01004384 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004386 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389
4390 ga_clear(&winsizes);
4391 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004392# ifdef FEAT_RIGHTLEFT
4393 cmdmsg_rl = save_cmdmsg_rl;
4394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 return cmdwin_result;
4400}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004401#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403/*
4404 * Used for commands that either take a simple command string argument, or:
4405 * cmd << endmarker
4406 * {script}
4407 * endmarker
4408 * Returns a pointer to allocated memory with {script} or NULL.
4409 */
4410 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004411script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 char_u *theline;
4414 char *end_pattern = NULL;
4415 char dot[] = ".";
4416 garray_T ga;
4417
4418 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4419 return NULL;
4420
4421 ga_init2(&ga, 1, 0x400);
4422
4423 if (cmd[2] != NUL)
4424 end_pattern = (char *)skipwhite(cmd + 2);
4425 else
4426 end_pattern = dot;
4427
4428 for (;;)
4429 {
4430 theline = eap->getline(
4431#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004432 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004434 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004437 {
4438 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 ga_concat(&ga, theline);
4443 ga_append(&ga, '\n');
4444 vim_free(theline);
4445 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004446 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 return (char_u *)ga.ga_data;
4449}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004450
4451#if defined(FEAT_EVAL) || defined(PROTO)
4452/*
4453 * This function is used by f_input() and f_inputdialog() functions. The third
4454 * argument to f_input() specifies the type of completion to use at the
4455 * prompt. The third argument to f_inputdialog() specifies the value to return
4456 * when the user cancels the prompt.
4457 */
4458 void
4459get_user_input(
4460 typval_T *argvars,
4461 typval_T *rettv,
4462 int inputdialog,
4463 int secret)
4464{
4465 char_u *prompt = tv_get_string_chk(&argvars[0]);
4466 char_u *p = NULL;
4467 int c;
4468 char_u buf[NUMBUFLEN];
4469 int cmd_silent_save = cmd_silent;
4470 char_u *defstr = (char_u *)"";
4471 int xp_type = EXPAND_NOTHING;
4472 char_u *xp_arg = NULL;
4473
4474 rettv->v_type = VAR_STRING;
4475 rettv->vval.v_string = NULL;
4476
4477#ifdef NO_CONSOLE_INPUT
4478 // While starting up, there is no place to enter text. When running tests
4479 // with --not-a-term we assume feedkeys() will be used.
4480 if (no_console_input() && !is_not_a_term())
4481 return;
4482#endif
4483
4484 cmd_silent = FALSE; // Want to see the prompt.
4485 if (prompt != NULL)
4486 {
4487 // Only the part of the message after the last NL is considered as
4488 // prompt for the command line
4489 p = vim_strrchr(prompt, '\n');
4490 if (p == NULL)
4491 p = prompt;
4492 else
4493 {
4494 ++p;
4495 c = *p;
4496 *p = NUL;
4497 msg_start();
4498 msg_clr_eos();
4499 msg_puts_attr((char *)prompt, get_echo_attr());
4500 msg_didout = FALSE;
4501 msg_starthere();
4502 *p = c;
4503 }
4504 cmdline_row = msg_row;
4505
4506 if (argvars[1].v_type != VAR_UNKNOWN)
4507 {
4508 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4509 if (defstr != NULL)
4510 stuffReadbuffSpec(defstr);
4511
4512 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4513 {
4514 char_u *xp_name;
4515 int xp_namelen;
4516 long argt;
4517
4518 // input() with a third argument: completion
4519 rettv->vval.v_string = NULL;
4520
4521 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4522 if (xp_name == NULL)
4523 return;
4524
4525 xp_namelen = (int)STRLEN(xp_name);
4526
4527 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4528 &xp_arg) == FAIL)
4529 return;
4530 }
4531 }
4532
4533 if (defstr != NULL)
4534 {
4535 int save_ex_normal_busy = ex_normal_busy;
4536
4537 ex_normal_busy = 0;
4538 rettv->vval.v_string =
4539 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4540 xp_type, xp_arg);
4541 ex_normal_busy = save_ex_normal_busy;
4542 }
4543 if (inputdialog && rettv->vval.v_string == NULL
4544 && argvars[1].v_type != VAR_UNKNOWN
4545 && argvars[2].v_type != VAR_UNKNOWN)
4546 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4547 &argvars[2], buf));
4548
4549 vim_free(xp_arg);
4550
4551 // since the user typed this, no need to wait for return
4552 need_wait_return = FALSE;
4553 msg_didout = FALSE;
4554 }
4555 cmd_silent = cmd_silent_save;
4556}
4557#endif