blob: 25f409b743279ba43c1c25add979c09807a83c5c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar438d1762018-09-30 17:11:48 +020020// The current cmdline_info. It is initialized in getcmdline() and after that
21// used by other functions. When invoking getcmdline() recursively it needs
22// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int new_cmdpos; // position set by set_cmdline_pos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
Bram Moolenaar217e1b82019-12-01 21:41:28 +010029static int extra_char = NUL; // extra character to display when redrawing
30 // the command line
Bram Moolenaar6a77d262017-07-16 15:24:01 +020031static int extra_char_shift;
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +010034static int cmd_hkmap = 0; // Hebrew mapping during command line
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
Bram Moolenaar438d1762018-09-30 17:11:48 +020037static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010038static int cmdline_charsize(int idx);
39static void set_cmdspos(void);
40static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020044static void save_cmdline(cmdline_info_T *ccp);
45static void restore_cmdline(cmdline_info_T *ccp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010050static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010051static int ccheck_abbr(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +020054static int open_cmdwin(void);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +010055
56static int cedit_key INIT(= -1); // key value of 'cedit' option
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020059
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020060 static void
61trigger_cmd_autocmd(int typechar, int evt)
62{
63 char_u typestr[2];
64
65 typestr[0] = typechar;
66 typestr[1] = NUL;
67 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
68}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020071 * Abandon the command line.
72 */
73 static void
74abandon_cmdline(void)
75{
Bram Moolenaard23a8232018-02-10 18:45:26 +010076 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020077 if (msg_scrolled == 0)
78 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010079 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020080 redraw_cmdline = TRUE;
81}
82
Bram Moolenaaree219b02017-12-17 14:55:01 +010083#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020084/*
Bram Moolenaar66216052017-12-16 16:33:44 +010085 * Guess that the pattern matches everything. Only finds specific cases, such
86 * as a trailing \|, which can happen while typing a pattern.
87 */
88 static int
89empty_pattern(char_u *p)
90{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010091 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010092
Bram Moolenaar217e1b82019-12-01 21:41:28 +010093 // remove trailing \v and the like
Bram Moolenaar66216052017-12-16 16:33:44 +010094 while (n >= 2 && p[n - 2] == '\\'
95 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
96 n -= 2;
97 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
98}
99
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200100// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200101typedef struct {
102 colnr_T vs_curswant;
103 colnr_T vs_leftcol;
104 linenr_T vs_topline;
105# ifdef FEAT_DIFF
106 int vs_topfill;
107# endif
108 linenr_T vs_botline;
109 linenr_T vs_empty_rows;
110} viewstate_T;
111
112 static void
113save_viewstate(viewstate_T *vs)
114{
115 vs->vs_curswant = curwin->w_curswant;
116 vs->vs_leftcol = curwin->w_leftcol;
117 vs->vs_topline = curwin->w_topline;
118# ifdef FEAT_DIFF
119 vs->vs_topfill = curwin->w_topfill;
120# endif
121 vs->vs_botline = curwin->w_botline;
122 vs->vs_empty_rows = curwin->w_empty_rows;
123}
124
125 static void
126restore_viewstate(viewstate_T *vs)
127{
128 curwin->w_curswant = vs->vs_curswant;
129 curwin->w_leftcol = vs->vs_leftcol;
130 curwin->w_topline = vs->vs_topline;
131# ifdef FEAT_DIFF
132 curwin->w_topfill = vs->vs_topfill;
133# endif
134 curwin->w_botline = vs->vs_botline;
135 curwin->w_empty_rows = vs->vs_empty_rows;
136}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200137
138// Struct to store the state of 'incsearch' highlighting.
139typedef struct {
140 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200141 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200142 viewstate_T init_viewstate;
143 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200144 pos_T match_start;
145 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200146 int did_incsearch;
147 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200148 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200149} incsearch_state_T;
150
151 static void
152init_incsearch_state(incsearch_state_T *is_state)
153{
154 is_state->match_start = curwin->w_cursor;
155 is_state->did_incsearch = FALSE;
156 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200157 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200158 CLEAR_POS(&is_state->match_end);
159 is_state->save_cursor = curwin->w_cursor; // may be restored later
160 is_state->search_start = curwin->w_cursor;
161 save_viewstate(&is_state->init_viewstate);
162 save_viewstate(&is_state->old_viewstate);
163}
164
165/*
166 * First move cursor to end of match, then to the start. This
167 * moves the whole match onto the screen when 'nowrap' is set.
168 */
169 static void
170set_search_match(pos_T *t)
171{
172 t->lnum += search_match_lines;
173 t->col = search_match_endcol;
174 if (t->lnum > curbuf->b_ml.ml_line_count)
175 {
176 t->lnum = curbuf->b_ml.ml_line_count;
177 coladvance((colnr_T)MAXCOL);
178 }
179}
180
181/*
182 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200183 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200184 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200185 */
186 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200187do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
188 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200189{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200190 char_u *cmd;
191 cmdmod_T save_cmdmod = cmdmod;
192 char_u *p;
193 int delim_optional = FALSE;
194 int delim;
195 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100196 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200197 exarg_T ea;
198 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200199 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100200 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200201
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200202 *skiplen = 0;
203 *patlen = ccline.cmdlen;
204
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200205 if (!p_is || cmd_silent)
206 return FALSE;
207
208 // by default search all lines
209 search_first_line = 0;
210 search_last_line = MAXLNUM;
211
212 if (firstc == '/' || firstc == '?')
213 return TRUE;
214 if (firstc != ':')
215 return FALSE;
216
Bram Moolenaarc6725252019-11-23 21:56:46 +0100217 ++emsg_off;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200218 vim_memset(&ea, 0, sizeof(ea));
219 ea.line1 = 1;
220 ea.line2 = 1;
221 ea.cmd = ccline.cmdbuff;
222 ea.addr_type = ADDR_LINES;
223
224 parse_command_modifiers(&ea, &dummy, TRUE);
225 cmdmod = save_cmdmod;
226
227 cmd = skip_range(ea.cmd, NULL);
228 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100229 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200230
231 // Skip over "substitute" to find the pattern separator.
232 for (p = cmd; ASCII_ISALPHA(*p); ++p)
233 ;
234 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100235 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
237 if (STRNCMP(cmd, "substitute", p - cmd) == 0
238 || STRNCMP(cmd, "smagic", p - cmd) == 0
239 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
240 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200241 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200242 if (*cmd == 's' && cmd[1] == 'm')
243 p_magic = TRUE;
244 else if (*cmd == 's' && cmd[1] == 'n')
245 p_magic = FALSE;
246 }
247 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
248 {
249 // skip over flags
250 while (ASCII_ISALPHA(*(p = skipwhite(p))))
251 ++p;
252 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100253 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200254 }
255 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
256 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
257 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
258 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
259 || STRNCMP(cmd, "global", p - cmd) == 0)
260 {
261 // skip over "!"
262 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200263 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200264 p++;
265 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100266 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200267 }
268 if (*cmd != 'g')
269 delim_optional = TRUE;
270 }
271 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100272 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200274 p = skipwhite(p);
275 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
276 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200277
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200278 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200279
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200280 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100281 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200282
283 // Don't do 'hlsearch' highlighting if the pattern matches everything.
284 if (!use_last_pat)
285 {
286 char c = *end;
287 int empty;
288
289 *end = NUL;
290 empty = empty_pattern(p);
291 *end = c;
292 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100293 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200294 }
295
296 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200297 *skiplen = (int)(p - ccline.cmdbuff);
298 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200299
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200300 // parse the address range
301 save_cursor = curwin->w_cursor;
302 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200303 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200304 if (ea.addr_count > 0)
305 {
306 // Allow for reverse match.
307 if (ea.line2 < ea.line1)
308 {
309 search_first_line = ea.line2;
310 search_last_line = ea.line1;
311 }
312 else
313 {
314 search_first_line = ea.line1;
315 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200316 }
317 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200318 else if (cmd[0] == 's' && cmd[1] != 'o')
319 {
320 // :s defaults to the current line
321 search_first_line = curwin->w_cursor.lnum;
322 search_last_line = curwin->w_cursor.lnum;
323 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200324
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200325 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100326 retval = TRUE;
327theend:
328 --emsg_off;
329 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200330}
331
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200332 static void
333finish_incsearch_highlighting(
334 int gotesc,
335 incsearch_state_T *is_state,
336 int call_update_screen)
337{
338 if (is_state->did_incsearch)
339 {
340 is_state->did_incsearch = FALSE;
341 if (gotesc)
342 curwin->w_cursor = is_state->save_cursor;
343 else
344 {
345 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
346 {
347 // put the '" mark at the original position
348 curwin->w_cursor = is_state->save_cursor;
349 setpcmark();
350 }
351 curwin->w_cursor = is_state->search_start;
352 }
353 restore_viewstate(&is_state->old_viewstate);
354 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200355
356 // by default search all lines
357 search_first_line = 0;
358 search_last_line = MAXLNUM;
359
360 p_magic = is_state->magic_save;
361
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100362 validate_cursor(); // needed for TAB
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200363 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200364 if (call_update_screen)
365 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200366 }
367}
368
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200369/*
370 * Do 'incsearch' highlighting if desired.
371 */
372 static void
373may_do_incsearch_highlighting(
374 int firstc,
375 long count,
376 incsearch_state_T *is_state)
377{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200378 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200379 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200380 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200381#ifdef FEAT_RELTIME
382 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200383 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200385 int next_char;
386 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200387 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200388
Bram Moolenaar198cb662018-09-06 21:44:17 +0200389 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100390 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200391 save_last_search_pattern();
392
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200393 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200394 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200395 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200396 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200397 if (did_do_incsearch && vpeekc() == NUL)
398 // may have skipped a redraw, do it now
399 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200400 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200401 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402
403 // If there is a character waiting, search and redraw later.
404 if (char_avail())
405 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200406 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407 is_state->incsearch_postponed = TRUE;
408 return;
409 }
410 is_state->incsearch_postponed = FALSE;
411
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200412 if (search_first_line == 0)
413 // start at the original cursor position
414 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100415 else if (search_first_line > curbuf->b_ml.ml_line_count)
416 {
417 // start after the last line
418 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
419 curwin->w_cursor.col = MAXCOL;
420 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200421 else
422 {
423 // start at the first line in the range
424 curwin->w_cursor.lnum = search_first_line;
425 curwin->w_cursor.col = 0;
426 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200427
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200428 // Use the previous pattern for ":s//".
429 next_char = ccline.cmdbuff[skiplen + patlen];
430 use_last_pat = patlen == 0 && skiplen > 0
431 && ccline.cmdbuff[skiplen - 1] == next_char;
432
433 // If there is no pattern, don't do anything.
434 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200435 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200436 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200437 set_no_hlsearch(TRUE); // turn off previous highlight
438 redraw_all_later(SOME_VALID);
439 }
440 else
441 {
442 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
443
444 cursor_off(); // so the user knows we're busy
445 out_flush();
446 ++emsg_off; // so it doesn't beep if bad expr
447#ifdef FEAT_RELTIME
448 // Set the time limit to half a second.
449 profile_setlimit(500L, &tm);
450#endif
451 if (!p_hls)
452 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200453 if (search_first_line != 0)
454 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200456#ifdef FEAT_RELTIME
457 vim_memset(&sia, 0, sizeof(sia));
458 sia.sa_tm = &tm;
459#endif
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200460 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200463 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200465 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466#endif
467 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469 --emsg_off;
470
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200471 if (curwin->w_cursor.lnum < search_first_line
472 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200473 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200474 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200475 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200476 curwin->w_cursor = is_state->search_start;
477 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200478
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 // if interrupted while searching, behave like it failed
480 if (got_int)
481 {
482 (void)vpeekc(); // remove <C-C> from input stream
483 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200484 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200485 }
486 else if (char_avail())
487 // cancelled searching because a char was typed
488 is_state->incsearch_postponed = TRUE;
489 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200490 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200491 highlight_match = TRUE; // highlight position
492 else
493 highlight_match = FALSE; // remove highlight
494
495 // First restore the old curwin values, so the screen is positioned in the
496 // same way as the actual search command.
497 restore_viewstate(&is_state->old_viewstate);
498 changed_cline_bef_curs();
499 update_topline();
500
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200501 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 {
503 pos_T save_pos = curwin->w_cursor;
504
505 is_state->match_start = curwin->w_cursor;
506 set_search_match(&curwin->w_cursor);
507 validate_cursor();
508 end_pos = curwin->w_cursor;
509 is_state->match_end = end_pos;
510 curwin->w_cursor = save_pos;
511 }
512 else
513 end_pos = curwin->w_cursor; // shutup gcc 4
514
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200515 // Disable 'hlsearch' highlighting if the pattern matches everything.
516 // Avoids a flash when typing "foo\|".
517 if (!use_last_pat)
518 {
519 next_char = ccline.cmdbuff[skiplen + patlen];
520 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200521 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
522 {
523 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200524 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200525 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200526 ccline.cmdbuff[skiplen + patlen] = next_char;
527 }
528
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 validate_cursor();
530 // May redraw the status line to show the cursor position.
531 if (p_ru && curwin->w_status_height > 0)
532 curwin->w_redr_status = TRUE;
533
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200535 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200536 restore_last_search_pattern();
537
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200538 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
539 // end of the pattern, e.g. for ":s/pat/".
540 if (ccline.cmdbuff[skiplen + patlen] != NUL)
541 curwin->w_cursor = is_state->search_start;
542 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200543 curwin->w_cursor = end_pos;
544
545 msg_starthere();
546 redrawcmdline();
547 is_state->did_incsearch = TRUE;
548}
549
550/*
551 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
552 * or previous match.
553 * Returns FAIL when jumping to cmdline_not_changed;
554 */
555 static int
556may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200557 int firstc,
558 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563 pos_T t;
564 char_u *pat;
565 int search_flags = SEARCH_NOOF;
566 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568
Bram Moolenaar198cb662018-09-06 21:44:17 +0200569 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100570 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200571 save_last_search_pattern();
572
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200574 {
575 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200577 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200578 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200579 {
580 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200581 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200582 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200583
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200585 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200586 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200587 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200588 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200589 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200590 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200591 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 cursor_off();
594 out_flush();
595 if (c == Ctrl_G)
596 {
597 t = is_state->match_end;
598 if (LT_POS(is_state->match_start, is_state->match_end))
599 // Start searching at the end of the match not at the beginning of
600 // the next column.
601 (void)decl(&t);
602 search_flags += SEARCH_COL;
603 }
604 else
605 t = is_state->match_start;
606 if (!p_hls)
607 search_flags += SEARCH_KEEP;
608 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 save = pat[patlen];
610 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100611 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200612 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200613 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616 if (i)
617 {
618 is_state->search_start = is_state->match_start;
619 is_state->match_end = t;
620 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622 {
623 // Move just before the current match, so that when nv_search
624 // finishes the cursor will be put back on the match.
625 is_state->search_start = t;
626 (void)decl(&is_state->search_start);
627 }
628 else if (c == Ctrl_G && firstc == '?')
629 {
630 // Move just after the current match, so that when nv_search
631 // finishes the cursor will be put back on the match.
632 is_state->search_start = t;
633 (void)incl(&is_state->search_start);
634 }
635 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
636 {
637 // wrap around
638 is_state->search_start = t;
639 if (firstc == '?')
640 (void)incl(&is_state->search_start);
641 else
642 (void)decl(&is_state->search_start);
643 }
644
645 set_search_match(&is_state->match_end);
646 curwin->w_cursor = is_state->match_start;
647 changed_cline_bef_curs();
648 update_topline();
649 validate_cursor();
650 highlight_match = TRUE;
651 save_viewstate(&is_state->old_viewstate);
652 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200653 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200655 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200656 }
657 else
658 vim_beep(BO_ERROR);
659 restore_last_search_pattern();
660 return FAIL;
661}
662
663/*
664 * When CTRL-L typed: add character from the match to the pattern.
665 * May set "*c" to the added character.
666 * Return OK when jumping to cmdline_not_changed.
667 */
668 static int
669may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
670{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200671 int skiplen, patlen;
672
Bram Moolenaar198cb662018-09-06 21:44:17 +0200673 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100674 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200675 save_last_search_pattern();
676
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200678 {
679 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200680 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200681 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100682 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200683
684 // Add a character from under the cursor for 'incsearch'.
685 if (is_state->did_incsearch)
686 {
687 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200688 *c = gchar_cursor();
689 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200691 // If 'ignorecase' and 'smartcase' are set and the
692 // command line has no uppercase characters, convert
693 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200694 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200695 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200696 if (*c == firstc || vim_strchr((char_u *)(
697 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200698 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200699 // put a backslash before special characters
700 stuffcharReadbuff(*c);
701 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200702 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200703 // add any composing characters
704 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
705 {
706 int save_c = *c;
707
708 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
709 {
710 curwin->w_cursor.col += mb_char2len(*c);
711 *c = gchar_cursor();
712 stuffcharReadbuff(*c);
713 }
714 *c = save_c;
715 }
716 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200717 }
718 }
719 return OK;
720}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200721#endif
722
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200723#ifdef FEAT_ARABIC
724/*
725 * Return TRUE if the command line has an Arabic character at or after "start"
726 * for "len" bytes.
727 */
728 static int
729cmdline_has_arabic(int start, int len)
730{
731 int j;
732 int mb_l;
733 int u8c;
734 char_u *p;
735 int u8cc[MAX_MCO];
736
737 if (!enc_utf8)
738 return FALSE;
739
740 for (j = start; j < start + len; j += mb_l)
741 {
742 p = ccline.cmdbuff + j;
743 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
744 mb_l = utfc_ptr2len_len(p, start + len - j);
745 if (ARABIC_CHAR(u8c))
746 return TRUE;
747 }
748 return FALSE;
749}
750#endif
751
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200752 void
753cmdline_init(void)
754{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200755 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200756}
757
Bram Moolenaar66216052017-12-16 16:33:44 +0100758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * getcmdline() - accept a command line starting with firstc.
760 *
761 * firstc == ':' get ":" command line.
762 * firstc == '/' or '?' get search pattern
763 * firstc == '=' get expression
764 * firstc == '@' get text for input() function
765 * firstc == '>' get text for debug mode
766 * firstc == NUL get text for :insert command
767 * firstc == -1 like NUL, and break on CTRL-C
768 *
769 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
770 * command line.
771 *
772 * Careful: getcmdline() can be called recursively!
773 *
774 * Return pointer to allocated string if there is a commandline, NULL
775 * otherwise.
776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100778getcmdline(
779 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200780 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200781 int indent, // indent for inside conditionals
782 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200783{
784 return getcmdline_int(firstc, count, indent, TRUE);
785}
786
787 static char_u *
788getcmdline_int(
789 int firstc,
790 long count UNUSED, // only used for incremental search
791 int indent, // indent for inside conditionals
792 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 int c;
795 int i;
796 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 int gotesc = FALSE; // TRUE when <ESC> just typed
798 int do_abbr; // when TRUE check for abbr.
799 char_u *lookfor = NULL; // string to match
800 int hiscnt; // current history line in use
801 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200803 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100805 int did_wild_list = FALSE; // did wild_list() recently
806 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 int res;
808 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100809 int save_State = State; // remember State when called
810 int some_key_typed = FALSE; // one of the keys was typed
811 // mouse drag and release events are ignored, unless they are
812 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_EVAL
815 int break_ctrl_c = FALSE;
816#endif
817 expand_T xpc;
818 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200819 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200820 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200821 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar438d1762018-09-30 17:11:48 +0200823 if (ccline.cmdbuff != NULL)
824 {
825 // Being called recursively. Since ccline is global, we need to save
826 // the current buffer and restore it when returning.
827 save_cmdline(&save_ccline);
828 did_save_ccline = TRUE;
829 }
830 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200831 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#ifdef FEAT_EVAL
834 if (firstc == -1)
835 {
836 firstc = NUL;
837 break_ctrl_c = TRUE;
838 }
839#endif
840#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (firstc == ':' || firstc == '=' || firstc == '>')
843 cmd_hkmap = 0;
844#endif
845
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100846 ccline.overstrike = FALSE; // always start in insert mode
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200849 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif
851
852 /*
853 * set some variables for redrawcmd()
854 */
855 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000856 ccline.cmdindent = (firstc > 0 ? indent : 0);
857
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // alloc initial ccline.cmdbuff
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000859 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200861 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 ccline.cmdlen = ccline.cmdpos = 0;
863 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100864 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100866 // autoindent for :insert and :append
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000867 if (firstc <= 0)
868 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200869 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000870 ccline.cmdbuff[indent] = NUL;
871 ccline.cmdpos = indent;
872 ccline.cmdspos = indent;
873 ccline.cmdlen = indent;
874 }
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000877 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879#ifdef FEAT_RIGHTLEFT
880 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
881 && (firstc == '/' || firstc == '?'))
882 cmdmsg_rl = TRUE;
883 else
884 cmdmsg_rl = FALSE;
885#endif
886
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100887 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (!cmd_silent)
889 {
890 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 gotocmdline(TRUE);
893 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 set_cmdspos();
896 }
897 xpc.xp_context = EXPAND_NOTHING;
898 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000899#ifndef BACKSLASH_IN_FILENAME
900 xpc.xp_shell = FALSE;
901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000903#if defined(FEAT_EVAL)
904 if (ccline.input_fn)
905 {
906 xpc.xp_context = ccline.xp_context;
907 xpc.xp_pattern = ccline.cmdbuff;
908 xpc.xp_arg = ccline.xp_arg;
909 }
910#endif
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /*
913 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
914 * doing ":@0" when register 0 doesn't contain a CR.
915 */
916 msg_scroll = FALSE;
917
918 State = CMDLINE;
919
920 if (firstc == '/' || firstc == '?' || firstc == '@')
921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100922 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
924 b_im_ptr = &curbuf->b_p_iminsert;
925 else
926 b_im_ptr = &curbuf->b_p_imsearch;
927 if (*b_im_ptr == B_IMODE_LMAP)
928 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 im_set_active(*b_im_ptr == B_IMODE_IM);
931#endif
932 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100933#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 else if (p_imcmdline)
935 im_set_active(TRUE);
936#endif
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100940 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#endif
942
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100943 // When inside an autocommand for writing "exiting" may be set and
944 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000945 settmode(TMODE_RAW);
946
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100947 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200948 cmdline_type = firstc == NUL ? '-' : firstc;
949 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100952 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100956 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#endif
958
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100959 // If something above caused an error, reset the flags, we do want to type
960 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200961 if (did_emsg)
962 redrawcmd();
963 did_emsg = FALSE;
964 got_int = FALSE;
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /*
967 * Collect the command string, handling editing keys.
968 */
969 for (;;)
970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100971 redir_off = TRUE; // Don't redirect the typed command.
972 // Repeated, because a ":redir" inside
973 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100975 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100979 did_emsg = FALSE; // There can't really be a reason why an error
980 // that occurs while typing a command should
981 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +0200982
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200983 // Trigger SafeState if nothing is pending.
984 may_trigger_safestate(xpc.xp_numfiles <= 0);
985
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100986 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +0000987
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100988 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
989 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +0000990 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000991 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100992 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (KeyTyped)
995 {
996 some_key_typed = TRUE;
997#ifdef FEAT_RIGHTLEFT
998 if (cmd_hkmap)
999 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (cmdmsg_rl && !KeyStuffed)
1001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001002 // Invert horizontal movements and operations. Only when
1003 // typed by the user directly, not when the result of a
1004 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 switch (c)
1006 {
1007 case K_RIGHT: c = K_LEFT; break;
1008 case K_S_RIGHT: c = K_S_LEFT; break;
1009 case K_C_RIGHT: c = K_C_LEFT; break;
1010 case K_LEFT: c = K_RIGHT; break;
1011 case K_S_LEFT: c = K_S_RIGHT; break;
1012 case K_C_LEFT: c = K_C_RIGHT; break;
1013 }
1014 }
1015#endif
1016 }
1017
1018 /*
1019 * Ignore got_int when CTRL-C was typed here.
1020 * Don't ignore it in :global, we really need to break then, e.g., for
1021 * ":g/pat/normal /pat" (without the <CR>).
1022 * Don't ignore it for the input() function.
1023 */
1024 if ((c == Ctrl_C
1025#ifdef UNIX
1026 || c == intr_char
1027#endif
1028 )
1029#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1030 && firstc != '@'
1031#endif
1032#ifdef FEAT_EVAL
1033 && !break_ctrl_c
1034#endif
1035 && !global_busy)
1036 got_int = FALSE;
1037
Bram Moolenaard7663c22019-08-06 21:59:57 +02001038 // free old command line when finished moving around in the history
1039 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001041 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && c != K_PAGEDOWN && c != K_PAGEUP
1044 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001045 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001047 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001050 * When there are matching completions to select <S-Tab> works like
1051 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001053 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 c = Ctrl_P;
1055
1056#ifdef FEAT_WILDMENU
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 // Special translations for 'wildmenu'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (did_wild_list && p_wmnu)
1059 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001060 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001062 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 c = Ctrl_N;
1064 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // Hitting CR after "emenu Name.": complete submenu
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1067 && ccline.cmdpos > 1
1068 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1069 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1070 && (c == '\n' || c == '\r' || c == K_KENTER))
1071 c = K_DOWN;
1072#endif
1073
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001074 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (xpc.xp_numfiles != -1
1076 && !(c == p_wc && KeyTyped) && c != p_wcm
1077 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1078 && c != Ctrl_L)
1079 {
1080 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1081 did_wild_list = FALSE;
1082#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001083 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085 xpc.xp_context = EXPAND_NOTHING;
1086 wim_index = 0;
1087#ifdef FEAT_WILDMENU
1088 if (p_wmnu && wild_menu_showing != 0)
1089 {
1090 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001091 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001092
1093 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001094 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 if (wild_menu_showing == WM_SCROLLED)
1097 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001098 // Entered command line, move it up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 cmdline_row--;
1100 redrawcmd();
1101 }
1102 else if (save_p_ls != -1)
1103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001104 // restore 'laststatus' and 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 p_ls = save_p_ls;
1106 p_wmh = save_p_wmh;
1107 last_status(FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001108 update_screen(VALID); // redraw the screen NOW
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 redrawcmd();
1110 save_p_ls = -1;
1111 }
1112 else
1113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 redraw_statuslines();
1116 }
1117 KeyTyped = skt;
1118 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001119 if (ccline.input_fn)
1120 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122#endif
1123 }
1124
1125#ifdef FEAT_WILDMENU
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // Special translations for 'wildmenu'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001129 // Hitting <Down> after "emenu Name.": complete submenu
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001130 if (c == K_DOWN && ccline.cmdpos > 0
1131 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001133 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135 // Hitting <Up>: Remove one submenu name in front of the
1136 // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 int found = FALSE;
1138
1139 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1140 i = 0;
1141 while (--j > 0)
1142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143 // check for start of menu name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (ccline.cmdbuff[j] == ' '
1145 && ccline.cmdbuff[j - 1] != '\\')
1146 {
1147 i = j + 1;
1148 break;
1149 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 // check for start of submenu name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (ccline.cmdbuff[j] == '.'
1152 && ccline.cmdbuff[j - 1] != '\\')
1153 {
1154 if (found)
1155 {
1156 i = j + 1;
1157 break;
1158 }
1159 else
1160 found = TRUE;
1161 }
1162 }
1163 if (i > 0)
1164 cmdline_del(i);
1165 c = p_wc;
1166 xpc.xp_context = EXPAND_NOTHING;
1167 }
1168 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001169 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001170 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001171 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 char_u upseg[5];
1174
1175 upseg[0] = PATHSEP;
1176 upseg[1] = '.';
1177 upseg[2] = '.';
1178 upseg[3] = PATHSEP;
1179 upseg[4] = NUL;
1180
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001181 if (c == K_DOWN
1182 && ccline.cmdpos > 0
1183 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1184 && (ccline.cmdpos < 3
1185 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1187 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001188 // go down a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 c = p_wc;
1190 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001191 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001193 // If in a direct ancestor, strip off one ../ to go down
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 int found = FALSE;
1195
1196 j = ccline.cmdpos;
1197 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1198 while (--j > i)
1199 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001200 if (has_mbyte)
1201 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (vim_ispathsep(ccline.cmdbuff[j]))
1203 {
1204 found = TRUE;
1205 break;
1206 }
1207 }
1208 if (found
1209 && ccline.cmdbuff[j - 1] == '.'
1210 && ccline.cmdbuff[j - 2] == '.'
1211 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1212 {
1213 cmdline_del(j - 2);
1214 c = p_wc;
1215 }
1216 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001217 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001219 // go up a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int found = FALSE;
1221
1222 j = ccline.cmdpos - 1;
1223 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1224 while (--j > i)
1225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (has_mbyte)
1227 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (vim_ispathsep(ccline.cmdbuff[j])
1229#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001230 && vim_strchr((char_u *)" *?[{`$%#",
1231 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233 )
1234 {
1235 if (found)
1236 {
1237 i = j + 1;
1238 break;
1239 }
1240 else
1241 found = TRUE;
1242 }
1243 }
1244
1245 if (!found)
1246 j = i;
1247 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1248 j += 4;
1249 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1250 && j == i)
1251 j += 3;
1252 else
1253 j = 0;
1254 if (j > 0)
1255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001256 // TODO this is only for DOS/UNIX systems - need to put in
1257 // machine-specific stuff here and in upseg init
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 cmdline_del(j);
1259 put_on_cmdline(upseg + 1, 3, FALSE);
1260 }
1261 else if (ccline.cmdpos > i)
1262 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001264 // Now complete in the new directory. Set KeyTyped in case the
1265 // Up key came from a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001267 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001271#endif // FEAT_WILDMENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001273 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1274 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (c == Ctrl_BSL)
1276 {
1277 ++no_mapping;
1278 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001279 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 --no_mapping;
1281 --allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001282 // CTRL-\ e doesn't work when obtaining an expression, unless it
1283 // is in a mapping.
Bram Moolenaarb7356812012-10-11 04:04:37 +02001284 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001285 || (ccline.cmdfirstc == '=' && KeyTyped)
1286#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001287 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001288#endif
1289 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 vungetc(c);
1292 c = Ctrl_BSL;
1293 }
1294#ifdef FEAT_EVAL
1295 else if (c == 'e')
1296 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001297 char_u *p = NULL;
1298 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 /*
1301 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001302 * Need to save and restore the current command line, to be
1303 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 */
1305 if (ccline.cmdpos == ccline.cmdlen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001306 new_cmdpos = 99999; // keep it at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 else
1308 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (c == '=')
1312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001313 // Need to save and restore ccline. And set "textlock"
1314 // to avoid nasty things like going to another buffer when
1315 // evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001316 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001318 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001319
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001320 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001322 len = (int)STRLEN(p);
1323 if (realloc_cmdbuff(len + 1) == OK)
1324 {
1325 ccline.cmdlen = len;
1326 STRCPY(ccline.cmdbuff, p);
1327 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001329 // Restore the cursor or use the position set with
1330 // set_cmdline_pos().
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001331 if (new_cmdpos > ccline.cmdlen)
1332 ccline.cmdpos = ccline.cmdlen;
1333 else
1334 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001336 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001337 redrawcmd();
1338 goto cmdline_changed;
1339 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001340 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342 }
1343 beep_flush();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001344 got_int = FALSE; // don't abandon the command line
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001345 did_emsg = FALSE;
1346 emsg_on_display = FALSE;
1347 redrawcmd();
1348 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350#endif
1351 else
1352 {
1353 if (c == Ctrl_G && p_im && restart_edit == 0)
1354 restart_edit = 'a';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001355 gotesc = TRUE; // will free ccline.cmdbuff after putting it
1356 // in history
1357 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360
1361#ifdef FEAT_CMDWIN
1362 if (c == cedit_key || c == K_CMDWIN)
1363 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001364 if (ex_normal_busy == 0 && got_int == FALSE)
1365 {
1366 /*
1367 * Open a window to edit the command line (and history).
1368 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001369 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001370 some_key_typed = TRUE;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373# ifdef FEAT_DIGRAPHS
1374 else
1375# endif
1376#endif
1377#ifdef FEAT_DIGRAPHS
1378 c = do_digraph(c);
1379#endif
1380
1381 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1382 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001384 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001385 if (exmode_active
1386 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001387 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001388 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001389 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001391 if (c == K_KENTER)
1392 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001394 else
1395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001396 gotesc = FALSE; // Might have typed ESC previously, don't
1397 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001398 if (ccheck_abbr(c + ABBR_OFF))
1399 goto cmdline_changed;
1400 if (!cmd_silent)
1401 {
1402 windgoto(msg_row, 0);
1403 out_flush();
1404 }
1405 break;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408
1409 /*
1410 * Completion for 'wildchar' or 'wildcharm' key.
1411 * - hitting <ESC> twice means: abandon command line.
1412 * - wildcard expansion is only done when the 'wildchar' key is really
1413 * typed, not when it comes from a macro
1414 */
1415 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1416 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001417 int options = WILD_NO_BEEP;
1418 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1419 options |= WILD_BUFLASTUSED;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001420 if (xpc.xp_numfiles > 0) // typed p_wc at least twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001422 // if 'wildmode' contains "list" may still need to list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (xpc.xp_numfiles > 1
1424 && !did_wild_list
1425 && (wim_flags[wim_index] & WIM_LIST))
1426 {
1427 (void)showmatches(&xpc, FALSE);
1428 redrawcmd();
1429 did_wild_list = TRUE;
1430 }
1431 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001432 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001433 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001435 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001436 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 res = OK; // don't insert 'wildchar' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001440 else // typed p_wc first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 {
1442 wim_index = 0;
1443 j = ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001444 // if 'wildmode' first contains "longest", get longest
1445 // common part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001447 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001448 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001450 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001451 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001453 // if interrupted while completing, behave like it failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (got_int)
1455 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001456 (void)vpeekc(); // remove <C-C> from input stream
1457 got_int = FALSE; // don't abandon the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1459#ifdef FEAT_WILDMENU
1460 xpc.xp_context = EXPAND_NOTHING;
1461#endif
1462 goto cmdline_changed;
1463 }
1464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 // when more than one match, and 'wildmode' first contains
1466 // "list", or no change and 'wildmode' contains "longest,list",
1467 // list all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (res == OK && xpc.xp_numfiles > 1)
1469 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001470 // a "longest" that didn't do anything is skipped (but not
1471 // "list:longest")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1473 wim_index = 1;
1474 if ((wim_flags[wim_index] & WIM_LIST)
1475#ifdef FEAT_WILDMENU
1476 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1477#endif
1478 )
1479 {
1480 if (!(wim_flags[0] & WIM_LONGEST))
1481 {
1482#ifdef FEAT_WILDMENU
1483 int p_wmnu_save = p_wmnu;
1484 p_wmnu = 0;
1485#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001486 // remove match
Bram Moolenaarb3479632012-11-28 16:49:58 +01001487 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#ifdef FEAT_WILDMENU
1489 p_wmnu = p_wmnu_save;
1490#endif
1491 }
1492#ifdef FEAT_WILDMENU
1493 (void)showmatches(&xpc, p_wmnu
1494 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1495#else
1496 (void)showmatches(&xpc, FALSE);
1497#endif
1498 redrawcmd();
1499 did_wild_list = TRUE;
1500 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001501 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001502 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001504 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001505 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001508 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510#ifdef FEAT_WILDMENU
1511 else if (xpc.xp_numfiles == -1)
1512 xpc.xp_context = EXPAND_NOTHING;
1513#endif
1514 }
1515 if (wim_index < 3)
1516 ++wim_index;
1517 if (c == ESC)
1518 gotesc = TRUE;
1519 if (res == OK)
1520 goto cmdline_changed;
1521 }
1522
1523 gotesc = FALSE;
1524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001525 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (c == K_S_TAB && KeyTyped)
1527 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001528 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1529 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1530 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 goto cmdline_changed;
1532 }
1533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001534 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 c = NL;
1536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001537 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
1539 /*
1540 * Big switch for a typed command line character.
1541 */
1542 switch (c)
1543 {
1544 case K_BS:
1545 case Ctrl_H:
1546 case K_DEL:
1547 case K_KDEL:
1548 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (c == K_KDEL)
1550 c = K_DEL;
1551
1552 /*
1553 * delete current character is the same as backspace on next
1554 * character, except at end of line
1555 */
1556 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1557 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (has_mbyte && c == K_DEL)
1559 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1560 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (ccline.cmdpos > 0)
1562 {
1563 char_u *p;
1564
1565 j = ccline.cmdpos;
1566 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (has_mbyte)
1568 {
1569 p = mb_prevptr(ccline.cmdbuff, p);
1570 if (c == Ctrl_W)
1571 {
1572 while (p > ccline.cmdbuff && vim_isspace(*p))
1573 p = mb_prevptr(ccline.cmdbuff, p);
1574 i = mb_get_class(p);
1575 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1576 p = mb_prevptr(ccline.cmdbuff, p);
1577 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001578 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001581 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1584 --p;
1585 i = vim_iswordc(p[-1]);
1586 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1587 && vim_iswordc(p[-1]) == i)
1588 --p;
1589 }
1590 else
1591 --p;
1592 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1593 ccline.cmdlen -= j - ccline.cmdpos;
1594 i = ccline.cmdpos;
1595 while (i < ccline.cmdlen)
1596 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001600#ifdef FEAT_SEARCH_EXTRA
1601 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001602 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001603 is_state.search_start = is_state.save_cursor;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001604 // save view settings, so that the screen
1605 // won't be restored at the wrong position
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001606 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001607 }
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 redrawcmd();
1610 }
1611 else if (ccline.cmdlen == 0 && c != Ctrl_W
1612 && ccline.cmdprompt == NULL && indent == 0)
1613 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001614 // In ex and debug mode it doesn't make sense to return.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (exmode_active
1616#ifdef FEAT_EVAL
1617 || ccline.cmdfirstc == '>'
1618#endif
1619 )
1620 goto cmdline_not_changed;
1621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001622 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (!cmd_silent)
1624 {
1625#ifdef FEAT_RIGHTLEFT
1626 if (cmdmsg_rl)
1627 msg_col = Columns;
1628 else
1629#endif
1630 msg_col = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001631 msg_putchar(' '); // delete ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001633#ifdef FEAT_SEARCH_EXTRA
1634 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001635 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 redraw_cmdline = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 goto cmdline_changed;
1641
1642 case K_INS:
1643 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 ccline.overstrike = !ccline.overstrike;
1645#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001646 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#endif
1648 goto cmdline_not_changed;
1649
1650 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001651 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001653 // ":lmap" mappings exists, toggle use of mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001655#ifdef HAVE_INPUT_METHOD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001656 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
1658 if (b_im_ptr != NULL)
1659 {
1660 if (State & LANGMAP)
1661 *b_im_ptr = B_IMODE_LMAP;
1662 else
1663 *b_im_ptr = B_IMODE_NONE;
1664 }
1665 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 else
1668 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001669 // There are no ":lmap" mappings, toggle IM. When
1670 // 'imdisable' is set don't try getting the status, it's
1671 // always off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if ((p_imdisable && b_im_ptr != NULL)
1673 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1674 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001675 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (b_im_ptr != NULL)
1677 *b_im_ptr = B_IMODE_NONE;
1678 }
1679 else
1680 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 im_set_active(TRUE); // Enable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (b_im_ptr != NULL)
1683 *b_im_ptr = B_IMODE_IM;
1684 }
1685 }
1686#endif
1687 if (b_im_ptr != NULL)
1688 {
1689 if (b_im_ptr == &curbuf->b_p_iminsert)
1690 set_iminsert_global();
1691 else
1692 set_imsearch_global();
1693 }
1694#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001697#if defined(FEAT_KEYMAP)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001698 // Show/unshow value of 'keymap' in status lines later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 status_redraw_curbuf();
1700#endif
1701 goto cmdline_not_changed;
1702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001703// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001705 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 j = ccline.cmdpos;
1707 ccline.cmdlen -= j;
1708 i = ccline.cmdpos = 0;
1709 while (i < ccline.cmdlen)
1710 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001711 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001713#ifdef FEAT_SEARCH_EXTRA
1714 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001715 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 redrawcmd();
1718 goto cmdline_changed;
1719
1720#ifdef FEAT_CLIPBOARD
1721 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001722 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (clip_star.state != SELECT_CLEARED)
1724 {
1725 if (clip_star.state == SELECT_DONE)
1726 clip_copy_modeless_selection(TRUE);
1727 goto cmdline_not_changed;
1728 }
1729 break;
1730#endif
1731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001734 // In exmode it doesn't make sense to return. Except when
1735 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001736 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001737 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 goto cmdline_not_changed;
1739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001740 gotesc = TRUE; // will free ccline.cmdbuff after
1741 // putting it in history
1742 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001744 case Ctrl_R: // insert register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001746 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#endif
1748 putcmdline('"', TRUE);
1749 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001750 ++allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 i = c = plain_vgetc(); // CTRL-R <char>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (i == Ctrl_O)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001753 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if (i == Ctrl_R)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 c = plain_vgetc(); // CTRL-R CTRL-R <char>
Bram Moolenaara92522f2017-07-15 15:21:38 +02001756 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001758 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#ifdef FEAT_EVAL
1760 /*
1761 * Insert the result of an expression.
1762 * Need to save the current command line, to be able to enter
1763 * a new one...
1764 */
1765 new_cmdpos = -1;
1766 if (c == '=')
1767 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001768 if (ccline.cmdfirstc == '=' // can't do this recursively
1769 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 beep_flush();
1772 c = ESC;
1773 }
1774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
1777#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001778 if (c != ESC) // use ESC to cancel inserting register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001780 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001781
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001782#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001783 // When there was a serious error abort getting the
1784 // command line.
Bram Moolenaaracf53452005-12-17 22:06:52 +00001785 if (aborting())
1786 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001787 gotesc = TRUE; // will free ccline.cmdbuff after
1788 // putting it in history
1789 goto returncmd; // back to cmd mode
Bram Moolenaaracf53452005-12-17 22:06:52 +00001790 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001791#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001792 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_EVAL
1794 if (new_cmdpos >= 0)
1795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001796 // set_cmdline_pos() was used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (new_cmdpos > ccline.cmdlen)
1798 ccline.cmdpos = ccline.cmdlen;
1799 else
1800 ccline.cmdpos = new_cmdpos;
1801 }
1802#endif
1803 }
1804 redrawcmd();
1805 goto cmdline_changed;
1806
1807 case Ctrl_D:
1808 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001809 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001812 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 case K_RIGHT:
1815 case K_S_RIGHT:
1816 case K_C_RIGHT:
1817 do
1818 {
1819 if (ccline.cmdpos >= ccline.cmdlen)
1820 break;
1821 i = cmdline_charsize(ccline.cmdpos);
1822 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1823 break;
1824 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001826 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 + ccline.cmdpos);
1828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 ++ccline.cmdpos;
1830 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001831 while ((c == K_S_RIGHT || c == K_C_RIGHT
1832 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (has_mbyte)
1835 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 goto cmdline_not_changed;
1837
1838 case K_LEFT:
1839 case K_S_LEFT:
1840 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001841 if (ccline.cmdpos == 0)
1842 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 do
1844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001846 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1848 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1850 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001851 while (ccline.cmdpos > 0
1852 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001853 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (has_mbyte)
1856 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 goto cmdline_not_changed;
1858
1859 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001860 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001861 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar4f974752019-02-17 17:44:42 +01001863#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001864 // On MS-Windows ignore <M-F4>, we get it when closing the window
1865 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001866 case K_F4:
1867 if (mod_mask == MOD_MASK_ALT)
1868 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001869 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00001870 goto cmdline_not_changed;
1871 }
1872 break;
1873#endif
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 case K_MIDDLEDRAG:
1876 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001877 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 case K_MIDDLEMOUSE:
1880# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001881 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (!gui.in_use)
1883# endif
1884 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001886# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001888 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001890# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001891 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 redrawcmd();
1893 goto cmdline_changed;
1894
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001895# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001897 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 redrawcmd();
1899 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 case K_LEFTDRAG:
1903 case K_LEFTRELEASE:
1904 case K_RIGHTDRAG:
1905 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001906 // Ignore drag and release events when the button-down wasn't
1907 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (ignore_drag_release)
1909 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001910 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 case K_LEFTMOUSE:
1912 case K_RIGHTMOUSE:
1913 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1914 ignore_drag_release = TRUE;
1915 else
1916 ignore_drag_release = FALSE;
1917# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001918 // When GUI is active, also move when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (!gui.in_use)
1920# endif
1921 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001922 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923# ifdef FEAT_CLIPBOARD
1924 if (mouse_row < cmdline_row && clip_star.available)
1925 {
1926 int button, is_click, is_drag;
1927
1928 /*
1929 * Handle modeless selection.
1930 */
1931 button = get_mouse_button(KEY2TERMCAP1(c),
1932 &is_click, &is_drag);
1933 if (mouse_model_popup() && button == MOUSE_LEFT
1934 && (mod_mask & MOD_MASK_SHIFT))
1935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // Translate shift-left to right button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 button = MOUSE_RIGHT;
1938 mod_mask &= ~MOD_MASK_SHIFT;
1939 }
1940 clip_modeless(button, is_click, is_drag);
1941 goto cmdline_not_changed;
1942 }
1943# endif
1944
1945 set_cmdspos();
1946 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1947 ++ccline.cmdpos)
1948 {
1949 i = cmdline_charsize(ccline.cmdpos);
1950 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1951 && mouse_col < ccline.cmdspos % Columns + i)
1952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (has_mbyte)
1954 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001955 // Count ">" for double-wide char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001957 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 + ccline.cmdpos) - 1;
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 ccline.cmdspos += i;
1961 }
1962 goto cmdline_not_changed;
1963
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 case K_MOUSEDOWN:
1966 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001967 case K_MOUSELEFT:
1968 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001969 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 case K_X1MOUSE:
1971 case K_X1DRAG:
1972 case K_X1RELEASE:
1973 case K_X2MOUSE:
1974 case K_X2DRAG:
1975 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001976 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto cmdline_not_changed;
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 case K_LEFTRELEASE_NM:
1982 goto cmdline_not_changed;
1983
1984 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001985 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 gui_do_scroll();
1988 redrawcmd();
1989 }
1990 goto cmdline_not_changed;
1991
1992 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001993 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001995 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 redrawcmd();
1997 }
1998 goto cmdline_not_changed;
1999#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002000#ifdef FEAT_GUI_TABLINE
2001 case K_TABLINE:
2002 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002003 // Don't want to change any tabs here. Make sure the same tab
2004 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002005 if (gui_use_tabline())
2006 gui_mch_set_curtab(tabpage_index(curtab));
2007 goto cmdline_not_changed;
2008#endif
2009
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002010 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 goto cmdline_not_changed;
2012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 case K_HOME:
2015 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 case K_S_HOME:
2017 case K_C_HOME:
2018 ccline.cmdpos = 0;
2019 set_cmdspos();
2020 goto cmdline_not_changed;
2021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002022 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 case K_END:
2024 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 case K_S_END:
2026 case K_C_END:
2027 ccline.cmdpos = ccline.cmdlen;
2028 set_cmdspos_cursor();
2029 goto cmdline_not_changed;
2030
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002031 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01002032 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 break;
2034 goto cmdline_changed;
2035
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002036 case Ctrl_L:
2037#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002038 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002039 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002040#endif
2041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002042 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002043 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 break;
2045 goto cmdline_changed;
2046
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002047 case Ctrl_N: // next match
2048 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002049 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002051 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2052 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002054 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002056 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 case K_UP:
2058 case K_DOWN:
2059 case K_S_UP:
2060 case K_S_DOWN:
2061 case K_PAGEUP:
2062 case K_KPAGEUP:
2063 case K_PAGEDOWN:
2064 case K_KPAGEDOWN:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 if (get_hislen() == 0 || firstc == NUL) // no history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 goto cmdline_not_changed;
2067
2068 i = hiscnt;
2069
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // save current command string so it can be restored later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (lookfor == NULL)
2072 {
2073 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2074 goto cmdline_not_changed;
2075 lookfor[ccline.cmdpos] = NUL;
2076 }
2077
2078 j = (int)STRLEN(lookfor);
2079 for (;;)
2080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002081 // one step backwards
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002082 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002083 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002085 if (hiscnt == get_hislen()) // first time
Bram Moolenaard7663c22019-08-06 21:59:57 +02002086 hiscnt = *get_hisidx(histype);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002087 else if (hiscnt == 0 && *get_hisidx(histype)
2088 != get_hislen() - 1)
Bram Moolenaard7663c22019-08-06 21:59:57 +02002089 hiscnt = get_hislen() - 1;
2090 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 --hiscnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002092 else // at top of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 hiscnt = i;
2095 break;
2096 }
2097 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002098 else // one step forwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 // on last entry, clear the line
Bram Moolenaard7663c22019-08-06 21:59:57 +02002101 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002103 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 break;
2105 }
2106
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // not on a history line, nothing to do
Bram Moolenaard7663c22019-08-06 21:59:57 +02002108 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002110 if (hiscnt == get_hislen() - 1) // wrap around
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 hiscnt = 0;
2112 else
2113 ++hiscnt;
2114 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
2116 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 hiscnt = i;
2119 break;
2120 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002121 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002122 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002123 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 lookfor, (size_t)j) == 0)
2125 break;
2126 }
2127
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 if (hiscnt != i) // jumped to other entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 char_u *p;
2131 int len;
2132 int old_firstc;
2133
Bram Moolenaar438d1762018-09-30 17:11:48 +02002134 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002135 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002136 if (hiscnt == get_hislen())
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 p = lookfor; // back to the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002139 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 if (histype == HIST_SEARCH
2142 && p != lookfor
2143 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2144 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002145 // Correct for the separator character used when
2146 // adding the history entry vs the one used now.
2147 // First loop: count length.
2148 // Second loop: copy the characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 for (i = 0; i <= 1; ++i)
2150 {
2151 len = 0;
2152 for (j = 0; p[j] != NUL; ++j)
2153 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002154 // Replace old sep with new sep, unless it is
2155 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (p[j] == old_firstc
2157 && (j == 0 || p[j - 1] != '\\'))
2158 {
2159 if (i > 0)
2160 ccline.cmdbuff[len] = firstc;
2161 }
2162 else
2163 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002164 // Escape new sep, unless it is already
2165 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (p[j] == firstc
2167 && (j == 0 || p[j - 1] != '\\'))
2168 {
2169 if (i > 0)
2170 ccline.cmdbuff[len] = '\\';
2171 ++len;
2172 }
2173 if (i > 0)
2174 ccline.cmdbuff[len] = p[j];
2175 }
2176 ++len;
2177 }
2178 if (i == 0)
2179 {
2180 alloc_cmdbuff(len);
2181 if (ccline.cmdbuff == NULL)
2182 goto returncmd;
2183 }
2184 }
2185 ccline.cmdbuff[len] = NUL;
2186 }
2187 else
2188 {
2189 alloc_cmdbuff((int)STRLEN(p));
2190 if (ccline.cmdbuff == NULL)
2191 goto returncmd;
2192 STRCPY(ccline.cmdbuff, p);
2193 }
2194
2195 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2196 redrawcmd();
2197 goto cmdline_changed;
2198 }
2199 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002200 goto cmdline_not_changed;
2201
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002202#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002203 case Ctrl_G: // next match
2204 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002205 if (may_adjust_incsearch_highlighting(
2206 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002207 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209#endif
2210
2211 case Ctrl_V:
2212 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002214 int prev_mod_mask = mod_mask;
2215
2216 ignore_drag_release = TRUE;
2217 putcmdline('^', TRUE);
2218 c = get_literal(); // get next (two) character(s)
2219 do_abbr = FALSE; // don't do abbreviation now
2220 extra_char = NUL;
2221 // may need to remove ^ when composing char was typed
2222 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2223 {
2224 draw_cmdline(ccline.cmdpos,
2225 ccline.cmdlen - ccline.cmdpos);
2226 msg_putchar(' ');
2227 cursorcmd();
2228 }
2229
2230 if ((c == ESC || c == CSI)
2231 && !(prev_mod_mask & MOD_MASK_SHIFT))
2232 // Using CTRL-V: Change any modifyOtherKeys ESC
2233 // sequence to a normal key. Don't do this for
2234 // CTRL-SHIFT-V.
2235 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002237
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 break;
2239
2240#ifdef FEAT_DIGRAPHS
2241 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002244# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002245 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002248 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (c != NUL)
2250 break;
2251
2252 redrawcmd();
2253 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002254#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002257 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (!p_ari)
2259 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002260 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 goto cmdline_not_changed;
2262#endif
2263
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002264 case K_PS:
2265 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2266 goto cmdline_changed;
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 default:
2269#ifdef UNIX
2270 if (c == intr_char)
2271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 gotesc = TRUE; // will free ccline.cmdbuff after
2273 // putting it in history
2274 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276#endif
2277 /*
2278 * Normal character with no special meaning. Just set mod_mask
2279 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2280 * the string <S-Space>. This should only happen after ^V.
2281 */
2282 if (!IS_SPECIAL(c))
2283 mod_mask = 0x0;
2284 break;
2285 }
2286 /*
2287 * End of switch on command line character.
2288 * We come here if we have a normal character.
2289 */
2290
Bram Moolenaar13505972019-01-24 15:04:48 +01002291 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2292 && (ccheck_abbr(
2293 // Add ABBR_OFF for characters above 0x100, this is
2294 // what check_abbr() expects.
2295 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2296 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 goto cmdline_changed;
2298
2299 /*
2300 * put the character in the command line
2301 */
2302 if (IS_SPECIAL(c) || mod_mask != 0)
2303 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2304 else
2305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (has_mbyte)
2307 {
2308 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002309 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 put_on_cmdline(IObuff, j, TRUE);
2311 }
2312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 IObuff[0] = c;
2315 put_on_cmdline(IObuff, 1, TRUE);
2316 }
2317 }
2318 goto cmdline_changed;
2319
2320/*
2321 * This part implements incremental searches for "/" and "?"
2322 * Jump to cmdline_not_changed when a character has been read but the command
2323 * line did not change. Then we only search and redraw if something changed in
2324 * the past.
2325 * Jump to cmdline_changed when the command line did change.
2326 * (Sorry for the goto's, I know it is ugly).
2327 */
2328cmdline_not_changed:
2329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002330 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 continue;
2332#endif
2333
2334cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002335 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002336 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002339 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#endif
2341
2342#ifdef FEAT_RIGHTLEFT
2343 if (cmdmsg_rl
2344# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002345 || (p_arshape && !p_tbidi
2346 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347# endif
2348 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002349 // Always redraw the whole command line to fix shaping and
2350 // right-left typing. Not efficient, but it works.
2351 // Do it only when there are no characters left to read
2352 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002353 if (vpeekc() == NUL)
2354 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#endif
2356 }
2357
2358returncmd:
2359
2360#ifdef FEAT_RIGHTLEFT
2361 cmdmsg_rl = FALSE;
2362#endif
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002365 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002368 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369#endif
2370
2371 if (ccline.cmdbuff != NULL)
2372 {
2373 /*
2374 * Put line in history buffer (":" and "=" only when it was typed).
2375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (ccline.cmdlen && firstc != NUL
2377 && (some_key_typed || histype == HIST_SEARCH))
2378 {
2379 add_to_history(histype, ccline.cmdbuff, TRUE,
2380 histype == HIST_SEARCH ? firstc : NUL);
2381 if (firstc == ':')
2382 {
2383 vim_free(new_last_cmdline);
2384 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2385 }
2386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002388 if (gotesc)
2389 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391
2392 /*
2393 * If the screen was shifted up, redraw the whole screen (later).
2394 * If the line is too long, clear it, so ruler and shown command do
2395 * not get printed in the middle of it.
2396 */
2397 msg_check();
2398 msg_scroll = save_msg_scroll;
2399 redir_off = FALSE;
2400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002401 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (some_key_typed)
2403 need_wait_return = FALSE;
2404
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002405 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002406 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002409#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2411 im_save_status(b_im_ptr);
2412 im_set_active(FALSE);
2413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002416 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002418 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar438d1762018-09-30 17:11:48 +02002420theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002421 {
2422 char_u *p = ccline.cmdbuff;
2423
Bram Moolenaar438d1762018-09-30 17:11:48 +02002424 if (did_save_ccline)
2425 restore_cmdline(&save_ccline);
2426 else
2427 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002428 return p;
2429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2433/*
2434 * Get a command line with a prompt.
2435 * This is prepared to be called recursively from getcmdline() (e.g. by
2436 * f_input() when evaluating an expression from CTRL-R =).
2437 * Returns the command line in allocated memory, or NULL.
2438 */
2439 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002440getcmdline_prompt(
2441 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002442 char_u *prompt, // command line prompt
2443 int attr, // attributes for prompt
2444 int xp_context, // type of expansion
2445 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002448 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002449 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002451 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaar438d1762018-09-30 17:11:48 +02002453 if (ccline.cmdbuff != NULL)
2454 {
2455 // Save the values of the current cmdline and restore them below.
2456 save_cmdline(&save_ccline);
2457 did_save_ccline = TRUE;
2458 }
2459
Bram Moolenaar66b51422019-08-18 21:44:12 +02002460 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 ccline.cmdprompt = prompt;
2462 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002463# ifdef FEAT_EVAL
2464 ccline.xp_context = xp_context;
2465 ccline.xp_arg = xp_arg;
2466 ccline.input_fn = (firstc == '@');
2467# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002468 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002469 s = getcmdline_int(firstc, 1L, 0, FALSE);
2470
2471 if (did_save_ccline)
2472 restore_cmdline(&save_ccline);
2473
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002474 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002475 // Restore msg_col, the prompt from input() may have changed it.
2476 // But only if called recursively and the commandline is therefore being
2477 // restored to an old one; if not, the input() prompt stays on the screen,
2478 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002479 if (ccline.cmdbuff != NULL)
2480 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
2482 return s;
2483}
2484#endif
2485
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002486/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002487 * Read the 'wildmode' option, fill wim_flags[].
2488 */
2489 int
2490check_opt_wim(void)
2491{
2492 char_u new_wim_flags[4];
2493 char_u *p;
2494 int i;
2495 int idx = 0;
2496
2497 for (i = 0; i < 4; ++i)
2498 new_wim_flags[i] = 0;
2499
2500 for (p = p_wim; *p; ++p)
2501 {
2502 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2503 ;
2504 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2505 return FAIL;
2506 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2507 new_wim_flags[idx] |= WIM_LONGEST;
2508 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2509 new_wim_flags[idx] |= WIM_FULL;
2510 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2511 new_wim_flags[idx] |= WIM_LIST;
2512 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2513 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2514 else
2515 return FAIL;
2516 p += i;
2517 if (*p == NUL)
2518 break;
2519 if (*p == ',')
2520 {
2521 if (idx == 3)
2522 return FAIL;
2523 ++idx;
2524 }
2525 }
2526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002527 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002528 while (idx < 3)
2529 {
2530 new_wim_flags[idx + 1] = new_wim_flags[idx];
2531 ++idx;
2532 }
2533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002534 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002535 for (i = 0; i < 4; ++i)
2536 wim_flags[i] = new_wim_flags[i];
2537 return OK;
2538}
2539
2540/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002541 * Return TRUE when the text must not be changed and we can't switch to
2542 * another window or buffer. Used when editing the command line, evaluating
2543 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002544 */
2545 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002546text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002547{
2548#ifdef FEAT_CMDWIN
2549 if (cmdwin_type != 0)
2550 return TRUE;
2551#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002552 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002553}
2554
2555/*
2556 * Give an error message for a command that isn't allowed while the cmdline
2557 * window is open or editing the cmdline in another way.
2558 */
2559 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002560text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002561{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002563}
2564
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002565 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002566get_text_locked_msg(void)
2567{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002568#ifdef FEAT_CMDWIN
2569 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002570 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002571#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002572 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002573}
2574
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002575/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002576 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2577 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002578 */
2579 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002580curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002581{
2582 if (curbuf_lock > 0)
2583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002584 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585 return TRUE;
2586 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002587 return allbuf_locked();
2588}
2589
2590/*
2591 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2592 * message.
2593 */
2594 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002595allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002596{
2597 if (allbuf_lock > 0)
2598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002599 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002600 return TRUE;
2601 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002602 return FALSE;
2603}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002606cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002609 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 return 1;
2611#endif
2612 return ptr2cells(ccline.cmdbuff + idx);
2613}
2614
2615/*
2616 * Compute the offset of the cursor on the command line for the prompt and
2617 * indent.
2618 */
2619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002620set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002622 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ccline.cmdspos = 1 + ccline.cmdindent;
2624 else
2625 ccline.cmdspos = 0 + ccline.cmdindent;
2626}
2627
2628/*
2629 * Compute the screen position for the cursor on the command line.
2630 */
2631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002632set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 int i, m, c;
2635
2636 set_cmdspos();
2637 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002640 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002641 m = MAXCOL;
2642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 else
2644 m = MAXCOL;
2645 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2646 {
2647 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002648 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (has_mbyte)
2650 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002651 // If the cmdline doesn't fit, show cursor on last visible char.
2652 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if ((ccline.cmdspos += c) >= m)
2654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ccline.cmdspos -= c;
2656 break;
2657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002659 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661}
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
2664 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2665 * character that doesn't fit, so that a ">" must be displayed.
2666 */
2667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002668correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002670 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2672 && ccline.cmdspos % Columns + cells > Columns)
2673 ccline.cmdspos++;
2674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676/*
2677 * Get an Ex command line for the ":" command.
2678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002680getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002681 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002682 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002683 int indent, // indent for inside conditionals
Bram Moolenaare96a2492019-06-25 04:12:16 +02002684 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002686 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (exec_from_reg && vpeekc() == ':')
2688 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002689 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692/*
2693 * Get an Ex command line for Ex mode.
2694 * In Ex mode we only use the OS supplied line editing features and no
2695 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002699getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002700 int promptc, // normally ':', NUL for ":append" and '?' for
2701 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002702 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002703 int indent, // indent for inside conditionals
Bram Moolenaare96a2492019-06-25 04:12:16 +02002704 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 garray_T line_ga;
2707 char_u *pend;
2708 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002709 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002710 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711 int vcol = 0;
2712 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002713 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002714 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002716 // Switch cursor on now. This avoids that it happens after the "\n", which
2717 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 cursor_on();
2719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002720 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002726 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 if (p_prompt)
2728 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 while (indent-- > 0)
2730 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733
2734 ga_init2(&line_ga, 1, 30);
2735
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002736 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002738 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002739 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002740 while (indent >= 8)
2741 {
2742 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002743 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002744 indent -= 8;
2745 }
2746 while (indent-- > 0)
2747 {
2748 ga_append(&line_ga, ' ');
2749 msg_putchar(' ');
2750 }
2751 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 ++no_mapping;
2753 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 /*
2756 * Get the line, one character at a time.
2757 */
2758 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002761 long sw;
2762 char_u *s;
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (ga_grow(&line_ga, 40) == FAIL)
2765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaarba748c82017-02-26 14:00:07 +01002767 /*
2768 * Get one character at a time.
2769 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002770 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002772 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002773 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2774 c1 = '\n';
2775 else
2776 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
2778 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 * Handle line editing.
2780 * Previously this was left to the system, putting the terminal in
2781 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 msg_putchar('\n');
2786 break;
2787 }
2788
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002789 if (c1 == K_PS)
2790 {
2791 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2792 goto redraw;
2793 }
2794
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 if (!escaped)
2796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002797 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 if (c1 == '\r')
2799 c1 = '\n';
2800
2801 if (c1 == BS || c1 == K_BS
2802 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 if (line_ga.ga_len > 0)
2805 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002806 if (has_mbyte)
2807 {
2808 p = (char_u *)line_ga.ga_data;
2809 p[line_ga.ga_len] = NUL;
2810 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2811 line_ga.ga_len -= len;
2812 }
2813 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002814 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815 goto redraw;
2816 }
2817 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 msg_col = startcol;
2823 msg_clr_eos();
2824 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002825 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 }
2827
2828 if (c1 == Ctrl_T)
2829 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002830 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 p = (char_u *)line_ga.ga_data;
2832 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002833 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002834 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002836 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002838 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002839 p = (char_u *)line_ga.ga_data;
2840 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2842 *s = ' ';
2843 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002846 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002849 p = (char_u *)line_ga.ga_data;
2850 p[line_ga.ga_len] = NUL;
2851 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002857 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002858 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002862 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002863 msg_outtrans_len(p, len);
2864 vcol += ptr2cells(p);
2865 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002869 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870 continue;
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 if (c1 == Ctrl_D)
2874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 p = (char_u *)line_ga.ga_data;
2877 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002879 if (prev_char == '^')
2880 ex_keep_indent = TRUE;
2881 indent = 0;
2882 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 else
2885 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002887 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002888 if (indent > 0)
2889 {
2890 --indent;
2891 indent -= indent % get_sw_value(curbuf);
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002894 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002896 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002897 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2898 --line_ga.ga_len;
2899 }
2900 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902
2903 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2904 {
2905 escaped = TRUE;
2906 continue;
2907 }
2908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002909 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 if (IS_SPECIAL(c1))
2911 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913
2914 if (IS_SPECIAL(c1))
2915 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002916 if (has_mbyte)
2917 len = (*mb_char2bytes)(c1,
2918 (char_u *)line_ga.ga_data + line_ga.ga_len);
2919 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002920 {
2921 len = 1;
2922 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2923 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002924 if (c1 == '\n')
2925 msg_putchar('\n');
2926 else if (c1 == TAB)
2927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002928 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002929 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002930 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002931 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002935 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002936 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002937 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002939 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002940 escaped = FALSE;
2941
2942 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002943 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 // We are done when a NL is entered, but not when it comes after an
2946 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002947 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002949 int bcount = 0;
2950
2951 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2952 ++bcount;
2953
2954 if (bcount > 0)
2955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002956 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2957 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002958 line_ga.ga_len -= (bcount + 1) / 2;
2959 pend -= (bcount + 1) / 2;
2960 pend[-1] = '\n';
2961 }
2962
2963 if ((bcount & 1) == 0)
2964 {
2965 --line_ga.ga_len;
2966 --pend;
2967 *pend = NUL;
2968 break;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971 }
2972
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002973 --no_mapping;
2974 --allow_keys;
2975
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002976 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 msg_didout = FALSE;
2978 msg_col = 0;
2979 if (msg_row < Rows - 1)
2980 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002981 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 if (got_int)
2984 ga_clear(&line_ga);
2985
2986 return (char_u *)line_ga.ga_data;
2987}
2988
Bram Moolenaare344bea2005-09-01 20:46:49 +00002989# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2990 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991/*
2992 * Return TRUE if ccline.overstrike is on.
2993 */
2994 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 return ccline.overstrike;
2998}
2999
3000/*
3001 * Return TRUE if the cursor is at the end of the cmdline.
3002 */
3003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003004cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 return (ccline.cmdpos >= ccline.cmdlen);
3007}
3008#endif
3009
Bram Moolenaar9372a112005-12-06 19:59:18 +00003010#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011/*
3012 * Return the virtual column number at the current cursor position.
3013 * This is used by the IM code to obtain the start of the preedit string.
3014 */
3015 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003016cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3019 return MAXCOL;
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (has_mbyte)
3022 {
3023 colnr_T col;
3024 int i = 0;
3025
3026 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003027 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 return col;
3030 }
3031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 return ccline.cmdpos;
3033}
3034#endif
3035
3036#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3037/*
3038 * If part of the command line is an IM preedit string, redraw it with
3039 * IM feedback attributes. The cursor position is restored after drawing.
3040 */
3041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 if ((State & CMDLINE)
3045 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003046 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 && !p_imdisable
3048 && im_is_preediting())
3049 {
3050 int cmdpos = 0;
3051 int cmdspos;
3052 int old_row;
3053 int old_col;
3054 colnr_T col;
3055
3056 old_row = msg_row;
3057 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003058 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (has_mbyte)
3061 {
3062 for (col = 0; col < preedit_start_col
3063 && cmdpos < ccline.cmdlen; ++col)
3064 {
3065 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003066 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 }
3068 }
3069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 cmdspos += preedit_start_col;
3072 cmdpos += preedit_start_col;
3073 }
3074
3075 msg_row = cmdline_row + (cmdspos / (int)Columns);
3076 msg_col = cmdspos % (int)Columns;
3077 if (msg_row >= Rows)
3078 msg_row = Rows - 1;
3079
3080 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3081 {
3082 int char_len;
3083 int char_attr;
3084
3085 char_attr = im_get_feedback_attr(col);
3086 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003087 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003090 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 char_len = 1;
3093
3094 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3095 cmdpos += char_len;
3096 }
3097
3098 msg_row = old_row;
3099 msg_col = old_col;
3100 }
3101}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003102#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104/*
3105 * Allocate a new command line buffer.
3106 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
3108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 /*
3112 * give some extra space to avoid having to allocate all the time
3113 */
3114 if (len < 80)
3115 len = 100;
3116 else
3117 len += 20;
3118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003119 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 ccline.cmdbufflen = len;
3121}
3122
3123/*
3124 * Re-allocate the command line to length len + something extra.
3125 * return FAIL for failure, OK otherwise
3126 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 char_u *p;
3131
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003132 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003133 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003136 alloc_cmdbuff(len); // will get some more
3137 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003139 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return FAIL;
3141 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003142 // There isn't always a NUL after the command, but it may need to be
3143 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003144 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3145 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003147
3148 if (ccline.xpc != NULL
3149 && ccline.xpc->xp_pattern != NULL
3150 && ccline.xpc->xp_context != EXPAND_NOTHING
3151 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3152 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003153 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003155 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3156 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003157 if (i >= 0 && i <= ccline.cmdlen)
3158 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3159 }
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return OK;
3162}
3163
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003164#if defined(FEAT_ARABIC) || defined(PROTO)
3165static char_u *arshape_buf = NULL;
3166
3167# if defined(EXITFREE) || defined(PROTO)
3168 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003169free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003170{
3171 vim_free(arshape_buf);
3172}
3173# endif
3174#endif
3175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176/*
3177 * Draw part of the cmdline at the current cursor position. But draw stars
3178 * when cmdline_star is TRUE.
3179 */
3180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003181draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3184 int i;
3185
3186 if (cmdline_star > 0)
3187 for (i = 0; i < len; ++i)
3188 {
3189 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003191 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 else
3194#endif
3195#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003196 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 static int buflen = 0;
3199 char_u *p;
3200 int j;
3201 int newlen = 0;
3202 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003203 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int prev_c = 0;
3205 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003206 int u8c;
3207 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
3210 /*
3211 * Do arabic shaping into a temporary buffer. This is very
3212 * inefficient!
3213 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003214 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003216 // Re-allocate the buffer. We keep it around to avoid a lot of
3217 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003218 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003219 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003220 arshape_buf = alloc(buflen);
3221 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003222 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003225 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003227 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003228 arshape_buf[0] = ' ';
3229 newlen = 1;
3230 }
3231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 for (j = start; j < start + len; j += mb_l)
3233 {
3234 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003235 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003236 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (ARABIC_CHAR(u8c))
3238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003239 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (cmdmsg_rl)
3241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003242 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 pc = prev_c;
3244 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003245 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (j + mb_l >= start + len)
3247 nc = NUL;
3248 else
3249 nc = utf_ptr2char(p + mb_l);
3250 }
3251 else
3252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003253 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 if (j + mb_l >= start + len)
3255 pc = NUL;
3256 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003257 {
3258 int pcc[MAX_MCO];
3259
3260 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003262 pc1 = pcc[0];
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 nc = prev_c;
3265 }
3266 prev_c = u8c;
3267
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003268 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003271 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003273 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3274 if (u8cc[1] != 0)
3275 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 }
3279 else
3280 {
3281 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003282 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 newlen += mb_l;
3284 }
3285 }
3286
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 else
3290#endif
3291 msg_outtrans_len(ccline.cmdbuff + start, len);
3292}
3293
3294/*
3295 * Put a character on the command line. Shifts the following text to the
3296 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3297 * "c" must be printable (fit in one display cell)!
3298 */
3299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003300putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 if (cmd_silent)
3303 return;
3304 msg_no_more = TRUE;
3305 msg_putchar(c);
3306 if (shift)
3307 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3308 msg_no_more = FALSE;
3309 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003310 extra_char = c;
3311 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313
3314/*
3315 * Undo a putcmdline(c, FALSE).
3316 */
3317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003318unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 if (cmd_silent)
3321 return;
3322 msg_no_more = TRUE;
3323 if (ccline.cmdlen == ccline.cmdpos)
3324 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003325 else if (has_mbyte)
3326 draw_cmdline(ccline.cmdpos,
3327 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else
3329 draw_cmdline(ccline.cmdpos, 1);
3330 msg_no_more = FALSE;
3331 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003332 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333}
3334
3335/*
3336 * Put the given string, of the given length, onto the command line.
3337 * If len is -1, then STRLEN() is used to calculate the length.
3338 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3339 * part will be redrawn, otherwise it will not. If this function is called
3340 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3341 * called afterwards.
3342 */
3343 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003344put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int retval;
3347 int i;
3348 int m;
3349 int c;
3350
3351 if (len < 0)
3352 len = (int)STRLEN(str);
3353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003354 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003356 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
3358 retval = OK;
3359 if (retval == OK)
3360 {
3361 if (!ccline.overstrike)
3362 {
3363 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3364 ccline.cmdbuff + ccline.cmdpos,
3365 (size_t)(ccline.cmdlen - ccline.cmdpos));
3366 ccline.cmdlen += len;
3367 }
3368 else
3369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (has_mbyte)
3371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003374 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003376 // Count nr of bytes in cmdline that are overwritten by these
3377 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003379 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 --m;
3381 if (i < ccline.cmdlen)
3382 {
3383 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3384 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3385 ccline.cmdlen += ccline.cmdpos + len - i;
3386 }
3387 else
3388 ccline.cmdlen = ccline.cmdpos + len;
3389 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003390 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 ccline.cmdlen = ccline.cmdpos + len;
3392 }
3393 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3394 ccline.cmdbuff[ccline.cmdlen] = NUL;
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (enc_utf8)
3397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003398 // When the inserted text starts with a composing character,
3399 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 i = 0;
3401 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3402 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3403 {
3404 i = (*mb_head_off)(ccline.cmdbuff,
3405 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3406 ccline.cmdpos -= i;
3407 len += i;
3408 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3409 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003410#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3412 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003413 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 i = (*mb_head_off)(ccline.cmdbuff,
3415 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3416 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3417 + ccline.cmdpos - i), c))
3418 {
3419 ccline.cmdpos -= i;
3420 len += i;
3421 }
3422 else
3423 i = 0;
3424 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (i != 0)
3427 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003428 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3430 ccline.cmdspos -= i;
3431 msg_col -= i;
3432 if (msg_col < 0)
3433 {
3434 msg_col += Columns;
3435 --msg_row;
3436 }
3437 }
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 if (redraw && !cmd_silent)
3441 {
3442 msg_no_more = TRUE;
3443 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003444 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003446 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (cmdline_row != i || ccline.overstrike)
3448 msg_clr_eos();
3449 msg_no_more = FALSE;
3450 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003451 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003453 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003454 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003456 }
3457 else
3458 m = MAXCOL;
3459 for (i = 0; i < len; ++i)
3460 {
3461 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003462 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003463 if (has_mbyte)
3464 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003465 // Stop cursor at the end of the screen, but do increment the
3466 // insert position, so that entering a very long command
3467 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003468 if (ccline.cmdspos + c < m)
3469 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003470
Bram Moolenaar14184a32019-02-16 15:10:30 +01003471 if (has_mbyte)
3472 {
3473 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3474 if (c > len - i - 1)
3475 c = len - i - 1;
3476 ccline.cmdpos += c;
3477 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003479 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 }
3482 if (redraw)
3483 msg_check();
3484 return retval;
3485}
3486
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487static cmdline_info_T prev_ccline;
3488static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003489
3490/*
3491 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3492 * and overwrite it. But get_cmdline_str() may need it, thus make it
3493 * available globally in prev_ccline.
3494 */
3495 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003497{
3498 if (!prev_ccline_used)
3499 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003501 prev_ccline_used = TRUE;
3502 }
3503 *ccp = prev_ccline;
3504 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003505 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003506}
3507
3508/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003509 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003510 */
3511 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003512restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003513{
3514 ccline = prev_ccline;
3515 prev_ccline = *ccp;
3516}
3517
Bram Moolenaar8299df92004-07-10 09:47:34 +00003518/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003519 * Paste a yank register into the command line.
3520 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003521 * insert_reg() can't be used here, because special characters from the
3522 * register contents will be interpreted as commands.
3523 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003524 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003525 */
3526 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003527cmdline_paste(
3528 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003529 int literally, // Insert text literally instead of "as typed"
3530 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531{
3532 long i;
3533 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003534 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003537 // check for valid regname; also accept special characters for CTRL-R in
3538 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003540 && regname != Ctrl_A && regname != Ctrl_L
3541 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003542 return FAIL;
3543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003544 // A register containing CTRL-R can cause an endless loop. Allow using
3545 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003546 line_breakcheck();
3547 if (got_int)
3548 return FAIL;
3549
3550#ifdef FEAT_CLIPBOARD
3551 regname = may_get_selection(regname);
3552#endif
3553
Bram Moolenaar438d1762018-09-30 17:11:48 +02003554 // Need to set "textlock" to avoid nasty things like going to another
3555 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003556 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003557 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003558 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003559
3560 if (i)
3561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003562 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003563 if (arg == NULL)
3564 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003566 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3567 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003568 p = arg;
3569 if (p_is && regname == Ctrl_W)
3570 {
3571 char_u *w;
3572 int len;
3573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003574 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003575 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003576 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003577 if (has_mbyte)
3578 {
3579 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3580 if (!vim_iswordc(mb_ptr2char(w - len)))
3581 break;
3582 w -= len;
3583 }
3584 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003585 {
3586 if (!vim_iswordc(w[-1]))
3587 break;
3588 --w;
3589 }
3590 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003591 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003592 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3593 p += len;
3594 }
3595
3596 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597 if (allocated)
3598 vim_free(arg);
3599 return OK;
3600 }
3601
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003602 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603}
3604
3605/*
3606 * Put a string on the command line.
3607 * When "literally" is TRUE, insert literally.
3608 * When "literally" is FALSE, insert as typed, but don't leave the command
3609 * line.
3610 */
3611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003612cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613{
3614 int c, cv;
3615
3616 if (literally)
3617 put_on_cmdline(s, -1, TRUE);
3618 else
3619 while (*s != NUL)
3620 {
3621 cv = *s;
3622 if (cv == Ctrl_V && s[1])
3623 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003624 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003625 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003626 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003627 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003628 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3629 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630#ifdef UNIX
3631 || c == intr_char
3632#endif
3633 || (c == Ctrl_BSL && *s == Ctrl_N))
3634 stuffcharReadbuff(Ctrl_V);
3635 stuffcharReadbuff(c);
3636 }
3637}
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#ifdef FEAT_WILDMENU
3640/*
3641 * Delete characters on the command line, from "from" to the current
3642 * position.
3643 */
3644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003645cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3648 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3649 ccline.cmdlen -= ccline.cmdpos - from;
3650 ccline.cmdpos = from;
3651}
3652#endif
3653
3654/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003655 * This function is called when the screen size changes and with incremental
3656 * search and in other situations where the command line may have been
3657 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
3659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003660redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003662 redrawcmdline_ex(TRUE);
3663}
3664
3665 void
3666redrawcmdline_ex(int do_compute_cmdrow)
3667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (cmd_silent)
3669 return;
3670 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003671 if (do_compute_cmdrow)
3672 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 redrawcmd();
3674 cursorcmd();
3675}
3676
3677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003678redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 int i;
3681
3682 if (cmd_silent)
3683 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003684 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 msg_putchar(ccline.cmdfirstc);
3686 if (ccline.cmdprompt != NULL)
3687 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003688 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003691 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 --ccline.cmdindent;
3693 }
3694 else
3695 for (i = ccline.cmdindent; i > 0; --i)
3696 msg_putchar(' ');
3697}
3698
3699/*
3700 * Redraw what is currently on the command line.
3701 */
3702 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003703redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 if (cmd_silent)
3706 return;
3707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003708 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003709 if (ccline.cmdbuff == NULL)
3710 {
3711 windgoto(cmdline_row, 0);
3712 msg_clr_eos();
3713 return;
3714 }
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 msg_start();
3717 redrawcmdprompt();
3718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 msg_no_more = TRUE;
3721 draw_cmdline(0, ccline.cmdlen);
3722 msg_clr_eos();
3723 msg_no_more = FALSE;
3724
3725 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003726 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003727 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 /*
3730 * An emsg() before may have set msg_scroll. This is used in normal mode,
3731 * in cmdline mode we can reset them now.
3732 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003733 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003735 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3736 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 skip_redraw = FALSE;
3738}
3739
3740 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003741compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003743 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 cmdline_row = Rows - 1;
3745 else
3746 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003747 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748}
3749
Bram Moolenaar66b51422019-08-18 21:44:12 +02003750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003751cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753 if (cmd_silent)
3754 return;
3755
3756#ifdef FEAT_RIGHTLEFT
3757 if (cmdmsg_rl)
3758 {
3759 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3760 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3761 if (msg_row <= 0)
3762 msg_row = Rows - 1;
3763 }
3764 else
3765#endif
3766 {
3767 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3768 msg_col = ccline.cmdspos % (int)Columns;
3769 if (msg_row >= Rows)
3770 msg_row = Rows - 1;
3771 }
3772
3773 windgoto(msg_row, msg_col);
3774#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003775 if (p_imst == IM_ON_THE_SPOT)
3776 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#endif
3778#ifdef MCH_CURSOR_SHAPE
3779 mch_update_cursor();
3780#endif
3781}
3782
3783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003784gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 msg_start();
3787#ifdef FEAT_RIGHTLEFT
3788 if (cmdmsg_rl)
3789 msg_col = Columns - 1;
3790 else
3791#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003792 msg_col = 0; // always start in column 0
3793 if (clr) // clear the bottom line(s)
3794 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 windgoto(cmdline_row, 0);
3796}
3797
3798/*
3799 * Check the word in front of the cursor for an abbreviation.
3800 * Called when the non-id character "c" has been entered.
3801 * When an abbreviation is recognized it is removed from the text with
3802 * backspaces and the replacement string is inserted, followed by "c".
3803 */
3804 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003805ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003807 int spos = 0;
3808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003809 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 return FALSE;
3811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3813 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003814 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3815 spos++;
3816 if (ccline.cmdlen - spos > 5
3817 && ccline.cmdbuff[spos] == '\''
3818 && ccline.cmdbuff[spos + 2] == ','
3819 && ccline.cmdbuff[spos + 3] == '\'')
3820 spos += 5;
3821 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003822 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003823 spos = 0;
3824
3825 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826}
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003829 * Escape special characters in "fname" for when used as a file name argument
3830 * after a Vim command, or, when "shell" is non-zero, a shell command.
3831 * Returns the result in allocated memory.
3832 */
3833 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003834vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003835{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003836 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003837#ifdef BACKSLASH_IN_FILENAME
3838 char_u buf[20];
3839 int j = 0;
3840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003841 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003842 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003843 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003844 buf[j++] = *p;
3845 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003846 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003847#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003848 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3849 if (shell && csh_like_shell() && p != NULL)
3850 {
3851 char_u *s;
3852
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003853 // For csh and similar shells need to put two backslashes before '!'.
3854 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003855 s = vim_strsave_escaped(p, (char_u *)"!");
3856 vim_free(p);
3857 p = s;
3858 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003859#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003861 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3862 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003863 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003864 escape_fname(&p);
3865
3866 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003867}
3868
3869/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003870 * Put a backslash before the file name in "pp", which is in allocated memory.
3871 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003873escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003874{
3875 char_u *p;
3876
Bram Moolenaar964b3742019-05-24 18:54:09 +02003877 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003878 if (p != NULL)
3879 {
3880 p[0] = '\\';
3881 STRCPY(p + 1, *pp);
3882 vim_free(*pp);
3883 *pp = p;
3884 }
3885}
3886
3887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 * For each file name in files[num_files]:
3889 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3890 */
3891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003892tilde_replace(
3893 char_u *orig_pat,
3894 int num_files,
3895 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
3897 int i;
3898 char_u *p;
3899
3900 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3901 {
3902 for (i = 0; i < num_files; ++i)
3903 {
3904 p = home_replace_save(NULL, files[i]);
3905 if (p != NULL)
3906 {
3907 vim_free(files[i]);
3908 files[i] = p;
3909 }
3910 }
3911 }
3912}
3913
3914/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003915 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003917 cmdline_info_T *
3918get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003920 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921}
3922
Bram Moolenaar064154c2016-03-19 22:50:43 +01003923#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003924/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003925 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003926 * ccline and put the previous value in prev_ccline.
3927 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003928 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003929get_ccline_ptr(void)
3930{
3931 if ((State & CMDLINE) == 0)
3932 return NULL;
3933 if (ccline.cmdbuff != NULL)
3934 return &ccline;
3935 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3936 return &prev_ccline;
3937 return NULL;
3938}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003939#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003940
Bram Moolenaar064154c2016-03-19 22:50:43 +01003941#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003942/*
3943 * Get the current command line in allocated memory.
3944 * Only works when the command line is being edited.
3945 * Returns NULL when something is wrong.
3946 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003947 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003948get_cmdline_str(void)
3949{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003950 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003951
Bram Moolenaaree91c332018-09-25 22:27:35 +02003952 if (cmdline_star > 0)
3953 return NULL;
3954 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003955 if (p == NULL)
3956 return NULL;
3957 return vim_strnsave(p->cmdbuff, p->cmdlen);
3958}
3959
3960/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003961 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003962 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003963 void
3964f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3965{
3966 rettv->v_type = VAR_STRING;
3967 rettv->vval.v_string = get_cmdline_str();
3968}
3969
3970/*
3971 * "getcmdpos()" function
3972 */
3973 void
3974f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003975{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003976 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003977
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003978 rettv->vval.v_number = 0;
3979 if (p != NULL)
3980 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003981}
3982
3983/*
3984 * Set the command line byte position to "pos". Zero is the first position.
3985 * Only works when the command line is being edited.
3986 * Returns 1 when failed, 0 when OK.
3987 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003988 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003989set_cmdline_pos(
3990 int pos)
3991{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003992 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003993
3994 if (p == NULL)
3995 return 1;
3996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003997 // The position is not set directly but after CTRL-\ e or CTRL-R = has
3998 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003999 if (pos < 0)
4000 new_cmdpos = 0;
4001 else
4002 new_cmdpos = pos;
4003 return 0;
4004}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004005
4006/*
4007 * "setcmdpos()" function
4008 */
4009 void
4010f_setcmdpos(typval_T *argvars, typval_T *rettv)
4011{
4012 int pos = (int)tv_get_number(&argvars[0]) - 1;
4013
4014 if (pos >= 0)
4015 rettv->vval.v_number = set_cmdline_pos(pos);
4016}
4017
4018/*
4019 * "getcmdtype()" function
4020 */
4021 void
4022f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4023{
4024 rettv->v_type = VAR_STRING;
4025 rettv->vval.v_string = alloc(2);
4026 if (rettv->vval.v_string != NULL)
4027 {
4028 rettv->vval.v_string[0] = get_cmdline_type();
4029 rettv->vval.v_string[1] = NUL;
4030 }
4031}
4032
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004033#endif
4034
4035#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4036/*
4037 * Get the current command-line type.
4038 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4039 * Only works when the command line is being edited.
4040 * Returns NUL when something is wrong.
4041 */
4042 int
4043get_cmdline_type(void)
4044{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004045 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004046
4047 if (p == NULL)
4048 return NUL;
4049 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004050 return
4051# ifdef FEAT_EVAL
4052 (p->input_fn) ? '@' :
4053# endif
4054 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004055 return p->cmdfirstc;
4056}
4057#endif
4058
Bram Moolenaard7663c22019-08-06 21:59:57 +02004059/*
4060 * Return the first character of the current command line.
4061 */
4062 int
4063get_cmdline_firstc(void)
4064{
4065 return ccline.cmdfirstc;
4066}
4067
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068/*
4069 * Get indices "num1,num2" that specify a range within a list (not a range of
4070 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4071 * Returns OK if parsed successfully, otherwise FAIL.
4072 */
4073 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004074get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
4076 int len;
4077 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004078 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
4080 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004081 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004083 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 *str += len;
4085 *num1 = (int)num;
4086 first = TRUE;
4087 }
4088 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004089 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
4091 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004092 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (len > 0)
4094 {
4095 *num2 = (int)num;
4096 *str = skipwhite(*str + len);
4097 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004098 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004101 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 *num2 = *num1;
4103 return OK;
4104}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#if defined(FEAT_CMDWIN) || defined(PROTO)
4107/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004108 * Check value of 'cedit' and set cedit_key.
4109 * Returns NULL if value is OK, error message otherwise.
4110 */
4111 char *
4112check_cedit(void)
4113{
4114 int n;
4115
4116 if (*p_cedit == NUL)
4117 cedit_key = -1;
4118 else
4119 {
4120 n = string_to_key(p_cedit, FALSE);
4121 if (vim_isprintc(n))
4122 return e_invarg;
4123 cedit_key = n;
4124 }
4125 return NULL;
4126}
4127
4128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Open a window on the current command line and history. Allow editing in
4130 * the window. Returns when the window is closed.
4131 * Returns:
4132 * CR if the command is to be executed
4133 * Ctrl_C if it is to be abandoned
4134 * K_IGNORE if editing continues
4135 */
4136 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004137open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004139 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004141 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 win_T *wp;
4143 int i;
4144 linenr_T lnum;
4145 int histtype;
4146 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int save_restart_edit = restart_edit;
4148 int save_State = State;
4149 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004150#ifdef FEAT_RIGHTLEFT
4151 int save_cmdmsg_rl = cmdmsg_rl;
4152#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004153#ifdef FEAT_FOLDING
4154 int save_KeyTyped;
4155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 if (cmdwin_type != 0
4159# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4160 || cmdline_star > 0
4161# endif
4162 )
4163 {
4164 beep_flush();
4165 return K_IGNORE;
4166 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004167 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004169 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 win_size_save(&winsizes);
4171
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004172 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4173 // command line window, but we don't want the popup menu then.
4174 pum_undisplay();
4175
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004176 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004177 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004178 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004179
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004180 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4182 {
4183 beep_flush();
4184 return K_IGNORE;
4185 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004186 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004188 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004189 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004190 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004193#ifdef FEAT_FOLDING
4194 curwin->w_p_fen = FALSE;
4195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004197 curwin->w_p_rl = cmdmsg_rl;
4198 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004200 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004202 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004203 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004205 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004206 need_wait_return = FALSE;
4207
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004208 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4210 {
4211 if (p_wc == TAB)
4212 {
4213 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4214 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4215 }
4216 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4217 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004218 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004220 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4221 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004222 curbuf->b_p_tw = 0;
4223
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004224 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004226 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004228 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (i >= 0)
4230 {
4231 lnum = 0;
4232 do
4233 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004234 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004236 if (get_histentry(histtype)[i].hisstr != NULL)
4237 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 (colnr_T)0, FALSE);
4239 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004240 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 }
4243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004244 // Replace the empty last line with the current command-line and put the
4245 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4247 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4248 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004249 changed_line_abv_curs();
4250 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004251 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar23324a02019-10-01 17:39:04 +02004253 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 exmode_active = 0;
4255
4256 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaar23324a02019-10-01 17:39:04 +02004259 // Reset here so it can be set by a CmdWinEnter autocommand.
4260 cmdwin_result = 0;
4261
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004262 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004263 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004264 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004265 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 i = RedrawingDisabled;
4268 RedrawingDisabled = 0;
4269
4270 /*
4271 * Call the main loop until <CR> or CTRL-C is typed.
4272 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004273 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 RedrawingDisabled = i;
4276
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004277# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004278 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004279# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004280
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004281 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004282 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004283
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004284# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004285 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004286 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287# endif
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 exmode_active = save_exmode;
4291
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004292 // Safety check: The old window or buffer was deleted: It's a bug when
4293 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004294 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004297 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else
4300 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004301# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004302 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (aborting() && cmdwin_result != K_IGNORE)
4304 cmdwin_result = Ctrl_C;
4305# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004306 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004308 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004310 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4311
4312 if (histtype == HIST_CMD)
4313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004314 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004315 ccline.cmdbuff = vim_strsave((char_u *)p);
4316 cmdwin_result = CAR;
4317 }
4318 else
4319 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004320 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004321 ccline.cmdbuff = NULL;
4322 stuffcharReadbuff(':');
4323 stuffReadbuff((char_u *)p);
4324 stuffcharReadbuff(CAR);
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004327 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4330 cmdwin_result = CAR;
4331 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004332 else if (cmdwin_result == Ctrl_C)
4333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004334 // :q or :close, don't execute any command
4335 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004336 ccline.cmdbuff = NULL;
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 else
4339 ccline.cmdbuff = vim_strsave(ml_get_curline());
4340 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004341 {
4342 ccline.cmdbuff = vim_strsave((char_u *)"");
4343 ccline.cmdlen = 0;
4344 ccline.cmdbufflen = 1;
4345 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 else
4349 {
4350 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4351 ccline.cmdbufflen = ccline.cmdlen + 1;
4352 ccline.cmdpos = curwin->w_cursor.col;
4353 if (ccline.cmdpos > ccline.cmdlen)
4354 ccline.cmdpos = ccline.cmdlen;
4355 if (cmdwin_result == K_IGNORE)
4356 {
4357 set_cmdspos_cursor();
4358 redrawcmd();
4359 }
4360 }
4361
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004362# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004363 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004364 curwin->w_p_cole = 0;
4365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004367 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 win_goto(old_curwin);
4369 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004370
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004371 // win_close() may have already wiped the buffer when 'bh' is
4372 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004373 if (bufref_valid(&bufref))
4374 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004376 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 ga_clear(&winsizes);
4381 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004382# ifdef FEAT_RIGHTLEFT
4383 cmdmsg_rl = save_cmdmsg_rl;
4384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 return cmdwin_result;
4390}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004391#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393/*
4394 * Used for commands that either take a simple command string argument, or:
4395 * cmd << endmarker
4396 * {script}
4397 * endmarker
4398 * Returns a pointer to allocated memory with {script} or NULL.
4399 */
4400 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
4403 char_u *theline;
4404 char *end_pattern = NULL;
4405 char dot[] = ".";
4406 garray_T ga;
4407
4408 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4409 return NULL;
4410
4411 ga_init2(&ga, 1, 0x400);
4412
4413 if (cmd[2] != NUL)
4414 end_pattern = (char *)skipwhite(cmd + 2);
4415 else
4416 end_pattern = dot;
4417
4418 for (;;)
4419 {
4420 theline = eap->getline(
4421#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004422 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004424 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004427 {
4428 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 ga_concat(&ga, theline);
4433 ga_append(&ga, '\n');
4434 vim_free(theline);
4435 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004436 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 return (char_u *)ga.ga_data;
4439}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004440
4441#if defined(FEAT_EVAL) || defined(PROTO)
4442/*
4443 * This function is used by f_input() and f_inputdialog() functions. The third
4444 * argument to f_input() specifies the type of completion to use at the
4445 * prompt. The third argument to f_inputdialog() specifies the value to return
4446 * when the user cancels the prompt.
4447 */
4448 void
4449get_user_input(
4450 typval_T *argvars,
4451 typval_T *rettv,
4452 int inputdialog,
4453 int secret)
4454{
4455 char_u *prompt = tv_get_string_chk(&argvars[0]);
4456 char_u *p = NULL;
4457 int c;
4458 char_u buf[NUMBUFLEN];
4459 int cmd_silent_save = cmd_silent;
4460 char_u *defstr = (char_u *)"";
4461 int xp_type = EXPAND_NOTHING;
4462 char_u *xp_arg = NULL;
4463
4464 rettv->v_type = VAR_STRING;
4465 rettv->vval.v_string = NULL;
4466
4467#ifdef NO_CONSOLE_INPUT
4468 // While starting up, there is no place to enter text. When running tests
4469 // with --not-a-term we assume feedkeys() will be used.
4470 if (no_console_input() && !is_not_a_term())
4471 return;
4472#endif
4473
4474 cmd_silent = FALSE; // Want to see the prompt.
4475 if (prompt != NULL)
4476 {
4477 // Only the part of the message after the last NL is considered as
4478 // prompt for the command line
4479 p = vim_strrchr(prompt, '\n');
4480 if (p == NULL)
4481 p = prompt;
4482 else
4483 {
4484 ++p;
4485 c = *p;
4486 *p = NUL;
4487 msg_start();
4488 msg_clr_eos();
4489 msg_puts_attr((char *)prompt, get_echo_attr());
4490 msg_didout = FALSE;
4491 msg_starthere();
4492 *p = c;
4493 }
4494 cmdline_row = msg_row;
4495
4496 if (argvars[1].v_type != VAR_UNKNOWN)
4497 {
4498 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4499 if (defstr != NULL)
4500 stuffReadbuffSpec(defstr);
4501
4502 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4503 {
4504 char_u *xp_name;
4505 int xp_namelen;
4506 long argt;
4507
4508 // input() with a third argument: completion
4509 rettv->vval.v_string = NULL;
4510
4511 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4512 if (xp_name == NULL)
4513 return;
4514
4515 xp_namelen = (int)STRLEN(xp_name);
4516
4517 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4518 &xp_arg) == FAIL)
4519 return;
4520 }
4521 }
4522
4523 if (defstr != NULL)
4524 {
4525 int save_ex_normal_busy = ex_normal_busy;
4526
4527 ex_normal_busy = 0;
4528 rettv->vval.v_string =
4529 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4530 xp_type, xp_arg);
4531 ex_normal_busy = save_ex_normal_busy;
4532 }
4533 if (inputdialog && rettv->vval.v_string == NULL
4534 && argvars[1].v_type != VAR_UNKNOWN
4535 && argvars[2].v_type != VAR_UNKNOWN)
4536 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4537 &argvars[2], buf));
4538
4539 vim_free(xp_arg);
4540
4541 // since the user typed this, no need to wait for return
4542 need_wait_return = FALSE;
4543 msg_didout = FALSE;
4544 }
4545 cmd_silent = cmd_silent_save;
4546}
4547#endif