blob: a00d6deb32abc259a6bc66382bc501cd7bb33007 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +020020// Return value when handling keys in command-line mode.
21#define CMDLINE_NOT_CHANGED 1
22#define CMDLINE_CHANGED 2
23#define GOTO_NORMAL_MODE 3
Bram Moolenaar9c929712020-09-07 22:05:28 +020024#define PROCESS_NEXT_KEY 4
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +020025
Bram Moolenaar438d1762018-09-30 17:11:48 +020026// The current cmdline_info. It is initialized in getcmdline() and after that
27// used by other functions. When invoking getcmdline() recursively it needs
28// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020029static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
31#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010032static int new_cmdpos; // position set by set_cmdline_pos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
34
Bram Moolenaar217e1b82019-12-01 21:41:28 +010035static int extra_char = NUL; // extra character to display when redrawing
36 // the command line
Bram Moolenaar6a77d262017-07-16 15:24:01 +020037static int extra_char_shift;
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +010040static int cmd_hkmap = 0; // Hebrew mapping during command line
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
42
Bram Moolenaar9c929712020-09-07 22:05:28 +020043static char_u *getcmdline_int(int firstc, long count, int indent, int clear_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010044static int cmdline_charsize(int idx);
45static void set_cmdspos(void);
46static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010047static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010049static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020050static void save_cmdline(cmdline_info_T *ccp);
51static void restore_cmdline(cmdline_info_T *ccp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010052static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010053static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010054static int ccheck_abbr(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
56#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +020057static int open_cmdwin(void);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +010058
59static int cedit_key INIT(= -1); // key value of 'cedit' option
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020062
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020063 static void
64trigger_cmd_autocmd(int typechar, int evt)
65{
66 char_u typestr[2];
67
68 typestr[0] = typechar;
69 typestr[1] = NUL;
70 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
71}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020072
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020074 * Abandon the command line.
75 */
76 static void
77abandon_cmdline(void)
78{
Bram Moolenaard23a8232018-02-10 18:45:26 +010079 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020080 if (msg_scrolled == 0)
81 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010082 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020083 redraw_cmdline = TRUE;
84}
85
Bram Moolenaaree219b02017-12-17 14:55:01 +010086#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020087/*
Bram Moolenaar66216052017-12-16 16:33:44 +010088 * Guess that the pattern matches everything. Only finds specific cases, such
89 * as a trailing \|, which can happen while typing a pattern.
90 */
91 static int
92empty_pattern(char_u *p)
93{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010094 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010095
Bram Moolenaar217e1b82019-12-01 21:41:28 +010096 // remove trailing \v and the like
Bram Moolenaar66216052017-12-16 16:33:44 +010097 while (n >= 2 && p[n - 2] == '\\'
98 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
99 n -= 2;
100 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
101}
102
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200103// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200104typedef struct {
105 colnr_T vs_curswant;
106 colnr_T vs_leftcol;
107 linenr_T vs_topline;
108# ifdef FEAT_DIFF
109 int vs_topfill;
110# endif
111 linenr_T vs_botline;
112 linenr_T vs_empty_rows;
113} viewstate_T;
114
115 static void
116save_viewstate(viewstate_T *vs)
117{
118 vs->vs_curswant = curwin->w_curswant;
119 vs->vs_leftcol = curwin->w_leftcol;
120 vs->vs_topline = curwin->w_topline;
121# ifdef FEAT_DIFF
122 vs->vs_topfill = curwin->w_topfill;
123# endif
124 vs->vs_botline = curwin->w_botline;
125 vs->vs_empty_rows = curwin->w_empty_rows;
126}
127
128 static void
129restore_viewstate(viewstate_T *vs)
130{
131 curwin->w_curswant = vs->vs_curswant;
132 curwin->w_leftcol = vs->vs_leftcol;
133 curwin->w_topline = vs->vs_topline;
134# ifdef FEAT_DIFF
135 curwin->w_topfill = vs->vs_topfill;
136# endif
137 curwin->w_botline = vs->vs_botline;
138 curwin->w_empty_rows = vs->vs_empty_rows;
139}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200140
141// Struct to store the state of 'incsearch' highlighting.
142typedef struct {
143 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200144 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200145 viewstate_T init_viewstate;
146 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200147 pos_T match_start;
148 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200149 int did_incsearch;
150 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200151 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200152} incsearch_state_T;
153
154 static void
155init_incsearch_state(incsearch_state_T *is_state)
156{
157 is_state->match_start = curwin->w_cursor;
158 is_state->did_incsearch = FALSE;
159 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200160 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200161 CLEAR_POS(&is_state->match_end);
162 is_state->save_cursor = curwin->w_cursor; // may be restored later
163 is_state->search_start = curwin->w_cursor;
164 save_viewstate(&is_state->init_viewstate);
165 save_viewstate(&is_state->old_viewstate);
166}
167
168/*
169 * First move cursor to end of match, then to the start. This
170 * moves the whole match onto the screen when 'nowrap' is set.
171 */
172 static void
173set_search_match(pos_T *t)
174{
175 t->lnum += search_match_lines;
176 t->col = search_match_endcol;
177 if (t->lnum > curbuf->b_ml.ml_line_count)
178 {
179 t->lnum = curbuf->b_ml.ml_line_count;
180 coladvance((colnr_T)MAXCOL);
181 }
182}
183
184/*
185 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200186 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200187 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200188 */
189 static int
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200190do_incsearch_highlighting(
191 int firstc,
192 int *search_delim,
193 incsearch_state_T *is_state,
194 int *skiplen,
195 int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200196{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200197 char_u *cmd;
Bram Moolenaare1004402020-10-24 20:49:43 +0200198 cmdmod_T dummy_cmdmod;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200199 char_u *p;
200 int delim_optional = FALSE;
201 int delim;
202 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100203 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200204 exarg_T ea;
205 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200206 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100207 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200208
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200209 *skiplen = 0;
210 *patlen = ccline.cmdlen;
211
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200212 if (!p_is || cmd_silent)
213 return FALSE;
214
215 // by default search all lines
216 search_first_line = 0;
217 search_last_line = MAXLNUM;
218
219 if (firstc == '/' || firstc == '?')
Bram Moolenaarc036e872020-02-21 21:30:52 +0100220 {
221 *search_delim = firstc;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200222 return TRUE;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100223 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200224 if (firstc != ':')
225 return FALSE;
226
Bram Moolenaarc6725252019-11-23 21:56:46 +0100227 ++emsg_off;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200228 CLEAR_FIELD(ea);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200229 ea.line1 = 1;
230 ea.line2 = 1;
231 ea.cmd = ccline.cmdbuff;
232 ea.addr_type = ADDR_LINES;
233
Bram Moolenaare1004402020-10-24 20:49:43 +0200234 CLEAR_FIELD(dummy_cmdmod);
235 parse_command_modifiers(&ea, &dummy, &dummy_cmdmod, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200237 cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200238 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100239 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200240
241 // Skip over "substitute" to find the pattern separator.
242 for (p = cmd; ASCII_ISALPHA(*p); ++p)
243 ;
244 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100245 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200246
247 if (STRNCMP(cmd, "substitute", p - cmd) == 0
248 || STRNCMP(cmd, "smagic", p - cmd) == 0
249 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
250 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200251 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200252 if (*cmd == 's' && cmd[1] == 'm')
253 p_magic = TRUE;
254 else if (*cmd == 's' && cmd[1] == 'n')
255 p_magic = FALSE;
256 }
257 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
258 {
Bram Moolenaar333015a2020-04-25 15:54:16 +0200259 // skip over ! and flags
260 if (*p == '!')
261 p = skipwhite(p + 1);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200262 while (ASCII_ISALPHA(*(p = skipwhite(p))))
263 ++p;
264 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100265 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200266 }
267 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
268 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
269 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
270 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
271 || STRNCMP(cmd, "global", p - cmd) == 0)
272 {
273 // skip over "!"
274 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200275 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276 p++;
277 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100278 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200279 }
280 if (*cmd != 'g')
281 delim_optional = TRUE;
282 }
283 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100284 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200285
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200286 p = skipwhite(p);
287 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100288 *search_delim = delim;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200289 end = skip_regexp(p, delim, p_magic);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200290
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200291 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200292
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200293 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100294 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200295
296 // Don't do 'hlsearch' highlighting if the pattern matches everything.
297 if (!use_last_pat)
298 {
299 char c = *end;
300 int empty;
301
302 *end = NUL;
303 empty = empty_pattern(p);
304 *end = c;
305 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100306 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200307 }
308
309 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200310 *skiplen = (int)(p - ccline.cmdbuff);
311 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200312
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200313 // parse the address range
314 save_cursor = curwin->w_cursor;
315 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200316 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200317 if (ea.addr_count > 0)
318 {
319 // Allow for reverse match.
320 if (ea.line2 < ea.line1)
321 {
322 search_first_line = ea.line2;
323 search_last_line = ea.line1;
324 }
325 else
326 {
327 search_first_line = ea.line1;
328 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 }
330 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200331 else if (cmd[0] == 's' && cmd[1] != 'o')
332 {
333 // :s defaults to the current line
334 search_first_line = curwin->w_cursor.lnum;
335 search_last_line = curwin->w_cursor.lnum;
336 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200337
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200338 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100339 retval = TRUE;
340theend:
341 --emsg_off;
342 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200343}
344
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200345 static void
346finish_incsearch_highlighting(
347 int gotesc,
348 incsearch_state_T *is_state,
349 int call_update_screen)
350{
351 if (is_state->did_incsearch)
352 {
353 is_state->did_incsearch = FALSE;
354 if (gotesc)
355 curwin->w_cursor = is_state->save_cursor;
356 else
357 {
358 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
359 {
360 // put the '" mark at the original position
361 curwin->w_cursor = is_state->save_cursor;
362 setpcmark();
363 }
364 curwin->w_cursor = is_state->search_start;
365 }
366 restore_viewstate(&is_state->old_viewstate);
367 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200368
369 // by default search all lines
370 search_first_line = 0;
371 search_last_line = MAXLNUM;
372
373 p_magic = is_state->magic_save;
374
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100375 validate_cursor(); // needed for TAB
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200376 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200377 if (call_update_screen)
378 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200379 }
380}
381
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200382/*
383 * Do 'incsearch' highlighting if desired.
384 */
385 static void
386may_do_incsearch_highlighting(
387 int firstc,
388 long count,
389 incsearch_state_T *is_state)
390{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200391 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200392 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200393 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200394#ifdef FEAT_RELTIME
395 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200396 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200397#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200398 int next_char;
399 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200400 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100401 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402
Bram Moolenaar198cb662018-09-06 21:44:17 +0200403 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100404 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200405 save_last_search_pattern();
406
Bram Moolenaara60053b2020-09-03 16:50:13 +0200407 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
408 &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200409 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200410 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200411 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200412 if (did_do_incsearch && vpeekc() == NUL)
413 // may have skipped a redraw, do it now
414 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200415 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200416 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200417
418 // If there is a character waiting, search and redraw later.
419 if (char_avail())
420 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200421 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422 is_state->incsearch_postponed = TRUE;
423 return;
424 }
425 is_state->incsearch_postponed = FALSE;
426
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200427 if (search_first_line == 0)
428 // start at the original cursor position
429 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100430 else if (search_first_line > curbuf->b_ml.ml_line_count)
431 {
432 // start after the last line
433 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
434 curwin->w_cursor.col = MAXCOL;
435 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200436 else
437 {
438 // start at the first line in the range
439 curwin->w_cursor.lnum = search_first_line;
440 curwin->w_cursor.col = 0;
441 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200442
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 // Use the previous pattern for ":s//".
444 next_char = ccline.cmdbuff[skiplen + patlen];
445 use_last_pat = patlen == 0 && skiplen > 0
446 && ccline.cmdbuff[skiplen - 1] == next_char;
447
448 // If there is no pattern, don't do anything.
449 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200450 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200451 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200452 set_no_hlsearch(TRUE); // turn off previous highlight
453 redraw_all_later(SOME_VALID);
454 }
455 else
456 {
457 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
458
459 cursor_off(); // so the user knows we're busy
460 out_flush();
461 ++emsg_off; // so it doesn't beep if bad expr
462#ifdef FEAT_RELTIME
463 // Set the time limit to half a second.
464 profile_setlimit(500L, &tm);
465#endif
466 if (!p_hls)
467 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200468 if (search_first_line != 0)
469 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200470 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200471#ifdef FEAT_RELTIME
Bram Moolenaara80faa82020-04-12 19:37:17 +0200472 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200473 sia.sa_tm = &tm;
474#endif
Bram Moolenaarc036e872020-02-21 21:30:52 +0100475 found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200476 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200477#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200478 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200480 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200481#endif
482 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200483 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200484 --emsg_off;
485
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200486 if (curwin->w_cursor.lnum < search_first_line
487 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200488 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200489 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200490 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200491 curwin->w_cursor = is_state->search_start;
492 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200493
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200494 // if interrupted while searching, behave like it failed
495 if (got_int)
496 {
497 (void)vpeekc(); // remove <C-C> from input stream
498 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200499 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500 }
501 else if (char_avail())
502 // cancelled searching because a char was typed
503 is_state->incsearch_postponed = TRUE;
504 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200505 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200506 highlight_match = TRUE; // highlight position
507 else
508 highlight_match = FALSE; // remove highlight
509
510 // First restore the old curwin values, so the screen is positioned in the
511 // same way as the actual search command.
512 restore_viewstate(&is_state->old_viewstate);
513 changed_cline_bef_curs();
514 update_topline();
515
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200516 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200517 {
518 pos_T save_pos = curwin->w_cursor;
519
520 is_state->match_start = curwin->w_cursor;
521 set_search_match(&curwin->w_cursor);
522 validate_cursor();
523 end_pos = curwin->w_cursor;
524 is_state->match_end = end_pos;
525 curwin->w_cursor = save_pos;
526 }
527 else
528 end_pos = curwin->w_cursor; // shutup gcc 4
529
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200530 // Disable 'hlsearch' highlighting if the pattern matches everything.
531 // Avoids a flash when typing "foo\|".
532 if (!use_last_pat)
533 {
534 next_char = ccline.cmdbuff[skiplen + patlen];
535 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200536 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
537 {
538 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200539 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200540 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200541 ccline.cmdbuff[skiplen + patlen] = next_char;
542 }
543
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200544 validate_cursor();
545 // May redraw the status line to show the cursor position.
546 if (p_ru && curwin->w_status_height > 0)
547 curwin->w_redr_status = TRUE;
548
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200550 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200551 restore_last_search_pattern();
552
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200553 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
554 // end of the pattern, e.g. for ":s/pat/".
555 if (ccline.cmdbuff[skiplen + patlen] != NUL)
556 curwin->w_cursor = is_state->search_start;
557 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200558 curwin->w_cursor = end_pos;
559
560 msg_starthere();
561 redrawcmdline();
562 is_state->did_incsearch = TRUE;
563}
564
565/*
566 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
567 * or previous match.
568 * Returns FAIL when jumping to cmdline_not_changed;
569 */
570 static int
571may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200572 int firstc,
573 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200575 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200577 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578 pos_T t;
579 char_u *pat;
580 int search_flags = SEARCH_NOOF;
581 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200582 int save;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100583 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200584
Bram Moolenaar198cb662018-09-06 21:44:17 +0200585 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100586 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200587 save_last_search_pattern();
588
Bram Moolenaarc036e872020-02-21 21:30:52 +0100589 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200590 {
591 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200593 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200594 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200595 {
596 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200598 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599
Bram Moolenaarc036e872020-02-21 21:30:52 +0100600 if (search_delim == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200601 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200603 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200604 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200605 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200606 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200607 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200608
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609 cursor_off();
610 out_flush();
611 if (c == Ctrl_G)
612 {
613 t = is_state->match_end;
614 if (LT_POS(is_state->match_start, is_state->match_end))
615 // Start searching at the end of the match not at the beginning of
616 // the next column.
617 (void)decl(&t);
618 search_flags += SEARCH_COL;
619 }
620 else
621 t = is_state->match_start;
622 if (!p_hls)
623 search_flags += SEARCH_KEEP;
624 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200625 save = pat[patlen];
626 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100627 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200629 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200631 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200632 if (i)
633 {
634 is_state->search_start = is_state->match_start;
635 is_state->match_end = t;
636 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200637 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200638 {
639 // Move just before the current match, so that when nv_search
640 // finishes the cursor will be put back on the match.
641 is_state->search_start = t;
642 (void)decl(&is_state->search_start);
643 }
644 else if (c == Ctrl_G && firstc == '?')
645 {
646 // Move just after the current match, so that when nv_search
647 // finishes the cursor will be put back on the match.
648 is_state->search_start = t;
649 (void)incl(&is_state->search_start);
650 }
651 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
652 {
653 // wrap around
654 is_state->search_start = t;
655 if (firstc == '?')
656 (void)incl(&is_state->search_start);
657 else
658 (void)decl(&is_state->search_start);
659 }
660
661 set_search_match(&is_state->match_end);
662 curwin->w_cursor = is_state->match_start;
663 changed_cline_bef_curs();
664 update_topline();
665 validate_cursor();
666 highlight_match = TRUE;
667 save_viewstate(&is_state->old_viewstate);
668 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200669 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200670 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200671 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200672 }
673 else
674 vim_beep(BO_ERROR);
675 restore_last_search_pattern();
676 return FAIL;
677}
678
679/*
680 * When CTRL-L typed: add character from the match to the pattern.
681 * May set "*c" to the added character.
682 * Return OK when jumping to cmdline_not_changed.
683 */
684 static int
685may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
686{
Bram Moolenaarc036e872020-02-21 21:30:52 +0100687 int skiplen, patlen, search_delim;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200688
Bram Moolenaar198cb662018-09-06 21:44:17 +0200689 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100690 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200691 save_last_search_pattern();
692
Bram Moolenaar9b7cce22020-06-06 15:14:08 +0200693 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
694 &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200695 {
696 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200697 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200698 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100699 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200700
701 // Add a character from under the cursor for 'incsearch'.
702 if (is_state->did_incsearch)
703 {
704 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200705 *c = gchar_cursor();
706 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200707 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200708 // If 'ignorecase' and 'smartcase' are set and the
709 // command line has no uppercase characters, convert
710 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200711 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200712 *c = MB_TOLOWER(*c);
Bram Moolenaarc036e872020-02-21 21:30:52 +0100713 if (*c == search_delim || vim_strchr((char_u *)(
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200714 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200715 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200716 // put a backslash before special characters
717 stuffcharReadbuff(*c);
718 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200719 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200720 // add any composing characters
721 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
722 {
723 int save_c = *c;
724
725 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
726 {
727 curwin->w_cursor.col += mb_char2len(*c);
728 *c = gchar_cursor();
729 stuffcharReadbuff(*c);
730 }
731 *c = save_c;
732 }
733 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200734 }
735 }
736 return OK;
737}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200738#endif
739
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200740#ifdef FEAT_ARABIC
741/*
742 * Return TRUE if the command line has an Arabic character at or after "start"
743 * for "len" bytes.
744 */
745 static int
746cmdline_has_arabic(int start, int len)
747{
748 int j;
749 int mb_l;
750 int u8c;
751 char_u *p;
752 int u8cc[MAX_MCO];
753
754 if (!enc_utf8)
755 return FALSE;
756
757 for (j = start; j < start + len; j += mb_l)
758 {
759 p = ccline.cmdbuff + j;
760 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
761 mb_l = utfc_ptr2len_len(p, start + len - j);
762 if (ARABIC_CHAR(u8c))
763 return TRUE;
764 }
765 return FALSE;
766}
767#endif
768
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200769 void
770cmdline_init(void)
771{
Bram Moolenaara80faa82020-04-12 19:37:17 +0200772 CLEAR_FIELD(ccline);
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200773}
774
Bram Moolenaar66216052017-12-16 16:33:44 +0100775/*
Bram Moolenaar9c929712020-09-07 22:05:28 +0200776 * Handle the backslash key pressed in the command-line mode. CTRL-\ CTRL-N
777 * goes to Normal mode, CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is
778 * set, CTRL-\ e prompts for an expression.
779 */
780 static int
781cmdline_handle_backslash_key(int c, int *gotesc)
782{
783 ++no_mapping;
784 ++allow_keys;
785 c = plain_vgetc();
786 --no_mapping;
787 --allow_keys;
788
789 // CTRL-\ e doesn't work when obtaining an expression, unless it
790 // is in a mapping.
791 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
792 || (ccline.cmdfirstc == '=' && KeyTyped)
793#ifdef FEAT_EVAL
794 || cmdline_star > 0
795#endif
796 ))
797 {
798 vungetc(c);
799 return PROCESS_NEXT_KEY;
800 }
801
802#ifdef FEAT_EVAL
803 if (c == 'e')
804 {
805 char_u *p = NULL;
806 int len;
807
808 /*
809 * Replace the command line with the result of an expression.
810 * Need to save and restore the current command line, to be
811 * able to enter a new one...
812 */
813 if (ccline.cmdpos == ccline.cmdlen)
814 new_cmdpos = 99999; // keep it at the end
815 else
816 new_cmdpos = ccline.cmdpos;
817
818 c = get_expr_register();
819 if (c == '=')
820 {
821 // Need to save and restore ccline. And set "textwinlock"
822 // to avoid nasty things like going to another buffer when
823 // evaluating an expression.
824 ++textwinlock;
825 p = get_expr_line();
826 --textwinlock;
827
828 if (p != NULL)
829 {
830 len = (int)STRLEN(p);
831 if (realloc_cmdbuff(len + 1) == OK)
832 {
833 ccline.cmdlen = len;
834 STRCPY(ccline.cmdbuff, p);
835 vim_free(p);
836
837 // Restore the cursor or use the position set with
838 // set_cmdline_pos().
839 if (new_cmdpos > ccline.cmdlen)
840 ccline.cmdpos = ccline.cmdlen;
841 else
842 ccline.cmdpos = new_cmdpos;
843
844 KeyTyped = FALSE; // Don't do p_wc completion.
845 redrawcmd();
846 return CMDLINE_CHANGED;
847 }
848 vim_free(p);
849 }
850 }
851 beep_flush();
852 got_int = FALSE; // don't abandon the command line
853 did_emsg = FALSE;
854 emsg_on_display = FALSE;
855 redrawcmd();
856 return CMDLINE_NOT_CHANGED;
857 }
858#endif
859
860 if (c == Ctrl_G && p_im && restart_edit == 0)
861 restart_edit = 'a';
862 *gotesc = TRUE; // will free ccline.cmdbuff after putting it
863 // in history
864 return GOTO_NORMAL_MODE;
865}
866
867/*
868 * Completion for 'wildchar' or 'wildcharm' key.
869 * - hitting <ESC> twice means: abandon command line.
870 * - wildcard expansion is only done when the 'wildchar' key is really
871 * typed, not when it comes from a macro
872 * Returns CMDLINE_CHANGED if command line is changed or CMDLINE_NOT_CHANGED.
873 */
874 static int
875cmdline_wildchar_complete(
876 int c,
877 int escape,
878 int *did_wild_list,
879 int *wim_index_p,
880 expand_T *xp,
881 int *gotesc)
882{
883 int wim_index = *wim_index_p;
884 int res;
885 int j;
886 int options = WILD_NO_BEEP;
887
888 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
889 options |= WILD_BUFLASTUSED;
890 if (xp->xp_numfiles > 0) // typed p_wc at least twice
891 {
892 // if 'wildmode' contains "list" may still need to list
893 if (xp->xp_numfiles > 1
894 && !*did_wild_list
895 && (wim_flags[wim_index] & WIM_LIST))
896 {
897 (void)showmatches(xp, FALSE);
898 redrawcmd();
899 *did_wild_list = TRUE;
900 }
901 if (wim_flags[wim_index] & WIM_LONGEST)
902 res = nextwild(xp, WILD_LONGEST, options, escape);
903 else if (wim_flags[wim_index] & WIM_FULL)
904 res = nextwild(xp, WILD_NEXT, options, escape);
905 else
906 res = OK; // don't insert 'wildchar' now
907 }
908 else // typed p_wc first time
909 {
910 wim_index = 0;
911 j = ccline.cmdpos;
912 // if 'wildmode' first contains "longest", get longest
913 // common part
914 if (wim_flags[0] & WIM_LONGEST)
915 res = nextwild(xp, WILD_LONGEST, options, escape);
916 else
917 res = nextwild(xp, WILD_EXPAND_KEEP, options, escape);
918
919 // if interrupted while completing, behave like it failed
920 if (got_int)
921 {
922 (void)vpeekc(); // remove <C-C> from input stream
923 got_int = FALSE; // don't abandon the command line
924 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
925#ifdef FEAT_WILDMENU
926 xp->xp_context = EXPAND_NOTHING;
927#endif
928 *wim_index_p = wim_index;
929 return CMDLINE_CHANGED;
930 }
931
932 // when more than one match, and 'wildmode' first contains
933 // "list", or no change and 'wildmode' contains "longest,list",
934 // list all matches
935 if (res == OK && xp->xp_numfiles > 1)
936 {
937 // a "longest" that didn't do anything is skipped (but not
938 // "list:longest")
939 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
940 wim_index = 1;
941 if ((wim_flags[wim_index] & WIM_LIST)
942#ifdef FEAT_WILDMENU
943 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
944#endif
945 )
946 {
947 if (!(wim_flags[0] & WIM_LONGEST))
948 {
949#ifdef FEAT_WILDMENU
950 int p_wmnu_save = p_wmnu;
951 p_wmnu = 0;
952#endif
953 // remove match
954 nextwild(xp, WILD_PREV, 0, escape);
955#ifdef FEAT_WILDMENU
956 p_wmnu = p_wmnu_save;
957#endif
958 }
959#ifdef FEAT_WILDMENU
960 (void)showmatches(xp, p_wmnu
961 && ((wim_flags[wim_index] & WIM_LIST) == 0));
962#else
963 (void)showmatches(xp, FALSE);
964#endif
965 redrawcmd();
966 *did_wild_list = TRUE;
967 if (wim_flags[wim_index] & WIM_LONGEST)
968 nextwild(xp, WILD_LONGEST, options, escape);
969 else if (wim_flags[wim_index] & WIM_FULL)
970 nextwild(xp, WILD_NEXT, options, escape);
971 }
972 else
973 vim_beep(BO_WILD);
974 }
975#ifdef FEAT_WILDMENU
976 else if (xp->xp_numfiles == -1)
977 xp->xp_context = EXPAND_NOTHING;
978#endif
979 }
980 if (wim_index < 3)
981 ++wim_index;
982 if (c == ESC)
983 *gotesc = TRUE;
984
985 *wim_index_p = wim_index;
986 return (res == OK) ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
987}
988
989/*
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +0200990 * Handle backspace, delete and CTRL-W keys in the command-line mode.
991 * Returns:
992 * CMDLINE_NOT_CHANGED - if the command line is not changed
993 * CMDLINE_CHANGED - if the command line is changed
994 * GOTO_NORMAL_MODE - go back to normal mode
995 */
996 static int
997cmdline_erase_chars(
998 int c,
999 int indent
1000#ifdef FEAT_SEARCH_EXTRA
1001 , incsearch_state_T *isp
1002#endif
1003 )
1004{
1005 int i;
1006 int j;
1007
1008 if (c == K_KDEL)
1009 c = K_DEL;
1010
1011 /*
1012 * Delete current character is the same as backspace on next
1013 * character, except at end of line.
1014 */
1015 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1016 ++ccline.cmdpos;
1017 if (has_mbyte && c == K_DEL)
1018 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1019 ccline.cmdbuff + ccline.cmdpos);
1020 if (ccline.cmdpos > 0)
1021 {
1022 char_u *p;
1023
1024 j = ccline.cmdpos;
1025 p = ccline.cmdbuff + j;
1026 if (has_mbyte)
1027 {
1028 p = mb_prevptr(ccline.cmdbuff, p);
1029 if (c == Ctrl_W)
1030 {
1031 while (p > ccline.cmdbuff && vim_isspace(*p))
1032 p = mb_prevptr(ccline.cmdbuff, p);
1033 i = mb_get_class(p);
1034 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1035 p = mb_prevptr(ccline.cmdbuff, p);
1036 if (mb_get_class(p) != i)
1037 p += (*mb_ptr2len)(p);
1038 }
1039 }
1040 else if (c == Ctrl_W)
1041 {
1042 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1043 --p;
1044 i = vim_iswordc(p[-1]);
1045 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1046 && vim_iswordc(p[-1]) == i)
1047 --p;
1048 }
1049 else
1050 --p;
1051 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1052 ccline.cmdlen -= j - ccline.cmdpos;
1053 i = ccline.cmdpos;
1054 while (i < ccline.cmdlen)
1055 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1056
1057 // Truncate at the end, required for multi-byte chars.
1058 ccline.cmdbuff[ccline.cmdlen] = NUL;
1059#ifdef FEAT_SEARCH_EXTRA
1060 if (ccline.cmdlen == 0)
1061 {
1062 isp->search_start = isp->save_cursor;
1063 // save view settings, so that the screen
1064 // won't be restored at the wrong position
1065 isp->old_viewstate = isp->init_viewstate;
1066 }
1067#endif
1068 redrawcmd();
1069 }
1070 else if (ccline.cmdlen == 0 && c != Ctrl_W
1071 && ccline.cmdprompt == NULL && indent == 0)
1072 {
1073 // In ex and debug mode it doesn't make sense to return.
1074 if (exmode_active
1075#ifdef FEAT_EVAL
1076 || ccline.cmdfirstc == '>'
1077#endif
1078 )
1079 return CMDLINE_NOT_CHANGED;
1080
1081 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
1082 if (!cmd_silent)
1083 {
1084#ifdef FEAT_RIGHTLEFT
1085 if (cmdmsg_rl)
1086 msg_col = Columns;
1087 else
1088#endif
1089 msg_col = 0;
1090 msg_putchar(' '); // delete ':'
1091 }
1092#ifdef FEAT_SEARCH_EXTRA
1093 if (ccline.cmdlen == 0)
1094 isp->search_start = isp->save_cursor;
1095#endif
1096 redraw_cmdline = TRUE;
1097 return GOTO_NORMAL_MODE;
1098 }
1099 return CMDLINE_CHANGED;
1100}
1101
1102/*
1103 * Handle the CTRL-^ key in the command-line mode and toggle the use of the
1104 * language :lmap mappings and/or Input Method.
1105 */
1106 static void
1107cmdline_toggle_langmap(long *b_im_ptr)
1108{
1109 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
1110 {
1111 // ":lmap" mappings exists, toggle use of mappings.
1112 State ^= LANGMAP;
1113#ifdef HAVE_INPUT_METHOD
1114 im_set_active(FALSE); // Disable input method
1115#endif
1116 if (b_im_ptr != NULL)
1117 {
1118 if (State & LANGMAP)
1119 *b_im_ptr = B_IMODE_LMAP;
1120 else
1121 *b_im_ptr = B_IMODE_NONE;
1122 }
1123 }
1124#ifdef HAVE_INPUT_METHOD
1125 else
1126 {
1127 // There are no ":lmap" mappings, toggle IM. When
1128 // 'imdisable' is set don't try getting the status, it's
1129 // always off.
1130 if ((p_imdisable && b_im_ptr != NULL)
1131 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1132 {
1133 im_set_active(FALSE); // Disable input method
1134 if (b_im_ptr != NULL)
1135 *b_im_ptr = B_IMODE_NONE;
1136 }
1137 else
1138 {
1139 im_set_active(TRUE); // Enable input method
1140 if (b_im_ptr != NULL)
1141 *b_im_ptr = B_IMODE_IM;
1142 }
1143 }
1144#endif
1145 if (b_im_ptr != NULL)
1146 {
1147 if (b_im_ptr == &curbuf->b_p_iminsert)
1148 set_iminsert_global();
1149 else
1150 set_imsearch_global();
1151 }
1152#ifdef CURSOR_SHAPE
1153 ui_cursor_shape(); // may show different cursor shape
1154#endif
1155#if defined(FEAT_KEYMAP)
1156 // Show/unshow value of 'keymap' in status lines later.
1157 status_redraw_curbuf();
1158#endif
1159}
1160
1161/*
1162 * Handle the CTRL-R key in the command-line mode and insert the contents of a
1163 * numbered or named register.
1164 */
1165 static int
1166cmdline_insert_reg(int *gotesc UNUSED)
1167{
1168 int i;
1169 int c;
1170
1171#ifdef USE_ON_FLY_SCROLL
1172 dont_scroll = TRUE; // disallow scrolling here
1173#endif
1174 putcmdline('"', TRUE);
1175 ++no_mapping;
1176 ++allow_keys;
1177 i = c = plain_vgetc(); // CTRL-R <char>
1178 if (i == Ctrl_O)
1179 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
1180 if (i == Ctrl_R)
1181 c = plain_vgetc(); // CTRL-R CTRL-R <char>
1182 extra_char = NUL;
1183 --no_mapping;
1184 --allow_keys;
1185#ifdef FEAT_EVAL
1186 /*
1187 * Insert the result of an expression.
1188 * Need to save the current command line, to be able to enter
1189 * a new one...
1190 */
1191 new_cmdpos = -1;
1192 if (c == '=')
1193 {
1194 if (ccline.cmdfirstc == '=' // can't do this recursively
1195 || cmdline_star > 0) // or when typing a password
1196 {
1197 beep_flush();
1198 c = ESC;
1199 }
1200 else
1201 c = get_expr_register();
1202 }
1203#endif
1204 if (c != ESC) // use ESC to cancel inserting register
1205 {
1206 cmdline_paste(c, i == Ctrl_R, FALSE);
1207
1208#ifdef FEAT_EVAL
1209 // When there was a serious error abort getting the
1210 // command line.
1211 if (aborting())
1212 {
1213 *gotesc = TRUE; // will free ccline.cmdbuff after
1214 // putting it in history
1215 return GOTO_NORMAL_MODE;
1216 }
1217#endif
1218 KeyTyped = FALSE; // Don't do p_wc completion.
1219#ifdef FEAT_EVAL
1220 if (new_cmdpos >= 0)
1221 {
1222 // set_cmdline_pos() was used
1223 if (new_cmdpos > ccline.cmdlen)
1224 ccline.cmdpos = ccline.cmdlen;
1225 else
1226 ccline.cmdpos = new_cmdpos;
1227 }
1228#endif
1229 }
1230 redrawcmd();
1231 return CMDLINE_CHANGED;
1232}
1233
1234/*
1235 * Handle the Left and Right mouse clicks in the command-line mode.
1236 */
1237 static void
1238cmdline_left_right_mouse(int c, int *ignore_drag_release)
1239{
1240 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1241 *ignore_drag_release = TRUE;
1242 else
1243 *ignore_drag_release = FALSE;
1244# ifdef FEAT_GUI
1245 // When GUI is active, also move when 'mouse' is empty
1246 if (!gui.in_use)
1247# endif
1248 if (!mouse_has(MOUSE_COMMAND))
1249 return;
1250# ifdef FEAT_CLIPBOARD
1251 if (mouse_row < cmdline_row && clip_star.available)
1252 {
1253 int button, is_click, is_drag;
1254
1255 /*
1256 * Handle modeless selection.
1257 */
1258 button = get_mouse_button(KEY2TERMCAP1(c),
1259 &is_click, &is_drag);
1260 if (mouse_model_popup() && button == MOUSE_LEFT
1261 && (mod_mask & MOD_MASK_SHIFT))
1262 {
1263 // Translate shift-left to right button.
1264 button = MOUSE_RIGHT;
1265 mod_mask &= ~MOD_MASK_SHIFT;
1266 }
1267 clip_modeless(button, is_click, is_drag);
1268 return;
1269 }
1270# endif
1271
1272 set_cmdspos();
1273 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1274 ++ccline.cmdpos)
1275 {
1276 int i;
1277
1278 i = cmdline_charsize(ccline.cmdpos);
1279 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1280 && mouse_col < ccline.cmdspos % Columns + i)
1281 break;
1282 if (has_mbyte)
1283 {
1284 // Count ">" for double-wide char that doesn't fit.
1285 correct_cmdspos(ccline.cmdpos, i);
1286 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
1287 + ccline.cmdpos) - 1;
1288 }
1289 ccline.cmdspos += i;
1290 }
1291}
1292
1293/*
1294 * Handle the Up, Down, Page Up, Page down, CTRL-N and CTRL-P key in the
1295 * command-line mode. The pressed key is in 'c'.
1296 * Returns:
1297 * CMDLINE_NOT_CHANGED - if the command line is not changed
1298 * CMDLINE_CHANGED - if the command line is changed
1299 * GOTO_NORMAL_MODE - go back to normal mode
1300 */
1301 static int
1302cmdline_browse_history(
1303 int c,
1304 int firstc,
1305 char_u **curcmdstr,
1306 int histype,
1307 int *hiscnt_p,
1308 expand_T *xp)
1309{
1310 int i;
1311 int j;
1312 char_u *lookfor = *curcmdstr;
1313 int hiscnt = *hiscnt_p;
1314 int res;
1315
1316 if (get_hislen() == 0 || firstc == NUL) // no history
1317 return CMDLINE_NOT_CHANGED;
1318
1319 i = hiscnt;
1320
1321 // save current command string so it can be restored later
1322 if (lookfor == NULL)
1323 {
1324 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1325 return CMDLINE_NOT_CHANGED;
1326 lookfor[ccline.cmdpos] = NUL;
1327 }
1328
1329 j = (int)STRLEN(lookfor);
1330 for (;;)
1331 {
1332 // one step backwards
1333 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
1334 || c == K_PAGEUP || c == K_KPAGEUP)
1335 {
1336 if (hiscnt == get_hislen()) // first time
1337 hiscnt = *get_hisidx(histype);
1338 else if (hiscnt == 0 && *get_hisidx(histype)
1339 != get_hislen() - 1)
1340 hiscnt = get_hislen() - 1;
1341 else if (hiscnt != *get_hisidx(histype) + 1)
1342 --hiscnt;
1343 else // at top of list
1344 {
1345 hiscnt = i;
1346 break;
1347 }
1348 }
1349 else // one step forwards
1350 {
1351 // on last entry, clear the line
1352 if (hiscnt == *get_hisidx(histype))
1353 {
1354 hiscnt = get_hislen();
1355 break;
1356 }
1357
1358 // not on a history line, nothing to do
1359 if (hiscnt == get_hislen())
1360 break;
1361 if (hiscnt == get_hislen() - 1) // wrap around
1362 hiscnt = 0;
1363 else
1364 ++hiscnt;
1365 }
1366 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
1367 == NULL)
1368 {
1369 hiscnt = i;
1370 break;
1371 }
1372 if ((c != K_UP && c != K_DOWN)
1373 || hiscnt == i
1374 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
1375 lookfor, (size_t)j) == 0)
1376 break;
1377 }
1378
1379 if (hiscnt != i) // jumped to other entry
1380 {
1381 char_u *p;
1382 int len;
1383 int old_firstc;
1384
1385 VIM_CLEAR(ccline.cmdbuff);
1386 xp->xp_context = EXPAND_NOTHING;
1387 if (hiscnt == get_hislen())
1388 p = lookfor; // back to the old one
1389 else
1390 p = get_histentry(histype)[hiscnt].hisstr;
1391
1392 if (histype == HIST_SEARCH
1393 && p != lookfor
1394 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1395 {
1396 // Correct for the separator character used when
1397 // adding the history entry vs the one used now.
1398 // First loop: count length.
1399 // Second loop: copy the characters.
1400 for (i = 0; i <= 1; ++i)
1401 {
1402 len = 0;
1403 for (j = 0; p[j] != NUL; ++j)
1404 {
1405 // Replace old sep with new sep, unless it is
1406 // escaped.
1407 if (p[j] == old_firstc
1408 && (j == 0 || p[j - 1] != '\\'))
1409 {
1410 if (i > 0)
1411 ccline.cmdbuff[len] = firstc;
1412 }
1413 else
1414 {
1415 // Escape new sep, unless it is already
1416 // escaped.
1417 if (p[j] == firstc
1418 && (j == 0 || p[j - 1] != '\\'))
1419 {
1420 if (i > 0)
1421 ccline.cmdbuff[len] = '\\';
1422 ++len;
1423 }
1424 if (i > 0)
1425 ccline.cmdbuff[len] = p[j];
1426 }
1427 ++len;
1428 }
1429 if (i == 0)
1430 {
1431 alloc_cmdbuff(len);
1432 if (ccline.cmdbuff == NULL)
1433 {
1434 res = GOTO_NORMAL_MODE;
1435 goto done;
1436 }
1437 }
1438 }
1439 ccline.cmdbuff[len] = NUL;
1440 }
1441 else
1442 {
1443 alloc_cmdbuff((int)STRLEN(p));
1444 if (ccline.cmdbuff == NULL)
1445 {
1446 res = GOTO_NORMAL_MODE;
1447 goto done;
1448 }
1449 STRCPY(ccline.cmdbuff, p);
1450 }
1451
1452 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1453 redrawcmd();
1454 res = CMDLINE_CHANGED;
1455 goto done;
1456 }
1457 beep_flush();
1458 res = CMDLINE_NOT_CHANGED;
1459
1460done:
1461 *curcmdstr = lookfor;
1462 *hiscnt_p = hiscnt;
1463 return res;
1464}
1465
1466/*
Bram Moolenaar9c929712020-09-07 22:05:28 +02001467 * Initialize the current command-line info.
1468 */
1469 static int
1470init_ccline(int firstc, int indent)
1471{
1472 ccline.overstrike = FALSE; // always start in insert mode
1473
1474 /*
1475 * set some variables for redrawcmd()
1476 */
1477 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
1478 ccline.cmdindent = (firstc > 0 ? indent : 0);
1479
1480 // alloc initial ccline.cmdbuff
1481 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
1482 if (ccline.cmdbuff == NULL)
1483 return FAIL;
1484 ccline.cmdlen = ccline.cmdpos = 0;
1485 ccline.cmdbuff[0] = NUL;
1486 sb_text_start_cmdline();
1487
1488 // autoindent for :insert and :append
1489 if (firstc <= 0)
1490 {
1491 vim_memset(ccline.cmdbuff, ' ', indent);
1492 ccline.cmdbuff[indent] = NUL;
1493 ccline.cmdpos = indent;
1494 ccline.cmdspos = indent;
1495 ccline.cmdlen = indent;
1496 }
1497
1498 return OK;
1499}
1500
1501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 * getcmdline() - accept a command line starting with firstc.
1503 *
1504 * firstc == ':' get ":" command line.
1505 * firstc == '/' or '?' get search pattern
1506 * firstc == '=' get expression
1507 * firstc == '@' get text for input() function
1508 * firstc == '>' get text for debug mode
1509 * firstc == NUL get text for :insert command
1510 * firstc == -1 like NUL, and break on CTRL-C
1511 *
1512 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
1513 * command line.
1514 *
1515 * Careful: getcmdline() can be called recursively!
1516 *
1517 * Return pointer to allocated string if there is a commandline, NULL
1518 * otherwise.
1519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001521getcmdline(
1522 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +02001523 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +02001524 int indent, // indent for inside conditionals
1525 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001526{
1527 return getcmdline_int(firstc, count, indent, TRUE);
1528}
1529
1530 static char_u *
1531getcmdline_int(
1532 int firstc,
1533 long count UNUSED, // only used for incremental search
1534 int indent, // indent for inside conditionals
Bram Moolenaar9c929712020-09-07 22:05:28 +02001535 int clear_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
1537 int c;
1538 int i;
1539 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001540 int gotesc = FALSE; // TRUE when <ESC> just typed
1541 int do_abbr; // when TRUE check for abbr.
1542 char_u *lookfor = NULL; // string to match
1543 int hiscnt; // current history line in use
1544 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001546 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001548 int did_wild_list = FALSE; // did wild_list() recently
1549 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 int res;
1551 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 int save_State = State; // remember State when called
1553 int some_key_typed = FALSE; // one of the keys was typed
1554 // mouse drag and release events are ignored, unless they are
1555 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557#ifdef FEAT_EVAL
1558 int break_ctrl_c = FALSE;
1559#endif
1560 expand_T xpc;
1561 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001562 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02001563 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001564 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaar438d1762018-09-30 17:11:48 +02001566 if (ccline.cmdbuff != NULL)
1567 {
1568 // Being called recursively. Since ccline is global, we need to save
1569 // the current buffer and restore it when returning.
1570 save_cmdline(&save_ccline);
1571 did_save_ccline = TRUE;
1572 }
Bram Moolenaar9c929712020-09-07 22:05:28 +02001573 if (clear_ccline)
Bram Moolenaara80faa82020-04-12 19:37:17 +02001574 CLEAR_FIELD(ccline);
Bram Moolenaar438d1762018-09-30 17:11:48 +02001575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#ifdef FEAT_EVAL
1577 if (firstc == -1)
1578 {
1579 firstc = NUL;
1580 break_ctrl_c = TRUE;
1581 }
1582#endif
1583#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001584 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (firstc == ':' || firstc == '=' || firstc == '>')
1586 cmd_hkmap = 0;
1587#endif
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001590 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591#endif
1592
Bram Moolenaar9c929712020-09-07 22:05:28 +02001593 if (init_ccline(firstc, indent) != OK)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001594 goto theend; // out of memory
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001597 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599#ifdef FEAT_RIGHTLEFT
1600 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
1601 && (firstc == '/' || firstc == '?'))
1602 cmdmsg_rl = TRUE;
1603 else
1604 cmdmsg_rl = FALSE;
1605#endif
1606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (!cmd_silent)
1609 {
1610 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001611 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 gotocmdline(TRUE);
1613 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001614 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 set_cmdspos();
1616 }
1617 xpc.xp_context = EXPAND_NOTHING;
1618 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001619#ifndef BACKSLASH_IN_FILENAME
1620 xpc.xp_shell = FALSE;
1621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001623#if defined(FEAT_EVAL)
1624 if (ccline.input_fn)
1625 {
1626 xpc.xp_context = ccline.xp_context;
1627 xpc.xp_pattern = ccline.cmdbuff;
1628 xpc.xp_arg = ccline.xp_arg;
1629 }
1630#endif
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 /*
1633 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
1634 * doing ":@0" when register 0 doesn't contain a CR.
1635 */
1636 msg_scroll = FALSE;
1637
1638 State = CMDLINE;
1639
1640 if (firstc == '/' || firstc == '?' || firstc == '@')
1641 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001642 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
1644 b_im_ptr = &curbuf->b_p_iminsert;
1645 else
1646 b_im_ptr = &curbuf->b_p_imsearch;
1647 if (*b_im_ptr == B_IMODE_LMAP)
1648 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 im_set_active(*b_im_ptr == B_IMODE_IM);
1651#endif
1652 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001653#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 else if (p_imcmdline)
1655 im_set_active(TRUE);
1656#endif
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001660 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
1662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001663 // When inside an autocommand for writing "exiting" may be set and
1664 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001665 settmode(TMODE_RAW);
1666
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001667 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001668 cmdline_type = firstc == NUL ? '-' : firstc;
1669 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001672 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001676 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
1678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // If something above caused an error, reset the flags, we do want to type
1680 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001681 if (did_emsg)
1682 redrawcmd();
1683 did_emsg = FALSE;
1684 got_int = FALSE;
1685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 /*
1687 * Collect the command string, handling editing keys.
1688 */
1689 for (;;)
1690 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 redir_off = TRUE; // Don't redirect the typed command.
1692 // Repeated, because a ":redir" inside
1693 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001697 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001699 did_emsg = FALSE; // There can't really be a reason why an error
1700 // that occurs while typing a command should
1701 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +02001702
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001703 // Trigger SafeState if nothing is pending.
1704 may_trigger_safestate(xpc.xp_numfiles <= 0);
1705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001706 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +00001707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001708 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
1709 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001710 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001711 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001712 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001713
Bram Moolenaar957cf672020-11-12 14:21:06 +01001714 if (c == K_COMMAND
1715 && do_cmdline(NULL, getcmdkeycmd, NULL, DOCMD_NOWAIT) == OK)
1716 goto cmdline_changed;
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (KeyTyped)
1719 {
1720 some_key_typed = TRUE;
1721#ifdef FEAT_RIGHTLEFT
1722 if (cmd_hkmap)
1723 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (cmdmsg_rl && !KeyStuffed)
1725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001726 // Invert horizontal movements and operations. Only when
1727 // typed by the user directly, not when the result of a
1728 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 switch (c)
1730 {
1731 case K_RIGHT: c = K_LEFT; break;
1732 case K_S_RIGHT: c = K_S_LEFT; break;
1733 case K_C_RIGHT: c = K_C_LEFT; break;
1734 case K_LEFT: c = K_RIGHT; break;
1735 case K_S_LEFT: c = K_S_RIGHT; break;
1736 case K_C_LEFT: c = K_C_RIGHT; break;
1737 }
1738 }
1739#endif
1740 }
1741
1742 /*
1743 * Ignore got_int when CTRL-C was typed here.
1744 * Don't ignore it in :global, we really need to break then, e.g., for
1745 * ":g/pat/normal /pat" (without the <CR>).
1746 * Don't ignore it for the input() function.
1747 */
1748 if ((c == Ctrl_C
1749#ifdef UNIX
1750 || c == intr_char
1751#endif
1752 )
1753#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1754 && firstc != '@'
1755#endif
1756#ifdef FEAT_EVAL
1757 && !break_ctrl_c
1758#endif
1759 && !global_busy)
1760 got_int = FALSE;
1761
Bram Moolenaard7663c22019-08-06 21:59:57 +02001762 // free old command line when finished moving around in the history
1763 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001765 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001766 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 && c != K_PAGEDOWN && c != K_PAGEUP
1768 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001769 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001771 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001774 * When there are matching completions to select <S-Tab> works like
1775 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001777 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 c = Ctrl_P;
1779
1780#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001781 c = wildmenu_translate_key(&ccline, c, &xpc, did_wild_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#endif
1783
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001784 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (xpc.xp_numfiles != -1
1786 && !(c == p_wc && KeyTyped) && c != p_wcm
1787 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1788 && c != Ctrl_L)
1789 {
1790 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1791 did_wild_list = FALSE;
1792#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001793 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#endif
1795 xpc.xp_context = EXPAND_NOTHING;
1796 wim_index = 0;
1797#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001798 wildmenu_cleanup(&ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#endif
1800 }
1801
1802#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001803 c = wildmenu_process_key(&ccline, c, &xpc);
1804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001806 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1807 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (c == Ctrl_BSL)
1809 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001810 res = cmdline_handle_backslash_key(c, &gotesc);
1811 if (res == CMDLINE_CHANGED)
1812 goto cmdline_changed;
1813 else if (res == CMDLINE_NOT_CHANGED)
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001814 goto cmdline_not_changed;
Bram Moolenaar9c929712020-09-07 22:05:28 +02001815 else if (res == GOTO_NORMAL_MODE)
1816 goto returncmd; // back to cmd mode
1817 c = Ctrl_BSL; // backslash key not processed by
1818 // cmdline_handle_backslash_key()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820
1821#ifdef FEAT_CMDWIN
1822 if (c == cedit_key || c == K_CMDWIN)
1823 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001824 // TODO: why is ex_normal_busy checked here?
1825 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001826 {
1827 /*
1828 * Open a window to edit the command line (and history).
1829 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001830 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001831 some_key_typed = TRUE;
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834# ifdef FEAT_DIGRAPHS
1835 else
1836# endif
1837#endif
1838#ifdef FEAT_DIGRAPHS
1839 c = do_digraph(c);
1840#endif
1841
1842 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1843 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001845 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001846 if (exmode_active
1847 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001848 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001849 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001850 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001852 if (c == K_KENTER)
1853 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001855 else
1856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001857 gotesc = FALSE; // Might have typed ESC previously, don't
1858 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001859 if (ccheck_abbr(c + ABBR_OFF))
1860 goto cmdline_changed;
1861 if (!cmd_silent)
1862 {
1863 windgoto(msg_row, 0);
1864 out_flush();
1865 }
1866 break;
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
Bram Moolenaar9c929712020-09-07 22:05:28 +02001870 // Completion for 'wildchar' or 'wildcharm' key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1872 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001873 res = cmdline_wildchar_complete(c, firstc != '@', &did_wild_list,
1874 &wim_index, &xpc, &gotesc);
1875 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 goto cmdline_changed;
1877 }
1878
1879 gotesc = FALSE;
1880
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001881 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (c == K_S_TAB && KeyTyped)
1883 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001884 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1885 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1886 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 goto cmdline_changed;
1888 }
1889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001890 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 c = NL;
1892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001893 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895 /*
1896 * Big switch for a typed command line character.
1897 */
1898 switch (c)
1899 {
1900 case K_BS:
1901 case Ctrl_H:
1902 case K_DEL:
1903 case K_KDEL:
1904 case Ctrl_W:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001905 res = cmdline_erase_chars(c, indent
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001907 , &is_state
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001908#endif
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001909 );
1910 if (res == CMDLINE_NOT_CHANGED)
1911 goto cmdline_not_changed;
1912 else if (res == GOTO_NORMAL_MODE)
1913 goto returncmd; // back to cmd mode
1914 goto cmdline_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 case K_INS:
1917 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 ccline.overstrike = !ccline.overstrike;
1919#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001920 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921#endif
1922 goto cmdline_not_changed;
1923
1924 case Ctrl_HAT:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001925 cmdline_toggle_langmap(b_im_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 goto cmdline_not_changed;
1927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 j = ccline.cmdpos;
1932 ccline.cmdlen -= j;
1933 i = ccline.cmdpos = 0;
1934 while (i < ccline.cmdlen)
1935 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001936 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001938#ifdef FEAT_SEARCH_EXTRA
1939 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001940 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 redrawcmd();
1943 goto cmdline_changed;
1944
1945#ifdef FEAT_CLIPBOARD
1946 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001947 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (clip_star.state != SELECT_CLEARED)
1949 {
1950 if (clip_star.state == SELECT_DONE)
1951 clip_copy_modeless_selection(TRUE);
1952 goto cmdline_not_changed;
1953 }
1954 break;
1955#endif
1956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001957 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001959 // In exmode it doesn't make sense to return. Except when
1960 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001961 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001962 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 goto cmdline_not_changed;
1964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001965 gotesc = TRUE; // will free ccline.cmdbuff after
1966 // putting it in history
1967 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001969 case Ctrl_R: // insert register
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001970 res = cmdline_insert_reg(&gotesc);
1971 if (res == CMDLINE_NOT_CHANGED)
1972 goto cmdline_not_changed;
1973 else if (res == GOTO_NORMAL_MODE)
1974 goto returncmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto cmdline_changed;
1976
1977 case Ctrl_D:
1978 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001979 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001982 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 case K_RIGHT:
1985 case K_S_RIGHT:
1986 case K_C_RIGHT:
1987 do
1988 {
1989 if (ccline.cmdpos >= ccline.cmdlen)
1990 break;
1991 i = cmdline_charsize(ccline.cmdpos);
1992 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1993 break;
1994 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001996 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 + ccline.cmdpos);
1998 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 ++ccline.cmdpos;
2000 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002001 while ((c == K_S_RIGHT || c == K_C_RIGHT
2002 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (has_mbyte)
2005 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 goto cmdline_not_changed;
2007
2008 case K_LEFT:
2009 case K_S_LEFT:
2010 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002011 if (ccline.cmdpos == 0)
2012 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 do
2014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
2018 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
2020 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002021 while (ccline.cmdpos > 0
2022 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002023 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (has_mbyte)
2026 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 goto cmdline_not_changed;
2028
2029 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002030 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00002031 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
Bram Moolenaar4f974752019-02-17 17:44:42 +01002033#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // On MS-Windows ignore <M-F4>, we get it when closing the window
2035 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002036 case K_F4:
2037 if (mod_mask == MOD_MASK_ALT)
2038 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002039 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00002040 goto cmdline_not_changed;
2041 }
2042 break;
2043#endif
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 case K_MIDDLEDRAG:
2046 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002047 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 case K_MIDDLEMOUSE:
2050# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (!gui.in_use)
2053# endif
2054 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002055 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002056# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002058 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002060# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002061 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 redrawcmd();
2063 goto cmdline_changed;
2064
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002065# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002067 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 redrawcmd();
2069 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 case K_LEFTDRAG:
2073 case K_LEFTRELEASE:
2074 case K_RIGHTDRAG:
2075 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002076 // Ignore drag and release events when the button-down wasn't
2077 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (ignore_drag_release)
2079 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 case K_LEFTMOUSE:
2082 case K_RIGHTMOUSE:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002083 cmdline_left_right_mouse(c, &ignore_drag_release);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 goto cmdline_not_changed;
2085
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002086 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 case K_MOUSEDOWN:
2088 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002089 case K_MOUSELEFT:
2090 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002091 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 case K_X1MOUSE:
2093 case K_X1DRAG:
2094 case K_X1RELEASE:
2095 case K_X2MOUSE:
2096 case K_X2DRAG:
2097 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002098 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 goto cmdline_not_changed;
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 case K_LEFTRELEASE_NM:
2104 goto cmdline_not_changed;
2105
2106 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002107 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 gui_do_scroll();
2110 redrawcmd();
2111 }
2112 goto cmdline_not_changed;
2113
2114 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002115 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002117 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 redrawcmd();
2119 }
2120 goto cmdline_not_changed;
2121#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002122#ifdef FEAT_GUI_TABLINE
2123 case K_TABLINE:
2124 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 // Don't want to change any tabs here. Make sure the same tab
2126 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002127 if (gui_use_tabline())
2128 gui_mch_set_curtab(tabpage_index(curtab));
2129 goto cmdline_not_changed;
2130#endif
2131
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 goto cmdline_not_changed;
2134
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 case K_HOME:
2137 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 case K_S_HOME:
2139 case K_C_HOME:
2140 ccline.cmdpos = 0;
2141 set_cmdspos();
2142 goto cmdline_not_changed;
2143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002144 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 case K_END:
2146 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 case K_S_END:
2148 case K_C_END:
2149 ccline.cmdpos = ccline.cmdlen;
2150 set_cmdspos_cursor();
2151 goto cmdline_not_changed;
2152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002153 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01002154 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 break;
2156 goto cmdline_changed;
2157
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002158 case Ctrl_L:
2159#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002160 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002161 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002162#endif
2163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002164 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002165 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 break;
2167 goto cmdline_changed;
2168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002169 case Ctrl_N: // next match
2170 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002171 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002173 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2174 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002176 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002178 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 case K_UP:
2180 case K_DOWN:
2181 case K_S_UP:
2182 case K_S_DOWN:
2183 case K_PAGEUP:
2184 case K_KPAGEUP:
2185 case K_PAGEDOWN:
2186 case K_KPAGEDOWN:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002187 res = cmdline_browse_history(c, firstc, &lookfor, histype,
2188 &hiscnt, &xpc);
2189 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 goto cmdline_changed;
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002191 else if (res == GOTO_NORMAL_MODE)
2192 goto returncmd;
Bram Moolenaar11956692016-08-27 16:26:56 +02002193 goto cmdline_not_changed;
2194
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002195#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 case Ctrl_G: // next match
2197 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002198 if (may_adjust_incsearch_highlighting(
2199 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002200 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#endif
2203
2204 case Ctrl_V:
2205 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002207 int prev_mod_mask = mod_mask;
2208
2209 ignore_drag_release = TRUE;
2210 putcmdline('^', TRUE);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002211 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002212 c = get_literal(); // get next (two) character(s)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002213 no_reduce_keys = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002214 do_abbr = FALSE; // don't do abbreviation now
2215 extra_char = NUL;
2216 // may need to remove ^ when composing char was typed
2217 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2218 {
2219 draw_cmdline(ccline.cmdpos,
2220 ccline.cmdlen - ccline.cmdpos);
2221 msg_putchar(' ');
2222 cursorcmd();
2223 }
2224
2225 if ((c == ESC || c == CSI)
2226 && !(prev_mod_mask & MOD_MASK_SHIFT))
2227 // Using CTRL-V: Change any modifyOtherKeys ESC
2228 // sequence to a normal key. Don't do this for
2229 // CTRL-SHIFT-V.
2230 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 break;
2234
2235#ifdef FEAT_DIGRAPHS
2236 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002239# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002240 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002243 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (c != NUL)
2245 break;
2246
2247 redrawcmd();
2248 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002249#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (!p_ari)
2254 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002255 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 goto cmdline_not_changed;
2257#endif
2258
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002259 case K_PS:
2260 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2261 goto cmdline_changed;
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 default:
2264#ifdef UNIX
2265 if (c == intr_char)
2266 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 gotesc = TRUE; // will free ccline.cmdbuff after
2268 // putting it in history
2269 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271#endif
2272 /*
2273 * Normal character with no special meaning. Just set mod_mask
2274 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2275 * the string <S-Space>. This should only happen after ^V.
2276 */
2277 if (!IS_SPECIAL(c))
2278 mod_mask = 0x0;
2279 break;
2280 }
2281 /*
2282 * End of switch on command line character.
2283 * We come here if we have a normal character.
2284 */
2285
Bram Moolenaar13505972019-01-24 15:04:48 +01002286 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2287 && (ccheck_abbr(
2288 // Add ABBR_OFF for characters above 0x100, this is
2289 // what check_abbr() expects.
2290 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2291 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 goto cmdline_changed;
2293
2294 /*
2295 * put the character in the command line
2296 */
2297 if (IS_SPECIAL(c) || mod_mask != 0)
2298 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2299 else
2300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (has_mbyte)
2302 {
2303 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002304 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 put_on_cmdline(IObuff, j, TRUE);
2306 }
2307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 IObuff[0] = c;
2310 put_on_cmdline(IObuff, 1, TRUE);
2311 }
2312 }
2313 goto cmdline_changed;
2314
2315/*
2316 * This part implements incremental searches for "/" and "?"
2317 * Jump to cmdline_not_changed when a character has been read but the command
2318 * line did not change. Then we only search and redraw if something changed in
2319 * the past.
2320 * Jump to cmdline_changed when the command line did change.
2321 * (Sorry for the goto's, I know it is ugly).
2322 */
2323cmdline_not_changed:
2324#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002325 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 continue;
2327#endif
2328
2329cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002330 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002331 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaara60053b2020-09-03 16:50:13 +02002334 if (xpc.xp_context == EXPAND_NOTHING)
2335 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#endif
2337
2338#ifdef FEAT_RIGHTLEFT
2339 if (cmdmsg_rl
2340# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002341 || (p_arshape && !p_tbidi
2342 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343# endif
2344 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 // Always redraw the whole command line to fix shaping and
2346 // right-left typing. Not efficient, but it works.
2347 // Do it only when there are no characters left to read
2348 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002349 if (vpeekc() == NUL)
2350 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#endif
2352 }
2353
2354returncmd:
2355
2356#ifdef FEAT_RIGHTLEFT
2357 cmdmsg_rl = FALSE;
2358#endif
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002361 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002364 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
2367 if (ccline.cmdbuff != NULL)
2368 {
2369 /*
2370 * Put line in history buffer (":" and "=" only when it was typed).
2371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (ccline.cmdlen && firstc != NUL
2373 && (some_key_typed || histype == HIST_SEARCH))
2374 {
2375 add_to_history(histype, ccline.cmdbuff, TRUE,
2376 histype == HIST_SEARCH ? firstc : NUL);
2377 if (firstc == ':')
2378 {
2379 vim_free(new_last_cmdline);
2380 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2381 }
2382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002384 if (gotesc)
2385 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387
2388 /*
2389 * If the screen was shifted up, redraw the whole screen (later).
2390 * If the line is too long, clear it, so ruler and shown command do
2391 * not get printed in the middle of it.
2392 */
2393 msg_check();
2394 msg_scroll = save_msg_scroll;
2395 redir_off = FALSE;
2396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002397 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (some_key_typed)
2399 need_wait_return = FALSE;
2400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002401 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002402 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002405#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2407 im_save_status(b_im_ptr);
2408 im_set_active(FALSE);
2409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002412 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002414 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
Bram Moolenaar438d1762018-09-30 17:11:48 +02002416theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002417 {
2418 char_u *p = ccline.cmdbuff;
2419
Bram Moolenaar438d1762018-09-30 17:11:48 +02002420 if (did_save_ccline)
2421 restore_cmdline(&save_ccline);
2422 else
2423 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002424 return p;
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426}
2427
2428#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2429/*
2430 * Get a command line with a prompt.
2431 * This is prepared to be called recursively from getcmdline() (e.g. by
2432 * f_input() when evaluating an expression from CTRL-R =).
2433 * Returns the command line in allocated memory, or NULL.
2434 */
2435 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002436getcmdline_prompt(
2437 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002438 char_u *prompt, // command line prompt
2439 int attr, // attributes for prompt
2440 int xp_context, // type of expansion
2441 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002444 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002445 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002447 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar438d1762018-09-30 17:11:48 +02002449 if (ccline.cmdbuff != NULL)
2450 {
2451 // Save the values of the current cmdline and restore them below.
2452 save_cmdline(&save_ccline);
2453 did_save_ccline = TRUE;
2454 }
2455
Bram Moolenaara80faa82020-04-12 19:37:17 +02002456 CLEAR_FIELD(ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 ccline.cmdprompt = prompt;
2458 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002459# ifdef FEAT_EVAL
2460 ccline.xp_context = xp_context;
2461 ccline.xp_arg = xp_arg;
2462 ccline.input_fn = (firstc == '@');
2463# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002464 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002465 s = getcmdline_int(firstc, 1L, 0, FALSE);
2466
2467 if (did_save_ccline)
2468 restore_cmdline(&save_ccline);
2469
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002470 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002471 // Restore msg_col, the prompt from input() may have changed it.
2472 // But only if called recursively and the commandline is therefore being
2473 // restored to an old one; if not, the input() prompt stays on the screen,
2474 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002475 if (ccline.cmdbuff != NULL)
2476 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 return s;
2479}
2480#endif
2481
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002482/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002483 * Read the 'wildmode' option, fill wim_flags[].
2484 */
2485 int
2486check_opt_wim(void)
2487{
2488 char_u new_wim_flags[4];
2489 char_u *p;
2490 int i;
2491 int idx = 0;
2492
2493 for (i = 0; i < 4; ++i)
2494 new_wim_flags[i] = 0;
2495
2496 for (p = p_wim; *p; ++p)
2497 {
2498 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2499 ;
2500 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2501 return FAIL;
2502 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2503 new_wim_flags[idx] |= WIM_LONGEST;
2504 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2505 new_wim_flags[idx] |= WIM_FULL;
2506 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2507 new_wim_flags[idx] |= WIM_LIST;
2508 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2509 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2510 else
2511 return FAIL;
2512 p += i;
2513 if (*p == NUL)
2514 break;
2515 if (*p == ',')
2516 {
2517 if (idx == 3)
2518 return FAIL;
2519 ++idx;
2520 }
2521 }
2522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002523 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002524 while (idx < 3)
2525 {
2526 new_wim_flags[idx + 1] = new_wim_flags[idx];
2527 ++idx;
2528 }
2529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002530 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002531 for (i = 0; i < 4; ++i)
2532 wim_flags[i] = new_wim_flags[i];
2533 return OK;
2534}
2535
2536/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002537 * Return TRUE when the text must not be changed and we can't switch to
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002538 * another window or buffer. TRUE when editing the command line, evaluating
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002539 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002540 */
2541 int
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002542text_and_win_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002543{
2544#ifdef FEAT_CMDWIN
2545 if (cmdwin_type != 0)
2546 return TRUE;
2547#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002548 return textwinlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549}
2550
2551/*
2552 * Give an error message for a command that isn't allowed while the cmdline
2553 * window is open or editing the cmdline in another way.
2554 */
2555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002556text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002557{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002558 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002559}
2560
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002561 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002562get_text_locked_msg(void)
2563{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002564#ifdef FEAT_CMDWIN
2565 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002566 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002568 if (textwinlock != 0)
2569 return e_textwinlock;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002570 return e_textlock;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002571}
2572
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002573/*
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002574 * Return TRUE when the text must not be changed and/or we cannot switch to
2575 * another window. TRUE while evaluating 'completefunc'.
2576 */
2577 int
2578text_locked(void)
2579{
2580 return text_and_win_locked() || textlock != 0;
2581}
2582
2583/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002584 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2585 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002586 */
2587 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002588curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002589{
2590 if (curbuf_lock > 0)
2591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002592 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002593 return TRUE;
2594 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002595 return allbuf_locked();
2596}
2597
2598/*
2599 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2600 * message.
2601 */
2602 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002604{
2605 if (allbuf_lock > 0)
2606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002608 return TRUE;
2609 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002610 return FALSE;
2611}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002617 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 return 1;
2619#endif
2620 return ptr2cells(ccline.cmdbuff + idx);
2621}
2622
2623/*
2624 * Compute the offset of the cursor on the command line for the prompt and
2625 * indent.
2626 */
2627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002628set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002630 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 ccline.cmdspos = 1 + ccline.cmdindent;
2632 else
2633 ccline.cmdspos = 0 + ccline.cmdindent;
2634}
2635
2636/*
2637 * Compute the screen position for the cursor on the command line.
2638 */
2639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002640set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
2642 int i, m, c;
2643
2644 set_cmdspos();
2645 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002648 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002649 m = MAXCOL;
2650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 else
2652 m = MAXCOL;
2653 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2654 {
2655 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002656 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (has_mbyte)
2658 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002659 // If the cmdline doesn't fit, show cursor on last visible char.
2660 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if ((ccline.cmdspos += c) >= m)
2662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ccline.cmdspos -= c;
2664 break;
2665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002667 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669}
2670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671/*
2672 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2673 * character that doesn't fit, so that a ">" must be displayed.
2674 */
2675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002676correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002678 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2680 && ccline.cmdspos % Columns + cells > Columns)
2681 ccline.cmdspos++;
2682}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684/*
2685 * Get an Ex command line for the ":" command.
2686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002688getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002690 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002691 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002692 getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002694 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (exec_from_reg && vpeekc() == ':')
2696 (void)vgetc();
Bram Moolenaar66250c92020-08-20 15:02:42 +02002697 return getcmdline(c, 1L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698}
2699
2700/*
2701 * Get an Ex command line for Ex mode.
2702 * In Ex mode we only use the OS supplied line editing features and no
2703 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002707getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002708 int promptc, // normally ':', NUL for ":append" and '?' for
2709 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002710 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002711 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002712 getline_opt_T options UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002714 garray_T line_ga;
2715 char_u *pend;
2716 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002717 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002718 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002719 int vcol = 0;
2720 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002721 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002722 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002724 // Switch cursor on now. This avoids that it happens after the "\n", which
2725 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 cursor_on();
2727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002728 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002734 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 if (p_prompt)
2736 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 while (indent-- > 0)
2738 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741
2742 ga_init2(&line_ga, 1, 30);
2743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002744 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002745 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002746 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002747 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002748 while (indent >= 8)
2749 {
2750 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002751 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002752 indent -= 8;
2753 }
2754 while (indent-- > 0)
2755 {
2756 ga_append(&line_ga, ' ');
2757 msg_putchar(' ');
2758 }
2759 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 ++no_mapping;
2761 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 /*
2764 * Get the line, one character at a time.
2765 */
2766 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002769 long sw;
2770 char_u *s;
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (ga_grow(&line_ga, 40) == FAIL)
2773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
Bram Moolenaarba748c82017-02-26 14:00:07 +01002775 /*
2776 * Get one character at a time.
2777 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002778 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002780 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002781 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2782 c1 = '\n';
2783 else
2784 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 * Handle line editing.
2788 * Previously this was left to the system, putting the terminal in
2789 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 msg_putchar('\n');
2794 break;
2795 }
2796
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002797 if (c1 == K_PS)
2798 {
2799 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2800 goto redraw;
2801 }
2802
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 if (!escaped)
2804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002805 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806 if (c1 == '\r')
2807 c1 = '\n';
2808
2809 if (c1 == BS || c1 == K_BS
2810 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 if (line_ga.ga_len > 0)
2813 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002814 if (has_mbyte)
2815 {
2816 p = (char_u *)line_ga.ga_data;
2817 p[line_ga.ga_len] = NUL;
2818 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2819 line_ga.ga_len -= len;
2820 }
2821 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002822 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 goto redraw;
2824 }
2825 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 msg_col = startcol;
2831 msg_clr_eos();
2832 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002833 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002834 }
2835
2836 if (c1 == Ctrl_T)
2837 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002838 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 p = (char_u *)line_ga.ga_data;
2840 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002841 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002842 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002844 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002846 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002847 p = (char_u *)line_ga.ga_data;
2848 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2850 *s = ' ';
2851 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002854 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002857 p = (char_u *)line_ga.ga_data;
2858 p[line_ga.ga_len] = NUL;
2859 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002865 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002866 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002870 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002871 msg_outtrans_len(p, len);
2872 vcol += ptr2cells(p);
2873 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002877 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878 continue;
2879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881 if (c1 == Ctrl_D)
2882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002883 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884 p = (char_u *)line_ga.ga_data;
2885 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002887 if (prev_char == '^')
2888 ex_keep_indent = TRUE;
2889 indent = 0;
2890 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892 else
2893 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002894 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002895 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002896 if (indent > 0)
2897 {
2898 --indent;
2899 indent -= indent % get_sw_value(curbuf);
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002902 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002904 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2906 --line_ga.ga_len;
2907 }
2908 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910
2911 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2912 {
2913 escaped = TRUE;
2914 continue;
2915 }
2916
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002917 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002918 if (IS_SPECIAL(c1))
2919 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002921
2922 if (IS_SPECIAL(c1))
2923 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002924 if (has_mbyte)
2925 len = (*mb_char2bytes)(c1,
2926 (char_u *)line_ga.ga_data + line_ga.ga_len);
2927 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002928 {
2929 len = 1;
2930 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2931 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002932 if (c1 == '\n')
2933 msg_putchar('\n');
2934 else if (c1 == TAB)
2935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002936 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002937 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002938 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002939 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002943 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002944 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002945 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002947 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002948 escaped = FALSE;
2949
2950 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002951 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002952
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002953 // We are done when a NL is entered, but not when it comes after an
2954 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002955 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002957 int bcount = 0;
2958
2959 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2960 ++bcount;
2961
2962 if (bcount > 0)
2963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002964 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2965 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002966 line_ga.ga_len -= (bcount + 1) / 2;
2967 pend -= (bcount + 1) / 2;
2968 pend[-1] = '\n';
2969 }
2970
2971 if ((bcount & 1) == 0)
2972 {
2973 --line_ga.ga_len;
2974 --pend;
2975 *pend = NUL;
2976 break;
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
2979 }
2980
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002981 --no_mapping;
2982 --allow_keys;
2983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002984 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 msg_didout = FALSE;
2986 msg_col = 0;
2987 if (msg_row < Rows - 1)
2988 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002989 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 if (got_int)
2992 ga_clear(&line_ga);
2993
2994 return (char_u *)line_ga.ga_data;
2995}
2996
Bram Moolenaare344bea2005-09-01 20:46:49 +00002997# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2998 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999/*
3000 * Return TRUE if ccline.overstrike is on.
3001 */
3002 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003003cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 return ccline.overstrike;
3006}
3007
3008/*
3009 * Return TRUE if the cursor is at the end of the cmdline.
3010 */
3011 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003012cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 return (ccline.cmdpos >= ccline.cmdlen);
3015}
3016#endif
3017
Bram Moolenaar9372a112005-12-06 19:59:18 +00003018#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019/*
3020 * Return the virtual column number at the current cursor position.
3021 * This is used by the IM code to obtain the start of the preedit string.
3022 */
3023 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003024cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3027 return MAXCOL;
3028
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (has_mbyte)
3030 {
3031 colnr_T col;
3032 int i = 0;
3033
3034 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003035 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 return col;
3038 }
3039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return ccline.cmdpos;
3041}
3042#endif
3043
3044#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3045/*
3046 * If part of the command line is an IM preedit string, redraw it with
3047 * IM feedback attributes. The cursor position is restored after drawing.
3048 */
3049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if ((State & CMDLINE)
3053 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003054 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 && !p_imdisable
3056 && im_is_preediting())
3057 {
3058 int cmdpos = 0;
3059 int cmdspos;
3060 int old_row;
3061 int old_col;
3062 colnr_T col;
3063
3064 old_row = msg_row;
3065 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003066 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (has_mbyte)
3069 {
3070 for (col = 0; col < preedit_start_col
3071 && cmdpos < ccline.cmdlen; ++col)
3072 {
3073 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003074 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076 }
3077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
3079 cmdspos += preedit_start_col;
3080 cmdpos += preedit_start_col;
3081 }
3082
3083 msg_row = cmdline_row + (cmdspos / (int)Columns);
3084 msg_col = cmdspos % (int)Columns;
3085 if (msg_row >= Rows)
3086 msg_row = Rows - 1;
3087
3088 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3089 {
3090 int char_len;
3091 int char_attr;
3092
3093 char_attr = im_get_feedback_attr(col);
3094 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003095 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003098 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 char_len = 1;
3101
3102 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3103 cmdpos += char_len;
3104 }
3105
3106 msg_row = old_row;
3107 msg_col = old_col;
3108 }
3109}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003110#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
3112/*
3113 * Allocate a new command line buffer.
3114 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 */
3116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003117alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 /*
3120 * give some extra space to avoid having to allocate all the time
3121 */
3122 if (len < 80)
3123 len = 100;
3124 else
3125 len += 20;
3126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003127 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 ccline.cmdbufflen = len;
3129}
3130
3131/*
3132 * Re-allocate the command line to length len + something extra.
3133 * return FAIL for failure, OK otherwise
3134 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 char_u *p;
3139
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003140 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003141 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003144 alloc_cmdbuff(len); // will get some more
3145 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003147 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 return FAIL;
3149 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003150 // There isn't always a NUL after the command, but it may need to be
3151 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003152 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3153 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003155
3156 if (ccline.xpc != NULL
3157 && ccline.xpc->xp_pattern != NULL
3158 && ccline.xpc->xp_context != EXPAND_NOTHING
3159 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3160 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003161 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003163 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3164 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003165 if (i >= 0 && i <= ccline.cmdlen)
3166 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3167 }
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return OK;
3170}
3171
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003172#if defined(FEAT_ARABIC) || defined(PROTO)
3173static char_u *arshape_buf = NULL;
3174
3175# if defined(EXITFREE) || defined(PROTO)
3176 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003177free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003178{
3179 vim_free(arshape_buf);
3180}
3181# endif
3182#endif
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184/*
3185 * Draw part of the cmdline at the current cursor position. But draw stars
3186 * when cmdline_star is TRUE.
3187 */
3188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003189draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3192 int i;
3193
3194 if (cmdline_star > 0)
3195 for (i = 0; i < len; ++i)
3196 {
3197 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003199 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 else
3202#endif
3203#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003204 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 static int buflen = 0;
3207 char_u *p;
3208 int j;
3209 int newlen = 0;
3210 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003211 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 int prev_c = 0;
3213 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003214 int u8c;
3215 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 /*
3219 * Do arabic shaping into a temporary buffer. This is very
3220 * inefficient!
3221 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003222 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003224 // Re-allocate the buffer. We keep it around to avoid a lot of
3225 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003226 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003227 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003228 arshape_buf = alloc(buflen);
3229 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003230 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003233 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3234 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003235 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003236 arshape_buf[0] = ' ';
3237 newlen = 1;
3238 }
3239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 for (j = start; j < start + len; j += mb_l)
3241 {
3242 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003243 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003244 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (ARABIC_CHAR(u8c))
3246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003247 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (cmdmsg_rl)
3249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003250 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 pc = prev_c;
3252 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003253 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 if (j + mb_l >= start + len)
3255 nc = NUL;
3256 else
3257 nc = utf_ptr2char(p + mb_l);
3258 }
3259 else
3260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003261 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (j + mb_l >= start + len)
3263 pc = NUL;
3264 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003265 {
3266 int pcc[MAX_MCO];
3267
3268 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003270 pc1 = pcc[0];
3271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 nc = prev_c;
3273 }
3274 prev_c = u8c;
3275
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003276 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003279 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003281 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3282 if (u8cc[1] != 0)
3283 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003284 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286 }
3287 else
3288 {
3289 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003290 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 newlen += mb_l;
3292 }
3293 }
3294
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003295 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 else
3298#endif
3299 msg_outtrans_len(ccline.cmdbuff + start, len);
3300}
3301
3302/*
3303 * Put a character on the command line. Shifts the following text to the
3304 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3305 * "c" must be printable (fit in one display cell)!
3306 */
3307 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003308putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 if (cmd_silent)
3311 return;
3312 msg_no_more = TRUE;
3313 msg_putchar(c);
3314 if (shift)
3315 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3316 msg_no_more = FALSE;
3317 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003318 extra_char = c;
3319 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
3323 * Undo a putcmdline(c, FALSE).
3324 */
3325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003326unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 if (cmd_silent)
3329 return;
3330 msg_no_more = TRUE;
3331 if (ccline.cmdlen == ccline.cmdpos)
3332 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003333 else if (has_mbyte)
3334 draw_cmdline(ccline.cmdpos,
3335 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 else
3337 draw_cmdline(ccline.cmdpos, 1);
3338 msg_no_more = FALSE;
3339 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003340 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341}
3342
3343/*
3344 * Put the given string, of the given length, onto the command line.
3345 * If len is -1, then STRLEN() is used to calculate the length.
3346 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3347 * part will be redrawn, otherwise it will not. If this function is called
3348 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3349 * called afterwards.
3350 */
3351 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003352put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int retval;
3355 int i;
3356 int m;
3357 int c;
3358
3359 if (len < 0)
3360 len = (int)STRLEN(str);
3361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003362 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003364 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 else
3366 retval = OK;
3367 if (retval == OK)
3368 {
3369 if (!ccline.overstrike)
3370 {
3371 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3372 ccline.cmdbuff + ccline.cmdpos,
3373 (size_t)(ccline.cmdlen - ccline.cmdpos));
3374 ccline.cmdlen += len;
3375 }
3376 else
3377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (has_mbyte)
3379 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003380 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003382 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003384 // Count nr of bytes in cmdline that are overwritten by these
3385 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003387 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 --m;
3389 if (i < ccline.cmdlen)
3390 {
3391 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3392 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3393 ccline.cmdlen += ccline.cmdpos + len - i;
3394 }
3395 else
3396 ccline.cmdlen = ccline.cmdpos + len;
3397 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003398 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 ccline.cmdlen = ccline.cmdpos + len;
3400 }
3401 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3402 ccline.cmdbuff[ccline.cmdlen] = NUL;
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (enc_utf8)
3405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003406 // When the inserted text starts with a composing character,
3407 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 i = 0;
3409 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3410 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3411 {
3412 i = (*mb_head_off)(ccline.cmdbuff,
3413 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3414 ccline.cmdpos -= i;
3415 len += i;
3416 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3417 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003418#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003421 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 i = (*mb_head_off)(ccline.cmdbuff,
3423 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3424 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3425 + ccline.cmdpos - i), c))
3426 {
3427 ccline.cmdpos -= i;
3428 len += i;
3429 }
3430 else
3431 i = 0;
3432 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (i != 0)
3435 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003436 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3438 ccline.cmdspos -= i;
3439 msg_col -= i;
3440 if (msg_col < 0)
3441 {
3442 msg_col += Columns;
3443 --msg_row;
3444 }
3445 }
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (redraw && !cmd_silent)
3449 {
3450 msg_no_more = TRUE;
3451 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003452 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003454 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (cmdline_row != i || ccline.overstrike)
3456 msg_clr_eos();
3457 msg_no_more = FALSE;
3458 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003459 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003461 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003462 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003464 }
3465 else
3466 m = MAXCOL;
3467 for (i = 0; i < len; ++i)
3468 {
3469 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003470 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003471 if (has_mbyte)
3472 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003473 // Stop cursor at the end of the screen, but do increment the
3474 // insert position, so that entering a very long command
3475 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003476 if (ccline.cmdspos + c < m)
3477 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003478
Bram Moolenaar14184a32019-02-16 15:10:30 +01003479 if (has_mbyte)
3480 {
3481 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3482 if (c > len - i - 1)
3483 c = len - i - 1;
3484 ccline.cmdpos += c;
3485 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003487 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489 }
3490 if (redraw)
3491 msg_check();
3492 return retval;
3493}
3494
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495static cmdline_info_T prev_ccline;
3496static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003497
3498/*
3499 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3500 * and overwrite it. But get_cmdline_str() may need it, thus make it
3501 * available globally in prev_ccline.
3502 */
3503 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003504save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003505{
3506 if (!prev_ccline_used)
3507 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003508 CLEAR_FIELD(prev_ccline);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003509 prev_ccline_used = TRUE;
3510 }
3511 *ccp = prev_ccline;
3512 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003513 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003514}
3515
3516/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003517 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003518 */
3519 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003520restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003521{
3522 ccline = prev_ccline;
3523 prev_ccline = *ccp;
3524}
3525
Bram Moolenaar8299df92004-07-10 09:47:34 +00003526/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003527 * Paste a yank register into the command line.
3528 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003529 * insert_reg() can't be used here, because special characters from the
3530 * register contents will be interpreted as commands.
3531 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003532 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003533 */
3534 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003535cmdline_paste(
3536 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003537 int literally, // Insert text literally instead of "as typed"
3538 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539{
3540 long i;
3541 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003542 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003544
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003545 // check for valid regname; also accept special characters for CTRL-R in
3546 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003548 && regname != Ctrl_A && regname != Ctrl_L
3549 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550 return FAIL;
3551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003552 // A register containing CTRL-R can cause an endless loop. Allow using
3553 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003554 line_breakcheck();
3555 if (got_int)
3556 return FAIL;
3557
3558#ifdef FEAT_CLIPBOARD
3559 regname = may_get_selection(regname);
3560#endif
3561
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003562 // Need to set "textwinlock" to avoid nasty things like going to another
Bram Moolenaar438d1762018-09-30 17:11:48 +02003563 // buffer when evaluating an expression.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003564 ++textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003565 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003566 --textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003567
3568 if (i)
3569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003570 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003571 if (arg == NULL)
3572 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003574 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3575 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003576 p = arg;
3577 if (p_is && regname == Ctrl_W)
3578 {
3579 char_u *w;
3580 int len;
3581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003582 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003583 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003584 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003585 if (has_mbyte)
3586 {
3587 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3588 if (!vim_iswordc(mb_ptr2char(w - len)))
3589 break;
3590 w -= len;
3591 }
3592 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003593 {
3594 if (!vim_iswordc(w[-1]))
3595 break;
3596 --w;
3597 }
3598 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003599 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003600 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3601 p += len;
3602 }
3603
3604 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003605 if (allocated)
3606 vim_free(arg);
3607 return OK;
3608 }
3609
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003610 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003611}
3612
3613/*
3614 * Put a string on the command line.
3615 * When "literally" is TRUE, insert literally.
3616 * When "literally" is FALSE, insert as typed, but don't leave the command
3617 * line.
3618 */
3619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003620cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003621{
3622 int c, cv;
3623
3624 if (literally)
3625 put_on_cmdline(s, -1, TRUE);
3626 else
3627 while (*s != NUL)
3628 {
3629 cv = *s;
3630 if (cv == Ctrl_V && s[1])
3631 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003632 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003633 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003634 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003635 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003636 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3637 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003638#ifdef UNIX
3639 || c == intr_char
3640#endif
3641 || (c == Ctrl_BSL && *s == Ctrl_N))
3642 stuffcharReadbuff(Ctrl_V);
3643 stuffcharReadbuff(c);
3644 }
3645}
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003648 * This function is called when the screen size changes and with incremental
3649 * search and in other situations where the command line may have been
3650 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
3652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003653redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003655 redrawcmdline_ex(TRUE);
3656}
3657
3658 void
3659redrawcmdline_ex(int do_compute_cmdrow)
3660{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (cmd_silent)
3662 return;
3663 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003664 if (do_compute_cmdrow)
3665 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 redrawcmd();
3667 cursorcmd();
3668}
3669
3670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003671redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 int i;
3674
3675 if (cmd_silent)
3676 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003677 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 msg_putchar(ccline.cmdfirstc);
3679 if (ccline.cmdprompt != NULL)
3680 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003681 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003683 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003684 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 --ccline.cmdindent;
3686 }
3687 else
3688 for (i = ccline.cmdindent; i > 0; --i)
3689 msg_putchar(' ');
3690}
3691
3692/*
3693 * Redraw what is currently on the command line.
3694 */
3695 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003696redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 if (cmd_silent)
3699 return;
3700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003701 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003702 if (ccline.cmdbuff == NULL)
3703 {
3704 windgoto(cmdline_row, 0);
3705 msg_clr_eos();
3706 return;
3707 }
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 msg_start();
3710 redrawcmdprompt();
3711
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003712 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 msg_no_more = TRUE;
3714 draw_cmdline(0, ccline.cmdlen);
3715 msg_clr_eos();
3716 msg_no_more = FALSE;
3717
3718 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003719 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003720 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 /*
3723 * An emsg() before may have set msg_scroll. This is used in normal mode,
3724 * in cmdline mode we can reset them now.
3725 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003726 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003728 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3729 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 skip_redraw = FALSE;
3731}
3732
3733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003734compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003736 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 cmdline_row = Rows - 1;
3738 else
3739 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003740 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741}
3742
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003744cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 if (cmd_silent)
3747 return;
3748
3749#ifdef FEAT_RIGHTLEFT
3750 if (cmdmsg_rl)
3751 {
3752 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3753 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3754 if (msg_row <= 0)
3755 msg_row = Rows - 1;
3756 }
3757 else
3758#endif
3759 {
3760 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3761 msg_col = ccline.cmdspos % (int)Columns;
3762 if (msg_row >= Rows)
3763 msg_row = Rows - 1;
3764 }
3765
3766 windgoto(msg_row, msg_col);
3767#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003768 if (p_imst == IM_ON_THE_SPOT)
3769 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770#endif
3771#ifdef MCH_CURSOR_SHAPE
3772 mch_update_cursor();
3773#endif
3774}
3775
3776 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003777gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 msg_start();
3780#ifdef FEAT_RIGHTLEFT
3781 if (cmdmsg_rl)
3782 msg_col = Columns - 1;
3783 else
3784#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003785 msg_col = 0; // always start in column 0
3786 if (clr) // clear the bottom line(s)
3787 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 windgoto(cmdline_row, 0);
3789}
3790
3791/*
3792 * Check the word in front of the cursor for an abbreviation.
3793 * Called when the non-id character "c" has been entered.
3794 * When an abbreviation is recognized it is removed from the text with
3795 * backspaces and the replacement string is inserted, followed by "c".
3796 */
3797 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003798ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003800 int spos = 0;
3801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003802 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return FALSE;
3804
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003805 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3806 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003807 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3808 spos++;
3809 if (ccline.cmdlen - spos > 5
3810 && ccline.cmdbuff[spos] == '\''
3811 && ccline.cmdbuff[spos + 2] == ','
3812 && ccline.cmdbuff[spos + 3] == '\'')
3813 spos += 5;
3814 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003815 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003816 spos = 0;
3817
3818 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819}
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003822 * Escape special characters in "fname" for when used as a file name argument
3823 * after a Vim command, or, when "shell" is non-zero, a shell command.
3824 * Returns the result in allocated memory.
3825 */
3826 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003827vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003829 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003830#ifdef BACKSLASH_IN_FILENAME
3831 char_u buf[20];
3832 int j = 0;
3833
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003834 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003835 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003836 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003837 buf[j++] = *p;
3838 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003839 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003840#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003841 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3842 if (shell && csh_like_shell() && p != NULL)
3843 {
3844 char_u *s;
3845
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003846 // For csh and similar shells need to put two backslashes before '!'.
3847 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003848 s = vim_strsave_escaped(p, (char_u *)"!");
3849 vim_free(p);
3850 p = s;
3851 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003852#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3855 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003856 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003857 escape_fname(&p);
3858
3859 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003860}
3861
3862/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003863 * Put a backslash before the file name in "pp", which is in allocated memory.
3864 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003865 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003866escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003867{
3868 char_u *p;
3869
Bram Moolenaar964b3742019-05-24 18:54:09 +02003870 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003871 if (p != NULL)
3872 {
3873 p[0] = '\\';
3874 STRCPY(p + 1, *pp);
3875 vim_free(*pp);
3876 *pp = p;
3877 }
3878}
3879
3880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 * For each file name in files[num_files]:
3882 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3883 */
3884 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003885tilde_replace(
3886 char_u *orig_pat,
3887 int num_files,
3888 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 int i;
3891 char_u *p;
3892
3893 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3894 {
3895 for (i = 0; i < num_files; ++i)
3896 {
3897 p = home_replace_save(NULL, files[i]);
3898 if (p != NULL)
3899 {
3900 vim_free(files[i]);
3901 files[i] = p;
3902 }
3903 }
3904 }
3905}
3906
3907/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003908 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003910 cmdline_info_T *
3911get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003913 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914}
3915
Bram Moolenaar064154c2016-03-19 22:50:43 +01003916#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003917/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003918 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003919 * ccline and put the previous value in prev_ccline.
3920 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003921 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003922get_ccline_ptr(void)
3923{
3924 if ((State & CMDLINE) == 0)
3925 return NULL;
3926 if (ccline.cmdbuff != NULL)
3927 return &ccline;
3928 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3929 return &prev_ccline;
3930 return NULL;
3931}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003932#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003933
Bram Moolenaar064154c2016-03-19 22:50:43 +01003934#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003935/*
3936 * Get the current command line in allocated memory.
3937 * Only works when the command line is being edited.
3938 * Returns NULL when something is wrong.
3939 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003940 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003941get_cmdline_str(void)
3942{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003943 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003944
Bram Moolenaaree91c332018-09-25 22:27:35 +02003945 if (cmdline_star > 0)
3946 return NULL;
3947 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003948 if (p == NULL)
3949 return NULL;
3950 return vim_strnsave(p->cmdbuff, p->cmdlen);
3951}
3952
3953/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003954 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003955 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003956 void
3957f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3958{
3959 rettv->v_type = VAR_STRING;
3960 rettv->vval.v_string = get_cmdline_str();
3961}
3962
3963/*
3964 * "getcmdpos()" function
3965 */
3966 void
3967f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003968{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003969 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003970
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003971 rettv->vval.v_number = 0;
3972 if (p != NULL)
3973 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003974}
3975
3976/*
3977 * Set the command line byte position to "pos". Zero is the first position.
3978 * Only works when the command line is being edited.
3979 * Returns 1 when failed, 0 when OK.
3980 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003981 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003982set_cmdline_pos(
3983 int pos)
3984{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003985 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003986
3987 if (p == NULL)
3988 return 1;
3989
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003990 // The position is not set directly but after CTRL-\ e or CTRL-R = has
3991 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003992 if (pos < 0)
3993 new_cmdpos = 0;
3994 else
3995 new_cmdpos = pos;
3996 return 0;
3997}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003998
3999/*
4000 * "setcmdpos()" function
4001 */
4002 void
4003f_setcmdpos(typval_T *argvars, typval_T *rettv)
4004{
4005 int pos = (int)tv_get_number(&argvars[0]) - 1;
4006
4007 if (pos >= 0)
4008 rettv->vval.v_number = set_cmdline_pos(pos);
4009}
4010
4011/*
4012 * "getcmdtype()" function
4013 */
4014 void
4015f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4016{
4017 rettv->v_type = VAR_STRING;
4018 rettv->vval.v_string = alloc(2);
4019 if (rettv->vval.v_string != NULL)
4020 {
4021 rettv->vval.v_string[0] = get_cmdline_type();
4022 rettv->vval.v_string[1] = NUL;
4023 }
4024}
4025
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004026#endif
4027
4028#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4029/*
4030 * Get the current command-line type.
4031 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4032 * Only works when the command line is being edited.
4033 * Returns NUL when something is wrong.
4034 */
4035 int
4036get_cmdline_type(void)
4037{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004038 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004039
4040 if (p == NULL)
4041 return NUL;
4042 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004043 return
4044# ifdef FEAT_EVAL
4045 (p->input_fn) ? '@' :
4046# endif
4047 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004048 return p->cmdfirstc;
4049}
4050#endif
4051
Bram Moolenaard7663c22019-08-06 21:59:57 +02004052/*
4053 * Return the first character of the current command line.
4054 */
4055 int
4056get_cmdline_firstc(void)
4057{
4058 return ccline.cmdfirstc;
4059}
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061/*
4062 * Get indices "num1,num2" that specify a range within a list (not a range of
4063 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4064 * Returns OK if parsed successfully, otherwise FAIL.
4065 */
4066 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004067get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 int len;
4070 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004071 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004074 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004076 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 *str += len;
4078 *num1 = (int)num;
4079 first = TRUE;
4080 }
4081 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004082 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
4084 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004085 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (len > 0)
4087 {
4088 *num2 = (int)num;
4089 *str = skipwhite(*str + len);
4090 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004091 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return FAIL;
4093 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004094 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 *num2 = *num1;
4096 return OK;
4097}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#if defined(FEAT_CMDWIN) || defined(PROTO)
4100/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004101 * Check value of 'cedit' and set cedit_key.
4102 * Returns NULL if value is OK, error message otherwise.
4103 */
4104 char *
4105check_cedit(void)
4106{
4107 int n;
4108
4109 if (*p_cedit == NUL)
4110 cedit_key = -1;
4111 else
4112 {
4113 n = string_to_key(p_cedit, FALSE);
4114 if (vim_isprintc(n))
4115 return e_invarg;
4116 cedit_key = n;
4117 }
4118 return NULL;
4119}
4120
4121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Open a window on the current command line and history. Allow editing in
4123 * the window. Returns when the window is closed.
4124 * Returns:
4125 * CR if the command is to be executed
4126 * Ctrl_C if it is to be abandoned
4127 * K_IGNORE if editing continues
4128 */
4129 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004130open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004132 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004134 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 win_T *wp;
4136 int i;
4137 linenr_T lnum;
4138 int histtype;
4139 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int save_restart_edit = restart_edit;
4141 int save_State = State;
4142 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004143#ifdef FEAT_RIGHTLEFT
4144 int save_cmdmsg_rl = cmdmsg_rl;
4145#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004146#ifdef FEAT_FOLDING
4147 int save_KeyTyped;
4148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 if (cmdwin_type != 0
4152# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4153 || cmdline_star > 0
4154# endif
4155 )
4156 {
4157 beep_flush();
4158 return K_IGNORE;
4159 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004160 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004162 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 win_size_save(&winsizes);
4164
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004165 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4166 // command line window, but we don't want the popup menu then.
4167 pum_undisplay();
4168
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004169 // don't use a new tab page
Bram Moolenaare1004402020-10-24 20:49:43 +02004170 cmdmod.cmod_tab = 0;
4171 cmdmod.cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004172
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004173 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4175 {
4176 beep_flush();
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004177 ga_clear(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return K_IGNORE;
4179 }
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004180 // Don't let quitting the More prompt make this fail.
4181 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004183 // Create the command-line buffer empty.
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004184 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL) == FAIL)
4185 {
4186 // Some autocommand messed it up?
4187 win_close(curwin, TRUE);
4188 ga_clear(&winsizes);
4189 return Ctrl_C;
4190 }
4191 cmdwin_type = get_cmdline_type();
4192
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004193 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004194 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004195 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004198#ifdef FEAT_FOLDING
4199 curwin->w_p_fen = FALSE;
4200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004202 curwin->w_p_rl = cmdmsg_rl;
4203 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004205 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004207 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004208 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004210 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004211 need_wait_return = FALSE;
4212
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004213 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4215 {
4216 if (p_wc == TAB)
4217 {
4218 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4219 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4220 }
4221 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4222 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004223 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004225 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4226 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004227 curbuf->b_p_tw = 0;
4228
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004229 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004231 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004233 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (i >= 0)
4235 {
4236 lnum = 0;
4237 do
4238 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004239 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004241 if (get_histentry(histtype)[i].hisstr != NULL)
4242 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 (colnr_T)0, FALSE);
4244 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004245 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 }
4248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004249 // Replace the empty last line with the current command-line and put the
4250 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4252 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4253 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004254 changed_line_abv_curs();
4255 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004256 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaar23324a02019-10-01 17:39:04 +02004258 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 exmode_active = 0;
4260
4261 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Bram Moolenaar23324a02019-10-01 17:39:04 +02004264 // Reset here so it can be set by a CmdWinEnter autocommand.
4265 cmdwin_result = 0;
4266
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004267 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004268 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004269 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004270 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 i = RedrawingDisabled;
4273 RedrawingDisabled = 0;
4274
4275 /*
4276 * Call the main loop until <CR> or CTRL-C is typed.
4277 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004278 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 RedrawingDisabled = i;
4281
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004282# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004283 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004284# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004285
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004286 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004287 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004288
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004289# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004290 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004291 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292# endif
4293
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 exmode_active = save_exmode;
4296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004297 // Safety check: The old window or buffer was deleted: It's a bug when
4298 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004299 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004302 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 else
4305 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004306# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 if (aborting() && cmdwin_result != K_IGNORE)
4309 cmdwin_result = Ctrl_C;
4310# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004311 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004313 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004315 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4316
4317 if (histtype == HIST_CMD)
4318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004320 ccline.cmdbuff = vim_strsave((char_u *)p);
4321 cmdwin_result = CAR;
4322 }
4323 else
4324 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004325 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004326 ccline.cmdbuff = NULL;
4327 stuffcharReadbuff(':');
4328 stuffReadbuff((char_u *)p);
4329 stuffcharReadbuff(CAR);
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 {
4334 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4335 cmdwin_result = CAR;
4336 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004337 else if (cmdwin_result == Ctrl_C)
4338 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // :q or :close, don't execute any command
4340 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004341 ccline.cmdbuff = NULL;
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 else
4344 ccline.cmdbuff = vim_strsave(ml_get_curline());
4345 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004346 {
4347 ccline.cmdbuff = vim_strsave((char_u *)"");
4348 ccline.cmdlen = 0;
4349 ccline.cmdbufflen = 1;
4350 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 else
4354 {
4355 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4356 ccline.cmdbufflen = ccline.cmdlen + 1;
4357 ccline.cmdpos = curwin->w_cursor.col;
4358 if (ccline.cmdpos > ccline.cmdlen)
4359 ccline.cmdpos = ccline.cmdlen;
4360 if (cmdwin_result == K_IGNORE)
4361 {
4362 set_cmdspos_cursor();
4363 redrawcmd();
4364 }
4365 }
4366
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004367# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004368 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004369 curwin->w_p_cole = 0;
4370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004372 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 win_goto(old_curwin);
4374 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004375
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004376 // win_close() may have already wiped the buffer when 'bh' is
4377 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004378 if (bufref_valid(&bufref))
Bram Moolenaara6e8f882019-12-14 16:18:15 +01004379 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004381 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384
4385 ga_clear(&winsizes);
4386 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004387# ifdef FEAT_RIGHTLEFT
4388 cmdmsg_rl = save_cmdmsg_rl;
4389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 return cmdwin_result;
4395}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004396#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398/*
4399 * Used for commands that either take a simple command string argument, or:
4400 * cmd << endmarker
4401 * {script}
4402 * endmarker
4403 * Returns a pointer to allocated memory with {script} or NULL.
4404 */
4405 char_u *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004406script_get(exarg_T *eap UNUSED, char_u *cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004408#ifdef FEAT_EVAL
4409 list_T *l;
4410 listitem_T *li;
4411 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 garray_T ga;
4413
4414 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4415 return NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004416 cmd += 2;
4417
4418 l = heredoc_get(eap, cmd, TRUE);
4419 if (l == NULL)
4420 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 ga_init2(&ga, 1, 0x400);
4423
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004424 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004426 s = tv_get_string(&li->li_tv);
4427 ga_concat(&ga, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004430 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004432 list_free(l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 return (char_u *)ga.ga_data;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004434#else
4435 return NULL;
4436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004438
4439#if defined(FEAT_EVAL) || defined(PROTO)
4440/*
4441 * This function is used by f_input() and f_inputdialog() functions. The third
4442 * argument to f_input() specifies the type of completion to use at the
4443 * prompt. The third argument to f_inputdialog() specifies the value to return
4444 * when the user cancels the prompt.
4445 */
4446 void
4447get_user_input(
4448 typval_T *argvars,
4449 typval_T *rettv,
4450 int inputdialog,
4451 int secret)
4452{
4453 char_u *prompt = tv_get_string_chk(&argvars[0]);
4454 char_u *p = NULL;
4455 int c;
4456 char_u buf[NUMBUFLEN];
4457 int cmd_silent_save = cmd_silent;
4458 char_u *defstr = (char_u *)"";
4459 int xp_type = EXPAND_NOTHING;
4460 char_u *xp_arg = NULL;
4461
4462 rettv->v_type = VAR_STRING;
4463 rettv->vval.v_string = NULL;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004464 if (input_busy)
4465 return; // this doesn't work recursively.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004466
4467#ifdef NO_CONSOLE_INPUT
4468 // While starting up, there is no place to enter text. When running tests
4469 // with --not-a-term we assume feedkeys() will be used.
4470 if (no_console_input() && !is_not_a_term())
4471 return;
4472#endif
4473
4474 cmd_silent = FALSE; // Want to see the prompt.
4475 if (prompt != NULL)
4476 {
4477 // Only the part of the message after the last NL is considered as
4478 // prompt for the command line
4479 p = vim_strrchr(prompt, '\n');
4480 if (p == NULL)
4481 p = prompt;
4482 else
4483 {
4484 ++p;
4485 c = *p;
4486 *p = NUL;
4487 msg_start();
4488 msg_clr_eos();
4489 msg_puts_attr((char *)prompt, get_echo_attr());
4490 msg_didout = FALSE;
4491 msg_starthere();
4492 *p = c;
4493 }
4494 cmdline_row = msg_row;
4495
4496 if (argvars[1].v_type != VAR_UNKNOWN)
4497 {
4498 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4499 if (defstr != NULL)
4500 stuffReadbuffSpec(defstr);
4501
4502 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4503 {
4504 char_u *xp_name;
4505 int xp_namelen;
4506 long argt;
4507
4508 // input() with a third argument: completion
4509 rettv->vval.v_string = NULL;
4510
4511 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4512 if (xp_name == NULL)
4513 return;
4514
4515 xp_namelen = (int)STRLEN(xp_name);
4516
4517 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4518 &xp_arg) == FAIL)
4519 return;
4520 }
4521 }
4522
4523 if (defstr != NULL)
4524 {
4525 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004526 int save_vgetc_busy = vgetc_busy;
4527 int save_input_busy = input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004528
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004529 input_busy |= vgetc_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004530 ex_normal_busy = 0;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004531 vgetc_busy = 0;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004532 rettv->vval.v_string =
4533 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4534 xp_type, xp_arg);
4535 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004536 vgetc_busy = save_vgetc_busy;
4537 input_busy = save_input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004538 }
4539 if (inputdialog && rettv->vval.v_string == NULL
4540 && argvars[1].v_type != VAR_UNKNOWN
4541 && argvars[2].v_type != VAR_UNKNOWN)
4542 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4543 &argvars[2], buf));
4544
4545 vim_free(xp_arg);
4546
4547 // since the user typed this, no need to wait for return
4548 need_wait_return = FALSE;
4549 msg_didout = FALSE;
4550 }
4551 cmd_silent = cmd_silent_save;
4552}
4553#endif