blob: 2c157ee52f3d95508f512592f3080fcc3209341c [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 Moolenaarc036e872020-02-21 21:30:52 +0100190do_incsearch_highlighting(int firstc, int *search_delim, incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200191 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200192{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200193 char_u *cmd;
194 cmdmod_T save_cmdmod = cmdmod;
195 char_u *p;
196 int delim_optional = FALSE;
197 int delim;
198 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100199 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200200 exarg_T ea;
201 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200202 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100203 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200204
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200205 *skiplen = 0;
206 *patlen = ccline.cmdlen;
207
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200208 if (!p_is || cmd_silent)
209 return FALSE;
210
211 // by default search all lines
212 search_first_line = 0;
213 search_last_line = MAXLNUM;
214
215 if (firstc == '/' || firstc == '?')
Bram Moolenaarc036e872020-02-21 21:30:52 +0100216 {
217 *search_delim = firstc;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200218 return TRUE;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100219 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200220 if (firstc != ':')
221 return FALSE;
222
Bram Moolenaarc6725252019-11-23 21:56:46 +0100223 ++emsg_off;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200224 CLEAR_FIELD(ea);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200225 ea.line1 = 1;
226 ea.line2 = 1;
227 ea.cmd = ccline.cmdbuff;
228 ea.addr_type = ADDR_LINES;
229
230 parse_command_modifiers(&ea, &dummy, TRUE);
231 cmdmod = save_cmdmod;
232
233 cmd = skip_range(ea.cmd, NULL);
234 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100235 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
237 // Skip over "substitute" to find the pattern separator.
238 for (p = cmd; ASCII_ISALPHA(*p); ++p)
239 ;
240 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100241 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200242
243 if (STRNCMP(cmd, "substitute", p - cmd) == 0
244 || STRNCMP(cmd, "smagic", p - cmd) == 0
245 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
246 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200247 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200248 if (*cmd == 's' && cmd[1] == 'm')
249 p_magic = TRUE;
250 else if (*cmd == 's' && cmd[1] == 'n')
251 p_magic = FALSE;
252 }
253 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
254 {
Bram Moolenaar333015a2020-04-25 15:54:16 +0200255 // skip over ! and flags
256 if (*p == '!')
257 p = skipwhite(p + 1);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200258 while (ASCII_ISALPHA(*(p = skipwhite(p))))
259 ++p;
260 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100261 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200262 }
263 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
264 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
265 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
266 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
267 || STRNCMP(cmd, "global", p - cmd) == 0)
268 {
269 // skip over "!"
270 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200271 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272 p++;
273 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100274 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200275 }
276 if (*cmd != 'g')
277 delim_optional = TRUE;
278 }
279 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100280 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200281
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200282 p = skipwhite(p);
283 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100284 *search_delim = delim;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200285 end = skip_regexp(p, delim, p_magic);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200286
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200287 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200288
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200289 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100290 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200291
292 // Don't do 'hlsearch' highlighting if the pattern matches everything.
293 if (!use_last_pat)
294 {
295 char c = *end;
296 int empty;
297
298 *end = NUL;
299 empty = empty_pattern(p);
300 *end = c;
301 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100302 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200303 }
304
305 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200306 *skiplen = (int)(p - ccline.cmdbuff);
307 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200308
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200309 // parse the address range
310 save_cursor = curwin->w_cursor;
311 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200312 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200313 if (ea.addr_count > 0)
314 {
315 // Allow for reverse match.
316 if (ea.line2 < ea.line1)
317 {
318 search_first_line = ea.line2;
319 search_last_line = ea.line1;
320 }
321 else
322 {
323 search_first_line = ea.line1;
324 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200325 }
326 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200327 else if (cmd[0] == 's' && cmd[1] != 'o')
328 {
329 // :s defaults to the current line
330 search_first_line = curwin->w_cursor.lnum;
331 search_last_line = curwin->w_cursor.lnum;
332 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200333
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200334 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100335 retval = TRUE;
336theend:
337 --emsg_off;
338 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200339}
340
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200341 static void
342finish_incsearch_highlighting(
343 int gotesc,
344 incsearch_state_T *is_state,
345 int call_update_screen)
346{
347 if (is_state->did_incsearch)
348 {
349 is_state->did_incsearch = FALSE;
350 if (gotesc)
351 curwin->w_cursor = is_state->save_cursor;
352 else
353 {
354 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
355 {
356 // put the '" mark at the original position
357 curwin->w_cursor = is_state->save_cursor;
358 setpcmark();
359 }
360 curwin->w_cursor = is_state->search_start;
361 }
362 restore_viewstate(&is_state->old_viewstate);
363 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200364
365 // by default search all lines
366 search_first_line = 0;
367 search_last_line = MAXLNUM;
368
369 p_magic = is_state->magic_save;
370
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100371 validate_cursor(); // needed for TAB
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200372 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200373 if (call_update_screen)
374 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200375 }
376}
377
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200378/*
379 * Do 'incsearch' highlighting if desired.
380 */
381 static void
382may_do_incsearch_highlighting(
383 int firstc,
384 long count,
385 incsearch_state_T *is_state)
386{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200387 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200388 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200389 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200390#ifdef FEAT_RELTIME
391 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200392 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200393#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200394 int next_char;
395 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200396 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100397 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200398
Bram Moolenaar198cb662018-09-06 21:44:17 +0200399 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100400 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200401 save_last_search_pattern();
402
Bram Moolenaara60053b2020-09-03 16:50:13 +0200403 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
404 &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200405 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200406 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200407 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200408 if (did_do_incsearch && vpeekc() == NUL)
409 // may have skipped a redraw, do it now
410 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200411 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200412 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200413
414 // If there is a character waiting, search and redraw later.
415 if (char_avail())
416 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200417 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200418 is_state->incsearch_postponed = TRUE;
419 return;
420 }
421 is_state->incsearch_postponed = FALSE;
422
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200423 if (search_first_line == 0)
424 // start at the original cursor position
425 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100426 else if (search_first_line > curbuf->b_ml.ml_line_count)
427 {
428 // start after the last line
429 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
430 curwin->w_cursor.col = MAXCOL;
431 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200432 else
433 {
434 // start at the first line in the range
435 curwin->w_cursor.lnum = search_first_line;
436 curwin->w_cursor.col = 0;
437 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200438
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200439 // Use the previous pattern for ":s//".
440 next_char = ccline.cmdbuff[skiplen + patlen];
441 use_last_pat = patlen == 0 && skiplen > 0
442 && ccline.cmdbuff[skiplen - 1] == next_char;
443
444 // If there is no pattern, don't do anything.
445 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200446 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200447 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200448 set_no_hlsearch(TRUE); // turn off previous highlight
449 redraw_all_later(SOME_VALID);
450 }
451 else
452 {
453 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
454
455 cursor_off(); // so the user knows we're busy
456 out_flush();
457 ++emsg_off; // so it doesn't beep if bad expr
458#ifdef FEAT_RELTIME
459 // Set the time limit to half a second.
460 profile_setlimit(500L, &tm);
461#endif
462 if (!p_hls)
463 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200464 if (search_first_line != 0)
465 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200467#ifdef FEAT_RELTIME
Bram Moolenaara80faa82020-04-12 19:37:17 +0200468 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200469 sia.sa_tm = &tm;
470#endif
Bram Moolenaarc036e872020-02-21 21:30:52 +0100471 found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200472 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200474 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200475#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200476 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200477#endif
478 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200479 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200480 --emsg_off;
481
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200482 if (curwin->w_cursor.lnum < search_first_line
483 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200484 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200485 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200486 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200487 curwin->w_cursor = is_state->search_start;
488 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200489
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 // if interrupted while searching, behave like it failed
491 if (got_int)
492 {
493 (void)vpeekc(); // remove <C-C> from input stream
494 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200495 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200496 }
497 else if (char_avail())
498 // cancelled searching because a char was typed
499 is_state->incsearch_postponed = TRUE;
500 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200501 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 highlight_match = TRUE; // highlight position
503 else
504 highlight_match = FALSE; // remove highlight
505
506 // First restore the old curwin values, so the screen is positioned in the
507 // same way as the actual search command.
508 restore_viewstate(&is_state->old_viewstate);
509 changed_cline_bef_curs();
510 update_topline();
511
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200512 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200513 {
514 pos_T save_pos = curwin->w_cursor;
515
516 is_state->match_start = curwin->w_cursor;
517 set_search_match(&curwin->w_cursor);
518 validate_cursor();
519 end_pos = curwin->w_cursor;
520 is_state->match_end = end_pos;
521 curwin->w_cursor = save_pos;
522 }
523 else
524 end_pos = curwin->w_cursor; // shutup gcc 4
525
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200526 // Disable 'hlsearch' highlighting if the pattern matches everything.
527 // Avoids a flash when typing "foo\|".
528 if (!use_last_pat)
529 {
530 next_char = ccline.cmdbuff[skiplen + patlen];
531 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200532 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
533 {
534 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200535 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200536 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200537 ccline.cmdbuff[skiplen + patlen] = next_char;
538 }
539
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540 validate_cursor();
541 // May redraw the status line to show the cursor position.
542 if (p_ru && curwin->w_status_height > 0)
543 curwin->w_redr_status = TRUE;
544
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200545 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200546 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547 restore_last_search_pattern();
548
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200549 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
550 // end of the pattern, e.g. for ":s/pat/".
551 if (ccline.cmdbuff[skiplen + patlen] != NUL)
552 curwin->w_cursor = is_state->search_start;
553 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200554 curwin->w_cursor = end_pos;
555
556 msg_starthere();
557 redrawcmdline();
558 is_state->did_incsearch = TRUE;
559}
560
561/*
562 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
563 * or previous match.
564 * Returns FAIL when jumping to cmdline_not_changed;
565 */
566 static int
567may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200568 int firstc,
569 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200570 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200571 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200572{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574 pos_T t;
575 char_u *pat;
576 int search_flags = SEARCH_NOOF;
577 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200578 int save;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100579 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200580
Bram Moolenaar198cb662018-09-06 21:44:17 +0200581 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100582 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200583 save_last_search_pattern();
584
Bram Moolenaarc036e872020-02-21 21:30:52 +0100585 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200586 {
587 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200588 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200589 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200590 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200591 {
592 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200594 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200595
Bram Moolenaarc036e872020-02-21 21:30:52 +0100596 if (search_delim == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200597 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200599 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200600 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200601 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200603 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200605 cursor_off();
606 out_flush();
607 if (c == Ctrl_G)
608 {
609 t = is_state->match_end;
610 if (LT_POS(is_state->match_start, is_state->match_end))
611 // Start searching at the end of the match not at the beginning of
612 // the next column.
613 (void)decl(&t);
614 search_flags += SEARCH_COL;
615 }
616 else
617 t = is_state->match_start;
618 if (!p_hls)
619 search_flags += SEARCH_KEEP;
620 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 save = pat[patlen];
622 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100623 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200625 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200626 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200627 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628 if (i)
629 {
630 is_state->search_start = is_state->match_start;
631 is_state->match_end = t;
632 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200633 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634 {
635 // Move just before the current match, so that when nv_search
636 // finishes the cursor will be put back on the match.
637 is_state->search_start = t;
638 (void)decl(&is_state->search_start);
639 }
640 else if (c == Ctrl_G && firstc == '?')
641 {
642 // Move just after the current match, so that when nv_search
643 // finishes the cursor will be put back on the match.
644 is_state->search_start = t;
645 (void)incl(&is_state->search_start);
646 }
647 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
648 {
649 // wrap around
650 is_state->search_start = t;
651 if (firstc == '?')
652 (void)incl(&is_state->search_start);
653 else
654 (void)decl(&is_state->search_start);
655 }
656
657 set_search_match(&is_state->match_end);
658 curwin->w_cursor = is_state->match_start;
659 changed_cline_bef_curs();
660 update_topline();
661 validate_cursor();
662 highlight_match = TRUE;
663 save_viewstate(&is_state->old_viewstate);
664 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200665 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200666 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200667 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200668 }
669 else
670 vim_beep(BO_ERROR);
671 restore_last_search_pattern();
672 return FAIL;
673}
674
675/*
676 * When CTRL-L typed: add character from the match to the pattern.
677 * May set "*c" to the added character.
678 * Return OK when jumping to cmdline_not_changed.
679 */
680 static int
681may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
682{
Bram Moolenaarc036e872020-02-21 21:30:52 +0100683 int skiplen, patlen, search_delim;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200684
Bram Moolenaar198cb662018-09-06 21:44:17 +0200685 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100686 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200687 save_last_search_pattern();
688
Bram Moolenaar9b7cce22020-06-06 15:14:08 +0200689 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
690 &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200691 {
692 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200693 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200694 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100695 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200696
697 // Add a character from under the cursor for 'incsearch'.
698 if (is_state->did_incsearch)
699 {
700 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200701 *c = gchar_cursor();
702 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200703 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200704 // If 'ignorecase' and 'smartcase' are set and the
705 // command line has no uppercase characters, convert
706 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200707 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200708 *c = MB_TOLOWER(*c);
Bram Moolenaarc036e872020-02-21 21:30:52 +0100709 if (*c == search_delim || vim_strchr((char_u *)(
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200710 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200711 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200712 // put a backslash before special characters
713 stuffcharReadbuff(*c);
714 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200715 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200716 // add any composing characters
717 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
718 {
719 int save_c = *c;
720
721 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
722 {
723 curwin->w_cursor.col += mb_char2len(*c);
724 *c = gchar_cursor();
725 stuffcharReadbuff(*c);
726 }
727 *c = save_c;
728 }
729 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200730 }
731 }
732 return OK;
733}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200734#endif
735
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200736#ifdef FEAT_ARABIC
737/*
738 * Return TRUE if the command line has an Arabic character at or after "start"
739 * for "len" bytes.
740 */
741 static int
742cmdline_has_arabic(int start, int len)
743{
744 int j;
745 int mb_l;
746 int u8c;
747 char_u *p;
748 int u8cc[MAX_MCO];
749
750 if (!enc_utf8)
751 return FALSE;
752
753 for (j = start; j < start + len; j += mb_l)
754 {
755 p = ccline.cmdbuff + j;
756 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
757 mb_l = utfc_ptr2len_len(p, start + len - j);
758 if (ARABIC_CHAR(u8c))
759 return TRUE;
760 }
761 return FALSE;
762}
763#endif
764
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200765 void
766cmdline_init(void)
767{
Bram Moolenaara80faa82020-04-12 19:37:17 +0200768 CLEAR_FIELD(ccline);
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200769}
770
Bram Moolenaar66216052017-12-16 16:33:44 +0100771/*
Bram Moolenaar9c929712020-09-07 22:05:28 +0200772 * Handle the backslash key pressed in the command-line mode. CTRL-\ CTRL-N
773 * goes to Normal mode, CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is
774 * set, CTRL-\ e prompts for an expression.
775 */
776 static int
777cmdline_handle_backslash_key(int c, int *gotesc)
778{
779 ++no_mapping;
780 ++allow_keys;
781 c = plain_vgetc();
782 --no_mapping;
783 --allow_keys;
784
785 // CTRL-\ e doesn't work when obtaining an expression, unless it
786 // is in a mapping.
787 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
788 || (ccline.cmdfirstc == '=' && KeyTyped)
789#ifdef FEAT_EVAL
790 || cmdline_star > 0
791#endif
792 ))
793 {
794 vungetc(c);
795 return PROCESS_NEXT_KEY;
796 }
797
798#ifdef FEAT_EVAL
799 if (c == 'e')
800 {
801 char_u *p = NULL;
802 int len;
803
804 /*
805 * Replace the command line with the result of an expression.
806 * Need to save and restore the current command line, to be
807 * able to enter a new one...
808 */
809 if (ccline.cmdpos == ccline.cmdlen)
810 new_cmdpos = 99999; // keep it at the end
811 else
812 new_cmdpos = ccline.cmdpos;
813
814 c = get_expr_register();
815 if (c == '=')
816 {
817 // Need to save and restore ccline. And set "textwinlock"
818 // to avoid nasty things like going to another buffer when
819 // evaluating an expression.
820 ++textwinlock;
821 p = get_expr_line();
822 --textwinlock;
823
824 if (p != NULL)
825 {
826 len = (int)STRLEN(p);
827 if (realloc_cmdbuff(len + 1) == OK)
828 {
829 ccline.cmdlen = len;
830 STRCPY(ccline.cmdbuff, p);
831 vim_free(p);
832
833 // Restore the cursor or use the position set with
834 // set_cmdline_pos().
835 if (new_cmdpos > ccline.cmdlen)
836 ccline.cmdpos = ccline.cmdlen;
837 else
838 ccline.cmdpos = new_cmdpos;
839
840 KeyTyped = FALSE; // Don't do p_wc completion.
841 redrawcmd();
842 return CMDLINE_CHANGED;
843 }
844 vim_free(p);
845 }
846 }
847 beep_flush();
848 got_int = FALSE; // don't abandon the command line
849 did_emsg = FALSE;
850 emsg_on_display = FALSE;
851 redrawcmd();
852 return CMDLINE_NOT_CHANGED;
853 }
854#endif
855
856 if (c == Ctrl_G && p_im && restart_edit == 0)
857 restart_edit = 'a';
858 *gotesc = TRUE; // will free ccline.cmdbuff after putting it
859 // in history
860 return GOTO_NORMAL_MODE;
861}
862
863/*
864 * Completion for 'wildchar' or 'wildcharm' key.
865 * - hitting <ESC> twice means: abandon command line.
866 * - wildcard expansion is only done when the 'wildchar' key is really
867 * typed, not when it comes from a macro
868 * Returns CMDLINE_CHANGED if command line is changed or CMDLINE_NOT_CHANGED.
869 */
870 static int
871cmdline_wildchar_complete(
872 int c,
873 int escape,
874 int *did_wild_list,
875 int *wim_index_p,
876 expand_T *xp,
877 int *gotesc)
878{
879 int wim_index = *wim_index_p;
880 int res;
881 int j;
882 int options = WILD_NO_BEEP;
883
884 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
885 options |= WILD_BUFLASTUSED;
886 if (xp->xp_numfiles > 0) // typed p_wc at least twice
887 {
888 // if 'wildmode' contains "list" may still need to list
889 if (xp->xp_numfiles > 1
890 && !*did_wild_list
891 && (wim_flags[wim_index] & WIM_LIST))
892 {
893 (void)showmatches(xp, FALSE);
894 redrawcmd();
895 *did_wild_list = TRUE;
896 }
897 if (wim_flags[wim_index] & WIM_LONGEST)
898 res = nextwild(xp, WILD_LONGEST, options, escape);
899 else if (wim_flags[wim_index] & WIM_FULL)
900 res = nextwild(xp, WILD_NEXT, options, escape);
901 else
902 res = OK; // don't insert 'wildchar' now
903 }
904 else // typed p_wc first time
905 {
906 wim_index = 0;
907 j = ccline.cmdpos;
908 // if 'wildmode' first contains "longest", get longest
909 // common part
910 if (wim_flags[0] & WIM_LONGEST)
911 res = nextwild(xp, WILD_LONGEST, options, escape);
912 else
913 res = nextwild(xp, WILD_EXPAND_KEEP, options, escape);
914
915 // if interrupted while completing, behave like it failed
916 if (got_int)
917 {
918 (void)vpeekc(); // remove <C-C> from input stream
919 got_int = FALSE; // don't abandon the command line
920 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
921#ifdef FEAT_WILDMENU
922 xp->xp_context = EXPAND_NOTHING;
923#endif
924 *wim_index_p = wim_index;
925 return CMDLINE_CHANGED;
926 }
927
928 // when more than one match, and 'wildmode' first contains
929 // "list", or no change and 'wildmode' contains "longest,list",
930 // list all matches
931 if (res == OK && xp->xp_numfiles > 1)
932 {
933 // a "longest" that didn't do anything is skipped (but not
934 // "list:longest")
935 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
936 wim_index = 1;
937 if ((wim_flags[wim_index] & WIM_LIST)
938#ifdef FEAT_WILDMENU
939 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
940#endif
941 )
942 {
943 if (!(wim_flags[0] & WIM_LONGEST))
944 {
945#ifdef FEAT_WILDMENU
946 int p_wmnu_save = p_wmnu;
947 p_wmnu = 0;
948#endif
949 // remove match
950 nextwild(xp, WILD_PREV, 0, escape);
951#ifdef FEAT_WILDMENU
952 p_wmnu = p_wmnu_save;
953#endif
954 }
955#ifdef FEAT_WILDMENU
956 (void)showmatches(xp, p_wmnu
957 && ((wim_flags[wim_index] & WIM_LIST) == 0));
958#else
959 (void)showmatches(xp, FALSE);
960#endif
961 redrawcmd();
962 *did_wild_list = TRUE;
963 if (wim_flags[wim_index] & WIM_LONGEST)
964 nextwild(xp, WILD_LONGEST, options, escape);
965 else if (wim_flags[wim_index] & WIM_FULL)
966 nextwild(xp, WILD_NEXT, options, escape);
967 }
968 else
969 vim_beep(BO_WILD);
970 }
971#ifdef FEAT_WILDMENU
972 else if (xp->xp_numfiles == -1)
973 xp->xp_context = EXPAND_NOTHING;
974#endif
975 }
976 if (wim_index < 3)
977 ++wim_index;
978 if (c == ESC)
979 *gotesc = TRUE;
980
981 *wim_index_p = wim_index;
982 return (res == OK) ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
983}
984
985/*
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +0200986 * Handle backspace, delete and CTRL-W keys in the command-line mode.
987 * Returns:
988 * CMDLINE_NOT_CHANGED - if the command line is not changed
989 * CMDLINE_CHANGED - if the command line is changed
990 * GOTO_NORMAL_MODE - go back to normal mode
991 */
992 static int
993cmdline_erase_chars(
994 int c,
995 int indent
996#ifdef FEAT_SEARCH_EXTRA
997 , incsearch_state_T *isp
998#endif
999 )
1000{
1001 int i;
1002 int j;
1003
1004 if (c == K_KDEL)
1005 c = K_DEL;
1006
1007 /*
1008 * Delete current character is the same as backspace on next
1009 * character, except at end of line.
1010 */
1011 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1012 ++ccline.cmdpos;
1013 if (has_mbyte && c == K_DEL)
1014 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1015 ccline.cmdbuff + ccline.cmdpos);
1016 if (ccline.cmdpos > 0)
1017 {
1018 char_u *p;
1019
1020 j = ccline.cmdpos;
1021 p = ccline.cmdbuff + j;
1022 if (has_mbyte)
1023 {
1024 p = mb_prevptr(ccline.cmdbuff, p);
1025 if (c == Ctrl_W)
1026 {
1027 while (p > ccline.cmdbuff && vim_isspace(*p))
1028 p = mb_prevptr(ccline.cmdbuff, p);
1029 i = mb_get_class(p);
1030 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1031 p = mb_prevptr(ccline.cmdbuff, p);
1032 if (mb_get_class(p) != i)
1033 p += (*mb_ptr2len)(p);
1034 }
1035 }
1036 else if (c == Ctrl_W)
1037 {
1038 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1039 --p;
1040 i = vim_iswordc(p[-1]);
1041 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1042 && vim_iswordc(p[-1]) == i)
1043 --p;
1044 }
1045 else
1046 --p;
1047 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1048 ccline.cmdlen -= j - ccline.cmdpos;
1049 i = ccline.cmdpos;
1050 while (i < ccline.cmdlen)
1051 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1052
1053 // Truncate at the end, required for multi-byte chars.
1054 ccline.cmdbuff[ccline.cmdlen] = NUL;
1055#ifdef FEAT_SEARCH_EXTRA
1056 if (ccline.cmdlen == 0)
1057 {
1058 isp->search_start = isp->save_cursor;
1059 // save view settings, so that the screen
1060 // won't be restored at the wrong position
1061 isp->old_viewstate = isp->init_viewstate;
1062 }
1063#endif
1064 redrawcmd();
1065 }
1066 else if (ccline.cmdlen == 0 && c != Ctrl_W
1067 && ccline.cmdprompt == NULL && indent == 0)
1068 {
1069 // In ex and debug mode it doesn't make sense to return.
1070 if (exmode_active
1071#ifdef FEAT_EVAL
1072 || ccline.cmdfirstc == '>'
1073#endif
1074 )
1075 return CMDLINE_NOT_CHANGED;
1076
1077 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
1078 if (!cmd_silent)
1079 {
1080#ifdef FEAT_RIGHTLEFT
1081 if (cmdmsg_rl)
1082 msg_col = Columns;
1083 else
1084#endif
1085 msg_col = 0;
1086 msg_putchar(' '); // delete ':'
1087 }
1088#ifdef FEAT_SEARCH_EXTRA
1089 if (ccline.cmdlen == 0)
1090 isp->search_start = isp->save_cursor;
1091#endif
1092 redraw_cmdline = TRUE;
1093 return GOTO_NORMAL_MODE;
1094 }
1095 return CMDLINE_CHANGED;
1096}
1097
1098/*
1099 * Handle the CTRL-^ key in the command-line mode and toggle the use of the
1100 * language :lmap mappings and/or Input Method.
1101 */
1102 static void
1103cmdline_toggle_langmap(long *b_im_ptr)
1104{
1105 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
1106 {
1107 // ":lmap" mappings exists, toggle use of mappings.
1108 State ^= LANGMAP;
1109#ifdef HAVE_INPUT_METHOD
1110 im_set_active(FALSE); // Disable input method
1111#endif
1112 if (b_im_ptr != NULL)
1113 {
1114 if (State & LANGMAP)
1115 *b_im_ptr = B_IMODE_LMAP;
1116 else
1117 *b_im_ptr = B_IMODE_NONE;
1118 }
1119 }
1120#ifdef HAVE_INPUT_METHOD
1121 else
1122 {
1123 // There are no ":lmap" mappings, toggle IM. When
1124 // 'imdisable' is set don't try getting the status, it's
1125 // always off.
1126 if ((p_imdisable && b_im_ptr != NULL)
1127 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1128 {
1129 im_set_active(FALSE); // Disable input method
1130 if (b_im_ptr != NULL)
1131 *b_im_ptr = B_IMODE_NONE;
1132 }
1133 else
1134 {
1135 im_set_active(TRUE); // Enable input method
1136 if (b_im_ptr != NULL)
1137 *b_im_ptr = B_IMODE_IM;
1138 }
1139 }
1140#endif
1141 if (b_im_ptr != NULL)
1142 {
1143 if (b_im_ptr == &curbuf->b_p_iminsert)
1144 set_iminsert_global();
1145 else
1146 set_imsearch_global();
1147 }
1148#ifdef CURSOR_SHAPE
1149 ui_cursor_shape(); // may show different cursor shape
1150#endif
1151#if defined(FEAT_KEYMAP)
1152 // Show/unshow value of 'keymap' in status lines later.
1153 status_redraw_curbuf();
1154#endif
1155}
1156
1157/*
1158 * Handle the CTRL-R key in the command-line mode and insert the contents of a
1159 * numbered or named register.
1160 */
1161 static int
1162cmdline_insert_reg(int *gotesc UNUSED)
1163{
1164 int i;
1165 int c;
1166
1167#ifdef USE_ON_FLY_SCROLL
1168 dont_scroll = TRUE; // disallow scrolling here
1169#endif
1170 putcmdline('"', TRUE);
1171 ++no_mapping;
1172 ++allow_keys;
1173 i = c = plain_vgetc(); // CTRL-R <char>
1174 if (i == Ctrl_O)
1175 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
1176 if (i == Ctrl_R)
1177 c = plain_vgetc(); // CTRL-R CTRL-R <char>
1178 extra_char = NUL;
1179 --no_mapping;
1180 --allow_keys;
1181#ifdef FEAT_EVAL
1182 /*
1183 * Insert the result of an expression.
1184 * Need to save the current command line, to be able to enter
1185 * a new one...
1186 */
1187 new_cmdpos = -1;
1188 if (c == '=')
1189 {
1190 if (ccline.cmdfirstc == '=' // can't do this recursively
1191 || cmdline_star > 0) // or when typing a password
1192 {
1193 beep_flush();
1194 c = ESC;
1195 }
1196 else
1197 c = get_expr_register();
1198 }
1199#endif
1200 if (c != ESC) // use ESC to cancel inserting register
1201 {
1202 cmdline_paste(c, i == Ctrl_R, FALSE);
1203
1204#ifdef FEAT_EVAL
1205 // When there was a serious error abort getting the
1206 // command line.
1207 if (aborting())
1208 {
1209 *gotesc = TRUE; // will free ccline.cmdbuff after
1210 // putting it in history
1211 return GOTO_NORMAL_MODE;
1212 }
1213#endif
1214 KeyTyped = FALSE; // Don't do p_wc completion.
1215#ifdef FEAT_EVAL
1216 if (new_cmdpos >= 0)
1217 {
1218 // set_cmdline_pos() was used
1219 if (new_cmdpos > ccline.cmdlen)
1220 ccline.cmdpos = ccline.cmdlen;
1221 else
1222 ccline.cmdpos = new_cmdpos;
1223 }
1224#endif
1225 }
1226 redrawcmd();
1227 return CMDLINE_CHANGED;
1228}
1229
1230/*
1231 * Handle the Left and Right mouse clicks in the command-line mode.
1232 */
1233 static void
1234cmdline_left_right_mouse(int c, int *ignore_drag_release)
1235{
1236 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1237 *ignore_drag_release = TRUE;
1238 else
1239 *ignore_drag_release = FALSE;
1240# ifdef FEAT_GUI
1241 // When GUI is active, also move when 'mouse' is empty
1242 if (!gui.in_use)
1243# endif
1244 if (!mouse_has(MOUSE_COMMAND))
1245 return;
1246# ifdef FEAT_CLIPBOARD
1247 if (mouse_row < cmdline_row && clip_star.available)
1248 {
1249 int button, is_click, is_drag;
1250
1251 /*
1252 * Handle modeless selection.
1253 */
1254 button = get_mouse_button(KEY2TERMCAP1(c),
1255 &is_click, &is_drag);
1256 if (mouse_model_popup() && button == MOUSE_LEFT
1257 && (mod_mask & MOD_MASK_SHIFT))
1258 {
1259 // Translate shift-left to right button.
1260 button = MOUSE_RIGHT;
1261 mod_mask &= ~MOD_MASK_SHIFT;
1262 }
1263 clip_modeless(button, is_click, is_drag);
1264 return;
1265 }
1266# endif
1267
1268 set_cmdspos();
1269 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1270 ++ccline.cmdpos)
1271 {
1272 int i;
1273
1274 i = cmdline_charsize(ccline.cmdpos);
1275 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1276 && mouse_col < ccline.cmdspos % Columns + i)
1277 break;
1278 if (has_mbyte)
1279 {
1280 // Count ">" for double-wide char that doesn't fit.
1281 correct_cmdspos(ccline.cmdpos, i);
1282 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
1283 + ccline.cmdpos) - 1;
1284 }
1285 ccline.cmdspos += i;
1286 }
1287}
1288
1289/*
1290 * Handle the Up, Down, Page Up, Page down, CTRL-N and CTRL-P key in the
1291 * command-line mode. The pressed key is in 'c'.
1292 * Returns:
1293 * CMDLINE_NOT_CHANGED - if the command line is not changed
1294 * CMDLINE_CHANGED - if the command line is changed
1295 * GOTO_NORMAL_MODE - go back to normal mode
1296 */
1297 static int
1298cmdline_browse_history(
1299 int c,
1300 int firstc,
1301 char_u **curcmdstr,
1302 int histype,
1303 int *hiscnt_p,
1304 expand_T *xp)
1305{
1306 int i;
1307 int j;
1308 char_u *lookfor = *curcmdstr;
1309 int hiscnt = *hiscnt_p;
1310 int res;
1311
1312 if (get_hislen() == 0 || firstc == NUL) // no history
1313 return CMDLINE_NOT_CHANGED;
1314
1315 i = hiscnt;
1316
1317 // save current command string so it can be restored later
1318 if (lookfor == NULL)
1319 {
1320 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1321 return CMDLINE_NOT_CHANGED;
1322 lookfor[ccline.cmdpos] = NUL;
1323 }
1324
1325 j = (int)STRLEN(lookfor);
1326 for (;;)
1327 {
1328 // one step backwards
1329 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
1330 || c == K_PAGEUP || c == K_KPAGEUP)
1331 {
1332 if (hiscnt == get_hislen()) // first time
1333 hiscnt = *get_hisidx(histype);
1334 else if (hiscnt == 0 && *get_hisidx(histype)
1335 != get_hislen() - 1)
1336 hiscnt = get_hislen() - 1;
1337 else if (hiscnt != *get_hisidx(histype) + 1)
1338 --hiscnt;
1339 else // at top of list
1340 {
1341 hiscnt = i;
1342 break;
1343 }
1344 }
1345 else // one step forwards
1346 {
1347 // on last entry, clear the line
1348 if (hiscnt == *get_hisidx(histype))
1349 {
1350 hiscnt = get_hislen();
1351 break;
1352 }
1353
1354 // not on a history line, nothing to do
1355 if (hiscnt == get_hislen())
1356 break;
1357 if (hiscnt == get_hislen() - 1) // wrap around
1358 hiscnt = 0;
1359 else
1360 ++hiscnt;
1361 }
1362 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
1363 == NULL)
1364 {
1365 hiscnt = i;
1366 break;
1367 }
1368 if ((c != K_UP && c != K_DOWN)
1369 || hiscnt == i
1370 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
1371 lookfor, (size_t)j) == 0)
1372 break;
1373 }
1374
1375 if (hiscnt != i) // jumped to other entry
1376 {
1377 char_u *p;
1378 int len;
1379 int old_firstc;
1380
1381 VIM_CLEAR(ccline.cmdbuff);
1382 xp->xp_context = EXPAND_NOTHING;
1383 if (hiscnt == get_hislen())
1384 p = lookfor; // back to the old one
1385 else
1386 p = get_histentry(histype)[hiscnt].hisstr;
1387
1388 if (histype == HIST_SEARCH
1389 && p != lookfor
1390 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1391 {
1392 // Correct for the separator character used when
1393 // adding the history entry vs the one used now.
1394 // First loop: count length.
1395 // Second loop: copy the characters.
1396 for (i = 0; i <= 1; ++i)
1397 {
1398 len = 0;
1399 for (j = 0; p[j] != NUL; ++j)
1400 {
1401 // Replace old sep with new sep, unless it is
1402 // escaped.
1403 if (p[j] == old_firstc
1404 && (j == 0 || p[j - 1] != '\\'))
1405 {
1406 if (i > 0)
1407 ccline.cmdbuff[len] = firstc;
1408 }
1409 else
1410 {
1411 // Escape new sep, unless it is already
1412 // escaped.
1413 if (p[j] == firstc
1414 && (j == 0 || p[j - 1] != '\\'))
1415 {
1416 if (i > 0)
1417 ccline.cmdbuff[len] = '\\';
1418 ++len;
1419 }
1420 if (i > 0)
1421 ccline.cmdbuff[len] = p[j];
1422 }
1423 ++len;
1424 }
1425 if (i == 0)
1426 {
1427 alloc_cmdbuff(len);
1428 if (ccline.cmdbuff == NULL)
1429 {
1430 res = GOTO_NORMAL_MODE;
1431 goto done;
1432 }
1433 }
1434 }
1435 ccline.cmdbuff[len] = NUL;
1436 }
1437 else
1438 {
1439 alloc_cmdbuff((int)STRLEN(p));
1440 if (ccline.cmdbuff == NULL)
1441 {
1442 res = GOTO_NORMAL_MODE;
1443 goto done;
1444 }
1445 STRCPY(ccline.cmdbuff, p);
1446 }
1447
1448 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1449 redrawcmd();
1450 res = CMDLINE_CHANGED;
1451 goto done;
1452 }
1453 beep_flush();
1454 res = CMDLINE_NOT_CHANGED;
1455
1456done:
1457 *curcmdstr = lookfor;
1458 *hiscnt_p = hiscnt;
1459 return res;
1460}
1461
1462/*
Bram Moolenaar9c929712020-09-07 22:05:28 +02001463 * Initialize the current command-line info.
1464 */
1465 static int
1466init_ccline(int firstc, int indent)
1467{
1468 ccline.overstrike = FALSE; // always start in insert mode
1469
1470 /*
1471 * set some variables for redrawcmd()
1472 */
1473 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
1474 ccline.cmdindent = (firstc > 0 ? indent : 0);
1475
1476 // alloc initial ccline.cmdbuff
1477 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
1478 if (ccline.cmdbuff == NULL)
1479 return FAIL;
1480 ccline.cmdlen = ccline.cmdpos = 0;
1481 ccline.cmdbuff[0] = NUL;
1482 sb_text_start_cmdline();
1483
1484 // autoindent for :insert and :append
1485 if (firstc <= 0)
1486 {
1487 vim_memset(ccline.cmdbuff, ' ', indent);
1488 ccline.cmdbuff[indent] = NUL;
1489 ccline.cmdpos = indent;
1490 ccline.cmdspos = indent;
1491 ccline.cmdlen = indent;
1492 }
1493
1494 return OK;
1495}
1496
1497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 * getcmdline() - accept a command line starting with firstc.
1499 *
1500 * firstc == ':' get ":" command line.
1501 * firstc == '/' or '?' get search pattern
1502 * firstc == '=' get expression
1503 * firstc == '@' get text for input() function
1504 * firstc == '>' get text for debug mode
1505 * firstc == NUL get text for :insert command
1506 * firstc == -1 like NUL, and break on CTRL-C
1507 *
1508 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
1509 * command line.
1510 *
1511 * Careful: getcmdline() can be called recursively!
1512 *
1513 * Return pointer to allocated string if there is a commandline, NULL
1514 * otherwise.
1515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001517getcmdline(
1518 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +02001519 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +02001520 int indent, // indent for inside conditionals
1521 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001522{
1523 return getcmdline_int(firstc, count, indent, TRUE);
1524}
1525
1526 static char_u *
1527getcmdline_int(
1528 int firstc,
1529 long count UNUSED, // only used for incremental search
1530 int indent, // indent for inside conditionals
Bram Moolenaar9c929712020-09-07 22:05:28 +02001531 int clear_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 int c;
1534 int i;
1535 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001536 int gotesc = FALSE; // TRUE when <ESC> just typed
1537 int do_abbr; // when TRUE check for abbr.
1538 char_u *lookfor = NULL; // string to match
1539 int hiscnt; // current history line in use
1540 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001542 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001544 int did_wild_list = FALSE; // did wild_list() recently
1545 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 int res;
1547 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 int save_State = State; // remember State when called
1549 int some_key_typed = FALSE; // one of the keys was typed
1550 // mouse drag and release events are ignored, unless they are
1551 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#ifdef FEAT_EVAL
1554 int break_ctrl_c = FALSE;
1555#endif
1556 expand_T xpc;
1557 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001558 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02001559 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001560 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaar438d1762018-09-30 17:11:48 +02001562 if (ccline.cmdbuff != NULL)
1563 {
1564 // Being called recursively. Since ccline is global, we need to save
1565 // the current buffer and restore it when returning.
1566 save_cmdline(&save_ccline);
1567 did_save_ccline = TRUE;
1568 }
Bram Moolenaar9c929712020-09-07 22:05:28 +02001569 if (clear_ccline)
Bram Moolenaara80faa82020-04-12 19:37:17 +02001570 CLEAR_FIELD(ccline);
Bram Moolenaar438d1762018-09-30 17:11:48 +02001571
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572#ifdef FEAT_EVAL
1573 if (firstc == -1)
1574 {
1575 firstc = NUL;
1576 break_ctrl_c = TRUE;
1577 }
1578#endif
1579#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001580 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (firstc == ':' || firstc == '=' || firstc == '>')
1582 cmd_hkmap = 0;
1583#endif
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001586 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
1588
Bram Moolenaar9c929712020-09-07 22:05:28 +02001589 if (init_ccline(firstc, indent) != OK)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001590 goto theend; // out of memory
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001593 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595#ifdef FEAT_RIGHTLEFT
1596 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
1597 && (firstc == '/' || firstc == '?'))
1598 cmdmsg_rl = TRUE;
1599 else
1600 cmdmsg_rl = FALSE;
1601#endif
1602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001603 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (!cmd_silent)
1605 {
1606 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 gotocmdline(TRUE);
1609 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001610 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 set_cmdspos();
1612 }
1613 xpc.xp_context = EXPAND_NOTHING;
1614 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001615#ifndef BACKSLASH_IN_FILENAME
1616 xpc.xp_shell = FALSE;
1617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001619#if defined(FEAT_EVAL)
1620 if (ccline.input_fn)
1621 {
1622 xpc.xp_context = ccline.xp_context;
1623 xpc.xp_pattern = ccline.cmdbuff;
1624 xpc.xp_arg = ccline.xp_arg;
1625 }
1626#endif
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /*
1629 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
1630 * doing ":@0" when register 0 doesn't contain a CR.
1631 */
1632 msg_scroll = FALSE;
1633
1634 State = CMDLINE;
1635
1636 if (firstc == '/' || firstc == '?' || firstc == '@')
1637 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001638 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
1640 b_im_ptr = &curbuf->b_p_iminsert;
1641 else
1642 b_im_ptr = &curbuf->b_p_imsearch;
1643 if (*b_im_ptr == B_IMODE_LMAP)
1644 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001645#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 im_set_active(*b_im_ptr == B_IMODE_IM);
1647#endif
1648 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 else if (p_imcmdline)
1651 im_set_active(TRUE);
1652#endif
1653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001656 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
1658
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001659 // When inside an autocommand for writing "exiting" may be set and
1660 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001661 settmode(TMODE_RAW);
1662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001663 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001664 cmdline_type = firstc == NUL ? '-' : firstc;
1665 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001668 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001672 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#endif
1674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001675 // If something above caused an error, reset the flags, we do want to type
1676 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001677 if (did_emsg)
1678 redrawcmd();
1679 did_emsg = FALSE;
1680 got_int = FALSE;
1681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 /*
1683 * Collect the command string, handling editing keys.
1684 */
1685 for (;;)
1686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001687 redir_off = TRUE; // Don't redirect the typed command.
1688 // Repeated, because a ":redir" inside
1689 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001693 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695 did_emsg = FALSE; // There can't really be a reason why an error
1696 // that occurs while typing a command should
1697 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +02001698
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001699 // Trigger SafeState if nothing is pending.
1700 may_trigger_safestate(xpc.xp_numfiles <= 0);
1701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001702 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +00001703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001704 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
1705 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001706 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001707 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001708 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (KeyTyped)
1711 {
1712 some_key_typed = TRUE;
1713#ifdef FEAT_RIGHTLEFT
1714 if (cmd_hkmap)
1715 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (cmdmsg_rl && !KeyStuffed)
1717 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001718 // Invert horizontal movements and operations. Only when
1719 // typed by the user directly, not when the result of a
1720 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 switch (c)
1722 {
1723 case K_RIGHT: c = K_LEFT; break;
1724 case K_S_RIGHT: c = K_S_LEFT; break;
1725 case K_C_RIGHT: c = K_C_LEFT; break;
1726 case K_LEFT: c = K_RIGHT; break;
1727 case K_S_LEFT: c = K_S_RIGHT; break;
1728 case K_C_LEFT: c = K_C_RIGHT; break;
1729 }
1730 }
1731#endif
1732 }
1733
1734 /*
1735 * Ignore got_int when CTRL-C was typed here.
1736 * Don't ignore it in :global, we really need to break then, e.g., for
1737 * ":g/pat/normal /pat" (without the <CR>).
1738 * Don't ignore it for the input() function.
1739 */
1740 if ((c == Ctrl_C
1741#ifdef UNIX
1742 || c == intr_char
1743#endif
1744 )
1745#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1746 && firstc != '@'
1747#endif
1748#ifdef FEAT_EVAL
1749 && !break_ctrl_c
1750#endif
1751 && !global_busy)
1752 got_int = FALSE;
1753
Bram Moolenaard7663c22019-08-06 21:59:57 +02001754 // free old command line when finished moving around in the history
1755 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001757 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001758 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 && c != K_PAGEDOWN && c != K_PAGEUP
1760 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001761 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001763 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001766 * When there are matching completions to select <S-Tab> works like
1767 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001769 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 c = Ctrl_P;
1771
1772#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001773 c = wildmenu_translate_key(&ccline, c, &xpc, did_wild_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
1775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001776 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if (xpc.xp_numfiles != -1
1778 && !(c == p_wc && KeyTyped) && c != p_wcm
1779 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1780 && c != Ctrl_L)
1781 {
1782 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1783 did_wild_list = FALSE;
1784#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001785 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
1787 xpc.xp_context = EXPAND_NOTHING;
1788 wim_index = 0;
1789#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001790 wildmenu_cleanup(&ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
1792 }
1793
1794#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001795 c = wildmenu_process_key(&ccline, c, &xpc);
1796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001798 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1799 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (c == Ctrl_BSL)
1801 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001802 res = cmdline_handle_backslash_key(c, &gotesc);
1803 if (res == CMDLINE_CHANGED)
1804 goto cmdline_changed;
1805 else if (res == CMDLINE_NOT_CHANGED)
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001806 goto cmdline_not_changed;
Bram Moolenaar9c929712020-09-07 22:05:28 +02001807 else if (res == GOTO_NORMAL_MODE)
1808 goto returncmd; // back to cmd mode
1809 c = Ctrl_BSL; // backslash key not processed by
1810 // cmdline_handle_backslash_key()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812
1813#ifdef FEAT_CMDWIN
1814 if (c == cedit_key || c == K_CMDWIN)
1815 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001816 // TODO: why is ex_normal_busy checked here?
1817 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001818 {
1819 /*
1820 * Open a window to edit the command line (and history).
1821 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001822 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001823 some_key_typed = TRUE;
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826# ifdef FEAT_DIGRAPHS
1827 else
1828# endif
1829#endif
1830#ifdef FEAT_DIGRAPHS
1831 c = do_digraph(c);
1832#endif
1833
1834 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1835 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001837 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001838 if (exmode_active
1839 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001840 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001841 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001842 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001844 if (c == K_KENTER)
1845 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001847 else
1848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001849 gotesc = FALSE; // Might have typed ESC previously, don't
1850 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001851 if (ccheck_abbr(c + ABBR_OFF))
1852 goto cmdline_changed;
1853 if (!cmd_silent)
1854 {
1855 windgoto(msg_row, 0);
1856 out_flush();
1857 }
1858 break;
1859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861
Bram Moolenaar9c929712020-09-07 22:05:28 +02001862 // Completion for 'wildchar' or 'wildcharm' key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1864 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001865 res = cmdline_wildchar_complete(c, firstc != '@', &did_wild_list,
1866 &wim_index, &xpc, &gotesc);
1867 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 goto cmdline_changed;
1869 }
1870
1871 gotesc = FALSE;
1872
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001873 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (c == K_S_TAB && KeyTyped)
1875 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001876 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1877 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1878 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 goto cmdline_changed;
1880 }
1881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001882 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 c = NL;
1884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887 /*
1888 * Big switch for a typed command line character.
1889 */
1890 switch (c)
1891 {
1892 case K_BS:
1893 case Ctrl_H:
1894 case K_DEL:
1895 case K_KDEL:
1896 case Ctrl_W:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001897 res = cmdline_erase_chars(c, indent
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001898#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001899 , &is_state
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001900#endif
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001901 );
1902 if (res == CMDLINE_NOT_CHANGED)
1903 goto cmdline_not_changed;
1904 else if (res == GOTO_NORMAL_MODE)
1905 goto returncmd; // back to cmd mode
1906 goto cmdline_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
1908 case K_INS:
1909 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 ccline.overstrike = !ccline.overstrike;
1911#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#endif
1914 goto cmdline_not_changed;
1915
1916 case Ctrl_HAT:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001917 cmdline_toggle_langmap(b_im_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 goto cmdline_not_changed;
1919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001920// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001922 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 j = ccline.cmdpos;
1924 ccline.cmdlen -= j;
1925 i = ccline.cmdpos = 0;
1926 while (i < ccline.cmdlen)
1927 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001930#ifdef FEAT_SEARCH_EXTRA
1931 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001932 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 redrawcmd();
1935 goto cmdline_changed;
1936
1937#ifdef FEAT_CLIPBOARD
1938 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001939 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (clip_star.state != SELECT_CLEARED)
1941 {
1942 if (clip_star.state == SELECT_DONE)
1943 clip_copy_modeless_selection(TRUE);
1944 goto cmdline_not_changed;
1945 }
1946 break;
1947#endif
1948
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001949 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001951 // In exmode it doesn't make sense to return. Except when
1952 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001953 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001954 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 goto cmdline_not_changed;
1956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001957 gotesc = TRUE; // will free ccline.cmdbuff after
1958 // putting it in history
1959 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001961 case Ctrl_R: // insert register
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001962 res = cmdline_insert_reg(&gotesc);
1963 if (res == CMDLINE_NOT_CHANGED)
1964 goto cmdline_not_changed;
1965 else if (res == GOTO_NORMAL_MODE)
1966 goto returncmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 goto cmdline_changed;
1968
1969 case Ctrl_D:
1970 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001974 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 case K_RIGHT:
1977 case K_S_RIGHT:
1978 case K_C_RIGHT:
1979 do
1980 {
1981 if (ccline.cmdpos >= ccline.cmdlen)
1982 break;
1983 i = cmdline_charsize(ccline.cmdpos);
1984 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1985 break;
1986 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001988 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 + ccline.cmdpos);
1990 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 ++ccline.cmdpos;
1992 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001993 while ((c == K_S_RIGHT || c == K_C_RIGHT
1994 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (has_mbyte)
1997 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 goto cmdline_not_changed;
1999
2000 case K_LEFT:
2001 case K_S_LEFT:
2002 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002003 if (ccline.cmdpos == 0)
2004 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 do
2006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002008 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
2010 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
2012 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002013 while (ccline.cmdpos > 0
2014 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002015 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (has_mbyte)
2018 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 goto cmdline_not_changed;
2020
2021 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002022 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00002023 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar4f974752019-02-17 17:44:42 +01002025#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // On MS-Windows ignore <M-F4>, we get it when closing the window
2027 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002028 case K_F4:
2029 if (mod_mask == MOD_MASK_ALT)
2030 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002031 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00002032 goto cmdline_not_changed;
2033 }
2034 break;
2035#endif
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 case K_MIDDLEDRAG:
2038 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041 case K_MIDDLEMOUSE:
2042# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002043 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (!gui.in_use)
2045# endif
2046 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002047 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002048# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002050 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002052# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002053 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 redrawcmd();
2055 goto cmdline_changed;
2056
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002057# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002059 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 redrawcmd();
2061 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002062# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064 case K_LEFTDRAG:
2065 case K_LEFTRELEASE:
2066 case K_RIGHTDRAG:
2067 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002068 // Ignore drag and release events when the button-down wasn't
2069 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 if (ignore_drag_release)
2071 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 case K_LEFTMOUSE:
2074 case K_RIGHTMOUSE:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002075 cmdline_left_right_mouse(c, &ignore_drag_release);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 goto cmdline_not_changed;
2077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002078 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 case K_MOUSEDOWN:
2080 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002081 case K_MOUSELEFT:
2082 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002083 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 case K_X1MOUSE:
2085 case K_X1DRAG:
2086 case K_X1RELEASE:
2087 case K_X2MOUSE:
2088 case K_X2DRAG:
2089 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002090 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 goto cmdline_not_changed;
2092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002094 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 case K_LEFTRELEASE_NM:
2096 goto cmdline_not_changed;
2097
2098 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002099 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101 gui_do_scroll();
2102 redrawcmd();
2103 }
2104 goto cmdline_not_changed;
2105
2106 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002107 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002109 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 redrawcmd();
2111 }
2112 goto cmdline_not_changed;
2113#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002114#ifdef FEAT_GUI_TABLINE
2115 case K_TABLINE:
2116 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002117 // Don't want to change any tabs here. Make sure the same tab
2118 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002119 if (gui_use_tabline())
2120 gui_mch_set_curtab(tabpage_index(curtab));
2121 goto cmdline_not_changed;
2122#endif
2123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 goto cmdline_not_changed;
2126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 case K_HOME:
2129 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 case K_S_HOME:
2131 case K_C_HOME:
2132 ccline.cmdpos = 0;
2133 set_cmdspos();
2134 goto cmdline_not_changed;
2135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002136 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 case K_END:
2138 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 case K_S_END:
2140 case K_C_END:
2141 ccline.cmdpos = ccline.cmdlen;
2142 set_cmdspos_cursor();
2143 goto cmdline_not_changed;
2144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002145 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01002146 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 break;
2148 goto cmdline_changed;
2149
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002150 case Ctrl_L:
2151#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002152 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002153 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002154#endif
2155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002157 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 break;
2159 goto cmdline_changed;
2160
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 case Ctrl_N: // next match
2162 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002163 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002165 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2166 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002168 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 case K_UP:
2172 case K_DOWN:
2173 case K_S_UP:
2174 case K_S_DOWN:
2175 case K_PAGEUP:
2176 case K_KPAGEUP:
2177 case K_PAGEDOWN:
2178 case K_KPAGEDOWN:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002179 res = cmdline_browse_history(c, firstc, &lookfor, histype,
2180 &hiscnt, &xpc);
2181 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 goto cmdline_changed;
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002183 else if (res == GOTO_NORMAL_MODE)
2184 goto returncmd;
Bram Moolenaar11956692016-08-27 16:26:56 +02002185 goto cmdline_not_changed;
2186
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002187#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002188 case Ctrl_G: // next match
2189 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002190 if (may_adjust_incsearch_highlighting(
2191 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002192 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002193 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#endif
2195
2196 case Ctrl_V:
2197 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002199 int prev_mod_mask = mod_mask;
2200
2201 ignore_drag_release = TRUE;
2202 putcmdline('^', TRUE);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002203 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002204 c = get_literal(); // get next (two) character(s)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002205 no_reduce_keys = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002206 do_abbr = FALSE; // don't do abbreviation now
2207 extra_char = NUL;
2208 // may need to remove ^ when composing char was typed
2209 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2210 {
2211 draw_cmdline(ccline.cmdpos,
2212 ccline.cmdlen - ccline.cmdpos);
2213 msg_putchar(' ');
2214 cursorcmd();
2215 }
2216
2217 if ((c == ESC || c == CSI)
2218 && !(prev_mod_mask & MOD_MASK_SHIFT))
2219 // Using CTRL-V: Change any modifyOtherKeys ESC
2220 // sequence to a normal key. Don't do this for
2221 // CTRL-SHIFT-V.
2222 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 break;
2226
2227#ifdef FEAT_DIGRAPHS
2228 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002231# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002232 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002235 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (c != NUL)
2237 break;
2238
2239 redrawcmd();
2240 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002241#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (!p_ari)
2246 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002247 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 goto cmdline_not_changed;
2249#endif
2250
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002251 case K_PS:
2252 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2253 goto cmdline_changed;
2254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 default:
2256#ifdef UNIX
2257 if (c == intr_char)
2258 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 gotesc = TRUE; // will free ccline.cmdbuff after
2260 // putting it in history
2261 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263#endif
2264 /*
2265 * Normal character with no special meaning. Just set mod_mask
2266 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2267 * the string <S-Space>. This should only happen after ^V.
2268 */
2269 if (!IS_SPECIAL(c))
2270 mod_mask = 0x0;
2271 break;
2272 }
2273 /*
2274 * End of switch on command line character.
2275 * We come here if we have a normal character.
2276 */
2277
Bram Moolenaar13505972019-01-24 15:04:48 +01002278 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2279 && (ccheck_abbr(
2280 // Add ABBR_OFF for characters above 0x100, this is
2281 // what check_abbr() expects.
2282 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2283 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 goto cmdline_changed;
2285
2286 /*
2287 * put the character in the command line
2288 */
2289 if (IS_SPECIAL(c) || mod_mask != 0)
2290 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2291 else
2292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (has_mbyte)
2294 {
2295 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002296 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 put_on_cmdline(IObuff, j, TRUE);
2298 }
2299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 IObuff[0] = c;
2302 put_on_cmdline(IObuff, 1, TRUE);
2303 }
2304 }
2305 goto cmdline_changed;
2306
2307/*
2308 * This part implements incremental searches for "/" and "?"
2309 * Jump to cmdline_not_changed when a character has been read but the command
2310 * line did not change. Then we only search and redraw if something changed in
2311 * the past.
2312 * Jump to cmdline_changed when the command line did change.
2313 * (Sorry for the goto's, I know it is ugly).
2314 */
2315cmdline_not_changed:
2316#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002317 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 continue;
2319#endif
2320
2321cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002322 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002323 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaara60053b2020-09-03 16:50:13 +02002326 if (xpc.xp_context == EXPAND_NOTHING)
2327 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329
2330#ifdef FEAT_RIGHTLEFT
2331 if (cmdmsg_rl
2332# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002333 || (p_arshape && !p_tbidi
2334 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335# endif
2336 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002337 // Always redraw the whole command line to fix shaping and
2338 // right-left typing. Not efficient, but it works.
2339 // Do it only when there are no characters left to read
2340 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002341 if (vpeekc() == NUL)
2342 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343#endif
2344 }
2345
2346returncmd:
2347
2348#ifdef FEAT_RIGHTLEFT
2349 cmdmsg_rl = FALSE;
2350#endif
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002353 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002356 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#endif
2358
2359 if (ccline.cmdbuff != NULL)
2360 {
2361 /*
2362 * Put line in history buffer (":" and "=" only when it was typed).
2363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (ccline.cmdlen && firstc != NUL
2365 && (some_key_typed || histype == HIST_SEARCH))
2366 {
2367 add_to_history(histype, ccline.cmdbuff, TRUE,
2368 histype == HIST_SEARCH ? firstc : NUL);
2369 if (firstc == ':')
2370 {
2371 vim_free(new_last_cmdline);
2372 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2373 }
2374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002376 if (gotesc)
2377 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379
2380 /*
2381 * If the screen was shifted up, redraw the whole screen (later).
2382 * If the line is too long, clear it, so ruler and shown command do
2383 * not get printed in the middle of it.
2384 */
2385 msg_check();
2386 msg_scroll = save_msg_scroll;
2387 redir_off = FALSE;
2388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002389 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (some_key_typed)
2391 need_wait_return = FALSE;
2392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002393 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002394 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002397#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2399 im_save_status(b_im_ptr);
2400 im_set_active(FALSE);
2401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002404 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002406 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
Bram Moolenaar438d1762018-09-30 17:11:48 +02002408theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002409 {
2410 char_u *p = ccline.cmdbuff;
2411
Bram Moolenaar438d1762018-09-30 17:11:48 +02002412 if (did_save_ccline)
2413 restore_cmdline(&save_ccline);
2414 else
2415 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002416 return p;
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418}
2419
2420#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2421/*
2422 * Get a command line with a prompt.
2423 * This is prepared to be called recursively from getcmdline() (e.g. by
2424 * f_input() when evaluating an expression from CTRL-R =).
2425 * Returns the command line in allocated memory, or NULL.
2426 */
2427 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002428getcmdline_prompt(
2429 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002430 char_u *prompt, // command line prompt
2431 int attr, // attributes for prompt
2432 int xp_context, // type of expansion
2433 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434{
2435 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002436 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002437 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002439 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
Bram Moolenaar438d1762018-09-30 17:11:48 +02002441 if (ccline.cmdbuff != NULL)
2442 {
2443 // Save the values of the current cmdline and restore them below.
2444 save_cmdline(&save_ccline);
2445 did_save_ccline = TRUE;
2446 }
2447
Bram Moolenaara80faa82020-04-12 19:37:17 +02002448 CLEAR_FIELD(ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 ccline.cmdprompt = prompt;
2450 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002451# ifdef FEAT_EVAL
2452 ccline.xp_context = xp_context;
2453 ccline.xp_arg = xp_arg;
2454 ccline.input_fn = (firstc == '@');
2455# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002456 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002457 s = getcmdline_int(firstc, 1L, 0, FALSE);
2458
2459 if (did_save_ccline)
2460 restore_cmdline(&save_ccline);
2461
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002462 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002463 // Restore msg_col, the prompt from input() may have changed it.
2464 // But only if called recursively and the commandline is therefore being
2465 // restored to an old one; if not, the input() prompt stays on the screen,
2466 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002467 if (ccline.cmdbuff != NULL)
2468 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 return s;
2471}
2472#endif
2473
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002474/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002475 * Read the 'wildmode' option, fill wim_flags[].
2476 */
2477 int
2478check_opt_wim(void)
2479{
2480 char_u new_wim_flags[4];
2481 char_u *p;
2482 int i;
2483 int idx = 0;
2484
2485 for (i = 0; i < 4; ++i)
2486 new_wim_flags[i] = 0;
2487
2488 for (p = p_wim; *p; ++p)
2489 {
2490 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2491 ;
2492 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2493 return FAIL;
2494 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2495 new_wim_flags[idx] |= WIM_LONGEST;
2496 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2497 new_wim_flags[idx] |= WIM_FULL;
2498 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2499 new_wim_flags[idx] |= WIM_LIST;
2500 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2501 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2502 else
2503 return FAIL;
2504 p += i;
2505 if (*p == NUL)
2506 break;
2507 if (*p == ',')
2508 {
2509 if (idx == 3)
2510 return FAIL;
2511 ++idx;
2512 }
2513 }
2514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002515 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002516 while (idx < 3)
2517 {
2518 new_wim_flags[idx + 1] = new_wim_flags[idx];
2519 ++idx;
2520 }
2521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002522 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002523 for (i = 0; i < 4; ++i)
2524 wim_flags[i] = new_wim_flags[i];
2525 return OK;
2526}
2527
2528/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002529 * Return TRUE when the text must not be changed and we can't switch to
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002530 * another window or buffer. TRUE when editing the command line, evaluating
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002531 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002532 */
2533 int
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002534text_and_win_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002535{
2536#ifdef FEAT_CMDWIN
2537 if (cmdwin_type != 0)
2538 return TRUE;
2539#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002540 return textwinlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002541}
2542
2543/*
2544 * Give an error message for a command that isn't allowed while the cmdline
2545 * window is open or editing the cmdline in another way.
2546 */
2547 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002548text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002550 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002551}
2552
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002553 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002554get_text_locked_msg(void)
2555{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002556#ifdef FEAT_CMDWIN
2557 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002558 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002559#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002560 if (textwinlock != 0)
2561 return e_textwinlock;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002562 return e_textlock;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002563}
2564
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002565/*
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002566 * Return TRUE when the text must not be changed and/or we cannot switch to
2567 * another window. TRUE while evaluating 'completefunc'.
2568 */
2569 int
2570text_locked(void)
2571{
2572 return text_and_win_locked() || textlock != 0;
2573}
2574
2575/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002576 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2577 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002578 */
2579 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002580curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002581{
2582 if (curbuf_lock > 0)
2583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002584 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585 return TRUE;
2586 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002587 return allbuf_locked();
2588}
2589
2590/*
2591 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2592 * message.
2593 */
2594 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002595allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002596{
2597 if (allbuf_lock > 0)
2598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002599 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002600 return TRUE;
2601 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002602 return FALSE;
2603}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002606cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002609 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 return 1;
2611#endif
2612 return ptr2cells(ccline.cmdbuff + idx);
2613}
2614
2615/*
2616 * Compute the offset of the cursor on the command line for the prompt and
2617 * indent.
2618 */
2619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002620set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002622 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ccline.cmdspos = 1 + ccline.cmdindent;
2624 else
2625 ccline.cmdspos = 0 + ccline.cmdindent;
2626}
2627
2628/*
2629 * Compute the screen position for the cursor on the command line.
2630 */
2631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002632set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 int i, m, c;
2635
2636 set_cmdspos();
2637 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002640 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002641 m = MAXCOL;
2642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 else
2644 m = MAXCOL;
2645 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2646 {
2647 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002648 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (has_mbyte)
2650 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002651 // If the cmdline doesn't fit, show cursor on last visible char.
2652 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if ((ccline.cmdspos += c) >= m)
2654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ccline.cmdspos -= c;
2656 break;
2657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002659 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661}
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
2664 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2665 * character that doesn't fit, so that a ">" must be displayed.
2666 */
2667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002668correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002670 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2672 && ccline.cmdspos % Columns + cells > Columns)
2673 ccline.cmdspos++;
2674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676/*
2677 * Get an Ex command line for the ":" command.
2678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002680getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002681 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002682 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002683 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002684 getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002686 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (exec_from_reg && vpeekc() == ':')
2688 (void)vgetc();
Bram Moolenaar66250c92020-08-20 15:02:42 +02002689 return getcmdline(c, 1L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690}
2691
2692/*
2693 * Get an Ex command line for Ex mode.
2694 * In Ex mode we only use the OS supplied line editing features and no
2695 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002699getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002700 int promptc, // normally ':', NUL for ":append" and '?' for
2701 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002702 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002703 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002704 getline_opt_T options UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 garray_T line_ga;
2707 char_u *pend;
2708 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002709 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002710 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711 int vcol = 0;
2712 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002713 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002714 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002716 // Switch cursor on now. This avoids that it happens after the "\n", which
2717 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 cursor_on();
2719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002720 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002726 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 if (p_prompt)
2728 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 while (indent-- > 0)
2730 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733
2734 ga_init2(&line_ga, 1, 30);
2735
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002736 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002738 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002739 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002740 while (indent >= 8)
2741 {
2742 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002743 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002744 indent -= 8;
2745 }
2746 while (indent-- > 0)
2747 {
2748 ga_append(&line_ga, ' ');
2749 msg_putchar(' ');
2750 }
2751 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 ++no_mapping;
2753 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 /*
2756 * Get the line, one character at a time.
2757 */
2758 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002761 long sw;
2762 char_u *s;
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (ga_grow(&line_ga, 40) == FAIL)
2765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaarba748c82017-02-26 14:00:07 +01002767 /*
2768 * Get one character at a time.
2769 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002770 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002772 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002773 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2774 c1 = '\n';
2775 else
2776 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
2778 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 * Handle line editing.
2780 * Previously this was left to the system, putting the terminal in
2781 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 msg_putchar('\n');
2786 break;
2787 }
2788
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002789 if (c1 == K_PS)
2790 {
2791 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2792 goto redraw;
2793 }
2794
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 if (!escaped)
2796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002797 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 if (c1 == '\r')
2799 c1 = '\n';
2800
2801 if (c1 == BS || c1 == K_BS
2802 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 if (line_ga.ga_len > 0)
2805 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002806 if (has_mbyte)
2807 {
2808 p = (char_u *)line_ga.ga_data;
2809 p[line_ga.ga_len] = NUL;
2810 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2811 line_ga.ga_len -= len;
2812 }
2813 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002814 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815 goto redraw;
2816 }
2817 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 msg_col = startcol;
2823 msg_clr_eos();
2824 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002825 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 }
2827
2828 if (c1 == Ctrl_T)
2829 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002830 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 p = (char_u *)line_ga.ga_data;
2832 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002833 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002834 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002836 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002838 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002839 p = (char_u *)line_ga.ga_data;
2840 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2842 *s = ' ';
2843 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002846 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002849 p = (char_u *)line_ga.ga_data;
2850 p[line_ga.ga_len] = NUL;
2851 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002857 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002858 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002862 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002863 msg_outtrans_len(p, len);
2864 vcol += ptr2cells(p);
2865 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002869 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870 continue;
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 if (c1 == Ctrl_D)
2874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002875 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 p = (char_u *)line_ga.ga_data;
2877 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002879 if (prev_char == '^')
2880 ex_keep_indent = TRUE;
2881 indent = 0;
2882 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 else
2885 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002887 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002888 if (indent > 0)
2889 {
2890 --indent;
2891 indent -= indent % get_sw_value(curbuf);
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002894 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002896 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002897 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2898 --line_ga.ga_len;
2899 }
2900 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902
2903 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2904 {
2905 escaped = TRUE;
2906 continue;
2907 }
2908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002909 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 if (IS_SPECIAL(c1))
2911 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913
2914 if (IS_SPECIAL(c1))
2915 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002916 if (has_mbyte)
2917 len = (*mb_char2bytes)(c1,
2918 (char_u *)line_ga.ga_data + line_ga.ga_len);
2919 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002920 {
2921 len = 1;
2922 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2923 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002924 if (c1 == '\n')
2925 msg_putchar('\n');
2926 else if (c1 == TAB)
2927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002928 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002929 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002930 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002931 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002935 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002936 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002937 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002939 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002940 escaped = FALSE;
2941
2942 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002943 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002945 // We are done when a NL is entered, but not when it comes after an
2946 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002947 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002949 int bcount = 0;
2950
2951 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2952 ++bcount;
2953
2954 if (bcount > 0)
2955 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002956 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2957 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002958 line_ga.ga_len -= (bcount + 1) / 2;
2959 pend -= (bcount + 1) / 2;
2960 pend[-1] = '\n';
2961 }
2962
2963 if ((bcount & 1) == 0)
2964 {
2965 --line_ga.ga_len;
2966 --pend;
2967 *pend = NUL;
2968 break;
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971 }
2972
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002973 --no_mapping;
2974 --allow_keys;
2975
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002976 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 msg_didout = FALSE;
2978 msg_col = 0;
2979 if (msg_row < Rows - 1)
2980 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002981 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 if (got_int)
2984 ga_clear(&line_ga);
2985
2986 return (char_u *)line_ga.ga_data;
2987}
2988
Bram Moolenaare344bea2005-09-01 20:46:49 +00002989# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2990 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991/*
2992 * Return TRUE if ccline.overstrike is on.
2993 */
2994 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 return ccline.overstrike;
2998}
2999
3000/*
3001 * Return TRUE if the cursor is at the end of the cmdline.
3002 */
3003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003004cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 return (ccline.cmdpos >= ccline.cmdlen);
3007}
3008#endif
3009
Bram Moolenaar9372a112005-12-06 19:59:18 +00003010#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011/*
3012 * Return the virtual column number at the current cursor position.
3013 * This is used by the IM code to obtain the start of the preedit string.
3014 */
3015 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003016cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3019 return MAXCOL;
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (has_mbyte)
3022 {
3023 colnr_T col;
3024 int i = 0;
3025
3026 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003027 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 return col;
3030 }
3031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 return ccline.cmdpos;
3033}
3034#endif
3035
3036#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3037/*
3038 * If part of the command line is an IM preedit string, redraw it with
3039 * IM feedback attributes. The cursor position is restored after drawing.
3040 */
3041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 if ((State & CMDLINE)
3045 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003046 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 && !p_imdisable
3048 && im_is_preediting())
3049 {
3050 int cmdpos = 0;
3051 int cmdspos;
3052 int old_row;
3053 int old_col;
3054 colnr_T col;
3055
3056 old_row = msg_row;
3057 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003058 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (has_mbyte)
3061 {
3062 for (col = 0; col < preedit_start_col
3063 && cmdpos < ccline.cmdlen; ++col)
3064 {
3065 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003066 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 }
3068 }
3069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 cmdspos += preedit_start_col;
3072 cmdpos += preedit_start_col;
3073 }
3074
3075 msg_row = cmdline_row + (cmdspos / (int)Columns);
3076 msg_col = cmdspos % (int)Columns;
3077 if (msg_row >= Rows)
3078 msg_row = Rows - 1;
3079
3080 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3081 {
3082 int char_len;
3083 int char_attr;
3084
3085 char_attr = im_get_feedback_attr(col);
3086 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003087 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003090 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 char_len = 1;
3093
3094 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3095 cmdpos += char_len;
3096 }
3097
3098 msg_row = old_row;
3099 msg_col = old_col;
3100 }
3101}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003102#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104/*
3105 * Allocate a new command line buffer.
3106 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
3108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 /*
3112 * give some extra space to avoid having to allocate all the time
3113 */
3114 if (len < 80)
3115 len = 100;
3116 else
3117 len += 20;
3118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003119 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 ccline.cmdbufflen = len;
3121}
3122
3123/*
3124 * Re-allocate the command line to length len + something extra.
3125 * return FAIL for failure, OK otherwise
3126 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 char_u *p;
3131
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003132 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003133 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003136 alloc_cmdbuff(len); // will get some more
3137 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003139 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return FAIL;
3141 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003142 // There isn't always a NUL after the command, but it may need to be
3143 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003144 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3145 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003147
3148 if (ccline.xpc != NULL
3149 && ccline.xpc->xp_pattern != NULL
3150 && ccline.xpc->xp_context != EXPAND_NOTHING
3151 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3152 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003153 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003155 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3156 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003157 if (i >= 0 && i <= ccline.cmdlen)
3158 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3159 }
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return OK;
3162}
3163
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003164#if defined(FEAT_ARABIC) || defined(PROTO)
3165static char_u *arshape_buf = NULL;
3166
3167# if defined(EXITFREE) || defined(PROTO)
3168 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003169free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003170{
3171 vim_free(arshape_buf);
3172}
3173# endif
3174#endif
3175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176/*
3177 * Draw part of the cmdline at the current cursor position. But draw stars
3178 * when cmdline_star is TRUE.
3179 */
3180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003181draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3184 int i;
3185
3186 if (cmdline_star > 0)
3187 for (i = 0; i < len; ++i)
3188 {
3189 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003191 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 else
3194#endif
3195#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003196 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 static int buflen = 0;
3199 char_u *p;
3200 int j;
3201 int newlen = 0;
3202 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003203 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int prev_c = 0;
3205 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003206 int u8c;
3207 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
3210 /*
3211 * Do arabic shaping into a temporary buffer. This is very
3212 * inefficient!
3213 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003214 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003216 // Re-allocate the buffer. We keep it around to avoid a lot of
3217 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003218 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003219 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003220 arshape_buf = alloc(buflen);
3221 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003222 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003225 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003227 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003228 arshape_buf[0] = ' ';
3229 newlen = 1;
3230 }
3231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 for (j = start; j < start + len; j += mb_l)
3233 {
3234 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003235 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003236 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (ARABIC_CHAR(u8c))
3238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003239 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (cmdmsg_rl)
3241 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003242 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 pc = prev_c;
3244 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003245 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (j + mb_l >= start + len)
3247 nc = NUL;
3248 else
3249 nc = utf_ptr2char(p + mb_l);
3250 }
3251 else
3252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003253 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 if (j + mb_l >= start + len)
3255 pc = NUL;
3256 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003257 {
3258 int pcc[MAX_MCO];
3259
3260 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003262 pc1 = pcc[0];
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 nc = prev_c;
3265 }
3266 prev_c = u8c;
3267
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003268 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003271 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003273 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3274 if (u8cc[1] != 0)
3275 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 }
3279 else
3280 {
3281 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003282 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 newlen += mb_l;
3284 }
3285 }
3286
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 else
3290#endif
3291 msg_outtrans_len(ccline.cmdbuff + start, len);
3292}
3293
3294/*
3295 * Put a character on the command line. Shifts the following text to the
3296 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3297 * "c" must be printable (fit in one display cell)!
3298 */
3299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003300putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 if (cmd_silent)
3303 return;
3304 msg_no_more = TRUE;
3305 msg_putchar(c);
3306 if (shift)
3307 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3308 msg_no_more = FALSE;
3309 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003310 extra_char = c;
3311 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313
3314/*
3315 * Undo a putcmdline(c, FALSE).
3316 */
3317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003318unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 if (cmd_silent)
3321 return;
3322 msg_no_more = TRUE;
3323 if (ccline.cmdlen == ccline.cmdpos)
3324 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003325 else if (has_mbyte)
3326 draw_cmdline(ccline.cmdpos,
3327 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else
3329 draw_cmdline(ccline.cmdpos, 1);
3330 msg_no_more = FALSE;
3331 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003332 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333}
3334
3335/*
3336 * Put the given string, of the given length, onto the command line.
3337 * If len is -1, then STRLEN() is used to calculate the length.
3338 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3339 * part will be redrawn, otherwise it will not. If this function is called
3340 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3341 * called afterwards.
3342 */
3343 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003344put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int retval;
3347 int i;
3348 int m;
3349 int c;
3350
3351 if (len < 0)
3352 len = (int)STRLEN(str);
3353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003354 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003356 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
3358 retval = OK;
3359 if (retval == OK)
3360 {
3361 if (!ccline.overstrike)
3362 {
3363 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3364 ccline.cmdbuff + ccline.cmdpos,
3365 (size_t)(ccline.cmdlen - ccline.cmdpos));
3366 ccline.cmdlen += len;
3367 }
3368 else
3369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (has_mbyte)
3371 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003372 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003374 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003376 // Count nr of bytes in cmdline that are overwritten by these
3377 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003379 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 --m;
3381 if (i < ccline.cmdlen)
3382 {
3383 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3384 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3385 ccline.cmdlen += ccline.cmdpos + len - i;
3386 }
3387 else
3388 ccline.cmdlen = ccline.cmdpos + len;
3389 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003390 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 ccline.cmdlen = ccline.cmdpos + len;
3392 }
3393 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3394 ccline.cmdbuff[ccline.cmdlen] = NUL;
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (enc_utf8)
3397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003398 // When the inserted text starts with a composing character,
3399 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 i = 0;
3401 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3402 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3403 {
3404 i = (*mb_head_off)(ccline.cmdbuff,
3405 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3406 ccline.cmdpos -= i;
3407 len += i;
3408 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3409 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003410#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3412 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003413 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 i = (*mb_head_off)(ccline.cmdbuff,
3415 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3416 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3417 + ccline.cmdpos - i), c))
3418 {
3419 ccline.cmdpos -= i;
3420 len += i;
3421 }
3422 else
3423 i = 0;
3424 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (i != 0)
3427 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003428 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3430 ccline.cmdspos -= i;
3431 msg_col -= i;
3432 if (msg_col < 0)
3433 {
3434 msg_col += Columns;
3435 --msg_row;
3436 }
3437 }
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 if (redraw && !cmd_silent)
3441 {
3442 msg_no_more = TRUE;
3443 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003444 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003446 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (cmdline_row != i || ccline.overstrike)
3448 msg_clr_eos();
3449 msg_no_more = FALSE;
3450 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003451 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003453 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003454 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003456 }
3457 else
3458 m = MAXCOL;
3459 for (i = 0; i < len; ++i)
3460 {
3461 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003462 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003463 if (has_mbyte)
3464 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003465 // Stop cursor at the end of the screen, but do increment the
3466 // insert position, so that entering a very long command
3467 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003468 if (ccline.cmdspos + c < m)
3469 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003470
Bram Moolenaar14184a32019-02-16 15:10:30 +01003471 if (has_mbyte)
3472 {
3473 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3474 if (c > len - i - 1)
3475 c = len - i - 1;
3476 ccline.cmdpos += c;
3477 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003479 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 }
3482 if (redraw)
3483 msg_check();
3484 return retval;
3485}
3486
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487static cmdline_info_T prev_ccline;
3488static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003489
3490/*
3491 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3492 * and overwrite it. But get_cmdline_str() may need it, thus make it
3493 * available globally in prev_ccline.
3494 */
3495 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003497{
3498 if (!prev_ccline_used)
3499 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003500 CLEAR_FIELD(prev_ccline);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003501 prev_ccline_used = TRUE;
3502 }
3503 *ccp = prev_ccline;
3504 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003505 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003506}
3507
3508/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003509 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003510 */
3511 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003512restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003513{
3514 ccline = prev_ccline;
3515 prev_ccline = *ccp;
3516}
3517
Bram Moolenaar8299df92004-07-10 09:47:34 +00003518/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003519 * Paste a yank register into the command line.
3520 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003521 * insert_reg() can't be used here, because special characters from the
3522 * register contents will be interpreted as commands.
3523 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003524 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003525 */
3526 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003527cmdline_paste(
3528 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003529 int literally, // Insert text literally instead of "as typed"
3530 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531{
3532 long i;
3533 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003534 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003537 // check for valid regname; also accept special characters for CTRL-R in
3538 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003540 && regname != Ctrl_A && regname != Ctrl_L
3541 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003542 return FAIL;
3543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003544 // A register containing CTRL-R can cause an endless loop. Allow using
3545 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003546 line_breakcheck();
3547 if (got_int)
3548 return FAIL;
3549
3550#ifdef FEAT_CLIPBOARD
3551 regname = may_get_selection(regname);
3552#endif
3553
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003554 // Need to set "textwinlock" to avoid nasty things like going to another
Bram Moolenaar438d1762018-09-30 17:11:48 +02003555 // buffer when evaluating an expression.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003556 ++textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003557 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003558 --textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003559
3560 if (i)
3561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003562 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003563 if (arg == NULL)
3564 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003566 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3567 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003568 p = arg;
3569 if (p_is && regname == Ctrl_W)
3570 {
3571 char_u *w;
3572 int len;
3573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003574 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003575 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003576 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003577 if (has_mbyte)
3578 {
3579 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3580 if (!vim_iswordc(mb_ptr2char(w - len)))
3581 break;
3582 w -= len;
3583 }
3584 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003585 {
3586 if (!vim_iswordc(w[-1]))
3587 break;
3588 --w;
3589 }
3590 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003591 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003592 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3593 p += len;
3594 }
3595
3596 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597 if (allocated)
3598 vim_free(arg);
3599 return OK;
3600 }
3601
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003602 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603}
3604
3605/*
3606 * Put a string on the command line.
3607 * When "literally" is TRUE, insert literally.
3608 * When "literally" is FALSE, insert as typed, but don't leave the command
3609 * line.
3610 */
3611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003612cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613{
3614 int c, cv;
3615
3616 if (literally)
3617 put_on_cmdline(s, -1, TRUE);
3618 else
3619 while (*s != NUL)
3620 {
3621 cv = *s;
3622 if (cv == Ctrl_V && s[1])
3623 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003624 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003625 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003626 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003627 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003628 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3629 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630#ifdef UNIX
3631 || c == intr_char
3632#endif
3633 || (c == Ctrl_BSL && *s == Ctrl_N))
3634 stuffcharReadbuff(Ctrl_V);
3635 stuffcharReadbuff(c);
3636 }
3637}
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003640 * This function is called when the screen size changes and with incremental
3641 * search and in other situations where the command line may have been
3642 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 */
3644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003645redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003647 redrawcmdline_ex(TRUE);
3648}
3649
3650 void
3651redrawcmdline_ex(int do_compute_cmdrow)
3652{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (cmd_silent)
3654 return;
3655 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003656 if (do_compute_cmdrow)
3657 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 redrawcmd();
3659 cursorcmd();
3660}
3661
3662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003663redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
3665 int i;
3666
3667 if (cmd_silent)
3668 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003669 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 msg_putchar(ccline.cmdfirstc);
3671 if (ccline.cmdprompt != NULL)
3672 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003673 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003675 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003676 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 --ccline.cmdindent;
3678 }
3679 else
3680 for (i = ccline.cmdindent; i > 0; --i)
3681 msg_putchar(' ');
3682}
3683
3684/*
3685 * Redraw what is currently on the command line.
3686 */
3687 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003688redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 if (cmd_silent)
3691 return;
3692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003693 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003694 if (ccline.cmdbuff == NULL)
3695 {
3696 windgoto(cmdline_row, 0);
3697 msg_clr_eos();
3698 return;
3699 }
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 msg_start();
3702 redrawcmdprompt();
3703
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003704 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 msg_no_more = TRUE;
3706 draw_cmdline(0, ccline.cmdlen);
3707 msg_clr_eos();
3708 msg_no_more = FALSE;
3709
3710 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003711 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003712 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 /*
3715 * An emsg() before may have set msg_scroll. This is used in normal mode,
3716 * in cmdline mode we can reset them now.
3717 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003718 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003720 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3721 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 skip_redraw = FALSE;
3723}
3724
3725 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003726compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003728 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 cmdline_row = Rows - 1;
3730 else
3731 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003732 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733}
3734
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003736cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
3738 if (cmd_silent)
3739 return;
3740
3741#ifdef FEAT_RIGHTLEFT
3742 if (cmdmsg_rl)
3743 {
3744 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3745 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3746 if (msg_row <= 0)
3747 msg_row = Rows - 1;
3748 }
3749 else
3750#endif
3751 {
3752 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3753 msg_col = ccline.cmdspos % (int)Columns;
3754 if (msg_row >= Rows)
3755 msg_row = Rows - 1;
3756 }
3757
3758 windgoto(msg_row, msg_col);
3759#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003760 if (p_imst == IM_ON_THE_SPOT)
3761 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#endif
3763#ifdef MCH_CURSOR_SHAPE
3764 mch_update_cursor();
3765#endif
3766}
3767
3768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003769gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 msg_start();
3772#ifdef FEAT_RIGHTLEFT
3773 if (cmdmsg_rl)
3774 msg_col = Columns - 1;
3775 else
3776#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003777 msg_col = 0; // always start in column 0
3778 if (clr) // clear the bottom line(s)
3779 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 windgoto(cmdline_row, 0);
3781}
3782
3783/*
3784 * Check the word in front of the cursor for an abbreviation.
3785 * Called when the non-id character "c" has been entered.
3786 * When an abbreviation is recognized it is removed from the text with
3787 * backspaces and the replacement string is inserted, followed by "c".
3788 */
3789 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003790ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003792 int spos = 0;
3793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003794 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 return FALSE;
3796
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003797 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3798 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003799 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3800 spos++;
3801 if (ccline.cmdlen - spos > 5
3802 && ccline.cmdbuff[spos] == '\''
3803 && ccline.cmdbuff[spos + 2] == ','
3804 && ccline.cmdbuff[spos + 3] == '\'')
3805 spos += 5;
3806 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003807 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003808 spos = 0;
3809
3810 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811}
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003814 * Escape special characters in "fname" for when used as a file name argument
3815 * after a Vim command, or, when "shell" is non-zero, a shell command.
3816 * Returns the result in allocated memory.
3817 */
3818 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003819vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003820{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003821 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003822#ifdef BACKSLASH_IN_FILENAME
3823 char_u buf[20];
3824 int j = 0;
3825
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003826 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003827 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003828 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003829 buf[j++] = *p;
3830 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003831 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003832#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003833 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3834 if (shell && csh_like_shell() && p != NULL)
3835 {
3836 char_u *s;
3837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003838 // For csh and similar shells need to put two backslashes before '!'.
3839 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003840 s = vim_strsave_escaped(p, (char_u *)"!");
3841 vim_free(p);
3842 p = s;
3843 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003844#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003845
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003846 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3847 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003848 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003849 escape_fname(&p);
3850
3851 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003852}
3853
3854/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003855 * Put a backslash before the file name in "pp", which is in allocated memory.
3856 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003858escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003859{
3860 char_u *p;
3861
Bram Moolenaar964b3742019-05-24 18:54:09 +02003862 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003863 if (p != NULL)
3864 {
3865 p[0] = '\\';
3866 STRCPY(p + 1, *pp);
3867 vim_free(*pp);
3868 *pp = p;
3869 }
3870}
3871
3872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 * For each file name in files[num_files]:
3874 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3875 */
3876 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003877tilde_replace(
3878 char_u *orig_pat,
3879 int num_files,
3880 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
3882 int i;
3883 char_u *p;
3884
3885 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3886 {
3887 for (i = 0; i < num_files; ++i)
3888 {
3889 p = home_replace_save(NULL, files[i]);
3890 if (p != NULL)
3891 {
3892 vim_free(files[i]);
3893 files[i] = p;
3894 }
3895 }
3896 }
3897}
3898
3899/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003900 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003902 cmdline_info_T *
3903get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003905 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906}
3907
Bram Moolenaar064154c2016-03-19 22:50:43 +01003908#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003909/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003910 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003911 * ccline and put the previous value in prev_ccline.
3912 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003913 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003914get_ccline_ptr(void)
3915{
3916 if ((State & CMDLINE) == 0)
3917 return NULL;
3918 if (ccline.cmdbuff != NULL)
3919 return &ccline;
3920 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3921 return &prev_ccline;
3922 return NULL;
3923}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003924#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003925
Bram Moolenaar064154c2016-03-19 22:50:43 +01003926#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003927/*
3928 * Get the current command line in allocated memory.
3929 * Only works when the command line is being edited.
3930 * Returns NULL when something is wrong.
3931 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003932 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003933get_cmdline_str(void)
3934{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003935 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003936
Bram Moolenaaree91c332018-09-25 22:27:35 +02003937 if (cmdline_star > 0)
3938 return NULL;
3939 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003940 if (p == NULL)
3941 return NULL;
3942 return vim_strnsave(p->cmdbuff, p->cmdlen);
3943}
3944
3945/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003946 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003947 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003948 void
3949f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3950{
3951 rettv->v_type = VAR_STRING;
3952 rettv->vval.v_string = get_cmdline_str();
3953}
3954
3955/*
3956 * "getcmdpos()" function
3957 */
3958 void
3959f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003960{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003961 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003962
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003963 rettv->vval.v_number = 0;
3964 if (p != NULL)
3965 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003966}
3967
3968/*
3969 * Set the command line byte position to "pos". Zero is the first position.
3970 * Only works when the command line is being edited.
3971 * Returns 1 when failed, 0 when OK.
3972 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003973 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003974set_cmdline_pos(
3975 int pos)
3976{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003977 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003978
3979 if (p == NULL)
3980 return 1;
3981
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003982 // The position is not set directly but after CTRL-\ e or CTRL-R = has
3983 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003984 if (pos < 0)
3985 new_cmdpos = 0;
3986 else
3987 new_cmdpos = pos;
3988 return 0;
3989}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003990
3991/*
3992 * "setcmdpos()" function
3993 */
3994 void
3995f_setcmdpos(typval_T *argvars, typval_T *rettv)
3996{
3997 int pos = (int)tv_get_number(&argvars[0]) - 1;
3998
3999 if (pos >= 0)
4000 rettv->vval.v_number = set_cmdline_pos(pos);
4001}
4002
4003/*
4004 * "getcmdtype()" function
4005 */
4006 void
4007f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4008{
4009 rettv->v_type = VAR_STRING;
4010 rettv->vval.v_string = alloc(2);
4011 if (rettv->vval.v_string != NULL)
4012 {
4013 rettv->vval.v_string[0] = get_cmdline_type();
4014 rettv->vval.v_string[1] = NUL;
4015 }
4016}
4017
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004018#endif
4019
4020#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4021/*
4022 * Get the current command-line type.
4023 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4024 * Only works when the command line is being edited.
4025 * Returns NUL when something is wrong.
4026 */
4027 int
4028get_cmdline_type(void)
4029{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004030 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004031
4032 if (p == NULL)
4033 return NUL;
4034 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004035 return
4036# ifdef FEAT_EVAL
4037 (p->input_fn) ? '@' :
4038# endif
4039 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004040 return p->cmdfirstc;
4041}
4042#endif
4043
Bram Moolenaard7663c22019-08-06 21:59:57 +02004044/*
4045 * Return the first character of the current command line.
4046 */
4047 int
4048get_cmdline_firstc(void)
4049{
4050 return ccline.cmdfirstc;
4051}
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053/*
4054 * Get indices "num1,num2" that specify a range within a list (not a range of
4055 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4056 * Returns OK if parsed successfully, otherwise FAIL.
4057 */
4058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004059get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 int len;
4062 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004063 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004066 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004068 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 *str += len;
4070 *num1 = (int)num;
4071 first = TRUE;
4072 }
4073 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004074 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
4076 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004077 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (len > 0)
4079 {
4080 *num2 = (int)num;
4081 *str = skipwhite(*str + len);
4082 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004083 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 return FAIL;
4085 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004086 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 *num2 = *num1;
4088 return OK;
4089}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#if defined(FEAT_CMDWIN) || defined(PROTO)
4092/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004093 * Check value of 'cedit' and set cedit_key.
4094 * Returns NULL if value is OK, error message otherwise.
4095 */
4096 char *
4097check_cedit(void)
4098{
4099 int n;
4100
4101 if (*p_cedit == NUL)
4102 cedit_key = -1;
4103 else
4104 {
4105 n = string_to_key(p_cedit, FALSE);
4106 if (vim_isprintc(n))
4107 return e_invarg;
4108 cedit_key = n;
4109 }
4110 return NULL;
4111}
4112
4113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * Open a window on the current command line and history. Allow editing in
4115 * the window. Returns when the window is closed.
4116 * Returns:
4117 * CR if the command is to be executed
4118 * Ctrl_C if it is to be abandoned
4119 * K_IGNORE if editing continues
4120 */
4121 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004122open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004124 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004126 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 win_T *wp;
4128 int i;
4129 linenr_T lnum;
4130 int histtype;
4131 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 int save_restart_edit = restart_edit;
4133 int save_State = State;
4134 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004135#ifdef FEAT_RIGHTLEFT
4136 int save_cmdmsg_rl = cmdmsg_rl;
4137#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004138#ifdef FEAT_FOLDING
4139 int save_KeyTyped;
4140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004142 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (cmdwin_type != 0
4144# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4145 || cmdline_star > 0
4146# endif
4147 )
4148 {
4149 beep_flush();
4150 return K_IGNORE;
4151 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004152 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004154 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 win_size_save(&winsizes);
4156
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004157 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4158 // command line window, but we don't want the popup menu then.
4159 pum_undisplay();
4160
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004161 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004162 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004163 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004164
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004165 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4167 {
4168 beep_flush();
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004169 ga_clear(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return K_IGNORE;
4171 }
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004172 // Don't let quitting the More prompt make this fail.
4173 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004175 // Create the command-line buffer empty.
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004176 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL) == FAIL)
4177 {
4178 // Some autocommand messed it up?
4179 win_close(curwin, TRUE);
4180 ga_clear(&winsizes);
4181 return Ctrl_C;
4182 }
4183 cmdwin_type = get_cmdline_type();
4184
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004185 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004186 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004187 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004190#ifdef FEAT_FOLDING
4191 curwin->w_p_fen = FALSE;
4192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004194 curwin->w_p_rl = cmdmsg_rl;
4195 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004197 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004199 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004200 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004202 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004203 need_wait_return = FALSE;
4204
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004205 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4207 {
4208 if (p_wc == TAB)
4209 {
4210 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4211 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4212 }
4213 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4214 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004215 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004217 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4218 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004219 curbuf->b_p_tw = 0;
4220
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004221 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004223 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004225 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (i >= 0)
4227 {
4228 lnum = 0;
4229 do
4230 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004231 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004233 if (get_histentry(histtype)[i].hisstr != NULL)
4234 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 (colnr_T)0, FALSE);
4236 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004237 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 }
4240
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004241 // Replace the empty last line with the current command-line and put the
4242 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4244 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4245 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004246 changed_line_abv_curs();
4247 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004248 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
Bram Moolenaar23324a02019-10-01 17:39:04 +02004250 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 exmode_active = 0;
4252
4253 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaar23324a02019-10-01 17:39:04 +02004256 // Reset here so it can be set by a CmdWinEnter autocommand.
4257 cmdwin_result = 0;
4258
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004259 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004260 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004261 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004262 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 i = RedrawingDisabled;
4265 RedrawingDisabled = 0;
4266
4267 /*
4268 * Call the main loop until <CR> or CTRL-C is typed.
4269 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004270 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 RedrawingDisabled = i;
4273
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004274# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004275 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004276# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004277
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004278 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004279 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004280
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004281# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004282 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004283 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284# endif
4285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 exmode_active = save_exmode;
4288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004289 // Safety check: The old window or buffer was deleted: It's a bug when
4290 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004291 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004294 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 else
4297 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004298# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004299 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (aborting() && cmdwin_result != K_IGNORE)
4301 cmdwin_result = Ctrl_C;
4302# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004305 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004307 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4308
4309 if (histtype == HIST_CMD)
4310 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004311 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004312 ccline.cmdbuff = vim_strsave((char_u *)p);
4313 cmdwin_result = CAR;
4314 }
4315 else
4316 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004318 ccline.cmdbuff = NULL;
4319 stuffcharReadbuff(':');
4320 stuffReadbuff((char_u *)p);
4321 stuffcharReadbuff(CAR);
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
4326 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4327 cmdwin_result = CAR;
4328 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004329 else if (cmdwin_result == Ctrl_C)
4330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004331 // :q or :close, don't execute any command
4332 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004333 ccline.cmdbuff = NULL;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 else
4336 ccline.cmdbuff = vim_strsave(ml_get_curline());
4337 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004338 {
4339 ccline.cmdbuff = vim_strsave((char_u *)"");
4340 ccline.cmdlen = 0;
4341 ccline.cmdbufflen = 1;
4342 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 else
4346 {
4347 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4348 ccline.cmdbufflen = ccline.cmdlen + 1;
4349 ccline.cmdpos = curwin->w_cursor.col;
4350 if (ccline.cmdpos > ccline.cmdlen)
4351 ccline.cmdpos = ccline.cmdlen;
4352 if (cmdwin_result == K_IGNORE)
4353 {
4354 set_cmdspos_cursor();
4355 redrawcmd();
4356 }
4357 }
4358
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004359# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004360 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004361 curwin->w_p_cole = 0;
4362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004364 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 win_goto(old_curwin);
4366 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004367
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004368 // win_close() may have already wiped the buffer when 'bh' is
4369 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004370 if (bufref_valid(&bufref))
Bram Moolenaara6e8f882019-12-14 16:18:15 +01004371 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004373 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376
4377 ga_clear(&winsizes);
4378 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004379# ifdef FEAT_RIGHTLEFT
4380 cmdmsg_rl = save_cmdmsg_rl;
4381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 return cmdwin_result;
4387}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004388#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390/*
4391 * Used for commands that either take a simple command string argument, or:
4392 * cmd << endmarker
4393 * {script}
4394 * endmarker
4395 * Returns a pointer to allocated memory with {script} or NULL.
4396 */
4397 char_u *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004398script_get(exarg_T *eap UNUSED, char_u *cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004400#ifdef FEAT_EVAL
4401 list_T *l;
4402 listitem_T *li;
4403 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 garray_T ga;
4405
4406 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4407 return NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004408 cmd += 2;
4409
4410 l = heredoc_get(eap, cmd, TRUE);
4411 if (l == NULL)
4412 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 ga_init2(&ga, 1, 0x400);
4415
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004416 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004418 s = tv_get_string(&li->li_tv);
4419 ga_concat(&ga, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004422 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004424 list_free(l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 return (char_u *)ga.ga_data;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004426#else
4427 return NULL;
4428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004430
4431#if defined(FEAT_EVAL) || defined(PROTO)
4432/*
4433 * This function is used by f_input() and f_inputdialog() functions. The third
4434 * argument to f_input() specifies the type of completion to use at the
4435 * prompt. The third argument to f_inputdialog() specifies the value to return
4436 * when the user cancels the prompt.
4437 */
4438 void
4439get_user_input(
4440 typval_T *argvars,
4441 typval_T *rettv,
4442 int inputdialog,
4443 int secret)
4444{
4445 char_u *prompt = tv_get_string_chk(&argvars[0]);
4446 char_u *p = NULL;
4447 int c;
4448 char_u buf[NUMBUFLEN];
4449 int cmd_silent_save = cmd_silent;
4450 char_u *defstr = (char_u *)"";
4451 int xp_type = EXPAND_NOTHING;
4452 char_u *xp_arg = NULL;
4453
4454 rettv->v_type = VAR_STRING;
4455 rettv->vval.v_string = NULL;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004456 if (input_busy)
4457 return; // this doesn't work recursively.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004458
4459#ifdef NO_CONSOLE_INPUT
4460 // While starting up, there is no place to enter text. When running tests
4461 // with --not-a-term we assume feedkeys() will be used.
4462 if (no_console_input() && !is_not_a_term())
4463 return;
4464#endif
4465
4466 cmd_silent = FALSE; // Want to see the prompt.
4467 if (prompt != NULL)
4468 {
4469 // Only the part of the message after the last NL is considered as
4470 // prompt for the command line
4471 p = vim_strrchr(prompt, '\n');
4472 if (p == NULL)
4473 p = prompt;
4474 else
4475 {
4476 ++p;
4477 c = *p;
4478 *p = NUL;
4479 msg_start();
4480 msg_clr_eos();
4481 msg_puts_attr((char *)prompt, get_echo_attr());
4482 msg_didout = FALSE;
4483 msg_starthere();
4484 *p = c;
4485 }
4486 cmdline_row = msg_row;
4487
4488 if (argvars[1].v_type != VAR_UNKNOWN)
4489 {
4490 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4491 if (defstr != NULL)
4492 stuffReadbuffSpec(defstr);
4493
4494 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4495 {
4496 char_u *xp_name;
4497 int xp_namelen;
4498 long argt;
4499
4500 // input() with a third argument: completion
4501 rettv->vval.v_string = NULL;
4502
4503 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4504 if (xp_name == NULL)
4505 return;
4506
4507 xp_namelen = (int)STRLEN(xp_name);
4508
4509 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4510 &xp_arg) == FAIL)
4511 return;
4512 }
4513 }
4514
4515 if (defstr != NULL)
4516 {
4517 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004518 int save_vgetc_busy = vgetc_busy;
4519 int save_input_busy = input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004520
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004521 input_busy |= vgetc_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004522 ex_normal_busy = 0;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004523 vgetc_busy = 0;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004524 rettv->vval.v_string =
4525 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4526 xp_type, xp_arg);
4527 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004528 vgetc_busy = save_vgetc_busy;
4529 input_busy = save_input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004530 }
4531 if (inputdialog && rettv->vval.v_string == NULL
4532 && argvars[1].v_type != VAR_UNKNOWN
4533 && argvars[2].v_type != VAR_UNKNOWN)
4534 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4535 &argvars[2], buf));
4536
4537 vim_free(xp_arg);
4538
4539 // since the user typed this, no need to wait for return
4540 need_wait_return = FALSE;
4541 msg_didout = FALSE;
4542 }
4543 cmd_silent = cmd_silent_save;
4544}
4545#endif