blob: eb922a1c4dc35298eb7a31cc5002a18de6b948c8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar438d1762018-09-30 17:11:48 +020020// The current cmdline_info. It is initialized in getcmdline() and after that
21// used by other functions. When invoking getcmdline() recursively it needs
22// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int new_cmdpos; // position set by set_cmdline_pos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
Bram Moolenaar217e1b82019-12-01 21:41:28 +010029static int extra_char = NUL; // extra character to display when redrawing
30 // the command line
Bram Moolenaar6a77d262017-07-16 15:24:01 +020031static int extra_char_shift;
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +010034static int cmd_hkmap = 0; // Hebrew mapping during command line
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
Bram Moolenaar438d1762018-09-30 17:11:48 +020037static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010038static int cmdline_charsize(int idx);
39static void set_cmdspos(void);
40static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020044static void save_cmdline(cmdline_info_T *ccp);
45static void restore_cmdline(cmdline_info_T *ccp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010047static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static int ccheck_abbr(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
50#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +020051static int open_cmdwin(void);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +010052
53static int cedit_key INIT(= -1); // key value of 'cedit' option
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#endif
55
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020056
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020057 static void
58trigger_cmd_autocmd(int typechar, int evt)
59{
60 char_u typestr[2];
61
62 typestr[0] = typechar;
63 typestr[1] = NUL;
64 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
65}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020066
Bram Moolenaar071d4272004-06-13 20:20:40 +000067/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020068 * Abandon the command line.
69 */
70 static void
71abandon_cmdline(void)
72{
Bram Moolenaard23a8232018-02-10 18:45:26 +010073 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020074 if (msg_scrolled == 0)
75 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010076 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020077 redraw_cmdline = TRUE;
78}
79
Bram Moolenaaree219b02017-12-17 14:55:01 +010080#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020081/*
Bram Moolenaar66216052017-12-16 16:33:44 +010082 * Guess that the pattern matches everything. Only finds specific cases, such
83 * as a trailing \|, which can happen while typing a pattern.
84 */
85 static int
86empty_pattern(char_u *p)
87{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010088 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010089
Bram Moolenaar217e1b82019-12-01 21:41:28 +010090 // remove trailing \v and the like
Bram Moolenaar66216052017-12-16 16:33:44 +010091 while (n >= 2 && p[n - 2] == '\\'
92 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
93 n -= 2;
94 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
95}
96
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +020097// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +020098typedef struct {
99 colnr_T vs_curswant;
100 colnr_T vs_leftcol;
101 linenr_T vs_topline;
102# ifdef FEAT_DIFF
103 int vs_topfill;
104# endif
105 linenr_T vs_botline;
106 linenr_T vs_empty_rows;
107} viewstate_T;
108
109 static void
110save_viewstate(viewstate_T *vs)
111{
112 vs->vs_curswant = curwin->w_curswant;
113 vs->vs_leftcol = curwin->w_leftcol;
114 vs->vs_topline = curwin->w_topline;
115# ifdef FEAT_DIFF
116 vs->vs_topfill = curwin->w_topfill;
117# endif
118 vs->vs_botline = curwin->w_botline;
119 vs->vs_empty_rows = curwin->w_empty_rows;
120}
121
122 static void
123restore_viewstate(viewstate_T *vs)
124{
125 curwin->w_curswant = vs->vs_curswant;
126 curwin->w_leftcol = vs->vs_leftcol;
127 curwin->w_topline = vs->vs_topline;
128# ifdef FEAT_DIFF
129 curwin->w_topfill = vs->vs_topfill;
130# endif
131 curwin->w_botline = vs->vs_botline;
132 curwin->w_empty_rows = vs->vs_empty_rows;
133}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200134
135// Struct to store the state of 'incsearch' highlighting.
136typedef struct {
137 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200138 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200139 viewstate_T init_viewstate;
140 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200141 pos_T match_start;
142 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200143 int did_incsearch;
144 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200145 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200146} incsearch_state_T;
147
148 static void
149init_incsearch_state(incsearch_state_T *is_state)
150{
151 is_state->match_start = curwin->w_cursor;
152 is_state->did_incsearch = FALSE;
153 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200154 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200155 CLEAR_POS(&is_state->match_end);
156 is_state->save_cursor = curwin->w_cursor; // may be restored later
157 is_state->search_start = curwin->w_cursor;
158 save_viewstate(&is_state->init_viewstate);
159 save_viewstate(&is_state->old_viewstate);
160}
161
162/*
163 * First move cursor to end of match, then to the start. This
164 * moves the whole match onto the screen when 'nowrap' is set.
165 */
166 static void
167set_search_match(pos_T *t)
168{
169 t->lnum += search_match_lines;
170 t->col = search_match_endcol;
171 if (t->lnum > curbuf->b_ml.ml_line_count)
172 {
173 t->lnum = curbuf->b_ml.ml_line_count;
174 coladvance((colnr_T)MAXCOL);
175 }
176}
177
178/*
179 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200180 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200181 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200182 */
183 static int
Bram Moolenaarc036e872020-02-21 21:30:52 +0100184do_incsearch_highlighting(int firstc, int *search_delim, incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200185 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200187 char_u *cmd;
188 cmdmod_T save_cmdmod = cmdmod;
189 char_u *p;
190 int delim_optional = FALSE;
191 int delim;
192 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100193 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200194 exarg_T ea;
195 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200196 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100197 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200198
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200199 *skiplen = 0;
200 *patlen = ccline.cmdlen;
201
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200202 if (!p_is || cmd_silent)
203 return FALSE;
204
205 // by default search all lines
206 search_first_line = 0;
207 search_last_line = MAXLNUM;
208
209 if (firstc == '/' || firstc == '?')
Bram Moolenaarc036e872020-02-21 21:30:52 +0100210 {
211 *search_delim = firstc;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200212 return TRUE;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100213 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200214 if (firstc != ':')
215 return FALSE;
216
Bram Moolenaarc6725252019-11-23 21:56:46 +0100217 ++emsg_off;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200218 CLEAR_FIELD(ea);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200219 ea.line1 = 1;
220 ea.line2 = 1;
221 ea.cmd = ccline.cmdbuff;
222 ea.addr_type = ADDR_LINES;
223
224 parse_command_modifiers(&ea, &dummy, TRUE);
225 cmdmod = save_cmdmod;
226
227 cmd = skip_range(ea.cmd, NULL);
228 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100229 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200230
231 // Skip over "substitute" to find the pattern separator.
232 for (p = cmd; ASCII_ISALPHA(*p); ++p)
233 ;
234 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100235 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
237 if (STRNCMP(cmd, "substitute", p - cmd) == 0
238 || STRNCMP(cmd, "smagic", p - cmd) == 0
239 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
240 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200241 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200242 if (*cmd == 's' && cmd[1] == 'm')
243 p_magic = TRUE;
244 else if (*cmd == 's' && cmd[1] == 'n')
245 p_magic = FALSE;
246 }
247 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
248 {
Bram Moolenaar333015a2020-04-25 15:54:16 +0200249 // skip over ! and flags
250 if (*p == '!')
251 p = skipwhite(p + 1);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200252 while (ASCII_ISALPHA(*(p = skipwhite(p))))
253 ++p;
254 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100255 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200256 }
257 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
258 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
259 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
260 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
261 || STRNCMP(cmd, "global", p - cmd) == 0)
262 {
263 // skip over "!"
264 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200265 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200266 p++;
267 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100268 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200269 }
270 if (*cmd != 'g')
271 delim_optional = TRUE;
272 }
273 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100274 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200275
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276 p = skipwhite(p);
277 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100278 *search_delim = delim;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +0200279 end = skip_regexp(p, delim, p_magic);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200280
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200281 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200282
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200283 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100284 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200285
286 // Don't do 'hlsearch' highlighting if the pattern matches everything.
287 if (!use_last_pat)
288 {
289 char c = *end;
290 int empty;
291
292 *end = NUL;
293 empty = empty_pattern(p);
294 *end = c;
295 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100296 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200297 }
298
299 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200300 *skiplen = (int)(p - ccline.cmdbuff);
301 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200302
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200303 // parse the address range
304 save_cursor = curwin->w_cursor;
305 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200306 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200307 if (ea.addr_count > 0)
308 {
309 // Allow for reverse match.
310 if (ea.line2 < ea.line1)
311 {
312 search_first_line = ea.line2;
313 search_last_line = ea.line1;
314 }
315 else
316 {
317 search_first_line = ea.line1;
318 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200319 }
320 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200321 else if (cmd[0] == 's' && cmd[1] != 'o')
322 {
323 // :s defaults to the current line
324 search_first_line = curwin->w_cursor.lnum;
325 search_last_line = curwin->w_cursor.lnum;
326 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200327
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200328 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100329 retval = TRUE;
330theend:
331 --emsg_off;
332 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200333}
334
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200335 static void
336finish_incsearch_highlighting(
337 int gotesc,
338 incsearch_state_T *is_state,
339 int call_update_screen)
340{
341 if (is_state->did_incsearch)
342 {
343 is_state->did_incsearch = FALSE;
344 if (gotesc)
345 curwin->w_cursor = is_state->save_cursor;
346 else
347 {
348 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
349 {
350 // put the '" mark at the original position
351 curwin->w_cursor = is_state->save_cursor;
352 setpcmark();
353 }
354 curwin->w_cursor = is_state->search_start;
355 }
356 restore_viewstate(&is_state->old_viewstate);
357 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200358
359 // by default search all lines
360 search_first_line = 0;
361 search_last_line = MAXLNUM;
362
363 p_magic = is_state->magic_save;
364
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100365 validate_cursor(); // needed for TAB
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200366 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200367 if (call_update_screen)
368 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200369 }
370}
371
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200372/*
373 * Do 'incsearch' highlighting if desired.
374 */
375 static void
376may_do_incsearch_highlighting(
377 int firstc,
378 long count,
379 incsearch_state_T *is_state)
380{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200381 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200382 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200383 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384#ifdef FEAT_RELTIME
385 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200386 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200387#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200388 int next_char;
389 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200390 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100391 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200392
Bram Moolenaar198cb662018-09-06 21:44:17 +0200393 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100394 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200395 save_last_search_pattern();
396
Bram Moolenaara60053b2020-09-03 16:50:13 +0200397 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
398 &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200399 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200400 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200401 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200402 if (did_do_incsearch && vpeekc() == NUL)
403 // may have skipped a redraw, do it now
404 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200405 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200406 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407
408 // If there is a character waiting, search and redraw later.
409 if (char_avail())
410 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200411 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200412 is_state->incsearch_postponed = TRUE;
413 return;
414 }
415 is_state->incsearch_postponed = FALSE;
416
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 if (search_first_line == 0)
418 // start at the original cursor position
419 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100420 else if (search_first_line > curbuf->b_ml.ml_line_count)
421 {
422 // start after the last line
423 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
424 curwin->w_cursor.col = MAXCOL;
425 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200426 else
427 {
428 // start at the first line in the range
429 curwin->w_cursor.lnum = search_first_line;
430 curwin->w_cursor.col = 0;
431 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200432
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200433 // Use the previous pattern for ":s//".
434 next_char = ccline.cmdbuff[skiplen + patlen];
435 use_last_pat = patlen == 0 && skiplen > 0
436 && ccline.cmdbuff[skiplen - 1] == next_char;
437
438 // If there is no pattern, don't do anything.
439 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200440 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200441 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200442 set_no_hlsearch(TRUE); // turn off previous highlight
443 redraw_all_later(SOME_VALID);
444 }
445 else
446 {
447 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
448
449 cursor_off(); // so the user knows we're busy
450 out_flush();
451 ++emsg_off; // so it doesn't beep if bad expr
452#ifdef FEAT_RELTIME
453 // Set the time limit to half a second.
454 profile_setlimit(500L, &tm);
455#endif
456 if (!p_hls)
457 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200458 if (search_first_line != 0)
459 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200460 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200461#ifdef FEAT_RELTIME
Bram Moolenaara80faa82020-04-12 19:37:17 +0200462 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200463 sia.sa_tm = &tm;
464#endif
Bram Moolenaarc036e872020-02-21 21:30:52 +0100465 found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200467#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200468 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200470 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200471#endif
472 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200473 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200474 --emsg_off;
475
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200476 if (curwin->w_cursor.lnum < search_first_line
477 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200478 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200479 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200480 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200481 curwin->w_cursor = is_state->search_start;
482 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200484 // if interrupted while searching, behave like it failed
485 if (got_int)
486 {
487 (void)vpeekc(); // remove <C-C> from input stream
488 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200489 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 }
491 else if (char_avail())
492 // cancelled searching because a char was typed
493 is_state->incsearch_postponed = TRUE;
494 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200495 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200496 highlight_match = TRUE; // highlight position
497 else
498 highlight_match = FALSE; // remove highlight
499
500 // First restore the old curwin values, so the screen is positioned in the
501 // same way as the actual search command.
502 restore_viewstate(&is_state->old_viewstate);
503 changed_cline_bef_curs();
504 update_topline();
505
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200506 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200507 {
508 pos_T save_pos = curwin->w_cursor;
509
510 is_state->match_start = curwin->w_cursor;
511 set_search_match(&curwin->w_cursor);
512 validate_cursor();
513 end_pos = curwin->w_cursor;
514 is_state->match_end = end_pos;
515 curwin->w_cursor = save_pos;
516 }
517 else
518 end_pos = curwin->w_cursor; // shutup gcc 4
519
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200520 // Disable 'hlsearch' highlighting if the pattern matches everything.
521 // Avoids a flash when typing "foo\|".
522 if (!use_last_pat)
523 {
524 next_char = ccline.cmdbuff[skiplen + patlen];
525 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200526 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
527 {
528 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200529 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200530 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200531 ccline.cmdbuff[skiplen + patlen] = next_char;
532 }
533
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 validate_cursor();
535 // May redraw the status line to show the cursor position.
536 if (p_ru && curwin->w_status_height > 0)
537 curwin->w_redr_status = TRUE;
538
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200540 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200541 restore_last_search_pattern();
542
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200543 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
544 // end of the pattern, e.g. for ":s/pat/".
545 if (ccline.cmdbuff[skiplen + patlen] != NUL)
546 curwin->w_cursor = is_state->search_start;
547 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200548 curwin->w_cursor = end_pos;
549
550 msg_starthere();
551 redrawcmdline();
552 is_state->did_incsearch = TRUE;
553}
554
555/*
556 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
557 * or previous match.
558 * Returns FAIL when jumping to cmdline_not_changed;
559 */
560 static int
561may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int firstc,
563 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200565 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200566{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568 pos_T t;
569 char_u *pat;
570 int search_flags = SEARCH_NOOF;
571 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200572 int save;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100573 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574
Bram Moolenaar198cb662018-09-06 21:44:17 +0200575 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100576 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200577 save_last_search_pattern();
578
Bram Moolenaarc036e872020-02-21 21:30:52 +0100579 if (!do_incsearch_highlighting(firstc, &search_delim, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200580 {
581 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200582 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200583 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200585 {
586 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200588 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200589
Bram Moolenaarc036e872020-02-21 21:30:52 +0100590 if (search_delim == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200591 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200593 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200594 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200595 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200597 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 cursor_off();
600 out_flush();
601 if (c == Ctrl_G)
602 {
603 t = is_state->match_end;
604 if (LT_POS(is_state->match_start, is_state->match_end))
605 // Start searching at the end of the match not at the beginning of
606 // the next column.
607 (void)decl(&t);
608 search_flags += SEARCH_COL;
609 }
610 else
611 t = is_state->match_start;
612 if (!p_hls)
613 search_flags += SEARCH_KEEP;
614 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 save = pat[patlen];
616 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100617 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200618 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200619 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622 if (i)
623 {
624 is_state->search_start = is_state->match_start;
625 is_state->match_end = t;
626 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200627 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628 {
629 // Move just before the current match, so that when nv_search
630 // finishes the cursor will be put back on the match.
631 is_state->search_start = t;
632 (void)decl(&is_state->search_start);
633 }
634 else if (c == Ctrl_G && firstc == '?')
635 {
636 // Move just after the current match, so that when nv_search
637 // finishes the cursor will be put back on the match.
638 is_state->search_start = t;
639 (void)incl(&is_state->search_start);
640 }
641 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
642 {
643 // wrap around
644 is_state->search_start = t;
645 if (firstc == '?')
646 (void)incl(&is_state->search_start);
647 else
648 (void)decl(&is_state->search_start);
649 }
650
651 set_search_match(&is_state->match_end);
652 curwin->w_cursor = is_state->match_start;
653 changed_cline_bef_curs();
654 update_topline();
655 validate_cursor();
656 highlight_match = TRUE;
657 save_viewstate(&is_state->old_viewstate);
658 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200659 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200661 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200662 }
663 else
664 vim_beep(BO_ERROR);
665 restore_last_search_pattern();
666 return FAIL;
667}
668
669/*
670 * When CTRL-L typed: add character from the match to the pattern.
671 * May set "*c" to the added character.
672 * Return OK when jumping to cmdline_not_changed.
673 */
674 static int
675may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
676{
Bram Moolenaarc036e872020-02-21 21:30:52 +0100677 int skiplen, patlen, search_delim;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200678
Bram Moolenaar198cb662018-09-06 21:44:17 +0200679 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100680 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200681 save_last_search_pattern();
682
Bram Moolenaar9b7cce22020-06-06 15:14:08 +0200683 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
684 &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200685 {
686 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200687 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200688 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100689 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690
691 // Add a character from under the cursor for 'incsearch'.
692 if (is_state->did_incsearch)
693 {
694 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200695 *c = gchar_cursor();
696 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200697 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200698 // If 'ignorecase' and 'smartcase' are set and the
699 // command line has no uppercase characters, convert
700 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200701 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200702 *c = MB_TOLOWER(*c);
Bram Moolenaarc036e872020-02-21 21:30:52 +0100703 if (*c == search_delim || vim_strchr((char_u *)(
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200704 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200705 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200706 // put a backslash before special characters
707 stuffcharReadbuff(*c);
708 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200709 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200710 // add any composing characters
711 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
712 {
713 int save_c = *c;
714
715 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
716 {
717 curwin->w_cursor.col += mb_char2len(*c);
718 *c = gchar_cursor();
719 stuffcharReadbuff(*c);
720 }
721 *c = save_c;
722 }
723 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200724 }
725 }
726 return OK;
727}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200728#endif
729
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200730#ifdef FEAT_ARABIC
731/*
732 * Return TRUE if the command line has an Arabic character at or after "start"
733 * for "len" bytes.
734 */
735 static int
736cmdline_has_arabic(int start, int len)
737{
738 int j;
739 int mb_l;
740 int u8c;
741 char_u *p;
742 int u8cc[MAX_MCO];
743
744 if (!enc_utf8)
745 return FALSE;
746
747 for (j = start; j < start + len; j += mb_l)
748 {
749 p = ccline.cmdbuff + j;
750 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
751 mb_l = utfc_ptr2len_len(p, start + len - j);
752 if (ARABIC_CHAR(u8c))
753 return TRUE;
754 }
755 return FALSE;
756}
757#endif
758
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200759 void
760cmdline_init(void)
761{
Bram Moolenaara80faa82020-04-12 19:37:17 +0200762 CLEAR_FIELD(ccline);
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200763}
764
Bram Moolenaar66216052017-12-16 16:33:44 +0100765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * getcmdline() - accept a command line starting with firstc.
767 *
768 * firstc == ':' get ":" command line.
769 * firstc == '/' or '?' get search pattern
770 * firstc == '=' get expression
771 * firstc == '@' get text for input() function
772 * firstc == '>' get text for debug mode
773 * firstc == NUL get text for :insert command
774 * firstc == -1 like NUL, and break on CTRL-C
775 *
776 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
777 * command line.
778 *
779 * Careful: getcmdline() can be called recursively!
780 *
781 * Return pointer to allocated string if there is a commandline, NULL
782 * otherwise.
783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100785getcmdline(
786 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200787 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200788 int indent, // indent for inside conditionals
789 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200790{
791 return getcmdline_int(firstc, count, indent, TRUE);
792}
793
794 static char_u *
795getcmdline_int(
796 int firstc,
797 long count UNUSED, // only used for incremental search
798 int indent, // indent for inside conditionals
799 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 int c;
802 int i;
803 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 int gotesc = FALSE; // TRUE when <ESC> just typed
805 int do_abbr; // when TRUE check for abbr.
806 char_u *lookfor = NULL; // string to match
807 int hiscnt; // current history line in use
808 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200810 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 int did_wild_list = FALSE; // did wild_list() recently
813 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 int res;
815 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 int save_State = State; // remember State when called
817 int some_key_typed = FALSE; // one of the keys was typed
818 // mouse drag and release events are ignored, unless they are
819 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#ifdef FEAT_EVAL
822 int break_ctrl_c = FALSE;
823#endif
824 expand_T xpc;
825 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200826 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200827 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200828 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar438d1762018-09-30 17:11:48 +0200830 if (ccline.cmdbuff != NULL)
831 {
832 // Being called recursively. Since ccline is global, we need to save
833 // the current buffer and restore it when returning.
834 save_cmdline(&save_ccline);
835 did_save_ccline = TRUE;
836 }
837 if (init_ccline)
Bram Moolenaara80faa82020-04-12 19:37:17 +0200838 CLEAR_FIELD(ccline);
Bram Moolenaar438d1762018-09-30 17:11:48 +0200839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#ifdef FEAT_EVAL
841 if (firstc == -1)
842 {
843 firstc = NUL;
844 break_ctrl_c = TRUE;
845 }
846#endif
847#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100848 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (firstc == ':' || firstc == '=' || firstc == '>')
850 cmd_hkmap = 0;
851#endif
852
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100853 ccline.overstrike = FALSE; // always start in insert mode
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200854
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200856 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858
859 /*
860 * set some variables for redrawcmd()
861 */
862 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000863 ccline.cmdindent = (firstc > 0 ? indent : 0);
864
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100865 // alloc initial ccline.cmdbuff
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000866 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200868 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 ccline.cmdlen = ccline.cmdpos = 0;
870 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100871 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // autoindent for :insert and :append
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000874 if (firstc <= 0)
875 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200876 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000877 ccline.cmdbuff[indent] = NUL;
878 ccline.cmdpos = indent;
879 ccline.cmdspos = indent;
880 ccline.cmdlen = indent;
881 }
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000884 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886#ifdef FEAT_RIGHTLEFT
887 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
888 && (firstc == '/' || firstc == '?'))
889 cmdmsg_rl = TRUE;
890 else
891 cmdmsg_rl = FALSE;
892#endif
893
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (!cmd_silent)
896 {
897 i = msg_scrolled;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 msg_scrolled = 0; // avoid wait_return message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 gotocmdline(TRUE);
900 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100901 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 set_cmdspos();
903 }
904 xpc.xp_context = EXPAND_NOTHING;
905 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000906#ifndef BACKSLASH_IN_FILENAME
907 xpc.xp_shell = FALSE;
908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000910#if defined(FEAT_EVAL)
911 if (ccline.input_fn)
912 {
913 xpc.xp_context = ccline.xp_context;
914 xpc.xp_pattern = ccline.cmdbuff;
915 xpc.xp_arg = ccline.xp_arg;
916 }
917#endif
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 /*
920 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
921 * doing ":@0" when register 0 doesn't contain a CR.
922 */
923 msg_scroll = FALSE;
924
925 State = CMDLINE;
926
927 if (firstc == '/' || firstc == '?' || firstc == '@')
928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100929 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
931 b_im_ptr = &curbuf->b_p_iminsert;
932 else
933 b_im_ptr = &curbuf->b_p_imsearch;
934 if (*b_im_ptr == B_IMODE_LMAP)
935 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100936#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 im_set_active(*b_im_ptr == B_IMODE_IM);
938#endif
939 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100940#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 else if (p_imcmdline)
942 im_set_active(TRUE);
943#endif
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100947 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100950 // When inside an autocommand for writing "exiting" may be set and
951 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000952 settmode(TMODE_RAW);
953
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100954 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200955 cmdline_type = firstc == NUL ? '-' : firstc;
956 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100959 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
962#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#endif
965
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 // If something above caused an error, reset the flags, we do want to type
967 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200968 if (did_emsg)
969 redrawcmd();
970 did_emsg = FALSE;
971 got_int = FALSE;
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 /*
974 * Collect the command string, handling editing keys.
975 */
976 for (;;)
977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100978 redir_off = TRUE; // Don't redirect the typed command.
979 // Repeated, because a ":redir" inside
980 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100982 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100986 did_emsg = FALSE; // There can't really be a reason why an error
987 // that occurs while typing a command should
988 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +0200989
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200990 // Trigger SafeState if nothing is pending.
991 may_trigger_safestate(xpc.xp_numfiles <= 0);
992
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100993 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +0000994
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100995 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
996 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +0000997 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000998 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100999 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (KeyTyped)
1002 {
1003 some_key_typed = TRUE;
1004#ifdef FEAT_RIGHTLEFT
1005 if (cmd_hkmap)
1006 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (cmdmsg_rl && !KeyStuffed)
1008 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001009 // Invert horizontal movements and operations. Only when
1010 // typed by the user directly, not when the result of a
1011 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 switch (c)
1013 {
1014 case K_RIGHT: c = K_LEFT; break;
1015 case K_S_RIGHT: c = K_S_LEFT; break;
1016 case K_C_RIGHT: c = K_C_LEFT; break;
1017 case K_LEFT: c = K_RIGHT; break;
1018 case K_S_LEFT: c = K_S_RIGHT; break;
1019 case K_C_LEFT: c = K_C_RIGHT; break;
1020 }
1021 }
1022#endif
1023 }
1024
1025 /*
1026 * Ignore got_int when CTRL-C was typed here.
1027 * Don't ignore it in :global, we really need to break then, e.g., for
1028 * ":g/pat/normal /pat" (without the <CR>).
1029 * Don't ignore it for the input() function.
1030 */
1031 if ((c == Ctrl_C
1032#ifdef UNIX
1033 || c == intr_char
1034#endif
1035 )
1036#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1037 && firstc != '@'
1038#endif
1039#ifdef FEAT_EVAL
1040 && !break_ctrl_c
1041#endif
1042 && !global_busy)
1043 got_int = FALSE;
1044
Bram Moolenaard7663c22019-08-06 21:59:57 +02001045 // free old command line when finished moving around in the history
1046 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001048 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001049 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 && c != K_PAGEDOWN && c != K_PAGEUP
1051 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001052 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001054 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001057 * When there are matching completions to select <S-Tab> works like
1058 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001060 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 c = Ctrl_P;
1062
1063#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001064 c = wildmenu_translate_key(&ccline, c, &xpc, did_wild_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#endif
1066
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // free expanded names when finished walking through matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (xpc.xp_numfiles != -1
1069 && !(c == p_wc && KeyTyped) && c != p_wcm
1070 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1071 && c != Ctrl_L)
1072 {
1073 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1074 did_wild_list = FALSE;
1075#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001076 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
1078 xpc.xp_context = EXPAND_NOTHING;
1079 wim_index = 0;
1080#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001081 wildmenu_cleanup(&ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#endif
1083 }
1084
1085#ifdef FEAT_WILDMENU
Bram Moolenaareadee482020-09-04 15:37:31 +02001086 c = wildmenu_process_key(&ccline, c, &xpc);
1087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001089 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1090 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (c == Ctrl_BSL)
1092 {
1093 ++no_mapping;
1094 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001095 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 --no_mapping;
1097 --allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001098 // CTRL-\ e doesn't work when obtaining an expression, unless it
1099 // is in a mapping.
Bram Moolenaarb7356812012-10-11 04:04:37 +02001100 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001101 || (ccline.cmdfirstc == '=' && KeyTyped)
1102#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001103 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001104#endif
1105 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
1107 vungetc(c);
1108 c = Ctrl_BSL;
1109 }
1110#ifdef FEAT_EVAL
1111 else if (c == 'e')
1112 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001113 char_u *p = NULL;
1114 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
1116 /*
1117 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001118 * Need to save and restore the current command line, to be
1119 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 */
1121 if (ccline.cmdpos == ccline.cmdlen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001122 new_cmdpos = 99999; // keep it at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else
1124 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (c == '=')
1128 {
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001129 // Need to save and restore ccline. And set "textwinlock"
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001130 // to avoid nasty things like going to another buffer when
1131 // evaluating an expression.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001132 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 p = get_expr_line();
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001134 --textwinlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001135
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001136 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001138 len = (int)STRLEN(p);
1139 if (realloc_cmdbuff(len + 1) == OK)
1140 {
1141 ccline.cmdlen = len;
1142 STRCPY(ccline.cmdbuff, p);
1143 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001145 // Restore the cursor or use the position set with
1146 // set_cmdline_pos().
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001147 if (new_cmdpos > ccline.cmdlen)
1148 ccline.cmdpos = ccline.cmdlen;
1149 else
1150 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001152 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001153 redrawcmd();
1154 goto cmdline_changed;
1155 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001156 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 }
1158 }
1159 beep_flush();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001160 got_int = FALSE; // don't abandon the command line
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001161 did_emsg = FALSE;
1162 emsg_on_display = FALSE;
1163 redrawcmd();
1164 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166#endif
1167 else
1168 {
1169 if (c == Ctrl_G && p_im && restart_edit == 0)
1170 restart_edit = 'a';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001171 gotesc = TRUE; // will free ccline.cmdbuff after putting it
1172 // in history
1173 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 }
1176
1177#ifdef FEAT_CMDWIN
1178 if (c == cedit_key || c == K_CMDWIN)
1179 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001180 // TODO: why is ex_normal_busy checked here?
1181 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001182 {
1183 /*
1184 * Open a window to edit the command line (and history).
1185 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001186 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001187 some_key_typed = TRUE;
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190# ifdef FEAT_DIGRAPHS
1191 else
1192# endif
1193#endif
1194#ifdef FEAT_DIGRAPHS
1195 c = do_digraph(c);
1196#endif
1197
1198 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1199 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001201 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001202 if (exmode_active
1203 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001204 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001205 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001206 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001208 if (c == K_KENTER)
1209 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001211 else
1212 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001213 gotesc = FALSE; // Might have typed ESC previously, don't
1214 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001215 if (ccheck_abbr(c + ABBR_OFF))
1216 goto cmdline_changed;
1217 if (!cmd_silent)
1218 {
1219 windgoto(msg_row, 0);
1220 out_flush();
1221 }
1222 break;
1223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225
1226 /*
1227 * Completion for 'wildchar' or 'wildcharm' key.
1228 * - hitting <ESC> twice means: abandon command line.
1229 * - wildcard expansion is only done when the 'wildchar' key is really
1230 * typed, not when it comes from a macro
1231 */
1232 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1233 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001234 int options = WILD_NO_BEEP;
Bram Moolenaara60053b2020-09-03 16:50:13 +02001235
Bram Moolenaar52410572019-10-27 05:12:45 +01001236 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1237 options |= WILD_BUFLASTUSED;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001238 if (xpc.xp_numfiles > 0) // typed p_wc at least twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001240 // if 'wildmode' contains "list" may still need to list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (xpc.xp_numfiles > 1
1242 && !did_wild_list
1243 && (wim_flags[wim_index] & WIM_LIST))
1244 {
1245 (void)showmatches(&xpc, FALSE);
1246 redrawcmd();
1247 did_wild_list = TRUE;
1248 }
1249 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001250 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001251 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaara60053b2020-09-03 16:50:13 +02001253 res = nextwild(&xpc, WILD_NEXT, options, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001255 res = OK; // don't insert 'wildchar' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001257 else // typed p_wc first time
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 wim_index = 0;
1260 j = ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001261 // if 'wildmode' first contains "longest", get longest
1262 // common part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaara60053b2020-09-03 16:50:13 +02001264 res = nextwild(&xpc, WILD_LONGEST, options, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001266 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaara60053b2020-09-03 16:50:13 +02001267 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001269 // if interrupted while completing, behave like it failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (got_int)
1271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001272 (void)vpeekc(); // remove <C-C> from input stream
1273 got_int = FALSE; // don't abandon the command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1275#ifdef FEAT_WILDMENU
1276 xpc.xp_context = EXPAND_NOTHING;
1277#endif
1278 goto cmdline_changed;
1279 }
1280
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001281 // when more than one match, and 'wildmode' first contains
1282 // "list", or no change and 'wildmode' contains "longest,list",
1283 // list all matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (res == OK && xpc.xp_numfiles > 1)
1285 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001286 // a "longest" that didn't do anything is skipped (but not
1287 // "list:longest")
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1289 wim_index = 1;
1290 if ((wim_flags[wim_index] & WIM_LIST)
1291#ifdef FEAT_WILDMENU
Bram Moolenaara60053b2020-09-03 16:50:13 +02001292 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#endif
1294 )
1295 {
1296 if (!(wim_flags[0] & WIM_LONGEST))
1297 {
1298#ifdef FEAT_WILDMENU
1299 int p_wmnu_save = p_wmnu;
1300 p_wmnu = 0;
1301#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001302 // remove match
Bram Moolenaarb3479632012-11-28 16:49:58 +01001303 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_WILDMENU
1305 p_wmnu = p_wmnu_save;
1306#endif
1307 }
1308#ifdef FEAT_WILDMENU
1309 (void)showmatches(&xpc, p_wmnu
1310 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1311#else
1312 (void)showmatches(&xpc, FALSE);
1313#endif
1314 redrawcmd();
1315 did_wild_list = TRUE;
1316 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001317 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001318 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaara60053b2020-09-03 16:50:13 +02001320 nextwild(&xpc, WILD_NEXT, options, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001323 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325#ifdef FEAT_WILDMENU
1326 else if (xpc.xp_numfiles == -1)
1327 xpc.xp_context = EXPAND_NOTHING;
1328#endif
1329 }
1330 if (wim_index < 3)
1331 ++wim_index;
1332 if (c == ESC)
1333 gotesc = TRUE;
1334 if (res == OK)
1335 goto cmdline_changed;
1336 }
1337
1338 gotesc = FALSE;
1339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001340 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (c == K_S_TAB && KeyTyped)
1342 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001343 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1344 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1345 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 goto cmdline_changed;
1347 }
1348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001349 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 c = NL;
1351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001352 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 /*
1355 * Big switch for a typed command line character.
1356 */
1357 switch (c)
1358 {
1359 case K_BS:
1360 case Ctrl_H:
1361 case K_DEL:
1362 case K_KDEL:
1363 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (c == K_KDEL)
1365 c = K_DEL;
1366
1367 /*
1368 * delete current character is the same as backspace on next
1369 * character, except at end of line
1370 */
1371 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1372 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (has_mbyte && c == K_DEL)
1374 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1375 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (ccline.cmdpos > 0)
1377 {
1378 char_u *p;
1379
1380 j = ccline.cmdpos;
1381 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (has_mbyte)
1383 {
1384 p = mb_prevptr(ccline.cmdbuff, p);
1385 if (c == Ctrl_W)
1386 {
1387 while (p > ccline.cmdbuff && vim_isspace(*p))
1388 p = mb_prevptr(ccline.cmdbuff, p);
1389 i = mb_get_class(p);
1390 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1391 p = mb_prevptr(ccline.cmdbuff, p);
1392 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001393 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001396 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
1398 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1399 --p;
1400 i = vim_iswordc(p[-1]);
1401 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1402 && vim_iswordc(p[-1]) == i)
1403 --p;
1404 }
1405 else
1406 --p;
1407 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1408 ccline.cmdlen -= j - ccline.cmdpos;
1409 i = ccline.cmdpos;
1410 while (i < ccline.cmdlen)
1411 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1412
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001413 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001415#ifdef FEAT_SEARCH_EXTRA
1416 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001417 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001418 is_state.search_start = is_state.save_cursor;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001419 // save view settings, so that the screen
1420 // won't be restored at the wrong position
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001421 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001422 }
1423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 redrawcmd();
1425 }
1426 else if (ccline.cmdlen == 0 && c != Ctrl_W
1427 && ccline.cmdprompt == NULL && indent == 0)
1428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 // In ex and debug mode it doesn't make sense to return.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (exmode_active
1431#ifdef FEAT_EVAL
1432 || ccline.cmdfirstc == '>'
1433#endif
1434 )
1435 goto cmdline_not_changed;
1436
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001437 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (!cmd_silent)
1439 {
1440#ifdef FEAT_RIGHTLEFT
1441 if (cmdmsg_rl)
1442 msg_col = Columns;
1443 else
1444#endif
1445 msg_col = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001446 msg_putchar(' '); // delete ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001448#ifdef FEAT_SEARCH_EXTRA
1449 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001450 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 redraw_cmdline = TRUE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001453 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455 goto cmdline_changed;
1456
1457 case K_INS:
1458 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 ccline.overstrike = !ccline.overstrike;
1460#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001461 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#endif
1463 goto cmdline_not_changed;
1464
1465 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001466 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 // ":lmap" mappings exists, toggle use of mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001470#ifdef HAVE_INPUT_METHOD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001471 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472#endif
1473 if (b_im_ptr != NULL)
1474 {
1475 if (State & LANGMAP)
1476 *b_im_ptr = B_IMODE_LMAP;
1477 else
1478 *b_im_ptr = B_IMODE_NONE;
1479 }
1480 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001481#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 else
1483 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001484 // There are no ":lmap" mappings, toggle IM. When
1485 // 'imdisable' is set don't try getting the status, it's
1486 // always off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if ((p_imdisable && b_im_ptr != NULL)
1488 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001490 im_set_active(FALSE); // Disable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (b_im_ptr != NULL)
1492 *b_im_ptr = B_IMODE_NONE;
1493 }
1494 else
1495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001496 im_set_active(TRUE); // Enable input method
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (b_im_ptr != NULL)
1498 *b_im_ptr = B_IMODE_IM;
1499 }
1500 }
1501#endif
1502 if (b_im_ptr != NULL)
1503 {
1504 if (b_im_ptr == &curbuf->b_p_iminsert)
1505 set_iminsert_global();
1506 else
1507 set_imsearch_global();
1508 }
1509#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001510 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001512#if defined(FEAT_KEYMAP)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001513 // Show/unshow value of 'keymap' in status lines later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 status_redraw_curbuf();
1515#endif
1516 goto cmdline_not_changed;
1517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 j = ccline.cmdpos;
1522 ccline.cmdlen -= j;
1523 i = ccline.cmdpos = 0;
1524 while (i < ccline.cmdlen)
1525 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001526 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001528#ifdef FEAT_SEARCH_EXTRA
1529 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001530 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 redrawcmd();
1533 goto cmdline_changed;
1534
1535#ifdef FEAT_CLIPBOARD
1536 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001537 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (clip_star.state != SELECT_CLEARED)
1539 {
1540 if (clip_star.state == SELECT_DONE)
1541 clip_copy_modeless_selection(TRUE);
1542 goto cmdline_not_changed;
1543 }
1544 break;
1545#endif
1546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001547 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001549 // In exmode it doesn't make sense to return. Except when
1550 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00001551 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001552 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 goto cmdline_not_changed;
1554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 gotesc = TRUE; // will free ccline.cmdbuff after
1556 // putting it in history
1557 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001559 case Ctrl_R: // insert register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001561 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#endif
1563 putcmdline('"', TRUE);
1564 ++no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001565 ++allow_keys;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001566 i = c = plain_vgetc(); // CTRL-R <char>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (i == Ctrl_O)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001568 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (i == Ctrl_R)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001570 c = plain_vgetc(); // CTRL-R CTRL-R <char>
Bram Moolenaara92522f2017-07-15 15:21:38 +02001571 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 --no_mapping;
Bram Moolenaar38571a02019-11-26 14:28:15 +01001573 --allow_keys;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574#ifdef FEAT_EVAL
1575 /*
1576 * Insert the result of an expression.
1577 * Need to save the current command line, to be able to enter
1578 * a new one...
1579 */
1580 new_cmdpos = -1;
1581 if (c == '=')
1582 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001583 if (ccline.cmdfirstc == '=' // can't do this recursively
1584 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
1586 beep_flush();
1587 c = ESC;
1588 }
1589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001593 if (c != ESC) // use ESC to cancel inserting register
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001595 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001596
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001597#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // When there was a serious error abort getting the
1599 // command line.
Bram Moolenaaracf53452005-12-17 22:06:52 +00001600 if (aborting())
1601 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001602 gotesc = TRUE; // will free ccline.cmdbuff after
1603 // putting it in history
1604 goto returncmd; // back to cmd mode
Bram Moolenaaracf53452005-12-17 22:06:52 +00001605 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001606#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 KeyTyped = FALSE; // Don't do p_wc completion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608#ifdef FEAT_EVAL
1609 if (new_cmdpos >= 0)
1610 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001611 // set_cmdline_pos() was used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (new_cmdpos > ccline.cmdlen)
1613 ccline.cmdpos = ccline.cmdlen;
1614 else
1615 ccline.cmdpos = new_cmdpos;
1616 }
1617#endif
1618 }
1619 redrawcmd();
1620 goto cmdline_changed;
1621
1622 case Ctrl_D:
1623 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001624 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001627 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 case K_RIGHT:
1630 case K_S_RIGHT:
1631 case K_C_RIGHT:
1632 do
1633 {
1634 if (ccline.cmdpos >= ccline.cmdlen)
1635 break;
1636 i = cmdline_charsize(ccline.cmdpos);
1637 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1638 break;
1639 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001641 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 + ccline.cmdpos);
1643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 ++ccline.cmdpos;
1645 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001646 while ((c == K_S_RIGHT || c == K_C_RIGHT
1647 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (has_mbyte)
1650 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 goto cmdline_not_changed;
1652
1653 case K_LEFT:
1654 case K_S_LEFT:
1655 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001656 if (ccline.cmdpos == 0)
1657 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 do
1659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1663 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1665 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001666 while (ccline.cmdpos > 0
1667 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001668 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (has_mbyte)
1671 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 goto cmdline_not_changed;
1673
1674 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001675 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001676 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar4f974752019-02-17 17:44:42 +01001678#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001679 // On MS-Windows ignore <M-F4>, we get it when closing the window
1680 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001681 case K_F4:
1682 if (mod_mask == MOD_MASK_ALT)
1683 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001684 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00001685 goto cmdline_not_changed;
1686 }
1687 break;
1688#endif
1689
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 case K_MIDDLEDRAG:
1691 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001692 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
1694 case K_MIDDLEMOUSE:
1695# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001696 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (!gui.in_use)
1698# endif
1699 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001700 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001701# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001703 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001705# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001706 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 redrawcmd();
1708 goto cmdline_changed;
1709
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001710# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001712 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 redrawcmd();
1714 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 case K_LEFTDRAG:
1718 case K_LEFTRELEASE:
1719 case K_RIGHTDRAG:
1720 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001721 // Ignore drag and release events when the button-down wasn't
1722 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (ignore_drag_release)
1724 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001725 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 case K_LEFTMOUSE:
1727 case K_RIGHTMOUSE:
1728 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1729 ignore_drag_release = TRUE;
1730 else
1731 ignore_drag_release = FALSE;
1732# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001733 // When GUI is active, also move when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (!gui.in_use)
1735# endif
1736 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001737 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738# ifdef FEAT_CLIPBOARD
1739 if (mouse_row < cmdline_row && clip_star.available)
1740 {
1741 int button, is_click, is_drag;
1742
1743 /*
1744 * Handle modeless selection.
1745 */
1746 button = get_mouse_button(KEY2TERMCAP1(c),
1747 &is_click, &is_drag);
1748 if (mouse_model_popup() && button == MOUSE_LEFT
1749 && (mod_mask & MOD_MASK_SHIFT))
1750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001751 // Translate shift-left to right button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 button = MOUSE_RIGHT;
1753 mod_mask &= ~MOD_MASK_SHIFT;
1754 }
1755 clip_modeless(button, is_click, is_drag);
1756 goto cmdline_not_changed;
1757 }
1758# endif
1759
1760 set_cmdspos();
1761 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1762 ++ccline.cmdpos)
1763 {
1764 i = cmdline_charsize(ccline.cmdpos);
1765 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1766 && mouse_col < ccline.cmdspos % Columns + i)
1767 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (has_mbyte)
1769 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // Count ">" for double-wide char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001772 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 + ccline.cmdpos) - 1;
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 ccline.cmdspos += i;
1776 }
1777 goto cmdline_not_changed;
1778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001779 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 case K_MOUSEDOWN:
1781 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001782 case K_MOUSELEFT:
1783 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001784 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 case K_X1MOUSE:
1786 case K_X1DRAG:
1787 case K_X1RELEASE:
1788 case K_X2MOUSE:
1789 case K_X2DRAG:
1790 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001791 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 goto cmdline_not_changed;
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001795 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 case K_LEFTRELEASE_NM:
1797 goto cmdline_not_changed;
1798
1799 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001800 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 gui_do_scroll();
1803 redrawcmd();
1804 }
1805 goto cmdline_not_changed;
1806
1807 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001808 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001810 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 redrawcmd();
1812 }
1813 goto cmdline_not_changed;
1814#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001815#ifdef FEAT_GUI_TABLINE
1816 case K_TABLINE:
1817 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001818 // Don't want to change any tabs here. Make sure the same tab
1819 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001820 if (gui_use_tabline())
1821 gui_mch_set_curtab(tabpage_index(curtab));
1822 goto cmdline_not_changed;
1823#endif
1824
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001825 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 goto cmdline_not_changed;
1827
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001828 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 case K_HOME:
1830 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 case K_S_HOME:
1832 case K_C_HOME:
1833 ccline.cmdpos = 0;
1834 set_cmdspos();
1835 goto cmdline_not_changed;
1836
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001837 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 case K_END:
1839 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 case K_S_END:
1841 case K_C_END:
1842 ccline.cmdpos = ccline.cmdlen;
1843 set_cmdspos_cursor();
1844 goto cmdline_not_changed;
1845
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001846 case Ctrl_A: // all matches
Bram Moolenaarb3479632012-11-28 16:49:58 +01001847 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 break;
1849 goto cmdline_changed;
1850
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001851 case Ctrl_L:
1852#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001853 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001854 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001855#endif
1856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001857 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01001858 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 break;
1860 goto cmdline_changed;
1861
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001862 case Ctrl_N: // next match
1863 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001864 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001866 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1867 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001869 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 case K_UP:
1873 case K_DOWN:
1874 case K_S_UP:
1875 case K_S_DOWN:
1876 case K_PAGEUP:
1877 case K_KPAGEUP:
1878 case K_PAGEDOWN:
1879 case K_KPAGEDOWN:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001880 if (get_hislen() == 0 || firstc == NUL) // no history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 goto cmdline_not_changed;
1882
1883 i = hiscnt;
1884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 // save current command string so it can be restored later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (lookfor == NULL)
1887 {
1888 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1889 goto cmdline_not_changed;
1890 lookfor[ccline.cmdpos] = NUL;
1891 }
1892
1893 j = (int)STRLEN(lookfor);
1894 for (;;)
1895 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001896 // one step backwards
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001897 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001898 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001900 if (hiscnt == get_hislen()) // first time
Bram Moolenaard7663c22019-08-06 21:59:57 +02001901 hiscnt = *get_hisidx(histype);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001902 else if (hiscnt == 0 && *get_hisidx(histype)
1903 != get_hislen() - 1)
Bram Moolenaard7663c22019-08-06 21:59:57 +02001904 hiscnt = get_hislen() - 1;
1905 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 --hiscnt;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001907 else // at top of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 hiscnt = i;
1910 break;
1911 }
1912 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001913 else // one step forwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001915 // on last entry, clear the line
Bram Moolenaard7663c22019-08-06 21:59:57 +02001916 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02001918 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 break;
1920 }
1921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001922 // not on a history line, nothing to do
Bram Moolenaard7663c22019-08-06 21:59:57 +02001923 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001925 if (hiscnt == get_hislen() - 1) // wrap around
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 hiscnt = 0;
1927 else
1928 ++hiscnt;
1929 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
1931 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 hiscnt = i;
1934 break;
1935 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001936 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001937 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02001938 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 lookfor, (size_t)j) == 0)
1940 break;
1941 }
1942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001943 if (hiscnt != i) // jumped to other entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 char_u *p;
1946 int len;
1947 int old_firstc;
1948
Bram Moolenaar438d1762018-09-30 17:11:48 +02001949 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001950 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02001951 if (hiscnt == get_hislen())
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 p = lookfor; // back to the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02001954 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 if (histype == HIST_SEARCH
1957 && p != lookfor
1958 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001960 // Correct for the separator character used when
1961 // adding the history entry vs the one used now.
1962 // First loop: count length.
1963 // Second loop: copy the characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 for (i = 0; i <= 1; ++i)
1965 {
1966 len = 0;
1967 for (j = 0; p[j] != NUL; ++j)
1968 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001969 // Replace old sep with new sep, unless it is
1970 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (p[j] == old_firstc
1972 && (j == 0 || p[j - 1] != '\\'))
1973 {
1974 if (i > 0)
1975 ccline.cmdbuff[len] = firstc;
1976 }
1977 else
1978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001979 // Escape new sep, unless it is already
1980 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (p[j] == firstc
1982 && (j == 0 || p[j - 1] != '\\'))
1983 {
1984 if (i > 0)
1985 ccline.cmdbuff[len] = '\\';
1986 ++len;
1987 }
1988 if (i > 0)
1989 ccline.cmdbuff[len] = p[j];
1990 }
1991 ++len;
1992 }
1993 if (i == 0)
1994 {
1995 alloc_cmdbuff(len);
1996 if (ccline.cmdbuff == NULL)
1997 goto returncmd;
1998 }
1999 }
2000 ccline.cmdbuff[len] = NUL;
2001 }
2002 else
2003 {
2004 alloc_cmdbuff((int)STRLEN(p));
2005 if (ccline.cmdbuff == NULL)
2006 goto returncmd;
2007 STRCPY(ccline.cmdbuff, p);
2008 }
2009
2010 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2011 redrawcmd();
2012 goto cmdline_changed;
2013 }
2014 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002015 goto cmdline_not_changed;
2016
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002017#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 case Ctrl_G: // next match
2019 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002020 if (may_adjust_incsearch_highlighting(
2021 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002022 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#endif
2025
2026 case Ctrl_V:
2027 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002029 int prev_mod_mask = mod_mask;
2030
2031 ignore_drag_release = TRUE;
2032 putcmdline('^', TRUE);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002033 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002034 c = get_literal(); // get next (two) character(s)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002035 no_reduce_keys = FALSE;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002036 do_abbr = FALSE; // don't do abbreviation now
2037 extra_char = NUL;
2038 // may need to remove ^ when composing char was typed
2039 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2040 {
2041 draw_cmdline(ccline.cmdpos,
2042 ccline.cmdlen - ccline.cmdpos);
2043 msg_putchar(' ');
2044 cursorcmd();
2045 }
2046
2047 if ((c == ESC || c == CSI)
2048 && !(prev_mod_mask & MOD_MASK_SHIFT))
2049 // Using CTRL-V: Change any modifyOtherKeys ESC
2050 // sequence to a normal key. Don't do this for
2051 // CTRL-SHIFT-V.
2052 c = decodeModifyOtherKeys(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 break;
2056
2057#ifdef FEAT_DIGRAPHS
2058 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002061# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002062 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002065 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (c != NUL)
2067 break;
2068
2069 redrawcmd();
2070 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002071#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002074 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (!p_ari)
2076 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002077 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 goto cmdline_not_changed;
2079#endif
2080
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002081 case K_PS:
2082 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2083 goto cmdline_changed;
2084
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 default:
2086#ifdef UNIX
2087 if (c == intr_char)
2088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002089 gotesc = TRUE; // will free ccline.cmdbuff after
2090 // putting it in history
2091 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093#endif
2094 /*
2095 * Normal character with no special meaning. Just set mod_mask
2096 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2097 * the string <S-Space>. This should only happen after ^V.
2098 */
2099 if (!IS_SPECIAL(c))
2100 mod_mask = 0x0;
2101 break;
2102 }
2103 /*
2104 * End of switch on command line character.
2105 * We come here if we have a normal character.
2106 */
2107
Bram Moolenaar13505972019-01-24 15:04:48 +01002108 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2109 && (ccheck_abbr(
2110 // Add ABBR_OFF for characters above 0x100, this is
2111 // what check_abbr() expects.
2112 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2113 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 goto cmdline_changed;
2115
2116 /*
2117 * put the character in the command line
2118 */
2119 if (IS_SPECIAL(c) || mod_mask != 0)
2120 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2121 else
2122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (has_mbyte)
2124 {
2125 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 put_on_cmdline(IObuff, j, TRUE);
2128 }
2129 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 IObuff[0] = c;
2132 put_on_cmdline(IObuff, 1, TRUE);
2133 }
2134 }
2135 goto cmdline_changed;
2136
2137/*
2138 * This part implements incremental searches for "/" and "?"
2139 * Jump to cmdline_not_changed when a character has been read but the command
2140 * line did not change. Then we only search and redraw if something changed in
2141 * the past.
2142 * Jump to cmdline_changed when the command line did change.
2143 * (Sorry for the goto's, I know it is ugly).
2144 */
2145cmdline_not_changed:
2146#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002147 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 continue;
2149#endif
2150
2151cmdline_changed:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 // Trigger CmdlineChanged autocommands.
Bram Moolenaar153b7042018-01-31 15:48:32 +01002153 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaara60053b2020-09-03 16:50:13 +02002156 if (xpc.xp_context == EXPAND_NOTHING)
2157 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158#endif
2159
2160#ifdef FEAT_RIGHTLEFT
2161 if (cmdmsg_rl
2162# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002163 || (p_arshape && !p_tbidi
2164 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# endif
2166 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002167 // Always redraw the whole command line to fix shaping and
2168 // right-left typing. Not efficient, but it works.
2169 // Do it only when there are no characters left to read
2170 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002171 if (vpeekc() == NUL)
2172 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#endif
2174 }
2175
2176returncmd:
2177
2178#ifdef FEAT_RIGHTLEFT
2179 cmdmsg_rl = FALSE;
2180#endif
2181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002183 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002186 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188
2189 if (ccline.cmdbuff != NULL)
2190 {
2191 /*
2192 * Put line in history buffer (":" and "=" only when it was typed).
2193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (ccline.cmdlen && firstc != NUL
2195 && (some_key_typed || histype == HIST_SEARCH))
2196 {
2197 add_to_history(histype, ccline.cmdbuff, TRUE,
2198 histype == HIST_SEARCH ? firstc : NUL);
2199 if (firstc == ':')
2200 {
2201 vim_free(new_last_cmdline);
2202 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2203 }
2204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002206 if (gotesc)
2207 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
2210 /*
2211 * If the screen was shifted up, redraw the whole screen (later).
2212 * If the line is too long, clear it, so ruler and shown command do
2213 * not get printed in the middle of it.
2214 */
2215 msg_check();
2216 msg_scroll = save_msg_scroll;
2217 redir_off = FALSE;
2218
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002219 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (some_key_typed)
2221 need_wait_return = FALSE;
2222
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002224 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002227#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2229 im_save_status(b_im_ptr);
2230 im_set_active(FALSE);
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002236 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
Bram Moolenaar438d1762018-09-30 17:11:48 +02002238theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002239 {
2240 char_u *p = ccline.cmdbuff;
2241
Bram Moolenaar438d1762018-09-30 17:11:48 +02002242 if (did_save_ccline)
2243 restore_cmdline(&save_ccline);
2244 else
2245 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002246 return p;
2247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248}
2249
2250#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2251/*
2252 * Get a command line with a prompt.
2253 * This is prepared to be called recursively from getcmdline() (e.g. by
2254 * f_input() when evaluating an expression from CTRL-R =).
2255 * Returns the command line in allocated memory, or NULL.
2256 */
2257 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002258getcmdline_prompt(
2259 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002260 char_u *prompt, // command line prompt
2261 int attr, // attributes for prompt
2262 int xp_context, // type of expansion
2263 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002266 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002267 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002269 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
Bram Moolenaar438d1762018-09-30 17:11:48 +02002271 if (ccline.cmdbuff != NULL)
2272 {
2273 // Save the values of the current cmdline and restore them below.
2274 save_cmdline(&save_ccline);
2275 did_save_ccline = TRUE;
2276 }
2277
Bram Moolenaara80faa82020-04-12 19:37:17 +02002278 CLEAR_FIELD(ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 ccline.cmdprompt = prompt;
2280 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002281# ifdef FEAT_EVAL
2282 ccline.xp_context = xp_context;
2283 ccline.xp_arg = xp_arg;
2284 ccline.input_fn = (firstc == '@');
2285# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002286 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002287 s = getcmdline_int(firstc, 1L, 0, FALSE);
2288
2289 if (did_save_ccline)
2290 restore_cmdline(&save_ccline);
2291
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002292 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // Restore msg_col, the prompt from input() may have changed it.
2294 // But only if called recursively and the commandline is therefore being
2295 // restored to an old one; if not, the input() prompt stays on the screen,
2296 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002297 if (ccline.cmdbuff != NULL)
2298 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
2300 return s;
2301}
2302#endif
2303
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002304/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002305 * Read the 'wildmode' option, fill wim_flags[].
2306 */
2307 int
2308check_opt_wim(void)
2309{
2310 char_u new_wim_flags[4];
2311 char_u *p;
2312 int i;
2313 int idx = 0;
2314
2315 for (i = 0; i < 4; ++i)
2316 new_wim_flags[i] = 0;
2317
2318 for (p = p_wim; *p; ++p)
2319 {
2320 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2321 ;
2322 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2323 return FAIL;
2324 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2325 new_wim_flags[idx] |= WIM_LONGEST;
2326 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2327 new_wim_flags[idx] |= WIM_FULL;
2328 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2329 new_wim_flags[idx] |= WIM_LIST;
2330 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2331 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2332 else
2333 return FAIL;
2334 p += i;
2335 if (*p == NUL)
2336 break;
2337 if (*p == ',')
2338 {
2339 if (idx == 3)
2340 return FAIL;
2341 ++idx;
2342 }
2343 }
2344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002345 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002346 while (idx < 3)
2347 {
2348 new_wim_flags[idx + 1] = new_wim_flags[idx];
2349 ++idx;
2350 }
2351
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002352 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002353 for (i = 0; i < 4; ++i)
2354 wim_flags[i] = new_wim_flags[i];
2355 return OK;
2356}
2357
2358/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002359 * Return TRUE when the text must not be changed and we can't switch to
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002360 * another window or buffer. TRUE when editing the command line, evaluating
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002361 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002362 */
2363 int
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002364text_and_win_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002365{
2366#ifdef FEAT_CMDWIN
2367 if (cmdwin_type != 0)
2368 return TRUE;
2369#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002370 return textwinlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002371}
2372
2373/*
2374 * Give an error message for a command that isn't allowed while the cmdline
2375 * window is open or editing the cmdline in another way.
2376 */
2377 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002378text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002379{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002380 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002381}
2382
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002384get_text_locked_msg(void)
2385{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002386#ifdef FEAT_CMDWIN
2387 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002388 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002389#endif
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002390 if (textwinlock != 0)
2391 return e_textwinlock;
Bram Moolenaarff06f282020-04-21 22:01:14 +02002392 return e_textlock;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002393}
2394
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002395/*
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002396 * Return TRUE when the text must not be changed and/or we cannot switch to
2397 * another window. TRUE while evaluating 'completefunc'.
2398 */
2399 int
2400text_locked(void)
2401{
2402 return text_and_win_locked() || textlock != 0;
2403}
2404
2405/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002406 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2407 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002408 */
2409 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002410curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002411{
2412 if (curbuf_lock > 0)
2413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002414 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002415 return TRUE;
2416 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002417 return allbuf_locked();
2418}
2419
2420/*
2421 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2422 * message.
2423 */
2424 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002425allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002426{
2427 if (allbuf_lock > 0)
2428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002429 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002430 return TRUE;
2431 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002432 return FALSE;
2433}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002436cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002439 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return 1;
2441#endif
2442 return ptr2cells(ccline.cmdbuff + idx);
2443}
2444
2445/*
2446 * Compute the offset of the cursor on the command line for the prompt and
2447 * indent.
2448 */
2449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002450set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002452 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 ccline.cmdspos = 1 + ccline.cmdindent;
2454 else
2455 ccline.cmdspos = 0 + ccline.cmdindent;
2456}
2457
2458/*
2459 * Compute the screen position for the cursor on the command line.
2460 */
2461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002462set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 int i, m, c;
2465
2466 set_cmdspos();
2467 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002470 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002471 m = MAXCOL;
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 else
2474 m = MAXCOL;
2475 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2476 {
2477 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 if (has_mbyte)
2480 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002481 // If the cmdline doesn't fit, show cursor on last visible char.
2482 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 if ((ccline.cmdspos += c) >= m)
2484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 ccline.cmdspos -= c;
2486 break;
2487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002489 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491}
2492
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493/*
2494 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2495 * character that doesn't fit, so that a ">" must be displayed.
2496 */
2497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002498correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2502 && ccline.cmdspos % Columns + cells > Columns)
2503 ccline.cmdspos++;
2504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506/*
2507 * Get an Ex command line for the ":" command.
2508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002510getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002511 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002512 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002513 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002514 getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002516 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (exec_from_reg && vpeekc() == ':')
2518 (void)vgetc();
Bram Moolenaar66250c92020-08-20 15:02:42 +02002519 return getcmdline(c, 1L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520}
2521
2522/*
2523 * Get an Ex command line for Ex mode.
2524 * In Ex mode we only use the OS supplied line editing features and no
2525 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002526 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002529getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002530 int promptc, // normally ':', NUL for ":append" and '?' for
2531 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002532 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002533 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002534 getline_opt_T options UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002536 garray_T line_ga;
2537 char_u *pend;
2538 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002539 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002540 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002541 int vcol = 0;
2542 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002543 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002544 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002546 // Switch cursor on now. This avoids that it happens after the "\n", which
2547 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 cursor_on();
2549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002550 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002552 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002554 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002556 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002557 if (p_prompt)
2558 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 while (indent-- > 0)
2560 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563
2564 ga_init2(&line_ga, 1, 30);
2565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002566 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002567 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002568 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002569 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002570 while (indent >= 8)
2571 {
2572 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002573 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002574 indent -= 8;
2575 }
2576 while (indent-- > 0)
2577 {
2578 ga_append(&line_ga, ' ');
2579 msg_putchar(' ');
2580 }
2581 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 ++no_mapping;
2583 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * Get the line, one character at a time.
2587 */
2588 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002589 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002591 long sw;
2592 char_u *s;
2593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 if (ga_grow(&line_ga, 40) == FAIL)
2595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaarba748c82017-02-26 14:00:07 +01002597 /*
2598 * Get one character at a time.
2599 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002600 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002602 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002603 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2604 c1 = '\n';
2605 else
2606 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
2608 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002609 * Handle line editing.
2610 * Previously this was left to the system, putting the terminal in
2611 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002613 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002615 msg_putchar('\n');
2616 break;
2617 }
2618
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002619 if (c1 == K_PS)
2620 {
2621 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2622 goto redraw;
2623 }
2624
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002625 if (!escaped)
2626 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002627 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002628 if (c1 == '\r')
2629 c1 = '\n';
2630
2631 if (c1 == BS || c1 == K_BS
2632 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002634 if (line_ga.ga_len > 0)
2635 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002636 if (has_mbyte)
2637 {
2638 p = (char_u *)line_ga.ga_data;
2639 p[line_ga.ga_len] = NUL;
2640 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2641 line_ga.ga_len -= len;
2642 }
2643 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002644 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002645 goto redraw;
2646 }
2647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002652 msg_col = startcol;
2653 msg_clr_eos();
2654 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002655 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002656 }
2657
2658 if (c1 == Ctrl_T)
2659 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002660 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002661 p = (char_u *)line_ga.ga_data;
2662 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002663 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002664 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002665add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002666 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002668 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02002669 p = (char_u *)line_ga.ga_data;
2670 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002671 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2672 *s = ' ';
2673 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002675redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002678 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002679 p = (char_u *)line_ga.ga_data;
2680 p[line_ga.ga_len] = NUL;
2681 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002683 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002685 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002687 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002688 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002692 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002693 msg_outtrans_len(p, len);
2694 vcol += ptr2cells(p);
2695 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002699 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 continue;
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002703 if (c1 == Ctrl_D)
2704 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002705 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 p = (char_u *)line_ga.ga_data;
2707 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002709 if (prev_char == '^')
2710 ex_keep_indent = TRUE;
2711 indent = 0;
2712 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
2715 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002716 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002717 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002718 if (indent > 0)
2719 {
2720 --indent;
2721 indent -= indent % get_sw_value(curbuf);
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002724 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002726 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2728 --line_ga.ga_len;
2729 }
2730 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732
2733 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2734 {
2735 escaped = TRUE;
2736 continue;
2737 }
2738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002740 if (IS_SPECIAL(c1))
2741 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002743
2744 if (IS_SPECIAL(c1))
2745 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002746 if (has_mbyte)
2747 len = (*mb_char2bytes)(c1,
2748 (char_u *)line_ga.ga_data + line_ga.ga_len);
2749 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002750 {
2751 len = 1;
2752 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2753 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 if (c1 == '\n')
2755 msg_putchar('\n');
2756 else if (c1 == TAB)
2757 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002758 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002761 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002766 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002769 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 escaped = FALSE;
2771
2772 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002773 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002775 // We are done when a NL is entered, but not when it comes after an
2776 // odd number of backslashes, that results in a NUL.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002777 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002779 int bcount = 0;
2780
2781 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2782 ++bcount;
2783
2784 if (bcount > 0)
2785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002786 // Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2787 // "\NL", etc.
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002788 line_ga.ga_len -= (bcount + 1) / 2;
2789 pend -= (bcount + 1) / 2;
2790 pend[-1] = '\n';
2791 }
2792
2793 if ((bcount & 1) == 0)
2794 {
2795 --line_ga.ga_len;
2796 --pend;
2797 *pend = NUL;
2798 break;
2799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
2802
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 --no_mapping;
2804 --allow_keys;
2805
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002806 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 msg_didout = FALSE;
2808 msg_col = 0;
2809 if (msg_row < Rows - 1)
2810 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002811 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 if (got_int)
2814 ga_clear(&line_ga);
2815
2816 return (char_u *)line_ga.ga_data;
2817}
2818
Bram Moolenaare344bea2005-09-01 20:46:49 +00002819# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2820 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821/*
2822 * Return TRUE if ccline.overstrike is on.
2823 */
2824 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002825cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 return ccline.overstrike;
2828}
2829
2830/*
2831 * Return TRUE if the cursor is at the end of the cmdline.
2832 */
2833 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002834cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 return (ccline.cmdpos >= ccline.cmdlen);
2837}
2838#endif
2839
Bram Moolenaar9372a112005-12-06 19:59:18 +00002840#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841/*
2842 * Return the virtual column number at the current cursor position.
2843 * This is used by the IM code to obtain the start of the preedit string.
2844 */
2845 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002846cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847{
2848 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2849 return MAXCOL;
2850
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (has_mbyte)
2852 {
2853 colnr_T col;
2854 int i = 0;
2855
2856 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002857 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 return col;
2860 }
2861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 return ccline.cmdpos;
2863}
2864#endif
2865
2866#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2867/*
2868 * If part of the command line is an IM preedit string, redraw it with
2869 * IM feedback attributes. The cursor position is restored after drawing.
2870 */
2871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002872redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 if ((State & CMDLINE)
2875 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002876 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 && !p_imdisable
2878 && im_is_preediting())
2879 {
2880 int cmdpos = 0;
2881 int cmdspos;
2882 int old_row;
2883 int old_col;
2884 colnr_T col;
2885
2886 old_row = msg_row;
2887 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002888 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 if (has_mbyte)
2891 {
2892 for (col = 0; col < preedit_start_col
2893 && cmdpos < ccline.cmdlen; ++col)
2894 {
2895 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002896 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
2899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 cmdspos += preedit_start_col;
2902 cmdpos += preedit_start_col;
2903 }
2904
2905 msg_row = cmdline_row + (cmdspos / (int)Columns);
2906 msg_col = cmdspos % (int)Columns;
2907 if (msg_row >= Rows)
2908 msg_row = Rows - 1;
2909
2910 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2911 {
2912 int char_len;
2913 int char_attr;
2914
2915 char_attr = im_get_feedback_attr(col);
2916 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002917 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002920 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 char_len = 1;
2923
2924 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2925 cmdpos += char_len;
2926 }
2927
2928 msg_row = old_row;
2929 msg_col = old_col;
2930 }
2931}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002932#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933
2934/*
2935 * Allocate a new command line buffer.
2936 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002939alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 /*
2942 * give some extra space to avoid having to allocate all the time
2943 */
2944 if (len < 80)
2945 len = 100;
2946 else
2947 len += 20;
2948
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002949 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 ccline.cmdbufflen = len;
2951}
2952
2953/*
2954 * Re-allocate the command line to length len + something extra.
2955 * return FAIL for failure, OK otherwise
2956 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002957 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002958realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 char_u *p;
2961
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002962 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002963 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002966 alloc_cmdbuff(len); // will get some more
2967 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002969 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 return FAIL;
2971 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002972 // There isn't always a NUL after the command, but it may need to be
2973 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02002974 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2975 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002977
2978 if (ccline.xpc != NULL
2979 && ccline.xpc->xp_pattern != NULL
2980 && ccline.xpc->xp_context != EXPAND_NOTHING
2981 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2982 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002983 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002985 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
2986 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002987 if (i >= 0 && i <= ccline.cmdlen)
2988 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2989 }
2990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 return OK;
2992}
2993
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002994#if defined(FEAT_ARABIC) || defined(PROTO)
2995static char_u *arshape_buf = NULL;
2996
2997# if defined(EXITFREE) || defined(PROTO)
2998 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02002999free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003000{
3001 vim_free(arshape_buf);
3002}
3003# endif
3004#endif
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006/*
3007 * Draw part of the cmdline at the current cursor position. But draw stars
3008 * when cmdline_star is TRUE.
3009 */
3010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003011draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3014 int i;
3015
3016 if (cmdline_star > 0)
3017 for (i = 0; i < len; ++i)
3018 {
3019 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003021 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
3023 else
3024#endif
3025#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003026 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 static int buflen = 0;
3029 char_u *p;
3030 int j;
3031 int newlen = 0;
3032 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003033 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 int prev_c = 0;
3035 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003036 int u8c;
3037 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 /*
3041 * Do arabic shaping into a temporary buffer. This is very
3042 * inefficient!
3043 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003044 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003046 // Re-allocate the buffer. We keep it around to avoid a lot of
3047 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003048 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003049 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003050 arshape_buf = alloc(buflen);
3051 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003052 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003055 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3056 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003057 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003058 arshape_buf[0] = ' ';
3059 newlen = 1;
3060 }
3061
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 for (j = start; j < start + len; j += mb_l)
3063 {
3064 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003065 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003066 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (ARABIC_CHAR(u8c))
3068 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003069 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (cmdmsg_rl)
3071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003072 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 pc = prev_c;
3074 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003075 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (j + mb_l >= start + len)
3077 nc = NUL;
3078 else
3079 nc = utf_ptr2char(p + mb_l);
3080 }
3081 else
3082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003083 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (j + mb_l >= start + len)
3085 pc = NUL;
3086 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003087 {
3088 int pcc[MAX_MCO];
3089
3090 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003092 pc1 = pcc[0];
3093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 nc = prev_c;
3095 }
3096 prev_c = u8c;
3097
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003098 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003100 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003101 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003103 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3104 if (u8cc[1] != 0)
3105 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003106 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108 }
3109 else
3110 {
3111 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003112 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 newlen += mb_l;
3114 }
3115 }
3116
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003117 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 else
3120#endif
3121 msg_outtrans_len(ccline.cmdbuff + start, len);
3122}
3123
3124/*
3125 * Put a character on the command line. Shifts the following text to the
3126 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3127 * "c" must be printable (fit in one display cell)!
3128 */
3129 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003130putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 if (cmd_silent)
3133 return;
3134 msg_no_more = TRUE;
3135 msg_putchar(c);
3136 if (shift)
3137 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3138 msg_no_more = FALSE;
3139 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003140 extra_char = c;
3141 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144/*
3145 * Undo a putcmdline(c, FALSE).
3146 */
3147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 if (cmd_silent)
3151 return;
3152 msg_no_more = TRUE;
3153 if (ccline.cmdlen == ccline.cmdpos)
3154 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003155 else if (has_mbyte)
3156 draw_cmdline(ccline.cmdpos,
3157 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else
3159 draw_cmdline(ccline.cmdpos, 1);
3160 msg_no_more = FALSE;
3161 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003162 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163}
3164
3165/*
3166 * Put the given string, of the given length, onto the command line.
3167 * If len is -1, then STRLEN() is used to calculate the length.
3168 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3169 * part will be redrawn, otherwise it will not. If this function is called
3170 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3171 * called afterwards.
3172 */
3173 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003174put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
3176 int retval;
3177 int i;
3178 int m;
3179 int c;
3180
3181 if (len < 0)
3182 len = (int)STRLEN(str);
3183
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003184 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003186 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
3188 retval = OK;
3189 if (retval == OK)
3190 {
3191 if (!ccline.overstrike)
3192 {
3193 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3194 ccline.cmdbuff + ccline.cmdpos,
3195 (size_t)(ccline.cmdlen - ccline.cmdpos));
3196 ccline.cmdlen += len;
3197 }
3198 else
3199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (has_mbyte)
3201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003202 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003204 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003206 // Count nr of bytes in cmdline that are overwritten by these
3207 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003209 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 --m;
3211 if (i < ccline.cmdlen)
3212 {
3213 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3214 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3215 ccline.cmdlen += ccline.cmdpos + len - i;
3216 }
3217 else
3218 ccline.cmdlen = ccline.cmdpos + len;
3219 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003220 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 ccline.cmdlen = ccline.cmdpos + len;
3222 }
3223 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3224 ccline.cmdbuff[ccline.cmdlen] = NUL;
3225
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (enc_utf8)
3227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 // When the inserted text starts with a composing character,
3229 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 i = 0;
3231 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3232 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3233 {
3234 i = (*mb_head_off)(ccline.cmdbuff,
3235 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3236 ccline.cmdpos -= i;
3237 len += i;
3238 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3239 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003240#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003243 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 i = (*mb_head_off)(ccline.cmdbuff,
3245 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3246 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3247 + ccline.cmdpos - i), c))
3248 {
3249 ccline.cmdpos -= i;
3250 len += i;
3251 }
3252 else
3253 i = 0;
3254 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (i != 0)
3257 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003258 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3260 ccline.cmdspos -= i;
3261 msg_col -= i;
3262 if (msg_col < 0)
3263 {
3264 msg_col += Columns;
3265 --msg_row;
3266 }
3267 }
3268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270 if (redraw && !cmd_silent)
3271 {
3272 msg_no_more = TRUE;
3273 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003274 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003276 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (cmdline_row != i || ccline.overstrike)
3278 msg_clr_eos();
3279 msg_no_more = FALSE;
3280 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003281 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003283 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003284 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003286 }
3287 else
3288 m = MAXCOL;
3289 for (i = 0; i < len; ++i)
3290 {
3291 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003292 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003293 if (has_mbyte)
3294 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003295 // Stop cursor at the end of the screen, but do increment the
3296 // insert position, so that entering a very long command
3297 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003298 if (ccline.cmdspos + c < m)
3299 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003300
Bram Moolenaar14184a32019-02-16 15:10:30 +01003301 if (has_mbyte)
3302 {
3303 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3304 if (c > len - i - 1)
3305 c = len - i - 1;
3306 ccline.cmdpos += c;
3307 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003309 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311 }
3312 if (redraw)
3313 msg_check();
3314 return retval;
3315}
3316
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317static cmdline_info_T prev_ccline;
3318static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003319
3320/*
3321 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3322 * and overwrite it. But get_cmdline_str() may need it, thus make it
3323 * available globally in prev_ccline.
3324 */
3325 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003326save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003327{
3328 if (!prev_ccline_used)
3329 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003330 CLEAR_FIELD(prev_ccline);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003331 prev_ccline_used = TRUE;
3332 }
3333 *ccp = prev_ccline;
3334 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003335 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003336}
3337
3338/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003339 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003340 */
3341 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003343{
3344 ccline = prev_ccline;
3345 prev_ccline = *ccp;
3346}
3347
Bram Moolenaar8299df92004-07-10 09:47:34 +00003348/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003349 * Paste a yank register into the command line.
3350 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003351 * insert_reg() can't be used here, because special characters from the
3352 * register contents will be interpreted as commands.
3353 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003354 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003355 */
3356 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003357cmdline_paste(
3358 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003359 int literally, // Insert text literally instead of "as typed"
3360 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003361{
3362 long i;
3363 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003364 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003365 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003366
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003367 // check for valid regname; also accept special characters for CTRL-R in
3368 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003369 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003370 && regname != Ctrl_A && regname != Ctrl_L
3371 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003372 return FAIL;
3373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003374 // A register containing CTRL-R can cause an endless loop. Allow using
3375 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003376 line_breakcheck();
3377 if (got_int)
3378 return FAIL;
3379
3380#ifdef FEAT_CLIPBOARD
3381 regname = may_get_selection(regname);
3382#endif
3383
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003384 // Need to set "textwinlock" to avoid nasty things like going to another
Bram Moolenaar438d1762018-09-30 17:11:48 +02003385 // buffer when evaluating an expression.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003386 ++textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003387 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02003388 --textwinlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003389
3390 if (i)
3391 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003392 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003393 if (arg == NULL)
3394 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003395
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003396 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3397 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003398 p = arg;
3399 if (p_is && regname == Ctrl_W)
3400 {
3401 char_u *w;
3402 int len;
3403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003404 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003405 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003406 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003407 if (has_mbyte)
3408 {
3409 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3410 if (!vim_iswordc(mb_ptr2char(w - len)))
3411 break;
3412 w -= len;
3413 }
3414 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003415 {
3416 if (!vim_iswordc(w[-1]))
3417 break;
3418 --w;
3419 }
3420 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003421 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003422 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3423 p += len;
3424 }
3425
3426 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003427 if (allocated)
3428 vim_free(arg);
3429 return OK;
3430 }
3431
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003432 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003433}
3434
3435/*
3436 * Put a string on the command line.
3437 * When "literally" is TRUE, insert literally.
3438 * When "literally" is FALSE, insert as typed, but don't leave the command
3439 * line.
3440 */
3441 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003442cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003443{
3444 int c, cv;
3445
3446 if (literally)
3447 put_on_cmdline(s, -1, TRUE);
3448 else
3449 while (*s != NUL)
3450 {
3451 cv = *s;
3452 if (cv == Ctrl_V && s[1])
3453 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003454 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003455 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003456 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003457 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003458 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3459 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003460#ifdef UNIX
3461 || c == intr_char
3462#endif
3463 || (c == Ctrl_BSL && *s == Ctrl_N))
3464 stuffcharReadbuff(Ctrl_V);
3465 stuffcharReadbuff(c);
3466 }
3467}
3468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003470 * This function is called when the screen size changes and with incremental
3471 * search and in other situations where the command line may have been
3472 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 */
3474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003475redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003477 redrawcmdline_ex(TRUE);
3478}
3479
3480 void
3481redrawcmdline_ex(int do_compute_cmdrow)
3482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (cmd_silent)
3484 return;
3485 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003486 if (do_compute_cmdrow)
3487 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 redrawcmd();
3489 cursorcmd();
3490}
3491
3492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003493redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 int i;
3496
3497 if (cmd_silent)
3498 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003499 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 msg_putchar(ccline.cmdfirstc);
3501 if (ccline.cmdprompt != NULL)
3502 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003503 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003505 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003506 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 --ccline.cmdindent;
3508 }
3509 else
3510 for (i = ccline.cmdindent; i > 0; --i)
3511 msg_putchar(' ');
3512}
3513
3514/*
3515 * Redraw what is currently on the command line.
3516 */
3517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003518redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 if (cmd_silent)
3521 return;
3522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003523 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003524 if (ccline.cmdbuff == NULL)
3525 {
3526 windgoto(cmdline_row, 0);
3527 msg_clr_eos();
3528 return;
3529 }
3530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 msg_start();
3532 redrawcmdprompt();
3533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003534 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 msg_no_more = TRUE;
3536 draw_cmdline(0, ccline.cmdlen);
3537 msg_clr_eos();
3538 msg_no_more = FALSE;
3539
3540 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003541 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003542 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 /*
3545 * An emsg() before may have set msg_scroll. This is used in normal mode,
3546 * in cmdline mode we can reset them now.
3547 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003548 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003550 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3551 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 skip_redraw = FALSE;
3553}
3554
3555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003556compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003558 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 cmdline_row = Rows - 1;
3560 else
3561 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003562 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
3564
Bram Moolenaar66b51422019-08-18 21:44:12 +02003565 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003566cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
3568 if (cmd_silent)
3569 return;
3570
3571#ifdef FEAT_RIGHTLEFT
3572 if (cmdmsg_rl)
3573 {
3574 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3575 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3576 if (msg_row <= 0)
3577 msg_row = Rows - 1;
3578 }
3579 else
3580#endif
3581 {
3582 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3583 msg_col = ccline.cmdspos % (int)Columns;
3584 if (msg_row >= Rows)
3585 msg_row = Rows - 1;
3586 }
3587
3588 windgoto(msg_row, msg_col);
3589#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003590 if (p_imst == IM_ON_THE_SPOT)
3591 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592#endif
3593#ifdef MCH_CURSOR_SHAPE
3594 mch_update_cursor();
3595#endif
3596}
3597
3598 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003599gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601 msg_start();
3602#ifdef FEAT_RIGHTLEFT
3603 if (cmdmsg_rl)
3604 msg_col = Columns - 1;
3605 else
3606#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003607 msg_col = 0; // always start in column 0
3608 if (clr) // clear the bottom line(s)
3609 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 windgoto(cmdline_row, 0);
3611}
3612
3613/*
3614 * Check the word in front of the cursor for an abbreviation.
3615 * Called when the non-id character "c" has been entered.
3616 * When an abbreviation is recognized it is removed from the text with
3617 * backspaces and the replacement string is inserted, followed by "c".
3618 */
3619 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003620ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003622 int spos = 0;
3623
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003624 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 return FALSE;
3626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003627 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
3628 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003629 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3630 spos++;
3631 if (ccline.cmdlen - spos > 5
3632 && ccline.cmdbuff[spos] == '\''
3633 && ccline.cmdbuff[spos + 2] == ','
3634 && ccline.cmdbuff[spos + 3] == '\'')
3635 spos += 5;
3636 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003637 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003638 spos = 0;
3639
3640 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641}
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003644 * Escape special characters in "fname" for when used as a file name argument
3645 * after a Vim command, or, when "shell" is non-zero, a shell command.
3646 * Returns the result in allocated memory.
3647 */
3648 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003649vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003650{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003651 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003652#ifdef BACKSLASH_IN_FILENAME
3653 char_u buf[20];
3654 int j = 0;
3655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003656 // Don't escape '[', '{' and '!' if they are in 'isfname'.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003657 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003658 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003659 buf[j++] = *p;
3660 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003661 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003662#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003663 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3664 if (shell && csh_like_shell() && p != NULL)
3665 {
3666 char_u *s;
3667
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003668 // For csh and similar shells need to put two backslashes before '!'.
3669 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003670 s = vim_strsave_escaped(p, (char_u *)"!");
3671 vim_free(p);
3672 p = s;
3673 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003674#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003676 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
3677 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003678 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003679 escape_fname(&p);
3680
3681 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003682}
3683
3684/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003685 * Put a backslash before the file name in "pp", which is in allocated memory.
3686 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003687 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003688escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003689{
3690 char_u *p;
3691
Bram Moolenaar964b3742019-05-24 18:54:09 +02003692 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003693 if (p != NULL)
3694 {
3695 p[0] = '\\';
3696 STRCPY(p + 1, *pp);
3697 vim_free(*pp);
3698 *pp = p;
3699 }
3700}
3701
3702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * For each file name in files[num_files]:
3704 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3705 */
3706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003707tilde_replace(
3708 char_u *orig_pat,
3709 int num_files,
3710 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 int i;
3713 char_u *p;
3714
3715 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3716 {
3717 for (i = 0; i < num_files; ++i)
3718 {
3719 p = home_replace_save(NULL, files[i]);
3720 if (p != NULL)
3721 {
3722 vim_free(files[i]);
3723 files[i] = p;
3724 }
3725 }
3726 }
3727}
3728
3729/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003730 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003732 cmdline_info_T *
3733get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736}
3737
Bram Moolenaar064154c2016-03-19 22:50:43 +01003738#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003739/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003740 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003741 * ccline and put the previous value in prev_ccline.
3742 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003744get_ccline_ptr(void)
3745{
3746 if ((State & CMDLINE) == 0)
3747 return NULL;
3748 if (ccline.cmdbuff != NULL)
3749 return &ccline;
3750 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3751 return &prev_ccline;
3752 return NULL;
3753}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003754#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003755
Bram Moolenaar064154c2016-03-19 22:50:43 +01003756#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003757/*
3758 * Get the current command line in allocated memory.
3759 * Only works when the command line is being edited.
3760 * Returns NULL when something is wrong.
3761 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003762 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003763get_cmdline_str(void)
3764{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003765 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003766
Bram Moolenaaree91c332018-09-25 22:27:35 +02003767 if (cmdline_star > 0)
3768 return NULL;
3769 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003770 if (p == NULL)
3771 return NULL;
3772 return vim_strnsave(p->cmdbuff, p->cmdlen);
3773}
3774
3775/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003776 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003777 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003778 void
3779f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3780{
3781 rettv->v_type = VAR_STRING;
3782 rettv->vval.v_string = get_cmdline_str();
3783}
3784
3785/*
3786 * "getcmdpos()" function
3787 */
3788 void
3789f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003790{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003791 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003792
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003793 rettv->vval.v_number = 0;
3794 if (p != NULL)
3795 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003796}
3797
3798/*
3799 * Set the command line byte position to "pos". Zero is the first position.
3800 * Only works when the command line is being edited.
3801 * Returns 1 when failed, 0 when OK.
3802 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003803 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003804set_cmdline_pos(
3805 int pos)
3806{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003808
3809 if (p == NULL)
3810 return 1;
3811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 // The position is not set directly but after CTRL-\ e or CTRL-R = has
3813 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003814 if (pos < 0)
3815 new_cmdpos = 0;
3816 else
3817 new_cmdpos = pos;
3818 return 0;
3819}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003820
3821/*
3822 * "setcmdpos()" function
3823 */
3824 void
3825f_setcmdpos(typval_T *argvars, typval_T *rettv)
3826{
3827 int pos = (int)tv_get_number(&argvars[0]) - 1;
3828
3829 if (pos >= 0)
3830 rettv->vval.v_number = set_cmdline_pos(pos);
3831}
3832
3833/*
3834 * "getcmdtype()" function
3835 */
3836 void
3837f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
3838{
3839 rettv->v_type = VAR_STRING;
3840 rettv->vval.v_string = alloc(2);
3841 if (rettv->vval.v_string != NULL)
3842 {
3843 rettv->vval.v_string[0] = get_cmdline_type();
3844 rettv->vval.v_string[1] = NUL;
3845 }
3846}
3847
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003848#endif
3849
3850#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
3851/*
3852 * Get the current command-line type.
3853 * Returns ':' or '/' or '?' or '@' or '>' or '-'
3854 * Only works when the command line is being edited.
3855 * Returns NUL when something is wrong.
3856 */
3857 int
3858get_cmdline_type(void)
3859{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003860 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003861
3862 if (p == NULL)
3863 return NUL;
3864 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01003865 return
3866# ifdef FEAT_EVAL
3867 (p->input_fn) ? '@' :
3868# endif
3869 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003870 return p->cmdfirstc;
3871}
3872#endif
3873
Bram Moolenaard7663c22019-08-06 21:59:57 +02003874/*
3875 * Return the first character of the current command line.
3876 */
3877 int
3878get_cmdline_firstc(void)
3879{
3880 return ccline.cmdfirstc;
3881}
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883/*
3884 * Get indices "num1,num2" that specify a range within a list (not a range of
3885 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
3886 * Returns OK if parsed successfully, otherwise FAIL.
3887 */
3888 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003889get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891 int len;
3892 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003893 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
3895 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003896 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003898 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 *str += len;
3900 *num1 = (int)num;
3901 first = TRUE;
3902 }
3903 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003904 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
3906 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003907 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (len > 0)
3909 {
3910 *num2 = (int)num;
3911 *str = skipwhite(*str + len);
3912 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003913 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 return FAIL;
3915 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003916 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 *num2 = *num1;
3918 return OK;
3919}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02003920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#if defined(FEAT_CMDWIN) || defined(PROTO)
3922/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003923 * Check value of 'cedit' and set cedit_key.
3924 * Returns NULL if value is OK, error message otherwise.
3925 */
3926 char *
3927check_cedit(void)
3928{
3929 int n;
3930
3931 if (*p_cedit == NUL)
3932 cedit_key = -1;
3933 else
3934 {
3935 n = string_to_key(p_cedit, FALSE);
3936 if (vim_isprintc(n))
3937 return e_invarg;
3938 cedit_key = n;
3939 }
3940 return NULL;
3941}
3942
3943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 * Open a window on the current command line and history. Allow editing in
3945 * the window. Returns when the window is closed.
3946 * Returns:
3947 * CR if the command is to be executed
3948 * Ctrl_C if it is to be abandoned
3949 * K_IGNORE if editing continues
3950 */
3951 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02003952open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003954 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003956 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 win_T *wp;
3958 int i;
3959 linenr_T lnum;
3960 int histtype;
3961 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 int save_restart_edit = restart_edit;
3963 int save_State = State;
3964 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00003965#ifdef FEAT_RIGHTLEFT
3966 int save_cmdmsg_rl = cmdmsg_rl;
3967#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02003968#ifdef FEAT_FOLDING
3969 int save_KeyTyped;
3970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003972 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (cmdwin_type != 0
3974# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3975 || cmdline_star > 0
3976# endif
3977 )
3978 {
3979 beep_flush();
3980 return K_IGNORE;
3981 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003982 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
Bram Moolenaar96e38a82019-09-09 18:35:33 +02003984 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 win_size_save(&winsizes);
3986
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01003987 // When using completion in Insert mode with <C-R>=<C-F> one can open the
3988 // command line window, but we don't want the popup menu then.
3989 pum_undisplay();
3990
Bram Moolenaar96e38a82019-09-09 18:35:33 +02003991 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003992 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02003993 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003994
Bram Moolenaar96e38a82019-09-09 18:35:33 +02003995 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
3997 {
3998 beep_flush();
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003999 ga_clear(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return K_IGNORE;
4001 }
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004002 // Don't let quitting the More prompt make this fail.
4003 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004005 // Create the command-line buffer empty.
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004006 if (do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL) == FAIL)
4007 {
4008 // Some autocommand messed it up?
4009 win_close(curwin, TRUE);
4010 ga_clear(&winsizes);
4011 return Ctrl_C;
4012 }
4013 cmdwin_type = get_cmdline_type();
4014
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004015 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004016 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar5e94a292020-03-19 18:46:57 +01004017 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004020#ifdef FEAT_FOLDING
4021 curwin->w_p_fen = FALSE;
4022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004024 curwin->w_p_rl = cmdmsg_rl;
4025 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004027 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004029 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004030 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004032 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004033 need_wait_return = FALSE;
4034
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004035 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4037 {
4038 if (p_wc == TAB)
4039 {
4040 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4041 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4042 }
4043 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4044 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004045 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004047 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4048 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004049 curbuf->b_p_tw = 0;
4050
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004051 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004053 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004055 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (i >= 0)
4057 {
4058 lnum = 0;
4059 do
4060 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004061 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004063 if (get_histentry(histtype)[i].hisstr != NULL)
4064 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 (colnr_T)0, FALSE);
4066 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004067 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 }
4070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004071 // Replace the empty last line with the current command-line and put the
4072 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4074 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4075 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004076 changed_line_abv_curs();
4077 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004078 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
Bram Moolenaar23324a02019-10-01 17:39:04 +02004080 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 exmode_active = 0;
4082
4083 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaar23324a02019-10-01 17:39:04 +02004086 // Reset here so it can be set by a CmdWinEnter autocommand.
4087 cmdwin_result = 0;
4088
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004089 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004090 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004091 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004092 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094 i = RedrawingDisabled;
4095 RedrawingDisabled = 0;
4096
4097 /*
4098 * Call the main loop until <CR> or CTRL-C is typed.
4099 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004100 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
4102 RedrawingDisabled = i;
4103
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004104# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004105 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004106# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004107
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004108 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004109 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004110
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004111# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004112 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004113 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114# endif
4115
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 exmode_active = save_exmode;
4118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004119 // Safety check: The old window or buffer was deleted: It's a bug when
4120 // this happens!
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004121 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
4123 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004124 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 else
4127 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004128# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004129 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (aborting() && cmdwin_result != K_IGNORE)
4131 cmdwin_result = Ctrl_C;
4132# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004133 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004135 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004137 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4138
4139 if (histtype == HIST_CMD)
4140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004141 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004142 ccline.cmdbuff = vim_strsave((char_u *)p);
4143 cmdwin_result = CAR;
4144 }
4145 else
4146 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004147 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004148 ccline.cmdbuff = NULL;
4149 stuffcharReadbuff(':');
4150 stuffReadbuff((char_u *)p);
4151 stuffcharReadbuff(CAR);
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004154 else if (cmdwin_result == K_XF2) // :qa typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
4156 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4157 cmdwin_result = CAR;
4158 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004159 else if (cmdwin_result == Ctrl_C)
4160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004161 // :q or :close, don't execute any command
4162 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004163 ccline.cmdbuff = NULL;
4164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 else
4166 ccline.cmdbuff = vim_strsave(ml_get_curline());
4167 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004168 {
4169 ccline.cmdbuff = vim_strsave((char_u *)"");
4170 ccline.cmdlen = 0;
4171 ccline.cmdbufflen = 1;
4172 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 else
4176 {
4177 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4178 ccline.cmdbufflen = ccline.cmdlen + 1;
4179 ccline.cmdpos = curwin->w_cursor.col;
4180 if (ccline.cmdpos > ccline.cmdlen)
4181 ccline.cmdpos = ccline.cmdlen;
4182 if (cmdwin_result == K_IGNORE)
4183 {
4184 set_cmdspos_cursor();
4185 redrawcmd();
4186 }
4187 }
4188
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004189# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004190 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004191 curwin->w_p_cole = 0;
4192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004194 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 win_goto(old_curwin);
4196 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004197
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004198 // win_close() may have already wiped the buffer when 'bh' is
4199 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004200 if (bufref_valid(&bufref))
Bram Moolenaara6e8f882019-12-14 16:18:15 +01004201 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004203 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206
4207 ga_clear(&winsizes);
4208 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004209# ifdef FEAT_RIGHTLEFT
4210 cmdmsg_rl = save_cmdmsg_rl;
4211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 return cmdwin_result;
4217}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004218#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220/*
4221 * Used for commands that either take a simple command string argument, or:
4222 * cmd << endmarker
4223 * {script}
4224 * endmarker
4225 * Returns a pointer to allocated memory with {script} or NULL.
4226 */
4227 char_u *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004228script_get(exarg_T *eap UNUSED, char_u *cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004230#ifdef FEAT_EVAL
4231 list_T *l;
4232 listitem_T *li;
4233 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 garray_T ga;
4235
4236 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4237 return NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004238 cmd += 2;
4239
4240 l = heredoc_get(eap, cmd, TRUE);
4241 if (l == NULL)
4242 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 ga_init2(&ga, 1, 0x400);
4245
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004246 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004248 s = tv_get_string(&li->li_tv);
4249 ga_concat(&ga, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004252 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004254 list_free(l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return (char_u *)ga.ga_data;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004256#else
4257 return NULL;
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004260
4261#if defined(FEAT_EVAL) || defined(PROTO)
4262/*
4263 * This function is used by f_input() and f_inputdialog() functions. The third
4264 * argument to f_input() specifies the type of completion to use at the
4265 * prompt. The third argument to f_inputdialog() specifies the value to return
4266 * when the user cancels the prompt.
4267 */
4268 void
4269get_user_input(
4270 typval_T *argvars,
4271 typval_T *rettv,
4272 int inputdialog,
4273 int secret)
4274{
4275 char_u *prompt = tv_get_string_chk(&argvars[0]);
4276 char_u *p = NULL;
4277 int c;
4278 char_u buf[NUMBUFLEN];
4279 int cmd_silent_save = cmd_silent;
4280 char_u *defstr = (char_u *)"";
4281 int xp_type = EXPAND_NOTHING;
4282 char_u *xp_arg = NULL;
4283
4284 rettv->v_type = VAR_STRING;
4285 rettv->vval.v_string = NULL;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004286 if (input_busy)
4287 return; // this doesn't work recursively.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004288
4289#ifdef NO_CONSOLE_INPUT
4290 // While starting up, there is no place to enter text. When running tests
4291 // with --not-a-term we assume feedkeys() will be used.
4292 if (no_console_input() && !is_not_a_term())
4293 return;
4294#endif
4295
4296 cmd_silent = FALSE; // Want to see the prompt.
4297 if (prompt != NULL)
4298 {
4299 // Only the part of the message after the last NL is considered as
4300 // prompt for the command line
4301 p = vim_strrchr(prompt, '\n');
4302 if (p == NULL)
4303 p = prompt;
4304 else
4305 {
4306 ++p;
4307 c = *p;
4308 *p = NUL;
4309 msg_start();
4310 msg_clr_eos();
4311 msg_puts_attr((char *)prompt, get_echo_attr());
4312 msg_didout = FALSE;
4313 msg_starthere();
4314 *p = c;
4315 }
4316 cmdline_row = msg_row;
4317
4318 if (argvars[1].v_type != VAR_UNKNOWN)
4319 {
4320 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4321 if (defstr != NULL)
4322 stuffReadbuffSpec(defstr);
4323
4324 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4325 {
4326 char_u *xp_name;
4327 int xp_namelen;
4328 long argt;
4329
4330 // input() with a third argument: completion
4331 rettv->vval.v_string = NULL;
4332
4333 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4334 if (xp_name == NULL)
4335 return;
4336
4337 xp_namelen = (int)STRLEN(xp_name);
4338
4339 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4340 &xp_arg) == FAIL)
4341 return;
4342 }
4343 }
4344
4345 if (defstr != NULL)
4346 {
4347 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004348 int save_vgetc_busy = vgetc_busy;
4349 int save_input_busy = input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004350
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004351 input_busy |= vgetc_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004352 ex_normal_busy = 0;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004353 vgetc_busy = 0;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004354 rettv->vval.v_string =
4355 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4356 xp_type, xp_arg);
4357 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004358 vgetc_busy = save_vgetc_busy;
4359 input_busy = save_input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004360 }
4361 if (inputdialog && rettv->vval.v_string == NULL
4362 && argvars[1].v_type != VAR_UNKNOWN
4363 && argvars[2].v_type != VAR_UNKNOWN)
4364 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4365 &argvars[2], buf));
4366
4367 vim_free(xp_arg);
4368
4369 // since the user typed this, no need to wait for return
4370 need_wait_return = FALSE;
4371 msg_didout = FALSE;
4372 }
4373 cmd_silent = cmd_silent_save;
4374}
4375#endif