blob: 1325f77d1e81a429cdd598201a351802b32f72da [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 Moolenaar2f3cd2e2020-09-06 15:54:00 +020020// Return value when handling keys in command-line mode.
21#define CMDLINE_NOT_CHANGED 1
22#define CMDLINE_CHANGED 2
23#define GOTO_NORMAL_MODE 3
Bram Moolenaar9c929712020-09-07 22:05:28 +020024#define PROCESS_NEXT_KEY 4
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +020025
Bram Moolenaar438d1762018-09-30 17:11:48 +020026// The current cmdline_info. It is initialized in getcmdline() and after that
27// used by other functions. When invoking getcmdline() recursively it needs
28// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020029static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
31#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010032static int new_cmdpos; // position set by set_cmdline_pos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
34
Bram Moolenaar217e1b82019-12-01 21:41:28 +010035static int extra_char = NUL; // extra character to display when redrawing
36 // the command line
Bram Moolenaar6a77d262017-07-16 15:24:01 +020037static int extra_char_shift;
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +010040static int cmd_hkmap = 0; // Hebrew mapping during command line
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
42
Bram Moolenaar9c929712020-09-07 22:05:28 +020043static char_u *getcmdline_int(int firstc, long count, int indent, int clear_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010044static int cmdline_charsize(int idx);
45static void set_cmdspos(void);
46static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010047static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010049static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020050static void save_cmdline(cmdline_info_T *ccp);
51static void restore_cmdline(cmdline_info_T *ccp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010052static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010053static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010054static int ccheck_abbr(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
56#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +020057static int open_cmdwin(void);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +010058
59static int cedit_key INIT(= -1); // key value of 'cedit' option
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020062
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020063 static void
64trigger_cmd_autocmd(int typechar, int evt)
65{
66 char_u typestr[2];
67
68 typestr[0] = typechar;
69 typestr[1] = NUL;
70 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
71}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020072
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020074 * Abandon the command line.
75 */
76 static void
77abandon_cmdline(void)
78{
Bram Moolenaard23a8232018-02-10 18:45:26 +010079 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020080 if (msg_scrolled == 0)
81 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010082 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020083 redraw_cmdline = TRUE;
84}
85
Bram Moolenaaree219b02017-12-17 14:55:01 +010086#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020087/*
Bram Moolenaar66216052017-12-16 16:33:44 +010088 * Guess that the pattern matches everything. Only finds specific cases, such
89 * as a trailing \|, which can happen while typing a pattern.
90 */
91 static int
92empty_pattern(char_u *p)
93{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010094 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010095
Bram Moolenaar217e1b82019-12-01 21:41:28 +010096 // remove trailing \v and the like
Bram Moolenaar66216052017-12-16 16:33:44 +010097 while (n >= 2 && p[n - 2] == '\\'
98 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
99 n -= 2;
100 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
101}
102
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200103// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200104typedef struct {
105 colnr_T vs_curswant;
106 colnr_T vs_leftcol;
107 linenr_T vs_topline;
108# ifdef FEAT_DIFF
109 int vs_topfill;
110# endif
111 linenr_T vs_botline;
112 linenr_T vs_empty_rows;
113} viewstate_T;
114
115 static void
116save_viewstate(viewstate_T *vs)
117{
118 vs->vs_curswant = curwin->w_curswant;
119 vs->vs_leftcol = curwin->w_leftcol;
120 vs->vs_topline = curwin->w_topline;
121# ifdef FEAT_DIFF
122 vs->vs_topfill = curwin->w_topfill;
123# endif
124 vs->vs_botline = curwin->w_botline;
125 vs->vs_empty_rows = curwin->w_empty_rows;
126}
127
128 static void
129restore_viewstate(viewstate_T *vs)
130{
131 curwin->w_curswant = vs->vs_curswant;
132 curwin->w_leftcol = vs->vs_leftcol;
133 curwin->w_topline = vs->vs_topline;
134# ifdef FEAT_DIFF
135 curwin->w_topfill = vs->vs_topfill;
136# endif
137 curwin->w_botline = vs->vs_botline;
138 curwin->w_empty_rows = vs->vs_empty_rows;
139}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200140
141// Struct to store the state of 'incsearch' highlighting.
142typedef struct {
143 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200144 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200145 viewstate_T init_viewstate;
146 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200147 pos_T match_start;
148 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200149 int did_incsearch;
150 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200151 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200152} incsearch_state_T;
153
154 static void
155init_incsearch_state(incsearch_state_T *is_state)
156{
157 is_state->match_start = curwin->w_cursor;
158 is_state->did_incsearch = FALSE;
159 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200160 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200161 CLEAR_POS(&is_state->match_end);
162 is_state->save_cursor = curwin->w_cursor; // may be restored later
163 is_state->search_start = curwin->w_cursor;
164 save_viewstate(&is_state->init_viewstate);
165 save_viewstate(&is_state->old_viewstate);
166}
167
168/*
169 * First move cursor to end of match, then to the start. This
170 * moves the whole match onto the screen when 'nowrap' is set.
171 */
172 static void
173set_search_match(pos_T *t)
174{
175 t->lnum += search_match_lines;
176 t->col = search_match_endcol;
177 if (t->lnum > curbuf->b_ml.ml_line_count)
178 {
179 t->lnum = curbuf->b_ml.ml_line_count;
180 coladvance((colnr_T)MAXCOL);
181 }
182}
183
184/*
185 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200186 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200187 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200188 */
189 static int
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200190do_incsearch_highlighting(
191 int firstc,
192 int *search_delim,
193 incsearch_state_T *is_state,
194 int *skiplen,
195 int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200196{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200197 char_u *cmd;
Bram Moolenaare1004402020-10-24 20:49:43 +0200198 cmdmod_T dummy_cmdmod;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200199 char_u *p;
200 int delim_optional = FALSE;
201 int delim;
202 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100203 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200204 exarg_T ea;
205 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200206 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100207 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200208
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200209 *skiplen = 0;
210 *patlen = ccline.cmdlen;
211
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200212 if (!p_is || cmd_silent)
213 return FALSE;
214
215 // by default search all lines
216 search_first_line = 0;
217 search_last_line = MAXLNUM;
218
219 if (firstc == '/' || firstc == '?')
Bram Moolenaarc036e872020-02-21 21:30:52 +0100220 {
221 *search_delim = firstc;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200222 return TRUE;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100223 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200224 if (firstc != ':')
225 return FALSE;
226
Bram Moolenaarc6725252019-11-23 21:56:46 +0100227 ++emsg_off;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200228 CLEAR_FIELD(ea);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200229 ea.line1 = 1;
230 ea.line2 = 1;
231 ea.cmd = ccline.cmdbuff;
232 ea.addr_type = ADDR_LINES;
233
Bram Moolenaare1004402020-10-24 20:49:43 +0200234 CLEAR_FIELD(dummy_cmdmod);
235 parse_command_modifiers(&ea, &dummy, &dummy_cmdmod, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200237 cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200238 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100239 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200240
241 // Skip over "substitute" to find the pattern separator.
242 for (p = cmd; ASCII_ISALPHA(*p); ++p)
243 ;
244 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100245 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200246
247 if (STRNCMP(cmd, "substitute", p - cmd) == 0
248 || STRNCMP(cmd, "smagic", p - cmd) == 0
249 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
250 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200251 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200252 if (*cmd == 's' && cmd[1] == 'm')
253 p_magic = TRUE;
254 else if (*cmd == 's' && cmd[1] == 'n')
255 p_magic = FALSE;
256 }
257 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
258 {
Bram Moolenaar333015a2020-04-25 15:54:16 +0200259 // skip over ! and flags
260 if (*p == '!')
261 p = skipwhite(p + 1);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200262 while (ASCII_ISALPHA(*(p = skipwhite(p))))
263 ++p;
264 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100265 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200266 }
267 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
268 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
269 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
270 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
271 || STRNCMP(cmd, "global", p - cmd) == 0)
272 {
273 // skip over "!"
274 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200275 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276 p++;
277 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100278 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200279 }
280 if (*cmd != 'g')
281 delim_optional = TRUE;
282 }
283 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100284 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200285
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200286 p = skipwhite(p);
287 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100288 *search_delim = delim;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200289 end = skip_regexp(p, delim, p_magic);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200290
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200291 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200292
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200293 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100294 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200295
296 // Don't do 'hlsearch' highlighting if the pattern matches everything.
297 if (!use_last_pat)
298 {
299 char c = *end;
300 int empty;
301
302 *end = NUL;
303 empty = empty_pattern(p);
304 *end = c;
305 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100306 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200307 }
308
309 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200310 *skiplen = (int)(p - ccline.cmdbuff);
311 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200312
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200313 // parse the address range
314 save_cursor = curwin->w_cursor;
315 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200316 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200317 if (ea.addr_count > 0)
318 {
319 // Allow for reverse match.
320 if (ea.line2 < ea.line1)
321 {
322 search_first_line = ea.line2;
323 search_last_line = ea.line1;
324 }
325 else
326 {
327 search_first_line = ea.line1;
328 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 }
330 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200331 else if (cmd[0] == 's' && cmd[1] != 'o')
332 {
333 // :s defaults to the current line
334 search_first_line = curwin->w_cursor.lnum;
335 search_last_line = curwin->w_cursor.lnum;
336 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200337
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200338 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100339 retval = TRUE;
340theend:
341 --emsg_off;
342 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200343}
344
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200345 static void
346finish_incsearch_highlighting(
347 int gotesc,
348 incsearch_state_T *is_state,
349 int call_update_screen)
350{
351 if (is_state->did_incsearch)
352 {
353 is_state->did_incsearch = FALSE;
354 if (gotesc)
355 curwin->w_cursor = is_state->save_cursor;
356 else
357 {
358 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
359 {
360 // put the '" mark at the original position
361 curwin->w_cursor = is_state->save_cursor;
362 setpcmark();
363 }
364 curwin->w_cursor = is_state->search_start;
365 }
366 restore_viewstate(&is_state->old_viewstate);
367 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200368
369 // by default search all lines
370 search_first_line = 0;
371 search_last_line = MAXLNUM;
372
373 p_magic = is_state->magic_save;
374
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100375 validate_cursor(); // needed for TAB
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200376 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200377 if (call_update_screen)
378 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200379 }
380}
381
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200382/*
383 * Do 'incsearch' highlighting if desired.
384 */
385 static void
386may_do_incsearch_highlighting(
387 int firstc,
388 long count,
389 incsearch_state_T *is_state)
390{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200391 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200392 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200393 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200394#ifdef FEAT_RELTIME
395 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200396 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200397#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200398 int next_char;
399 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200400 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100401 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402
Bram Moolenaar198cb662018-09-06 21:44:17 +0200403 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100404 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200405 save_last_search_pattern();
406
Bram Moolenaara60053b2020-09-03 16:50:13 +0200407 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
408 &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200409 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200410 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200411 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200412 if (did_do_incsearch && vpeekc() == NUL)
413 // may have skipped a redraw, do it now
414 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200415 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200416 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200417
418 // If there is a character waiting, search and redraw later.
419 if (char_avail())
420 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200421 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422 is_state->incsearch_postponed = TRUE;
423 return;
424 }
425 is_state->incsearch_postponed = FALSE;
426
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200427 if (search_first_line == 0)
428 // start at the original cursor position
429 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100430 else if (search_first_line > curbuf->b_ml.ml_line_count)
431 {
432 // start after the last line
433 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
434 curwin->w_cursor.col = MAXCOL;
435 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200436 else
437 {
438 // start at the first line in the range
439 curwin->w_cursor.lnum = search_first_line;
440 curwin->w_cursor.col = 0;
441 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200442
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 // Use the previous pattern for ":s//".
444 next_char = ccline.cmdbuff[skiplen + patlen];
445 use_last_pat = patlen == 0 && skiplen > 0
446 && ccline.cmdbuff[skiplen - 1] == next_char;
447
448 // If there is no pattern, don't do anything.
449 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200450 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200451 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200452 set_no_hlsearch(TRUE); // turn off previous highlight
453 redraw_all_later(SOME_VALID);
454 }
455 else
456 {
457 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
458
459 cursor_off(); // so the user knows we're busy
460 out_flush();
461 ++emsg_off; // so it doesn't beep if bad expr
462#ifdef FEAT_RELTIME
463 // Set the time limit to half a second.
464 profile_setlimit(500L, &tm);
465#endif
466 if (!p_hls)
467 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200468 if (search_first_line != 0)
469 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200470 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200471#ifdef FEAT_RELTIME
Bram Moolenaara80faa82020-04-12 19:37:17 +0200472 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200473 sia.sa_tm = &tm;
474#endif
Bram Moolenaarc036e872020-02-21 21:30:52 +0100475 found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200476 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200477#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200478 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200480 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200481#endif
482 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200483 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200484 --emsg_off;
485
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200486 if (curwin->w_cursor.lnum < search_first_line
487 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200488 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200489 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200490 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200491 curwin->w_cursor = is_state->search_start;
492 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200493
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200494 // if interrupted while searching, behave like it failed
495 if (got_int)
496 {
497 (void)vpeekc(); // remove <C-C> from input stream
498 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200499 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500 }
501 else if (char_avail())
502 // cancelled searching because a char was typed
503 is_state->incsearch_postponed = TRUE;
504 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200505 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200506 highlight_match = TRUE; // highlight position
507 else
508 highlight_match = FALSE; // remove highlight
509
510 // First restore the old curwin values, so the screen is positioned in the
511 // same way as the actual search command.
512 restore_viewstate(&is_state->old_viewstate);
513 changed_cline_bef_curs();
514 update_topline();
515
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200516 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200517 {
518 pos_T save_pos = curwin->w_cursor;
519
520 is_state->match_start = curwin->w_cursor;
521 set_search_match(&curwin->w_cursor);
522 validate_cursor();
523 end_pos = curwin->w_cursor;
524 is_state->match_end = end_pos;
525 curwin->w_cursor = save_pos;
526 }
527 else
528 end_pos = curwin->w_cursor; // shutup gcc 4
529
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200530 // Disable 'hlsearch' highlighting if the pattern matches everything.
531 // Avoids a flash when typing "foo\|".
532 if (!use_last_pat)
533 {
534 next_char = ccline.cmdbuff[skiplen + patlen];
535 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200536 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
537 {
538 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200539 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200540 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200541 ccline.cmdbuff[skiplen + patlen] = next_char;
542 }
543
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200544 validate_cursor();
545 // May redraw the status line to show the cursor position.
546 if (p_ru && curwin->w_status_height > 0)
547 curwin->w_redr_status = TRUE;
548
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200550 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200551 restore_last_search_pattern();
552
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200553 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
554 // end of the pattern, e.g. for ":s/pat/".
555 if (ccline.cmdbuff[skiplen + patlen] != NUL)
556 curwin->w_cursor = is_state->search_start;
557 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200558 curwin->w_cursor = end_pos;
559
560 msg_starthere();
561 redrawcmdline();
562 is_state->did_incsearch = TRUE;
563}
564
565/*
566 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
567 * or previous match.
568 * Returns FAIL when jumping to cmdline_not_changed;
569 */
570 static int
571may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200572 int firstc,
573 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200575 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200577 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578 pos_T t;
579 char_u *pat;
580 int search_flags = SEARCH_NOOF;
581 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200582 int save;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100583 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200584
Bram Moolenaar198cb662018-09-06 21:44:17 +0200585 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100586 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200587 save_last_search_pattern();
588
Bram Moolenaarc036e872020-02-21 21:30:52 +0100589 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200590 {
591 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200593 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200594 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200595 {
596 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200598 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599
Bram Moolenaarc036e872020-02-21 21:30:52 +0100600 if (search_delim == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200601 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200603 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200604 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200605 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200606 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200607 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200608
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609 cursor_off();
610 out_flush();
611 if (c == Ctrl_G)
612 {
613 t = is_state->match_end;
614 if (LT_POS(is_state->match_start, is_state->match_end))
615 // Start searching at the end of the match not at the beginning of
616 // the next column.
617 (void)decl(&t);
618 search_flags += SEARCH_COL;
619 }
620 else
621 t = is_state->match_start;
622 if (!p_hls)
623 search_flags += SEARCH_KEEP;
624 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200625 save = pat[patlen];
626 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100627 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200629 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200631 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200632 if (i)
633 {
634 is_state->search_start = is_state->match_start;
635 is_state->match_end = t;
636 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200637 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200638 {
639 // Move just before the current match, so that when nv_search
640 // finishes the cursor will be put back on the match.
641 is_state->search_start = t;
642 (void)decl(&is_state->search_start);
643 }
644 else if (c == Ctrl_G && firstc == '?')
645 {
646 // Move just after the current match, so that when nv_search
647 // finishes the cursor will be put back on the match.
648 is_state->search_start = t;
649 (void)incl(&is_state->search_start);
650 }
651 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
652 {
653 // wrap around
654 is_state->search_start = t;
655 if (firstc == '?')
656 (void)incl(&is_state->search_start);
657 else
658 (void)decl(&is_state->search_start);
659 }
660
661 set_search_match(&is_state->match_end);
662 curwin->w_cursor = is_state->match_start;
663 changed_cline_bef_curs();
664 update_topline();
665 validate_cursor();
666 highlight_match = TRUE;
667 save_viewstate(&is_state->old_viewstate);
668 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200669 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200670 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200671 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200672 }
673 else
674 vim_beep(BO_ERROR);
675 restore_last_search_pattern();
676 return FAIL;
677}
678
679/*
680 * When CTRL-L typed: add character from the match to the pattern.
681 * May set "*c" to the added character.
682 * Return OK when jumping to cmdline_not_changed.
683 */
684 static int
685may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
686{
Bram Moolenaarc036e872020-02-21 21:30:52 +0100687 int skiplen, patlen, search_delim;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200688
Bram Moolenaar198cb662018-09-06 21:44:17 +0200689 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100690 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200691 save_last_search_pattern();
692
Bram Moolenaar9b7cce22020-06-06 15:14:08 +0200693 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
694 &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200695 {
696 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200697 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200698 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100699 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200700
701 // Add a character from under the cursor for 'incsearch'.
702 if (is_state->did_incsearch)
703 {
704 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200705 *c = gchar_cursor();
706 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200707 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200708 // If 'ignorecase' and 'smartcase' are set and the
709 // command line has no uppercase characters, convert
710 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200711 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200712 *c = MB_TOLOWER(*c);
Bram Moolenaarc036e872020-02-21 21:30:52 +0100713 if (*c == search_delim || vim_strchr((char_u *)(
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200714 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200715 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200716 // put a backslash before special characters
717 stuffcharReadbuff(*c);
718 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200719 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200720 // add any composing characters
721 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
722 {
723 int save_c = *c;
724
725 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
726 {
727 curwin->w_cursor.col += mb_char2len(*c);
728 *c = gchar_cursor();
729 stuffcharReadbuff(*c);
730 }
731 *c = save_c;
732 }
733 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200734 }
735 }
736 return OK;
737}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200738#endif
739
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200740#ifdef FEAT_ARABIC
741/*
742 * Return TRUE if the command line has an Arabic character at or after "start"
743 * for "len" bytes.
744 */
745 static int
746cmdline_has_arabic(int start, int len)
747{
748 int j;
749 int mb_l;
750 int u8c;
751 char_u *p;
752 int u8cc[MAX_MCO];
753
754 if (!enc_utf8)
755 return FALSE;
756
757 for (j = start; j < start + len; j += mb_l)
758 {
759 p = ccline.cmdbuff + j;
760 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
761 mb_l = utfc_ptr2len_len(p, start + len - j);
762 if (ARABIC_CHAR(u8c))
763 return TRUE;
764 }
765 return FALSE;
766}
767#endif
768
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200769 void
770cmdline_init(void)
771{
Bram Moolenaara80faa82020-04-12 19:37:17 +0200772 CLEAR_FIELD(ccline);
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200773}
774
Bram Moolenaar66216052017-12-16 16:33:44 +0100775/*
Bram Moolenaar9c929712020-09-07 22:05:28 +0200776 * Handle the backslash key pressed in the command-line mode. CTRL-\ CTRL-N
777 * goes to Normal mode, CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is
778 * set, CTRL-\ e prompts for an expression.
779 */
780 static int
781cmdline_handle_backslash_key(int c, int *gotesc)
782{
783 ++no_mapping;
784 ++allow_keys;
785 c = plain_vgetc();
786 --no_mapping;
787 --allow_keys;
788
789 // CTRL-\ e doesn't work when obtaining an expression, unless it
790 // is in a mapping.
791 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
792 || (ccline.cmdfirstc == '=' && KeyTyped)
793#ifdef FEAT_EVAL
794 || cmdline_star > 0
795#endif
796 ))
797 {
798 vungetc(c);
799 return PROCESS_NEXT_KEY;
800 }
801
802#ifdef FEAT_EVAL
803 if (c == 'e')
804 {
805 char_u *p = NULL;
806 int len;
807
808 /*
809 * Replace the command line with the result of an expression.
810 * Need to save and restore the current command line, to be
811 * able to enter a new one...
812 */
813 if (ccline.cmdpos == ccline.cmdlen)
814 new_cmdpos = 99999; // keep it at the end
815 else
816 new_cmdpos = ccline.cmdpos;
817
818 c = get_expr_register();
819 if (c == '=')
820 {
821 // Need to save and restore ccline. And set "textwinlock"
822 // to avoid nasty things like going to another buffer when
823 // evaluating an expression.
824 ++textwinlock;
825 p = get_expr_line();
826 --textwinlock;
827
828 if (p != NULL)
829 {
830 len = (int)STRLEN(p);
831 if (realloc_cmdbuff(len + 1) == OK)
832 {
833 ccline.cmdlen = len;
834 STRCPY(ccline.cmdbuff, p);
835 vim_free(p);
836
837 // Restore the cursor or use the position set with
838 // set_cmdline_pos().
839 if (new_cmdpos > ccline.cmdlen)
840 ccline.cmdpos = ccline.cmdlen;
841 else
842 ccline.cmdpos = new_cmdpos;
843
844 KeyTyped = FALSE; // Don't do p_wc completion.
845 redrawcmd();
846 return CMDLINE_CHANGED;
847 }
848 vim_free(p);
849 }
850 }
851 beep_flush();
852 got_int = FALSE; // don't abandon the command line
853 did_emsg = FALSE;
854 emsg_on_display = FALSE;
855 redrawcmd();
856 return CMDLINE_NOT_CHANGED;
857 }
858#endif
859
860 if (c == Ctrl_G && p_im && restart_edit == 0)
861 restart_edit = 'a';
862 *gotesc = TRUE; // will free ccline.cmdbuff after putting it
863 // in history
864 return GOTO_NORMAL_MODE;
865}
866
867/*
868 * Completion for 'wildchar' or 'wildcharm' key.
869 * - hitting <ESC> twice means: abandon command line.
870 * - wildcard expansion is only done when the 'wildchar' key is really
871 * typed, not when it comes from a macro
872 * Returns CMDLINE_CHANGED if command line is changed or CMDLINE_NOT_CHANGED.
873 */
874 static int
875cmdline_wildchar_complete(
876 int c,
877 int escape,
878 int *did_wild_list,
879 int *wim_index_p,
880 expand_T *xp,
881 int *gotesc)
882{
883 int wim_index = *wim_index_p;
884 int res;
885 int j;
886 int options = WILD_NO_BEEP;
887
888 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
889 options |= WILD_BUFLASTUSED;
890 if (xp->xp_numfiles > 0) // typed p_wc at least twice
891 {
892 // if 'wildmode' contains "list" may still need to list
893 if (xp->xp_numfiles > 1
894 && !*did_wild_list
895 && (wim_flags[wim_index] & WIM_LIST))
896 {
897 (void)showmatches(xp, FALSE);
898 redrawcmd();
899 *did_wild_list = TRUE;
900 }
901 if (wim_flags[wim_index] & WIM_LONGEST)
902 res = nextwild(xp, WILD_LONGEST, options, escape);
903 else if (wim_flags[wim_index] & WIM_FULL)
904 res = nextwild(xp, WILD_NEXT, options, escape);
905 else
906 res = OK; // don't insert 'wildchar' now
907 }
908 else // typed p_wc first time
909 {
910 wim_index = 0;
911 j = ccline.cmdpos;
912 // if 'wildmode' first contains "longest", get longest
913 // common part
914 if (wim_flags[0] & WIM_LONGEST)
915 res = nextwild(xp, WILD_LONGEST, options, escape);
916 else
917 res = nextwild(xp, WILD_EXPAND_KEEP, options, escape);
918
919 // if interrupted while completing, behave like it failed
920 if (got_int)
921 {
922 (void)vpeekc(); // remove <C-C> from input stream
923 got_int = FALSE; // don't abandon the command line
924 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
925#ifdef FEAT_WILDMENU
926 xp->xp_context = EXPAND_NOTHING;
927#endif
928 *wim_index_p = wim_index;
929 return CMDLINE_CHANGED;
930 }
931
932 // when more than one match, and 'wildmode' first contains
933 // "list", or no change and 'wildmode' contains "longest,list",
934 // list all matches
935 if (res == OK && xp->xp_numfiles > 1)
936 {
937 // a "longest" that didn't do anything is skipped (but not
938 // "list:longest")
939 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
940 wim_index = 1;
941 if ((wim_flags[wim_index] & WIM_LIST)
942#ifdef FEAT_WILDMENU
943 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
944#endif
945 )
946 {
947 if (!(wim_flags[0] & WIM_LONGEST))
948 {
949#ifdef FEAT_WILDMENU
950 int p_wmnu_save = p_wmnu;
951 p_wmnu = 0;
952#endif
953 // remove match
954 nextwild(xp, WILD_PREV, 0, escape);
955#ifdef FEAT_WILDMENU
956 p_wmnu = p_wmnu_save;
957#endif
958 }
959#ifdef FEAT_WILDMENU
960 (void)showmatches(xp, p_wmnu
961 && ((wim_flags[wim_index] & WIM_LIST) == 0));
962#else
963 (void)showmatches(xp, FALSE);
964#endif
965 redrawcmd();
966 *did_wild_list = TRUE;
967 if (wim_flags[wim_index] & WIM_LONGEST)
968 nextwild(xp, WILD_LONGEST, options, escape);
969 else if (wim_flags[wim_index] & WIM_FULL)
970 nextwild(xp, WILD_NEXT, options, escape);
971 }
972 else
973 vim_beep(BO_WILD);
974 }
975#ifdef FEAT_WILDMENU
976 else if (xp->xp_numfiles == -1)
977 xp->xp_context = EXPAND_NOTHING;
978#endif
979 }
980 if (wim_index < 3)
981 ++wim_index;
982 if (c == ESC)
983 *gotesc = TRUE;
984
985 *wim_index_p = wim_index;
986 return (res == OK) ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
987}
988
989/*
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +0200990 * Handle backspace, delete and CTRL-W keys in the command-line mode.
991 * Returns:
992 * CMDLINE_NOT_CHANGED - if the command line is not changed
993 * CMDLINE_CHANGED - if the command line is changed
994 * GOTO_NORMAL_MODE - go back to normal mode
995 */
996 static int
997cmdline_erase_chars(
998 int c,
999 int indent
1000#ifdef FEAT_SEARCH_EXTRA
1001 , incsearch_state_T *isp
1002#endif
1003 )
1004{
1005 int i;
1006 int j;
1007
1008 if (c == K_KDEL)
1009 c = K_DEL;
1010
1011 /*
1012 * Delete current character is the same as backspace on next
1013 * character, except at end of line.
1014 */
1015 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1016 ++ccline.cmdpos;
1017 if (has_mbyte && c == K_DEL)
1018 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1019 ccline.cmdbuff + ccline.cmdpos);
1020 if (ccline.cmdpos > 0)
1021 {
1022 char_u *p;
1023
1024 j = ccline.cmdpos;
1025 p = ccline.cmdbuff + j;
1026 if (has_mbyte)
1027 {
1028 p = mb_prevptr(ccline.cmdbuff, p);
1029 if (c == Ctrl_W)
1030 {
1031 while (p > ccline.cmdbuff && vim_isspace(*p))
1032 p = mb_prevptr(ccline.cmdbuff, p);
1033 i = mb_get_class(p);
1034 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1035 p = mb_prevptr(ccline.cmdbuff, p);
1036 if (mb_get_class(p) != i)
1037 p += (*mb_ptr2len)(p);
1038 }
1039 }
1040 else if (c == Ctrl_W)
1041 {
1042 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1043 --p;
1044 i = vim_iswordc(p[-1]);
1045 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1046 && vim_iswordc(p[-1]) == i)
1047 --p;
1048 }
1049 else
1050 --p;
1051 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1052 ccline.cmdlen -= j - ccline.cmdpos;
1053 i = ccline.cmdpos;
1054 while (i < ccline.cmdlen)
1055 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1056
1057 // Truncate at the end, required for multi-byte chars.
1058 ccline.cmdbuff[ccline.cmdlen] = NUL;
1059#ifdef FEAT_SEARCH_EXTRA
1060 if (ccline.cmdlen == 0)
1061 {
1062 isp->search_start = isp->save_cursor;
1063 // save view settings, so that the screen
1064 // won't be restored at the wrong position
1065 isp->old_viewstate = isp->init_viewstate;
1066 }
1067#endif
1068 redrawcmd();
1069 }
1070 else if (ccline.cmdlen == 0 && c != Ctrl_W
1071 && ccline.cmdprompt == NULL && indent == 0)
1072 {
1073 // In ex and debug mode it doesn't make sense to return.
1074 if (exmode_active
1075#ifdef FEAT_EVAL
1076 || ccline.cmdfirstc == '>'
1077#endif
1078 )
1079 return CMDLINE_NOT_CHANGED;
1080
1081 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
1082 if (!cmd_silent)
1083 {
1084#ifdef FEAT_RIGHTLEFT
1085 if (cmdmsg_rl)
1086 msg_col = Columns;
1087 else
1088#endif
1089 msg_col = 0;
1090 msg_putchar(' '); // delete ':'
1091 }
1092#ifdef FEAT_SEARCH_EXTRA
1093 if (ccline.cmdlen == 0)
1094 isp->search_start = isp->save_cursor;
1095#endif
1096 redraw_cmdline = TRUE;
1097 return GOTO_NORMAL_MODE;
1098 }
1099 return CMDLINE_CHANGED;
1100}
1101
1102/*
1103 * Handle the CTRL-^ key in the command-line mode and toggle the use of the
1104 * language :lmap mappings and/or Input Method.
1105 */
1106 static void
1107cmdline_toggle_langmap(long *b_im_ptr)
1108{
1109 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
1110 {
1111 // ":lmap" mappings exists, toggle use of mappings.
1112 State ^= LANGMAP;
1113#ifdef HAVE_INPUT_METHOD
1114 im_set_active(FALSE); // Disable input method
1115#endif
1116 if (b_im_ptr != NULL)
1117 {
1118 if (State & LANGMAP)
1119 *b_im_ptr = B_IMODE_LMAP;
1120 else
1121 *b_im_ptr = B_IMODE_NONE;
1122 }
1123 }
1124#ifdef HAVE_INPUT_METHOD
1125 else
1126 {
1127 // There are no ":lmap" mappings, toggle IM. When
1128 // 'imdisable' is set don't try getting the status, it's
1129 // always off.
1130 if ((p_imdisable && b_im_ptr != NULL)
1131 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1132 {
1133 im_set_active(FALSE); // Disable input method
1134 if (b_im_ptr != NULL)
1135 *b_im_ptr = B_IMODE_NONE;
1136 }
1137 else
1138 {
1139 im_set_active(TRUE); // Enable input method
1140 if (b_im_ptr != NULL)
1141 *b_im_ptr = B_IMODE_IM;
1142 }
1143 }
1144#endif
1145 if (b_im_ptr != NULL)
1146 {
1147 if (b_im_ptr == &curbuf->b_p_iminsert)
1148 set_iminsert_global();
1149 else
1150 set_imsearch_global();
1151 }
1152#ifdef CURSOR_SHAPE
1153 ui_cursor_shape(); // may show different cursor shape
1154#endif
1155#if defined(FEAT_KEYMAP)
1156 // Show/unshow value of 'keymap' in status lines later.
1157 status_redraw_curbuf();
1158#endif
1159}
1160
1161/*
1162 * Handle the CTRL-R key in the command-line mode and insert the contents of a
1163 * numbered or named register.
1164 */
1165 static int
1166cmdline_insert_reg(int *gotesc UNUSED)
1167{
1168 int i;
1169 int c;
1170
1171#ifdef USE_ON_FLY_SCROLL
1172 dont_scroll = TRUE; // disallow scrolling here
1173#endif
1174 putcmdline('"', TRUE);
1175 ++no_mapping;
1176 ++allow_keys;
1177 i = c = plain_vgetc(); // CTRL-R <char>
1178 if (i == Ctrl_O)
1179 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
1180 if (i == Ctrl_R)
1181 c = plain_vgetc(); // CTRL-R CTRL-R <char>
1182 extra_char = NUL;
1183 --no_mapping;
1184 --allow_keys;
1185#ifdef FEAT_EVAL
1186 /*
1187 * Insert the result of an expression.
1188 * Need to save the current command line, to be able to enter
1189 * a new one...
1190 */
1191 new_cmdpos = -1;
1192 if (c == '=')
1193 {
1194 if (ccline.cmdfirstc == '=' // can't do this recursively
1195 || cmdline_star > 0) // or when typing a password
1196 {
1197 beep_flush();
1198 c = ESC;
1199 }
1200 else
1201 c = get_expr_register();
1202 }
1203#endif
1204 if (c != ESC) // use ESC to cancel inserting register
1205 {
1206 cmdline_paste(c, i == Ctrl_R, FALSE);
1207
1208#ifdef FEAT_EVAL
1209 // When there was a serious error abort getting the
1210 // command line.
1211 if (aborting())
1212 {
1213 *gotesc = TRUE; // will free ccline.cmdbuff after
1214 // putting it in history
1215 return GOTO_NORMAL_MODE;
1216 }
1217#endif
1218 KeyTyped = FALSE; // Don't do p_wc completion.
1219#ifdef FEAT_EVAL
1220 if (new_cmdpos >= 0)
1221 {
1222 // set_cmdline_pos() was used
1223 if (new_cmdpos > ccline.cmdlen)
1224 ccline.cmdpos = ccline.cmdlen;
1225 else
1226 ccline.cmdpos = new_cmdpos;
1227 }
1228#endif
1229 }
1230 redrawcmd();
1231 return CMDLINE_CHANGED;
1232}
1233
1234/*
1235 * Handle the Left and Right mouse clicks in the command-line mode.
1236 */
1237 static void
1238cmdline_left_right_mouse(int c, int *ignore_drag_release)
1239{
1240 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1241 *ignore_drag_release = TRUE;
1242 else
1243 *ignore_drag_release = FALSE;
1244# ifdef FEAT_GUI
1245 // When GUI is active, also move when 'mouse' is empty
1246 if (!gui.in_use)
1247# endif
1248 if (!mouse_has(MOUSE_COMMAND))
1249 return;
1250# ifdef FEAT_CLIPBOARD
1251 if (mouse_row < cmdline_row && clip_star.available)
1252 {
1253 int button, is_click, is_drag;
1254
1255 /*
1256 * Handle modeless selection.
1257 */
1258 button = get_mouse_button(KEY2TERMCAP1(c),
1259 &is_click, &is_drag);
1260 if (mouse_model_popup() && button == MOUSE_LEFT
1261 && (mod_mask & MOD_MASK_SHIFT))
1262 {
1263 // Translate shift-left to right button.
1264 button = MOUSE_RIGHT;
1265 mod_mask &= ~MOD_MASK_SHIFT;
1266 }
1267 clip_modeless(button, is_click, is_drag);
1268 return;
1269 }
1270# endif
1271
1272 set_cmdspos();
1273 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1274 ++ccline.cmdpos)
1275 {
1276 int i;
1277
1278 i = cmdline_charsize(ccline.cmdpos);
1279 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1280 && mouse_col < ccline.cmdspos % Columns + i)
1281 break;
1282 if (has_mbyte)
1283 {
1284 // Count ">" for double-wide char that doesn't fit.
1285 correct_cmdspos(ccline.cmdpos, i);
1286 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
1287 + ccline.cmdpos) - 1;
1288 }
1289 ccline.cmdspos += i;
1290 }
1291}
1292
1293/*
1294 * Handle the Up, Down, Page Up, Page down, CTRL-N and CTRL-P key in the
1295 * command-line mode. The pressed key is in 'c'.
1296 * Returns:
1297 * CMDLINE_NOT_CHANGED - if the command line is not changed
1298 * CMDLINE_CHANGED - if the command line is changed
1299 * GOTO_NORMAL_MODE - go back to normal mode
1300 */
1301 static int
1302cmdline_browse_history(
1303 int c,
1304 int firstc,
1305 char_u **curcmdstr,
1306 int histype,
1307 int *hiscnt_p,
1308 expand_T *xp)
1309{
1310 int i;
1311 int j;
1312 char_u *lookfor = *curcmdstr;
1313 int hiscnt = *hiscnt_p;
1314 int res;
1315
1316 if (get_hislen() == 0 || firstc == NUL) // no history
1317 return CMDLINE_NOT_CHANGED;
1318
1319 i = hiscnt;
1320
1321 // save current command string so it can be restored later
1322 if (lookfor == NULL)
1323 {
1324 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1325 return CMDLINE_NOT_CHANGED;
1326 lookfor[ccline.cmdpos] = NUL;
1327 }
1328
1329 j = (int)STRLEN(lookfor);
1330 for (;;)
1331 {
1332 // one step backwards
1333 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
1334 || c == K_PAGEUP || c == K_KPAGEUP)
1335 {
1336 if (hiscnt == get_hislen()) // first time
1337 hiscnt = *get_hisidx(histype);
1338 else if (hiscnt == 0 && *get_hisidx(histype)
1339 != get_hislen() - 1)
1340 hiscnt = get_hislen() - 1;
1341 else if (hiscnt != *get_hisidx(histype) + 1)
1342 --hiscnt;
1343 else // at top of list
1344 {
1345 hiscnt = i;
1346 break;
1347 }
1348 }
1349 else // one step forwards
1350 {
1351 // on last entry, clear the line
1352 if (hiscnt == *get_hisidx(histype))
1353 {
1354 hiscnt = get_hislen();
1355 break;
1356 }
1357
1358 // not on a history line, nothing to do
1359 if (hiscnt == get_hislen())
1360 break;
1361 if (hiscnt == get_hislen() - 1) // wrap around
1362 hiscnt = 0;
1363 else
1364 ++hiscnt;
1365 }
1366 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
1367 == NULL)
1368 {
1369 hiscnt = i;
1370 break;
1371 }
1372 if ((c != K_UP && c != K_DOWN)
1373 || hiscnt == i
1374 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
1375 lookfor, (size_t)j) == 0)
1376 break;
1377 }
1378
1379 if (hiscnt != i) // jumped to other entry
1380 {
1381 char_u *p;
1382 int len;
1383 int old_firstc;
1384
1385 VIM_CLEAR(ccline.cmdbuff);
1386 xp->xp_context = EXPAND_NOTHING;
1387 if (hiscnt == get_hislen())
1388 p = lookfor; // back to the old one
1389 else
1390 p = get_histentry(histype)[hiscnt].hisstr;
1391
1392 if (histype == HIST_SEARCH
1393 && p != lookfor
1394 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1395 {
1396 // Correct for the separator character used when
1397 // adding the history entry vs the one used now.
1398 // First loop: count length.
1399 // Second loop: copy the characters.
1400 for (i = 0; i <= 1; ++i)
1401 {
1402 len = 0;
1403 for (j = 0; p[j] != NUL; ++j)
1404 {
1405 // Replace old sep with new sep, unless it is
1406 // escaped.
1407 if (p[j] == old_firstc
1408 && (j == 0 || p[j - 1] != '\\'))
1409 {
1410 if (i > 0)
1411 ccline.cmdbuff[len] = firstc;
1412 }
1413 else
1414 {
1415 // Escape new sep, unless it is already
1416 // escaped.
1417 if (p[j] == firstc
1418 && (j == 0 || p[j - 1] != '\\'))
1419 {
1420 if (i > 0)
1421 ccline.cmdbuff[len] = '\\';
1422 ++len;
1423 }
1424 if (i > 0)
1425 ccline.cmdbuff[len] = p[j];
1426 }
1427 ++len;
1428 }
1429 if (i == 0)
1430 {
1431 alloc_cmdbuff(len);
1432 if (ccline.cmdbuff == NULL)
1433 {
1434 res = GOTO_NORMAL_MODE;
1435 goto done;
1436 }
1437 }
1438 }
1439 ccline.cmdbuff[len] = NUL;
1440 }
1441 else
1442 {
1443 alloc_cmdbuff((int)STRLEN(p));
1444 if (ccline.cmdbuff == NULL)
1445 {
1446 res = GOTO_NORMAL_MODE;
1447 goto done;
1448 }
1449 STRCPY(ccline.cmdbuff, p);
1450 }
1451
1452 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1453 redrawcmd();
1454 res = CMDLINE_CHANGED;
1455 goto done;
1456 }
1457 beep_flush();
1458 res = CMDLINE_NOT_CHANGED;
1459
1460done:
1461 *curcmdstr = lookfor;
1462 *hiscnt_p = hiscnt;
1463 return res;
1464}
1465
1466/*
Bram Moolenaar9c929712020-09-07 22:05:28 +02001467 * Initialize the current command-line info.
1468 */
1469 static int
1470init_ccline(int firstc, int indent)
1471{
1472 ccline.overstrike = FALSE; // always start in insert mode
1473
1474 /*
1475 * set some variables for redrawcmd()
1476 */
1477 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
1478 ccline.cmdindent = (firstc > 0 ? indent : 0);
1479
1480 // alloc initial ccline.cmdbuff
1481 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
1482 if (ccline.cmdbuff == NULL)
1483 return FAIL;
1484 ccline.cmdlen = ccline.cmdpos = 0;
1485 ccline.cmdbuff[0] = NUL;
1486 sb_text_start_cmdline();
1487
1488 // autoindent for :insert and :append
1489 if (firstc <= 0)
1490 {
1491 vim_memset(ccline.cmdbuff, ' ', indent);
1492 ccline.cmdbuff[indent] = NUL;
1493 ccline.cmdpos = indent;
1494 ccline.cmdspos = indent;
1495 ccline.cmdlen = indent;
1496 }
1497
1498 return OK;
1499}
1500
1501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 * getcmdline() - accept a command line starting with firstc.
1503 *
1504 * firstc == ':' get ":" command line.
1505 * firstc == '/' or '?' get search pattern
1506 * firstc == '=' get expression
1507 * firstc == '@' get text for input() function
1508 * firstc == '>' get text for debug mode
1509 * firstc == NUL get text for :insert command
1510 * firstc == -1 like NUL, and break on CTRL-C
1511 *
1512 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
1513 * command line.
1514 *
1515 * Careful: getcmdline() can be called recursively!
1516 *
1517 * Return pointer to allocated string if there is a commandline, NULL
1518 * otherwise.
1519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001521getcmdline(
1522 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +02001523 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +02001524 int indent, // indent for inside conditionals
1525 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001526{
1527 return getcmdline_int(firstc, count, indent, TRUE);
1528}
1529
1530 static char_u *
1531getcmdline_int(
1532 int firstc,
1533 long count UNUSED, // only used for incremental search
1534 int indent, // indent for inside conditionals
Bram Moolenaar9c929712020-09-07 22:05:28 +02001535 int clear_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 int c;
1538 int i;
1539 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001540 int gotesc = FALSE; // TRUE when <ESC> just typed
1541 int do_abbr; // when TRUE check for abbr.
1542 char_u *lookfor = NULL; // string to match
1543 int hiscnt; // current history line in use
1544 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001546 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 int did_wild_list = FALSE; // did wild_list() recently
1549 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 int res;
1551 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 int save_State = State; // remember State when called
1553 int some_key_typed = FALSE; // one of the keys was typed
1554 // mouse drag and release events are ignored, unless they are
1555 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557#ifdef FEAT_EVAL
1558 int break_ctrl_c = FALSE;
1559#endif
1560 expand_T xpc;
1561 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001562 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02001563 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001564 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaar438d1762018-09-30 17:11:48 +02001566 if (ccline.cmdbuff != NULL)
1567 {
1568 // Being called recursively. Since ccline is global, we need to save
1569 // the current buffer and restore it when returning.
1570 save_cmdline(&save_ccline);
1571 did_save_ccline = TRUE;
1572 }
Bram Moolenaar9c929712020-09-07 22:05:28 +02001573 if (clear_ccline)
Bram Moolenaara80faa82020-04-12 19:37:17 +02001574 CLEAR_FIELD(ccline);
Bram Moolenaar438d1762018-09-30 17:11:48 +02001575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#ifdef FEAT_EVAL
1577 if (firstc == -1)
1578 {
1579 firstc = NUL;
1580 break_ctrl_c = TRUE;
1581 }
1582#endif
1583#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001584 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (firstc == ':' || firstc == '=' || firstc == '>')
1586 cmd_hkmap = 0;
1587#endif
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001590 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591#endif
1592
Bram Moolenaar9c929712020-09-07 22:05:28 +02001593 if (init_ccline(firstc, indent) != OK)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001594 goto theend; // out of memory
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001597 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599#ifdef FEAT_RIGHTLEFT
1600 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
1601 && (firstc == '/' || firstc == '?'))
1602 cmdmsg_rl = TRUE;
1603 else
1604 cmdmsg_rl = FALSE;
1605#endif
1606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (!cmd_silent)
1609 {
1610 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001611 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 gotocmdline(TRUE);
1613 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001614 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 set_cmdspos();
1616 }
1617 xpc.xp_context = EXPAND_NOTHING;
1618 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001619#ifndef BACKSLASH_IN_FILENAME
1620 xpc.xp_shell = FALSE;
1621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001623#if defined(FEAT_EVAL)
1624 if (ccline.input_fn)
1625 {
1626 xpc.xp_context = ccline.xp_context;
1627 xpc.xp_pattern = ccline.cmdbuff;
1628 xpc.xp_arg = ccline.xp_arg;
1629 }
1630#endif
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 /*
1633 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
1634 * doing ":@0" when register 0 doesn't contain a CR.
1635 */
1636 msg_scroll = FALSE;
1637
1638 State = CMDLINE;
1639
1640 if (firstc == '/' || firstc == '?' || firstc == '@')
1641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001642 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
1644 b_im_ptr = &curbuf->b_p_iminsert;
1645 else
1646 b_im_ptr = &curbuf->b_p_imsearch;
1647 if (*b_im_ptr == B_IMODE_LMAP)
1648 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 im_set_active(*b_im_ptr == B_IMODE_IM);
1651#endif
1652 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001653#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 else if (p_imcmdline)
1655 im_set_active(TRUE);
1656#endif
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001660 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
1662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001663 // When inside an autocommand for writing "exiting" may be set and
1664 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001665 settmode(TMODE_RAW);
1666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001667 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001668 cmdline_type = firstc == NUL ? '-' : firstc;
1669 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001672 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
1678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // If something above caused an error, reset the flags, we do want to type
1680 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001681 if (did_emsg)
1682 redrawcmd();
1683 did_emsg = FALSE;
1684 got_int = FALSE;
1685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 /*
1687 * Collect the command string, handling editing keys.
1688 */
1689 for (;;)
1690 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 redir_off = TRUE; // Don't redirect the typed command.
1692 // Repeated, because a ":redir" inside
1693 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001697 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001699 did_emsg = FALSE; // There can't really be a reason why an error
1700 // that occurs while typing a command should
1701 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +02001702
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001703 // Trigger SafeState if nothing is pending.
1704 may_trigger_safestate(xpc.xp_numfiles <= 0);
1705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001706 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +00001707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001708 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
1709 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001710 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001711 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001712 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (KeyTyped)
1715 {
1716 some_key_typed = TRUE;
1717#ifdef FEAT_RIGHTLEFT
1718 if (cmd_hkmap)
1719 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (cmdmsg_rl && !KeyStuffed)
1721 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001722 // Invert horizontal movements and operations. Only when
1723 // typed by the user directly, not when the result of a
1724 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 switch (c)
1726 {
1727 case K_RIGHT: c = K_LEFT; break;
1728 case K_S_RIGHT: c = K_S_LEFT; break;
1729 case K_C_RIGHT: c = K_C_LEFT; break;
1730 case K_LEFT: c = K_RIGHT; break;
1731 case K_S_LEFT: c = K_S_RIGHT; break;
1732 case K_C_LEFT: c = K_C_RIGHT; break;
1733 }
1734 }
1735#endif
1736 }
1737
1738 /*
1739 * Ignore got_int when CTRL-C was typed here.
1740 * Don't ignore it in :global, we really need to break then, e.g., for
1741 * ":g/pat/normal /pat" (without the <CR>).
1742 * Don't ignore it for the input() function.
1743 */
1744 if ((c == Ctrl_C
1745#ifdef UNIX
1746 || c == intr_char
1747#endif
1748 )
1749#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1750 && firstc != '@'
1751#endif
1752#ifdef FEAT_EVAL
1753 && !break_ctrl_c
1754#endif
1755 && !global_busy)
1756 got_int = FALSE;
1757
Bram Moolenaard7663c22019-08-06 21:59:57 +02001758 // free old command line when finished moving around in the history
1759 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001761 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001762 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 && c != K_PAGEDOWN && c != K_PAGEUP
1764 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001765 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001767 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001770 * When there are matching completions to select <S-Tab> works like
1771 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001773 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 c = Ctrl_P;
1775
1776#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001777 c = wildmenu_translate_key(&ccline, c, &xpc, did_wild_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
1779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001780 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (xpc.xp_numfiles != -1
1782 && !(c == p_wc && KeyTyped) && c != p_wcm
1783 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1784 && c != Ctrl_L)
1785 {
1786 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1787 did_wild_list = FALSE;
1788#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001789 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#endif
1791 xpc.xp_context = EXPAND_NOTHING;
1792 wim_index = 0;
1793#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001794 wildmenu_cleanup(&ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#endif
1796 }
1797
1798#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001799 c = wildmenu_process_key(&ccline, c, &xpc);
1800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001802 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1803 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (c == Ctrl_BSL)
1805 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001806 res = cmdline_handle_backslash_key(c, &gotesc);
1807 if (res == CMDLINE_CHANGED)
1808 goto cmdline_changed;
1809 else if (res == CMDLINE_NOT_CHANGED)
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001810 goto cmdline_not_changed;
Bram Moolenaar9c929712020-09-07 22:05:28 +02001811 else if (res == GOTO_NORMAL_MODE)
1812 goto returncmd; // back to cmd mode
1813 c = Ctrl_BSL; // backslash key not processed by
1814 // cmdline_handle_backslash_key()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816
1817#ifdef FEAT_CMDWIN
1818 if (c == cedit_key || c == K_CMDWIN)
1819 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001820 // TODO: why is ex_normal_busy checked here?
1821 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001822 {
1823 /*
1824 * Open a window to edit the command line (and history).
1825 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001826 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001827 some_key_typed = TRUE;
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830# ifdef FEAT_DIGRAPHS
1831 else
1832# endif
1833#endif
1834#ifdef FEAT_DIGRAPHS
1835 c = do_digraph(c);
1836#endif
1837
1838 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1839 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1840 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001841 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001842 if (exmode_active
1843 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001844 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001845 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001846 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001848 if (c == K_KENTER)
1849 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001851 else
1852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001853 gotesc = FALSE; // Might have typed ESC previously, don't
1854 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001855 if (ccheck_abbr(c + ABBR_OFF))
1856 goto cmdline_changed;
1857 if (!cmd_silent)
1858 {
1859 windgoto(msg_row, 0);
1860 out_flush();
1861 }
1862 break;
1863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865
Bram Moolenaar9c929712020-09-07 22:05:28 +02001866 // Completion for 'wildchar' or 'wildcharm' key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1868 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001869 res = cmdline_wildchar_complete(c, firstc != '@', &did_wild_list,
1870 &wim_index, &xpc, &gotesc);
1871 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 goto cmdline_changed;
1873 }
1874
1875 gotesc = FALSE;
1876
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001877 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (c == K_S_TAB && KeyTyped)
1879 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001880 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1881 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1882 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 goto cmdline_changed;
1884 }
1885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 c = NL;
1888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001889 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 /*
1892 * Big switch for a typed command line character.
1893 */
1894 switch (c)
1895 {
1896 case K_BS:
1897 case Ctrl_H:
1898 case K_DEL:
1899 case K_KDEL:
1900 case Ctrl_W:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001901 res = cmdline_erase_chars(c, indent
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001903 , &is_state
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001904#endif
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001905 );
1906 if (res == CMDLINE_NOT_CHANGED)
1907 goto cmdline_not_changed;
1908 else if (res == GOTO_NORMAL_MODE)
1909 goto returncmd; // back to cmd mode
1910 goto cmdline_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912 case K_INS:
1913 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 ccline.overstrike = !ccline.overstrike;
1915#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001916 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
1918 goto cmdline_not_changed;
1919
1920 case Ctrl_HAT:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001921 cmdline_toggle_langmap(b_im_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 goto cmdline_not_changed;
1923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001924// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001926 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 j = ccline.cmdpos;
1928 ccline.cmdlen -= j;
1929 i = ccline.cmdpos = 0;
1930 while (i < ccline.cmdlen)
1931 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001934#ifdef FEAT_SEARCH_EXTRA
1935 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001936 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 redrawcmd();
1939 goto cmdline_changed;
1940
1941#ifdef FEAT_CLIPBOARD
1942 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (clip_star.state != SELECT_CLEARED)
1945 {
1946 if (clip_star.state == SELECT_DONE)
1947 clip_copy_modeless_selection(TRUE);
1948 goto cmdline_not_changed;
1949 }
1950 break;
1951#endif
1952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001953 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001955 // In exmode it doesn't make sense to return. Except when
1956 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001957 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001958 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 goto cmdline_not_changed;
1960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001961 gotesc = TRUE; // will free ccline.cmdbuff after
1962 // putting it in history
1963 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 case Ctrl_R: // insert register
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001966 res = cmdline_insert_reg(&gotesc);
1967 if (res == CMDLINE_NOT_CHANGED)
1968 goto cmdline_not_changed;
1969 else if (res == GOTO_NORMAL_MODE)
1970 goto returncmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 goto cmdline_changed;
1972
1973 case Ctrl_D:
1974 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 case K_RIGHT:
1981 case K_S_RIGHT:
1982 case K_C_RIGHT:
1983 do
1984 {
1985 if (ccline.cmdpos >= ccline.cmdlen)
1986 break;
1987 i = cmdline_charsize(ccline.cmdpos);
1988 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1989 break;
1990 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001992 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 + ccline.cmdpos);
1994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 ++ccline.cmdpos;
1996 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001997 while ((c == K_S_RIGHT || c == K_C_RIGHT
1998 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (has_mbyte)
2001 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 goto cmdline_not_changed;
2003
2004 case K_LEFT:
2005 case K_S_LEFT:
2006 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002007 if (ccline.cmdpos == 0)
2008 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 do
2010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
2014 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
2016 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002017 while (ccline.cmdpos > 0
2018 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002019 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (has_mbyte)
2022 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 goto cmdline_not_changed;
2024
2025 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00002027 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaar4f974752019-02-17 17:44:42 +01002029#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // On MS-Windows ignore <M-F4>, we get it when closing the window
2031 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002032 case K_F4:
2033 if (mod_mask == MOD_MASK_ALT)
2034 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002035 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00002036 goto cmdline_not_changed;
2037 }
2038 break;
2039#endif
2040
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 case K_MIDDLEDRAG:
2042 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 case K_MIDDLEMOUSE:
2046# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002047 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (!gui.in_use)
2049# endif
2050 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002052# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002054 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002056# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002057 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 redrawcmd();
2059 goto cmdline_changed;
2060
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002061# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002063 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 redrawcmd();
2065 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 case K_LEFTDRAG:
2069 case K_LEFTRELEASE:
2070 case K_RIGHTDRAG:
2071 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // Ignore drag and release events when the button-down wasn't
2073 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (ignore_drag_release)
2075 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002076 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 case K_LEFTMOUSE:
2078 case K_RIGHTMOUSE:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002079 cmdline_left_right_mouse(c, &ignore_drag_release);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 goto cmdline_not_changed;
2081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 case K_MOUSEDOWN:
2084 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002085 case K_MOUSELEFT:
2086 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002087 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 case K_X1MOUSE:
2089 case K_X1DRAG:
2090 case K_X1RELEASE:
2091 case K_X2MOUSE:
2092 case K_X2DRAG:
2093 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002094 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto cmdline_not_changed;
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002098 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 case K_LEFTRELEASE_NM:
2100 goto cmdline_not_changed;
2101
2102 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002103 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 gui_do_scroll();
2106 redrawcmd();
2107 }
2108 goto cmdline_not_changed;
2109
2110 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002111 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002113 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 redrawcmd();
2115 }
2116 goto cmdline_not_changed;
2117#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002118#ifdef FEAT_GUI_TABLINE
2119 case K_TABLINE:
2120 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002121 // Don't want to change any tabs here. Make sure the same tab
2122 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002123 if (gui_use_tabline())
2124 gui_mch_set_curtab(tabpage_index(curtab));
2125 goto cmdline_not_changed;
2126#endif
2127
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002128 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto cmdline_not_changed;
2130
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 case K_HOME:
2133 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 case K_S_HOME:
2135 case K_C_HOME:
2136 ccline.cmdpos = 0;
2137 set_cmdspos();
2138 goto cmdline_not_changed;
2139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 case K_END:
2142 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 case K_S_END:
2144 case K_C_END:
2145 ccline.cmdpos = ccline.cmdlen;
2146 set_cmdspos_cursor();
2147 goto cmdline_not_changed;
2148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01002150 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 break;
2152 goto cmdline_changed;
2153
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002154 case Ctrl_L:
2155#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002156 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002157 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002158#endif
2159
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002160 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002161 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 break;
2163 goto cmdline_changed;
2164
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002165 case Ctrl_N: // next match
2166 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002167 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002169 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2170 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002172 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002174 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 case K_UP:
2176 case K_DOWN:
2177 case K_S_UP:
2178 case K_S_DOWN:
2179 case K_PAGEUP:
2180 case K_KPAGEUP:
2181 case K_PAGEDOWN:
2182 case K_KPAGEDOWN:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002183 res = cmdline_browse_history(c, firstc, &lookfor, histype,
2184 &hiscnt, &xpc);
2185 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 goto cmdline_changed;
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002187 else if (res == GOTO_NORMAL_MODE)
2188 goto returncmd;
Bram Moolenaar11956692016-08-27 16:26:56 +02002189 goto cmdline_not_changed;
2190
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002191#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002192 case Ctrl_G: // next match
2193 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002194 if (may_adjust_incsearch_highlighting(
2195 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002196 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200 case Ctrl_V:
2201 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002203 int prev_mod_mask = mod_mask;
2204
2205 ignore_drag_release = TRUE;
2206 putcmdline('^', TRUE);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002207 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002208 c = get_literal(); // get next (two) character(s)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002209 no_reduce_keys = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002210 do_abbr = FALSE; // don't do abbreviation now
2211 extra_char = NUL;
2212 // may need to remove ^ when composing char was typed
2213 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2214 {
2215 draw_cmdline(ccline.cmdpos,
2216 ccline.cmdlen - ccline.cmdpos);
2217 msg_putchar(' ');
2218 cursorcmd();
2219 }
2220
2221 if ((c == ESC || c == CSI)
2222 && !(prev_mod_mask & MOD_MASK_SHIFT))
2223 // Using CTRL-V: Change any modifyOtherKeys ESC
2224 // sequence to a normal key. Don't do this for
2225 // CTRL-SHIFT-V.
2226 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 break;
2230
2231#ifdef FEAT_DIGRAPHS
2232 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002235# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002239 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (c != NUL)
2241 break;
2242
2243 redrawcmd();
2244 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002245#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002248 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (!p_ari)
2250 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002251 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 goto cmdline_not_changed;
2253#endif
2254
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002255 case K_PS:
2256 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2257 goto cmdline_changed;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 default:
2260#ifdef UNIX
2261 if (c == intr_char)
2262 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002263 gotesc = TRUE; // will free ccline.cmdbuff after
2264 // putting it in history
2265 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
2267#endif
2268 /*
2269 * Normal character with no special meaning. Just set mod_mask
2270 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2271 * the string <S-Space>. This should only happen after ^V.
2272 */
2273 if (!IS_SPECIAL(c))
2274 mod_mask = 0x0;
2275 break;
2276 }
2277 /*
2278 * End of switch on command line character.
2279 * We come here if we have a normal character.
2280 */
2281
Bram Moolenaar13505972019-01-24 15:04:48 +01002282 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2283 && (ccheck_abbr(
2284 // Add ABBR_OFF for characters above 0x100, this is
2285 // what check_abbr() expects.
2286 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2287 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 goto cmdline_changed;
2289
2290 /*
2291 * put the character in the command line
2292 */
2293 if (IS_SPECIAL(c) || mod_mask != 0)
2294 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2295 else
2296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (has_mbyte)
2298 {
2299 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002300 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 put_on_cmdline(IObuff, j, TRUE);
2302 }
2303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 IObuff[0] = c;
2306 put_on_cmdline(IObuff, 1, TRUE);
2307 }
2308 }
2309 goto cmdline_changed;
2310
2311/*
2312 * This part implements incremental searches for "/" and "?"
2313 * Jump to cmdline_not_changed when a character has been read but the command
2314 * line did not change. Then we only search and redraw if something changed in
2315 * the past.
2316 * Jump to cmdline_changed when the command line did change.
2317 * (Sorry for the goto's, I know it is ugly).
2318 */
2319cmdline_not_changed:
2320#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002321 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 continue;
2323#endif
2324
2325cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002327 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaara60053b2020-09-03 16:50:13 +02002330 if (xpc.xp_context == EXPAND_NOTHING)
2331 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#endif
2333
2334#ifdef FEAT_RIGHTLEFT
2335 if (cmdmsg_rl
2336# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002337 || (p_arshape && !p_tbidi
2338 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339# endif
2340 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002341 // Always redraw the whole command line to fix shaping and
2342 // right-left typing. Not efficient, but it works.
2343 // Do it only when there are no characters left to read
2344 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002345 if (vpeekc() == NUL)
2346 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348 }
2349
2350returncmd:
2351
2352#ifdef FEAT_RIGHTLEFT
2353 cmdmsg_rl = FALSE;
2354#endif
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002357 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002360 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361#endif
2362
2363 if (ccline.cmdbuff != NULL)
2364 {
2365 /*
2366 * Put line in history buffer (":" and "=" only when it was typed).
2367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (ccline.cmdlen && firstc != NUL
2369 && (some_key_typed || histype == HIST_SEARCH))
2370 {
2371 add_to_history(histype, ccline.cmdbuff, TRUE,
2372 histype == HIST_SEARCH ? firstc : NUL);
2373 if (firstc == ':')
2374 {
2375 vim_free(new_last_cmdline);
2376 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2377 }
2378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002380 if (gotesc)
2381 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
2383
2384 /*
2385 * If the screen was shifted up, redraw the whole screen (later).
2386 * If the line is too long, clear it, so ruler and shown command do
2387 * not get printed in the middle of it.
2388 */
2389 msg_check();
2390 msg_scroll = save_msg_scroll;
2391 redir_off = FALSE;
2392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002393 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (some_key_typed)
2395 need_wait_return = FALSE;
2396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002397 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002398 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002401#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2403 im_save_status(b_im_ptr);
2404 im_set_active(FALSE);
2405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002408 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002410 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar438d1762018-09-30 17:11:48 +02002412theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002413 {
2414 char_u *p = ccline.cmdbuff;
2415
Bram Moolenaar438d1762018-09-30 17:11:48 +02002416 if (did_save_ccline)
2417 restore_cmdline(&save_ccline);
2418 else
2419 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002420 return p;
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422}
2423
2424#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2425/*
2426 * Get a command line with a prompt.
2427 * This is prepared to be called recursively from getcmdline() (e.g. by
2428 * f_input() when evaluating an expression from CTRL-R =).
2429 * Returns the command line in allocated memory, or NULL.
2430 */
2431 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002432getcmdline_prompt(
2433 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 char_u *prompt, // command line prompt
2435 int attr, // attributes for prompt
2436 int xp_context, // type of expansion
2437 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002440 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002441 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002443 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaar438d1762018-09-30 17:11:48 +02002445 if (ccline.cmdbuff != NULL)
2446 {
2447 // Save the values of the current cmdline and restore them below.
2448 save_cmdline(&save_ccline);
2449 did_save_ccline = TRUE;
2450 }
2451
Bram Moolenaara80faa82020-04-12 19:37:17 +02002452 CLEAR_FIELD(ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 ccline.cmdprompt = prompt;
2454 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002455# ifdef FEAT_EVAL
2456 ccline.xp_context = xp_context;
2457 ccline.xp_arg = xp_arg;
2458 ccline.input_fn = (firstc == '@');
2459# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002460 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002461 s = getcmdline_int(firstc, 1L, 0, FALSE);
2462
2463 if (did_save_ccline)
2464 restore_cmdline(&save_ccline);
2465
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002466 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002467 // Restore msg_col, the prompt from input() may have changed it.
2468 // But only if called recursively and the commandline is therefore being
2469 // restored to an old one; if not, the input() prompt stays on the screen,
2470 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002471 if (ccline.cmdbuff != NULL)
2472 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 return s;
2475}
2476#endif
2477
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002478/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002479 * Read the 'wildmode' option, fill wim_flags[].
2480 */
2481 int
2482check_opt_wim(void)
2483{
2484 char_u new_wim_flags[4];
2485 char_u *p;
2486 int i;
2487 int idx = 0;
2488
2489 for (i = 0; i < 4; ++i)
2490 new_wim_flags[i] = 0;
2491
2492 for (p = p_wim; *p; ++p)
2493 {
2494 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2495 ;
2496 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2497 return FAIL;
2498 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2499 new_wim_flags[idx] |= WIM_LONGEST;
2500 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2501 new_wim_flags[idx] |= WIM_FULL;
2502 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2503 new_wim_flags[idx] |= WIM_LIST;
2504 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2505 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2506 else
2507 return FAIL;
2508 p += i;
2509 if (*p == NUL)
2510 break;
2511 if (*p == ',')
2512 {
2513 if (idx == 3)
2514 return FAIL;
2515 ++idx;
2516 }
2517 }
2518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002519 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002520 while (idx < 3)
2521 {
2522 new_wim_flags[idx + 1] = new_wim_flags[idx];
2523 ++idx;
2524 }
2525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002526 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002527 for (i = 0; i < 4; ++i)
2528 wim_flags[i] = new_wim_flags[i];
2529 return OK;
2530}
2531
2532/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002533 * Return TRUE when the text must not be changed and we can't switch to
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002534 * another window or buffer. TRUE when editing the command line, evaluating
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002535 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002536 */
2537 int
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002538text_and_win_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002539{
2540#ifdef FEAT_CMDWIN
2541 if (cmdwin_type != 0)
2542 return TRUE;
2543#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002544 return textwinlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002545}
2546
2547/*
2548 * Give an error message for a command that isn't allowed while the cmdline
2549 * window is open or editing the cmdline in another way.
2550 */
2551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002552text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002553{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002554 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002555}
2556
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002558get_text_locked_msg(void)
2559{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002560#ifdef FEAT_CMDWIN
2561 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002562 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002563#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002564 if (textwinlock != 0)
2565 return e_textwinlock;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002566 return e_textlock;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567}
2568
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002569/*
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002570 * Return TRUE when the text must not be changed and/or we cannot switch to
2571 * another window. TRUE while evaluating 'completefunc'.
2572 */
2573 int
2574text_locked(void)
2575{
2576 return text_and_win_locked() || textlock != 0;
2577}
2578
2579/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002580 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2581 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002582 */
2583 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002584curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585{
2586 if (curbuf_lock > 0)
2587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002588 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002589 return TRUE;
2590 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002591 return allbuf_locked();
2592}
2593
2594/*
2595 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2596 * message.
2597 */
2598 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002599allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002600{
2601 if (allbuf_lock > 0)
2602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002603 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002604 return TRUE;
2605 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002606 return FALSE;
2607}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002610cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002613 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return 1;
2615#endif
2616 return ptr2cells(ccline.cmdbuff + idx);
2617}
2618
2619/*
2620 * Compute the offset of the cursor on the command line for the prompt and
2621 * indent.
2622 */
2623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002624set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002626 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 ccline.cmdspos = 1 + ccline.cmdindent;
2628 else
2629 ccline.cmdspos = 0 + ccline.cmdindent;
2630}
2631
2632/*
2633 * Compute the screen position for the cursor on the command line.
2634 */
2635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002636set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 int i, m, c;
2639
2640 set_cmdspos();
2641 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002644 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002645 m = MAXCOL;
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else
2648 m = MAXCOL;
2649 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2650 {
2651 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002652 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (has_mbyte)
2654 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002655 // If the cmdline doesn't fit, show cursor on last visible char.
2656 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if ((ccline.cmdspos += c) >= m)
2658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ccline.cmdspos -= c;
2660 break;
2661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002663 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665}
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667/*
2668 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2669 * character that doesn't fit, so that a ">" must be displayed.
2670 */
2671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002672correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002674 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2676 && ccline.cmdspos % Columns + cells > Columns)
2677 ccline.cmdspos++;
2678}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680/*
2681 * Get an Ex command line for the ":" command.
2682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002684getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002685 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002686 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002687 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002688 getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002690 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (exec_from_reg && vpeekc() == ':')
2692 (void)vgetc();
Bram Moolenaar66250c92020-08-20 15:02:42 +02002693 return getcmdline(c, 1L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694}
2695
2696/*
2697 * Get an Ex command line for Ex mode.
2698 * In Ex mode we only use the OS supplied line editing features and no
2699 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002703getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002704 int promptc, // normally ':', NUL for ":append" and '?' for
2705 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002706 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002707 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002708 getline_opt_T options UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 garray_T line_ga;
2711 char_u *pend;
2712 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002713 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002714 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 int vcol = 0;
2716 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002717 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002718 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002720 // Switch cursor on now. This avoids that it happens after the "\n", which
2721 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 cursor_on();
2723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002724 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002730 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 if (p_prompt)
2732 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 while (indent-- > 0)
2734 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
2737
2738 ga_init2(&line_ga, 1, 30);
2739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002740 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002741 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002742 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002743 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002744 while (indent >= 8)
2745 {
2746 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002747 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002748 indent -= 8;
2749 }
2750 while (indent-- > 0)
2751 {
2752 ga_append(&line_ga, ' ');
2753 msg_putchar(' ');
2754 }
2755 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 ++no_mapping;
2757 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002758
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 /*
2760 * Get the line, one character at a time.
2761 */
2762 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002765 long sw;
2766 char_u *s;
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (ga_grow(&line_ga, 40) == FAIL)
2769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
Bram Moolenaarba748c82017-02-26 14:00:07 +01002771 /*
2772 * Get one character at a time.
2773 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002774 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002776 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002777 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2778 c1 = '\n';
2779 else
2780 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 * Handle line editing.
2784 * Previously this was left to the system, putting the terminal in
2785 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002789 msg_putchar('\n');
2790 break;
2791 }
2792
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002793 if (c1 == K_PS)
2794 {
2795 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2796 goto redraw;
2797 }
2798
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 if (!escaped)
2800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002801 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 if (c1 == '\r')
2803 c1 = '\n';
2804
2805 if (c1 == BS || c1 == K_BS
2806 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002808 if (line_ga.ga_len > 0)
2809 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002810 if (has_mbyte)
2811 {
2812 p = (char_u *)line_ga.ga_data;
2813 p[line_ga.ga_len] = NUL;
2814 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2815 line_ga.ga_len -= len;
2816 }
2817 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002818 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 goto redraw;
2820 }
2821 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 msg_col = startcol;
2827 msg_clr_eos();
2828 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002829 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 }
2831
2832 if (c1 == Ctrl_T)
2833 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002834 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 p = (char_u *)line_ga.ga_data;
2836 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002837 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002838 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002840 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002842 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002843 p = (char_u *)line_ga.ga_data;
2844 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2846 *s = ' ';
2847 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002850 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002853 p = (char_u *)line_ga.ga_data;
2854 p[line_ga.ga_len] = NUL;
2855 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002861 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002862 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002866 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002867 msg_outtrans_len(p, len);
2868 vcol += ptr2cells(p);
2869 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002872 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002873 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 continue;
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 if (c1 == Ctrl_D)
2878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002879 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 p = (char_u *)line_ga.ga_data;
2881 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 if (prev_char == '^')
2884 ex_keep_indent = TRUE;
2885 indent = 0;
2886 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888 else
2889 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002890 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002891 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002892 if (indent > 0)
2893 {
2894 --indent;
2895 indent -= indent % get_sw_value(curbuf);
2896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002898 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002899 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002900 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002901 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2902 --line_ga.ga_len;
2903 }
2904 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002906
2907 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2908 {
2909 escaped = TRUE;
2910 continue;
2911 }
2912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002913 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002914 if (IS_SPECIAL(c1))
2915 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002917
2918 if (IS_SPECIAL(c1))
2919 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002920 if (has_mbyte)
2921 len = (*mb_char2bytes)(c1,
2922 (char_u *)line_ga.ga_data + line_ga.ga_len);
2923 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002924 {
2925 len = 1;
2926 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2927 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002928 if (c1 == '\n')
2929 msg_putchar('\n');
2930 else if (c1 == TAB)
2931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002932 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002934 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002935 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002939 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002940 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002943 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944 escaped = FALSE;
2945
2946 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002947 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002948
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002949 // We are done when a NL is entered, but not when it comes after an
2950 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002951 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002953 int bcount = 0;
2954
2955 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2956 ++bcount;
2957
2958 if (bcount > 0)
2959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002960 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2961 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002962 line_ga.ga_len -= (bcount + 1) / 2;
2963 pend -= (bcount + 1) / 2;
2964 pend[-1] = '\n';
2965 }
2966
2967 if ((bcount & 1) == 0)
2968 {
2969 --line_ga.ga_len;
2970 --pend;
2971 *pend = NUL;
2972 break;
2973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 }
2975 }
2976
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002977 --no_mapping;
2978 --allow_keys;
2979
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002980 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 msg_didout = FALSE;
2982 msg_col = 0;
2983 if (msg_row < Rows - 1)
2984 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002985 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 if (got_int)
2988 ga_clear(&line_ga);
2989
2990 return (char_u *)line_ga.ga_data;
2991}
2992
Bram Moolenaare344bea2005-09-01 20:46:49 +00002993# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2994 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
2996 * Return TRUE if ccline.overstrike is on.
2997 */
2998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002999cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 return ccline.overstrike;
3002}
3003
3004/*
3005 * Return TRUE if the cursor is at the end of the cmdline.
3006 */
3007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003008cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 return (ccline.cmdpos >= ccline.cmdlen);
3011}
3012#endif
3013
Bram Moolenaar9372a112005-12-06 19:59:18 +00003014#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015/*
3016 * Return the virtual column number at the current cursor position.
3017 * This is used by the IM code to obtain the start of the preedit string.
3018 */
3019 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3023 return MAXCOL;
3024
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (has_mbyte)
3026 {
3027 colnr_T col;
3028 int i = 0;
3029
3030 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003031 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
3033 return col;
3034 }
3035 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 return ccline.cmdpos;
3037}
3038#endif
3039
3040#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3041/*
3042 * If part of the command line is an IM preedit string, redraw it with
3043 * IM feedback attributes. The cursor position is restored after drawing.
3044 */
3045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003046redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
3048 if ((State & CMDLINE)
3049 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003050 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 && !p_imdisable
3052 && im_is_preediting())
3053 {
3054 int cmdpos = 0;
3055 int cmdspos;
3056 int old_row;
3057 int old_col;
3058 colnr_T col;
3059
3060 old_row = msg_row;
3061 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003062 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 if (has_mbyte)
3065 {
3066 for (col = 0; col < preedit_start_col
3067 && cmdpos < ccline.cmdlen; ++col)
3068 {
3069 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003070 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 }
3073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 cmdspos += preedit_start_col;
3076 cmdpos += preedit_start_col;
3077 }
3078
3079 msg_row = cmdline_row + (cmdspos / (int)Columns);
3080 msg_col = cmdspos % (int)Columns;
3081 if (msg_row >= Rows)
3082 msg_row = Rows - 1;
3083
3084 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3085 {
3086 int char_len;
3087 int char_attr;
3088
3089 char_attr = im_get_feedback_attr(col);
3090 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003091 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003094 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 char_len = 1;
3097
3098 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3099 cmdpos += char_len;
3100 }
3101
3102 msg_row = old_row;
3103 msg_col = old_col;
3104 }
3105}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003106#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108/*
3109 * Allocate a new command line buffer.
3110 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 */
3112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003113alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 /*
3116 * give some extra space to avoid having to allocate all the time
3117 */
3118 if (len < 80)
3119 len = 100;
3120 else
3121 len += 20;
3122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003123 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 ccline.cmdbufflen = len;
3125}
3126
3127/*
3128 * Re-allocate the command line to length len + something extra.
3129 * return FAIL for failure, OK otherwise
3130 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003132realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 char_u *p;
3135
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003136 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003137 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003140 alloc_cmdbuff(len); // will get some more
3141 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003143 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 return FAIL;
3145 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003146 // There isn't always a NUL after the command, but it may need to be
3147 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003148 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3149 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003151
3152 if (ccline.xpc != NULL
3153 && ccline.xpc->xp_pattern != NULL
3154 && ccline.xpc->xp_context != EXPAND_NOTHING
3155 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3156 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003157 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003159 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3160 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003161 if (i >= 0 && i <= ccline.cmdlen)
3162 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3163 }
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 return OK;
3166}
3167
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003168#if defined(FEAT_ARABIC) || defined(PROTO)
3169static char_u *arshape_buf = NULL;
3170
3171# if defined(EXITFREE) || defined(PROTO)
3172 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003173free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003174{
3175 vim_free(arshape_buf);
3176}
3177# endif
3178#endif
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180/*
3181 * Draw part of the cmdline at the current cursor position. But draw stars
3182 * when cmdline_star is TRUE.
3183 */
3184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003185draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3188 int i;
3189
3190 if (cmdline_star > 0)
3191 for (i = 0; i < len; ++i)
3192 {
3193 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003195 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197 else
3198#endif
3199#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003200 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 static int buflen = 0;
3203 char_u *p;
3204 int j;
3205 int newlen = 0;
3206 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003207 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 int prev_c = 0;
3209 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003210 int u8c;
3211 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 /*
3215 * Do arabic shaping into a temporary buffer. This is very
3216 * inefficient!
3217 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003218 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003220 // Re-allocate the buffer. We keep it around to avoid a lot of
3221 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003222 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003223 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003224 arshape_buf = alloc(buflen);
3225 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003226 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003229 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003231 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003232 arshape_buf[0] = ' ';
3233 newlen = 1;
3234 }
3235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 for (j = start; j < start + len; j += mb_l)
3237 {
3238 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003239 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003240 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 if (ARABIC_CHAR(u8c))
3242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003243 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (cmdmsg_rl)
3245 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003246 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 pc = prev_c;
3248 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003249 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (j + mb_l >= start + len)
3251 nc = NUL;
3252 else
3253 nc = utf_ptr2char(p + mb_l);
3254 }
3255 else
3256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003257 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (j + mb_l >= start + len)
3259 pc = NUL;
3260 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003261 {
3262 int pcc[MAX_MCO];
3263
3264 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003266 pc1 = pcc[0];
3267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 nc = prev_c;
3269 }
3270 prev_c = u8c;
3271
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003272 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003275 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003277 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3278 if (u8cc[1] != 0)
3279 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 }
3283 else
3284 {
3285 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003286 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 newlen += mb_l;
3288 }
3289 }
3290
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003291 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293 else
3294#endif
3295 msg_outtrans_len(ccline.cmdbuff + start, len);
3296}
3297
3298/*
3299 * Put a character on the command line. Shifts the following text to the
3300 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3301 * "c" must be printable (fit in one display cell)!
3302 */
3303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003304putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 if (cmd_silent)
3307 return;
3308 msg_no_more = TRUE;
3309 msg_putchar(c);
3310 if (shift)
3311 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3312 msg_no_more = FALSE;
3313 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003314 extra_char = c;
3315 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316}
3317
3318/*
3319 * Undo a putcmdline(c, FALSE).
3320 */
3321 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003322unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 if (cmd_silent)
3325 return;
3326 msg_no_more = TRUE;
3327 if (ccline.cmdlen == ccline.cmdpos)
3328 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003329 else if (has_mbyte)
3330 draw_cmdline(ccline.cmdpos,
3331 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
3333 draw_cmdline(ccline.cmdpos, 1);
3334 msg_no_more = FALSE;
3335 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003336 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337}
3338
3339/*
3340 * Put the given string, of the given length, onto the command line.
3341 * If len is -1, then STRLEN() is used to calculate the length.
3342 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3343 * part will be redrawn, otherwise it will not. If this function is called
3344 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3345 * called afterwards.
3346 */
3347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003348put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 int retval;
3351 int i;
3352 int m;
3353 int c;
3354
3355 if (len < 0)
3356 len = (int)STRLEN(str);
3357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003358 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003360 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else
3362 retval = OK;
3363 if (retval == OK)
3364 {
3365 if (!ccline.overstrike)
3366 {
3367 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3368 ccline.cmdbuff + ccline.cmdpos,
3369 (size_t)(ccline.cmdlen - ccline.cmdpos));
3370 ccline.cmdlen += len;
3371 }
3372 else
3373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (has_mbyte)
3375 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003376 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003378 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003380 // Count nr of bytes in cmdline that are overwritten by these
3381 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003383 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 --m;
3385 if (i < ccline.cmdlen)
3386 {
3387 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3388 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3389 ccline.cmdlen += ccline.cmdpos + len - i;
3390 }
3391 else
3392 ccline.cmdlen = ccline.cmdpos + len;
3393 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003394 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 ccline.cmdlen = ccline.cmdpos + len;
3396 }
3397 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3398 ccline.cmdbuff[ccline.cmdlen] = NUL;
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (enc_utf8)
3401 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003402 // When the inserted text starts with a composing character,
3403 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 i = 0;
3405 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3406 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3407 {
3408 i = (*mb_head_off)(ccline.cmdbuff,
3409 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3410 ccline.cmdpos -= i;
3411 len += i;
3412 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3413 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003414#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003417 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 i = (*mb_head_off)(ccline.cmdbuff,
3419 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3420 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3421 + ccline.cmdpos - i), c))
3422 {
3423 ccline.cmdpos -= i;
3424 len += i;
3425 }
3426 else
3427 i = 0;
3428 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (i != 0)
3431 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003432 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3434 ccline.cmdspos -= i;
3435 msg_col -= i;
3436 if (msg_col < 0)
3437 {
3438 msg_col += Columns;
3439 --msg_row;
3440 }
3441 }
3442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 if (redraw && !cmd_silent)
3445 {
3446 msg_no_more = TRUE;
3447 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003448 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003450 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (cmdline_row != i || ccline.overstrike)
3452 msg_clr_eos();
3453 msg_no_more = FALSE;
3454 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003455 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003457 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003458 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003460 }
3461 else
3462 m = MAXCOL;
3463 for (i = 0; i < len; ++i)
3464 {
3465 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003466 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003467 if (has_mbyte)
3468 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003469 // Stop cursor at the end of the screen, but do increment the
3470 // insert position, so that entering a very long command
3471 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003472 if (ccline.cmdspos + c < m)
3473 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003474
Bram Moolenaar14184a32019-02-16 15:10:30 +01003475 if (has_mbyte)
3476 {
3477 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3478 if (c > len - i - 1)
3479 c = len - i - 1;
3480 ccline.cmdpos += c;
3481 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003483 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485 }
3486 if (redraw)
3487 msg_check();
3488 return retval;
3489}
3490
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491static cmdline_info_T prev_ccline;
3492static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003493
3494/*
3495 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3496 * and overwrite it. But get_cmdline_str() may need it, thus make it
3497 * available globally in prev_ccline.
3498 */
3499 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003501{
3502 if (!prev_ccline_used)
3503 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003504 CLEAR_FIELD(prev_ccline);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003505 prev_ccline_used = TRUE;
3506 }
3507 *ccp = prev_ccline;
3508 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003509 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003510}
3511
3512/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003513 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003514 */
3515 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003517{
3518 ccline = prev_ccline;
3519 prev_ccline = *ccp;
3520}
3521
Bram Moolenaar8299df92004-07-10 09:47:34 +00003522/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003523 * Paste a yank register into the command line.
3524 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003525 * insert_reg() can't be used here, because special characters from the
3526 * register contents will be interpreted as commands.
3527 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003528 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003529 */
3530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003531cmdline_paste(
3532 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003533 int literally, // Insert text literally instead of "as typed"
3534 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535{
3536 long i;
3537 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003538 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003540
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003541 // check for valid regname; also accept special characters for CTRL-R in
3542 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003544 && regname != Ctrl_A && regname != Ctrl_L
3545 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003546 return FAIL;
3547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003548 // A register containing CTRL-R can cause an endless loop. Allow using
3549 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550 line_breakcheck();
3551 if (got_int)
3552 return FAIL;
3553
3554#ifdef FEAT_CLIPBOARD
3555 regname = may_get_selection(regname);
3556#endif
3557
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003558 // Need to set "textwinlock" to avoid nasty things like going to another
Bram Moolenaar438d1762018-09-30 17:11:48 +02003559 // buffer when evaluating an expression.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003560 ++textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003561 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003562 --textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003563
3564 if (i)
3565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003566 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003567 if (arg == NULL)
3568 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003569
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003570 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3571 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003572 p = arg;
3573 if (p_is && regname == Ctrl_W)
3574 {
3575 char_u *w;
3576 int len;
3577
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003578 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003579 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003580 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003581 if (has_mbyte)
3582 {
3583 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3584 if (!vim_iswordc(mb_ptr2char(w - len)))
3585 break;
3586 w -= len;
3587 }
3588 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003589 {
3590 if (!vim_iswordc(w[-1]))
3591 break;
3592 --w;
3593 }
3594 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003595 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003596 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3597 p += len;
3598 }
3599
3600 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003601 if (allocated)
3602 vim_free(arg);
3603 return OK;
3604 }
3605
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003606 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003607}
3608
3609/*
3610 * Put a string on the command line.
3611 * When "literally" is TRUE, insert literally.
3612 * When "literally" is FALSE, insert as typed, but don't leave the command
3613 * line.
3614 */
3615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003616cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003617{
3618 int c, cv;
3619
3620 if (literally)
3621 put_on_cmdline(s, -1, TRUE);
3622 else
3623 while (*s != NUL)
3624 {
3625 cv = *s;
3626 if (cv == Ctrl_V && s[1])
3627 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003628 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003629 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003631 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003632 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3633 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003634#ifdef UNIX
3635 || c == intr_char
3636#endif
3637 || (c == Ctrl_BSL && *s == Ctrl_N))
3638 stuffcharReadbuff(Ctrl_V);
3639 stuffcharReadbuff(c);
3640 }
3641}
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003644 * This function is called when the screen size changes and with incremental
3645 * search and in other situations where the command line may have been
3646 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003649redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003651 redrawcmdline_ex(TRUE);
3652}
3653
3654 void
3655redrawcmdline_ex(int do_compute_cmdrow)
3656{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (cmd_silent)
3658 return;
3659 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003660 if (do_compute_cmdrow)
3661 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 redrawcmd();
3663 cursorcmd();
3664}
3665
3666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003667redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669 int i;
3670
3671 if (cmd_silent)
3672 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003673 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 msg_putchar(ccline.cmdfirstc);
3675 if (ccline.cmdprompt != NULL)
3676 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003679 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003680 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 --ccline.cmdindent;
3682 }
3683 else
3684 for (i = ccline.cmdindent; i > 0; --i)
3685 msg_putchar(' ');
3686}
3687
3688/*
3689 * Redraw what is currently on the command line.
3690 */
3691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003692redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 if (cmd_silent)
3695 return;
3696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003697 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003698 if (ccline.cmdbuff == NULL)
3699 {
3700 windgoto(cmdline_row, 0);
3701 msg_clr_eos();
3702 return;
3703 }
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 msg_start();
3706 redrawcmdprompt();
3707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003708 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 msg_no_more = TRUE;
3710 draw_cmdline(0, ccline.cmdlen);
3711 msg_clr_eos();
3712 msg_no_more = FALSE;
3713
3714 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003715 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003716 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /*
3719 * An emsg() before may have set msg_scroll. This is used in normal mode,
3720 * in cmdline mode we can reset them now.
3721 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003722 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003724 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3725 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 skip_redraw = FALSE;
3727}
3728
3729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003730compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003732 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 cmdline_row = Rows - 1;
3734 else
3735 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003736 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737}
3738
Bram Moolenaar66b51422019-08-18 21:44:12 +02003739 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003740cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 if (cmd_silent)
3743 return;
3744
3745#ifdef FEAT_RIGHTLEFT
3746 if (cmdmsg_rl)
3747 {
3748 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3749 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3750 if (msg_row <= 0)
3751 msg_row = Rows - 1;
3752 }
3753 else
3754#endif
3755 {
3756 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3757 msg_col = ccline.cmdspos % (int)Columns;
3758 if (msg_row >= Rows)
3759 msg_row = Rows - 1;
3760 }
3761
3762 windgoto(msg_row, msg_col);
3763#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003764 if (p_imst == IM_ON_THE_SPOT)
3765 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766#endif
3767#ifdef MCH_CURSOR_SHAPE
3768 mch_update_cursor();
3769#endif
3770}
3771
3772 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003773gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 msg_start();
3776#ifdef FEAT_RIGHTLEFT
3777 if (cmdmsg_rl)
3778 msg_col = Columns - 1;
3779 else
3780#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003781 msg_col = 0; // always start in column 0
3782 if (clr) // clear the bottom line(s)
3783 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 windgoto(cmdline_row, 0);
3785}
3786
3787/*
3788 * Check the word in front of the cursor for an abbreviation.
3789 * Called when the non-id character "c" has been entered.
3790 * When an abbreviation is recognized it is removed from the text with
3791 * backspaces and the replacement string is inserted, followed by "c".
3792 */
3793 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003794ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003796 int spos = 0;
3797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003798 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 return FALSE;
3800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003801 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3802 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003803 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3804 spos++;
3805 if (ccline.cmdlen - spos > 5
3806 && ccline.cmdbuff[spos] == '\''
3807 && ccline.cmdbuff[spos + 2] == ','
3808 && ccline.cmdbuff[spos + 3] == '\'')
3809 spos += 5;
3810 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003811 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003812 spos = 0;
3813
3814 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815}
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003818 * Escape special characters in "fname" for when used as a file name argument
3819 * after a Vim command, or, when "shell" is non-zero, a shell command.
3820 * Returns the result in allocated memory.
3821 */
3822 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003823vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003824{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003825 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003826#ifdef BACKSLASH_IN_FILENAME
3827 char_u buf[20];
3828 int j = 0;
3829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003830 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003831 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003832 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003833 buf[j++] = *p;
3834 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003835 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003836#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003837 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3838 if (shell && csh_like_shell() && p != NULL)
3839 {
3840 char_u *s;
3841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003842 // For csh and similar shells need to put two backslashes before '!'.
3843 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003844 s = vim_strsave_escaped(p, (char_u *)"!");
3845 vim_free(p);
3846 p = s;
3847 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003848#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003850 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3851 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003852 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003853 escape_fname(&p);
3854
3855 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003856}
3857
3858/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003859 * Put a backslash before the file name in "pp", which is in allocated memory.
3860 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003861 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003862escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003863{
3864 char_u *p;
3865
Bram Moolenaar964b3742019-05-24 18:54:09 +02003866 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003867 if (p != NULL)
3868 {
3869 p[0] = '\\';
3870 STRCPY(p + 1, *pp);
3871 vim_free(*pp);
3872 *pp = p;
3873 }
3874}
3875
3876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 * For each file name in files[num_files]:
3878 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3879 */
3880 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003881tilde_replace(
3882 char_u *orig_pat,
3883 int num_files,
3884 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886 int i;
3887 char_u *p;
3888
3889 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3890 {
3891 for (i = 0; i < num_files; ++i)
3892 {
3893 p = home_replace_save(NULL, files[i]);
3894 if (p != NULL)
3895 {
3896 vim_free(files[i]);
3897 files[i] = p;
3898 }
3899 }
3900 }
3901}
3902
3903/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003904 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003906 cmdline_info_T *
3907get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003909 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910}
3911
Bram Moolenaar064154c2016-03-19 22:50:43 +01003912#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003913/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003914 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003915 * ccline and put the previous value in prev_ccline.
3916 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003917 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003918get_ccline_ptr(void)
3919{
3920 if ((State & CMDLINE) == 0)
3921 return NULL;
3922 if (ccline.cmdbuff != NULL)
3923 return &ccline;
3924 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3925 return &prev_ccline;
3926 return NULL;
3927}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003928#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003929
Bram Moolenaar064154c2016-03-19 22:50:43 +01003930#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003931/*
3932 * Get the current command line in allocated memory.
3933 * Only works when the command line is being edited.
3934 * Returns NULL when something is wrong.
3935 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003936 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003937get_cmdline_str(void)
3938{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003939 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003940
Bram Moolenaaree91c332018-09-25 22:27:35 +02003941 if (cmdline_star > 0)
3942 return NULL;
3943 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003944 if (p == NULL)
3945 return NULL;
3946 return vim_strnsave(p->cmdbuff, p->cmdlen);
3947}
3948
3949/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003950 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003951 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003952 void
3953f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3954{
3955 rettv->v_type = VAR_STRING;
3956 rettv->vval.v_string = get_cmdline_str();
3957}
3958
3959/*
3960 * "getcmdpos()" function
3961 */
3962 void
3963f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003964{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003965 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003966
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003967 rettv->vval.v_number = 0;
3968 if (p != NULL)
3969 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003970}
3971
3972/*
3973 * Set the command line byte position to "pos". Zero is the first position.
3974 * Only works when the command line is being edited.
3975 * Returns 1 when failed, 0 when OK.
3976 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003977 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003978set_cmdline_pos(
3979 int pos)
3980{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003981 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003982
3983 if (p == NULL)
3984 return 1;
3985
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003986 // The position is not set directly but after CTRL-\ e or CTRL-R = has
3987 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003988 if (pos < 0)
3989 new_cmdpos = 0;
3990 else
3991 new_cmdpos = pos;
3992 return 0;
3993}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003994
3995/*
3996 * "setcmdpos()" function
3997 */
3998 void
3999f_setcmdpos(typval_T *argvars, typval_T *rettv)
4000{
4001 int pos = (int)tv_get_number(&argvars[0]) - 1;
4002
4003 if (pos >= 0)
4004 rettv->vval.v_number = set_cmdline_pos(pos);
4005}
4006
4007/*
4008 * "getcmdtype()" function
4009 */
4010 void
4011f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4012{
4013 rettv->v_type = VAR_STRING;
4014 rettv->vval.v_string = alloc(2);
4015 if (rettv->vval.v_string != NULL)
4016 {
4017 rettv->vval.v_string[0] = get_cmdline_type();
4018 rettv->vval.v_string[1] = NUL;
4019 }
4020}
4021
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004022#endif
4023
4024#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4025/*
4026 * Get the current command-line type.
4027 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4028 * Only works when the command line is being edited.
4029 * Returns NUL when something is wrong.
4030 */
4031 int
4032get_cmdline_type(void)
4033{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004034 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004035
4036 if (p == NULL)
4037 return NUL;
4038 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004039 return
4040# ifdef FEAT_EVAL
4041 (p->input_fn) ? '@' :
4042# endif
4043 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004044 return p->cmdfirstc;
4045}
4046#endif
4047
Bram Moolenaard7663c22019-08-06 21:59:57 +02004048/*
4049 * Return the first character of the current command line.
4050 */
4051 int
4052get_cmdline_firstc(void)
4053{
4054 return ccline.cmdfirstc;
4055}
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057/*
4058 * Get indices "num1,num2" that specify a range within a list (not a range of
4059 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4060 * Returns OK if parsed successfully, otherwise FAIL.
4061 */
4062 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004063get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
4065 int len;
4066 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004067 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004070 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004072 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 *str += len;
4074 *num1 = (int)num;
4075 first = TRUE;
4076 }
4077 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004078 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
4080 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004081 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (len > 0)
4083 {
4084 *num2 = (int)num;
4085 *str = skipwhite(*str + len);
4086 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004087 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004090 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 *num2 = *num1;
4092 return OK;
4093}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#if defined(FEAT_CMDWIN) || defined(PROTO)
4096/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004097 * Check value of 'cedit' and set cedit_key.
4098 * Returns NULL if value is OK, error message otherwise.
4099 */
4100 char *
4101check_cedit(void)
4102{
4103 int n;
4104
4105 if (*p_cedit == NUL)
4106 cedit_key = -1;
4107 else
4108 {
4109 n = string_to_key(p_cedit, FALSE);
4110 if (vim_isprintc(n))
4111 return e_invarg;
4112 cedit_key = n;
4113 }
4114 return NULL;
4115}
4116
4117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Open a window on the current command line and history. Allow editing in
4119 * the window. Returns when the window is closed.
4120 * Returns:
4121 * CR if the command is to be executed
4122 * Ctrl_C if it is to be abandoned
4123 * K_IGNORE if editing continues
4124 */
4125 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004126open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004128 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004130 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 win_T *wp;
4132 int i;
4133 linenr_T lnum;
4134 int histtype;
4135 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 int save_restart_edit = restart_edit;
4137 int save_State = State;
4138 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004139#ifdef FEAT_RIGHTLEFT
4140 int save_cmdmsg_rl = cmdmsg_rl;
4141#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004142#ifdef FEAT_FOLDING
4143 int save_KeyTyped;
4144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004146 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (cmdwin_type != 0
4148# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4149 || cmdline_star > 0
4150# endif
4151 )
4152 {
4153 beep_flush();
4154 return K_IGNORE;
4155 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004156 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004158 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 win_size_save(&winsizes);
4160
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004161 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4162 // command line window, but we don't want the popup menu then.
4163 pum_undisplay();
4164
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004165 // don't use a new tab page
Bram Moolenaare1004402020-10-24 20:49:43 +02004166 cmdmod.cmod_tab = 0;
4167 cmdmod.cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004168
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004169 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4171 {
4172 beep_flush();
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004173 ga_clear(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return K_IGNORE;
4175 }
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004176 // Don't let quitting the More prompt make this fail.
4177 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004179 // Create the command-line buffer empty.
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004180 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL) == FAIL)
4181 {
4182 // Some autocommand messed it up?
4183 win_close(curwin, TRUE);
4184 ga_clear(&winsizes);
4185 return Ctrl_C;
4186 }
4187 cmdwin_type = get_cmdline_type();
4188
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004189 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004190 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004191 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004194#ifdef FEAT_FOLDING
4195 curwin->w_p_fen = FALSE;
4196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004198 curwin->w_p_rl = cmdmsg_rl;
4199 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004201 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004203 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004204 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004206 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004207 need_wait_return = FALSE;
4208
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004209 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4211 {
4212 if (p_wc == TAB)
4213 {
4214 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4215 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4216 }
4217 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4218 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004219 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004221 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4222 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004223 curbuf->b_p_tw = 0;
4224
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004225 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004227 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004229 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (i >= 0)
4231 {
4232 lnum = 0;
4233 do
4234 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004235 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004237 if (get_histentry(histtype)[i].hisstr != NULL)
4238 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 (colnr_T)0, FALSE);
4240 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004241 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004245 // Replace the empty last line with the current command-line and put the
4246 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4248 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4249 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004250 changed_line_abv_curs();
4251 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004252 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar23324a02019-10-01 17:39:04 +02004254 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 exmode_active = 0;
4256
4257 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaar23324a02019-10-01 17:39:04 +02004260 // Reset here so it can be set by a CmdWinEnter autocommand.
4261 cmdwin_result = 0;
4262
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004263 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004264 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004265 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004266 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 i = RedrawingDisabled;
4269 RedrawingDisabled = 0;
4270
4271 /*
4272 * Call the main loop until <CR> or CTRL-C is typed.
4273 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004274 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 RedrawingDisabled = i;
4277
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004278# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004279 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004280# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004281
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004282 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004283 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004284
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004285# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004286 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004287 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288# endif
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 exmode_active = save_exmode;
4292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004293 // Safety check: The old window or buffer was deleted: It's a bug when
4294 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004295 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 {
4297 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004298 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
4301 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004302# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (aborting() && cmdwin_result != K_IGNORE)
4305 cmdwin_result = Ctrl_C;
4306# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004311 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4312
4313 if (histtype == HIST_CMD)
4314 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004315 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004316 ccline.cmdbuff = vim_strsave((char_u *)p);
4317 cmdwin_result = CAR;
4318 }
4319 else
4320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004322 ccline.cmdbuff = NULL;
4323 stuffcharReadbuff(':');
4324 stuffReadbuff((char_u *)p);
4325 stuffcharReadbuff(CAR);
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4331 cmdwin_result = CAR;
4332 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004333 else if (cmdwin_result == Ctrl_C)
4334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004335 // :q or :close, don't execute any command
4336 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004337 ccline.cmdbuff = NULL;
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 else
4340 ccline.cmdbuff = vim_strsave(ml_get_curline());
4341 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004342 {
4343 ccline.cmdbuff = vim_strsave((char_u *)"");
4344 ccline.cmdlen = 0;
4345 ccline.cmdbufflen = 1;
4346 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else
4350 {
4351 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4352 ccline.cmdbufflen = ccline.cmdlen + 1;
4353 ccline.cmdpos = curwin->w_cursor.col;
4354 if (ccline.cmdpos > ccline.cmdlen)
4355 ccline.cmdpos = ccline.cmdlen;
4356 if (cmdwin_result == K_IGNORE)
4357 {
4358 set_cmdspos_cursor();
4359 redrawcmd();
4360 }
4361 }
4362
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004363# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004364 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004365 curwin->w_p_cole = 0;
4366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004368 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 win_goto(old_curwin);
4370 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004371
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004372 // win_close() may have already wiped the buffer when 'bh' is
4373 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004374 if (bufref_valid(&bufref))
Bram Moolenaara6e8f882019-12-14 16:18:15 +01004375 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004377 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380
4381 ga_clear(&winsizes);
4382 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004383# ifdef FEAT_RIGHTLEFT
4384 cmdmsg_rl = save_cmdmsg_rl;
4385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 return cmdwin_result;
4391}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004392#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394/*
4395 * Used for commands that either take a simple command string argument, or:
4396 * cmd << endmarker
4397 * {script}
4398 * endmarker
4399 * Returns a pointer to allocated memory with {script} or NULL.
4400 */
4401 char_u *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004402script_get(exarg_T *eap UNUSED, char_u *cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004404#ifdef FEAT_EVAL
4405 list_T *l;
4406 listitem_T *li;
4407 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 garray_T ga;
4409
4410 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4411 return NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004412 cmd += 2;
4413
4414 l = heredoc_get(eap, cmd, TRUE);
4415 if (l == NULL)
4416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 ga_init2(&ga, 1, 0x400);
4419
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004420 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004422 s = tv_get_string(&li->li_tv);
4423 ga_concat(&ga, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004426 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004428 list_free(l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 return (char_u *)ga.ga_data;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004430#else
4431 return NULL;
4432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004434
4435#if defined(FEAT_EVAL) || defined(PROTO)
4436/*
4437 * This function is used by f_input() and f_inputdialog() functions. The third
4438 * argument to f_input() specifies the type of completion to use at the
4439 * prompt. The third argument to f_inputdialog() specifies the value to return
4440 * when the user cancels the prompt.
4441 */
4442 void
4443get_user_input(
4444 typval_T *argvars,
4445 typval_T *rettv,
4446 int inputdialog,
4447 int secret)
4448{
4449 char_u *prompt = tv_get_string_chk(&argvars[0]);
4450 char_u *p = NULL;
4451 int c;
4452 char_u buf[NUMBUFLEN];
4453 int cmd_silent_save = cmd_silent;
4454 char_u *defstr = (char_u *)"";
4455 int xp_type = EXPAND_NOTHING;
4456 char_u *xp_arg = NULL;
4457
4458 rettv->v_type = VAR_STRING;
4459 rettv->vval.v_string = NULL;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004460 if (input_busy)
4461 return; // this doesn't work recursively.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004462
4463#ifdef NO_CONSOLE_INPUT
4464 // While starting up, there is no place to enter text. When running tests
4465 // with --not-a-term we assume feedkeys() will be used.
4466 if (no_console_input() && !is_not_a_term())
4467 return;
4468#endif
4469
4470 cmd_silent = FALSE; // Want to see the prompt.
4471 if (prompt != NULL)
4472 {
4473 // Only the part of the message after the last NL is considered as
4474 // prompt for the command line
4475 p = vim_strrchr(prompt, '\n');
4476 if (p == NULL)
4477 p = prompt;
4478 else
4479 {
4480 ++p;
4481 c = *p;
4482 *p = NUL;
4483 msg_start();
4484 msg_clr_eos();
4485 msg_puts_attr((char *)prompt, get_echo_attr());
4486 msg_didout = FALSE;
4487 msg_starthere();
4488 *p = c;
4489 }
4490 cmdline_row = msg_row;
4491
4492 if (argvars[1].v_type != VAR_UNKNOWN)
4493 {
4494 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4495 if (defstr != NULL)
4496 stuffReadbuffSpec(defstr);
4497
4498 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4499 {
4500 char_u *xp_name;
4501 int xp_namelen;
4502 long argt;
4503
4504 // input() with a third argument: completion
4505 rettv->vval.v_string = NULL;
4506
4507 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4508 if (xp_name == NULL)
4509 return;
4510
4511 xp_namelen = (int)STRLEN(xp_name);
4512
4513 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4514 &xp_arg) == FAIL)
4515 return;
4516 }
4517 }
4518
4519 if (defstr != NULL)
4520 {
4521 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004522 int save_vgetc_busy = vgetc_busy;
4523 int save_input_busy = input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004524
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004525 input_busy |= vgetc_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004526 ex_normal_busy = 0;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004527 vgetc_busy = 0;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004528 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;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004532 vgetc_busy = save_vgetc_busy;
4533 input_busy = save_input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004534 }
4535 if (inputdialog && rettv->vval.v_string == NULL
4536 && argvars[1].v_type != VAR_UNKNOWN
4537 && argvars[2].v_type != VAR_UNKNOWN)
4538 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4539 &argvars[2], buf));
4540
4541 vim_free(xp_arg);
4542
4543 // since the user typed this, no need to wait for return
4544 need_wait_return = FALSE;
4545 msg_didout = FALSE;
4546 }
4547 cmd_silent = cmd_silent_save;
4548}
4549#endif