blob: 61f8379df559e563247fd3dae8a98fb915625c5e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +020020// Return value when handling keys in command-line mode.
21#define CMDLINE_NOT_CHANGED 1
22#define CMDLINE_CHANGED 2
23#define GOTO_NORMAL_MODE 3
Bram Moolenaar9c929712020-09-07 22:05:28 +020024#define PROCESS_NEXT_KEY 4
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +020025
Bram Moolenaar438d1762018-09-30 17:11:48 +020026// The current cmdline_info. It is initialized in getcmdline() and after that
27// used by other functions. When invoking getcmdline() recursively it needs
28// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020029static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
31#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +010032static int new_cmdpos; // position set by set_cmdline_pos()
Shougo Matsushita69084282024-09-23 20:34:47 +020033static char_u current_prompt[CMDBUFFSIZE + 1] = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
35
Bram Moolenaar217e1b82019-12-01 21:41:28 +010036static int extra_char = NUL; // extra character to display when redrawing
37 // the command line
Bram Moolenaar6a77d262017-07-16 15:24:01 +020038static int extra_char_shift;
39
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +010041static int cmd_hkmap = 0; // Hebrew mapping during command line
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
43
Bram Moolenaar9c929712020-09-07 22:05:28 +020044static char_u *getcmdline_int(int firstc, long count, int indent, int clear_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010045static int cmdline_charsize(int idx);
46static void set_cmdspos(void);
47static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010049static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010050static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020051static void save_cmdline(cmdline_info_T *ccp);
52static void restore_cmdline(cmdline_info_T *ccp);
Shougo Matsushita69084282024-09-23 20:34:47 +020053#ifdef FEAT_EVAL
54static char_u *get_prompt(void);
55#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010056static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010057static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010058static int ccheck_abbr(int);
Martin Tournoij7904fa42022-10-04 16:28:45 +010059static int open_cmdwin(void);
Bram Moolenaard93a7fc2021-01-04 12:42:13 +010060#ifdef FEAT_SEARCH_EXTRA
61static int empty_pattern_magic(char_u *pat, size_t len, magic_T magic_val);
62#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
zeertzjq9b7d2a92022-08-26 10:33:54 +010064static int cedit_key = -1; // key value of 'cedit' option
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020065
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020066 static void
67trigger_cmd_autocmd(int typechar, int evt)
68{
69 char_u typestr[2];
70
71 typestr[0] = typechar;
72 typestr[1] = NUL;
73 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
74}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020075
Bram Moolenaar071d4272004-06-13 20:20:40 +000076/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020077 * Abandon the command line.
78 */
79 static void
80abandon_cmdline(void)
81{
Bram Moolenaard23a8232018-02-10 18:45:26 +010082 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020083 if (msg_scrolled == 0)
84 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010085 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020086 redraw_cmdline = TRUE;
87}
88
Bram Moolenaaree219b02017-12-17 14:55:01 +010089#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020090/*
Bram Moolenaar66216052017-12-16 16:33:44 +010091 * Guess that the pattern matches everything. Only finds specific cases, such
92 * as a trailing \|, which can happen while typing a pattern.
93 */
94 static int
Bram Moolenaard93a7fc2021-01-04 12:42:13 +010095empty_pattern(char_u *p, int delim)
Bram Moolenaar66216052017-12-16 16:33:44 +010096{
Bram Moolenaard93a7fc2021-01-04 12:42:13 +010097 size_t n = STRLEN(p);
98 magic_T magic_val = MAGIC_ON;
Bram Moolenaar66216052017-12-16 16:33:44 +010099
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100100 if (n > 0)
101 (void) skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic_val);
102 else
103 return TRUE;
104
105 return empty_pattern_magic(p, n, magic_val);
106}
107
108 static int
109empty_pattern_magic(char_u *p, size_t len, magic_T magic_val)
110{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100111 // remove trailing \v and the like
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100112 while (len >= 2 && p[len - 2] == '\\'
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100113 && vim_strchr((char_u *)"mMvVcCZ", p[len - 1]) != NULL)
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100114 len -= 2;
115
116 // true, if the pattern is empty, or the pattern ends with \| and magic is
117 // set (or it ends with '|' and very magic is set)
118 return len == 0 || (len > 1
119 && ((p[len - 2] == '\\'
120 && p[len - 1] == '|' && magic_val == MAGIC_ON)
121 || (p[len - 2] != '\\'
122 && p[len - 1] == '|' && magic_val == MAGIC_ALL)));
Bram Moolenaar66216052017-12-16 16:33:44 +0100123}
124
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200125// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200126typedef struct {
127 colnr_T vs_curswant;
128 colnr_T vs_leftcol;
zeertzjq7b7b4cb2023-08-11 23:48:27 +0200129 colnr_T vs_skipcol;
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200130 linenr_T vs_topline;
131# ifdef FEAT_DIFF
132 int vs_topfill;
133# endif
134 linenr_T vs_botline;
135 linenr_T vs_empty_rows;
136} viewstate_T;
137
138 static void
139save_viewstate(viewstate_T *vs)
140{
141 vs->vs_curswant = curwin->w_curswant;
142 vs->vs_leftcol = curwin->w_leftcol;
zeertzjq7b7b4cb2023-08-11 23:48:27 +0200143 vs->vs_skipcol = curwin->w_skipcol;
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200144 vs->vs_topline = curwin->w_topline;
145# ifdef FEAT_DIFF
146 vs->vs_topfill = curwin->w_topfill;
147# endif
148 vs->vs_botline = curwin->w_botline;
149 vs->vs_empty_rows = curwin->w_empty_rows;
150}
151
152 static void
153restore_viewstate(viewstate_T *vs)
154{
155 curwin->w_curswant = vs->vs_curswant;
156 curwin->w_leftcol = vs->vs_leftcol;
zeertzjq7b7b4cb2023-08-11 23:48:27 +0200157 curwin->w_skipcol = vs->vs_skipcol;
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200158 curwin->w_topline = vs->vs_topline;
159# ifdef FEAT_DIFF
160 curwin->w_topfill = vs->vs_topfill;
161# endif
162 curwin->w_botline = vs->vs_botline;
163 curwin->w_empty_rows = vs->vs_empty_rows;
164}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200165
166// Struct to store the state of 'incsearch' highlighting.
167typedef struct {
168 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200169 pos_T save_cursor;
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +0100170 int winid; // window where this state is valid
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200171 viewstate_T init_viewstate;
172 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200173 pos_T match_start;
174 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200175 int did_incsearch;
176 int incsearch_postponed;
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100177 optmagic_T magic_overruled_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200178} incsearch_state_T;
179
180 static void
181init_incsearch_state(incsearch_state_T *is_state)
182{
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +0100183 is_state->winid = curwin->w_id;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200184 is_state->match_start = curwin->w_cursor;
185 is_state->did_incsearch = FALSE;
186 is_state->incsearch_postponed = FALSE;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100187 is_state->magic_overruled_save = magic_overruled;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200188 CLEAR_POS(&is_state->match_end);
189 is_state->save_cursor = curwin->w_cursor; // may be restored later
190 is_state->search_start = curwin->w_cursor;
191 save_viewstate(&is_state->init_viewstate);
192 save_viewstate(&is_state->old_viewstate);
193}
194
195/*
196 * First move cursor to end of match, then to the start. This
197 * moves the whole match onto the screen when 'nowrap' is set.
198 */
199 static void
200set_search_match(pos_T *t)
201{
202 t->lnum += search_match_lines;
203 t->col = search_match_endcol;
204 if (t->lnum > curbuf->b_ml.ml_line_count)
205 {
206 t->lnum = curbuf->b_ml.ml_line_count;
207 coladvance((colnr_T)MAXCOL);
208 }
209}
210
211/*
212 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200213 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200214 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200215 */
216 static int
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200217do_incsearch_highlighting(
218 int firstc,
219 int *search_delim,
220 incsearch_state_T *is_state,
221 int *skiplen,
222 int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200224 char_u *cmd;
Bram Moolenaare1004402020-10-24 20:49:43 +0200225 cmdmod_T dummy_cmdmod;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200226 char_u *p;
227 int delim_optional = FALSE;
228 int delim;
229 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100230 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200231 exarg_T ea;
232 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200233 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100234 int retval = FALSE;
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100235 magic_T magic = 0;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200237 *skiplen = 0;
238 *patlen = ccline.cmdlen;
239
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200240 if (!p_is || cmd_silent)
241 return FALSE;
242
243 // by default search all lines
244 search_first_line = 0;
245 search_last_line = MAXLNUM;
246
247 if (firstc == '/' || firstc == '?')
Bram Moolenaarc036e872020-02-21 21:30:52 +0100248 {
249 *search_delim = firstc;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200250 return TRUE;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100251 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200252 if (firstc != ':')
253 return FALSE;
254
Bram Moolenaarc6725252019-11-23 21:56:46 +0100255 ++emsg_off;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200256 CLEAR_FIELD(ea);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200257 ea.line1 = 1;
258 ea.line2 = 1;
259 ea.cmd = ccline.cmdbuff;
260 ea.addr_type = ADDR_LINES;
261
Bram Moolenaare1004402020-10-24 20:49:43 +0200262 parse_command_modifiers(&ea, &dummy, &dummy_cmdmod, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200263
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200264 cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200265 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100266 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200267
268 // Skip over "substitute" to find the pattern separator.
269 for (p = cmd; ASCII_ISALPHA(*p); ++p)
270 ;
271 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100272 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200273
274 if (STRNCMP(cmd, "substitute", p - cmd) == 0
275 || STRNCMP(cmd, "smagic", p - cmd) == 0
276 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
277 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200278 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200279 if (*cmd == 's' && cmd[1] == 'm')
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100280 magic_overruled = OPTION_MAGIC_ON;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200281 else if (*cmd == 's' && cmd[1] == 'n')
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100282 magic_overruled = OPTION_MAGIC_OFF;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200283 }
284 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
285 {
Bram Moolenaar333015a2020-04-25 15:54:16 +0200286 // skip over ! and flags
287 if (*p == '!')
288 p = skipwhite(p + 1);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200289 while (ASCII_ISALPHA(*(p = skipwhite(p))))
290 ++p;
291 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100292 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200293 }
294 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
295 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
296 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
297 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
298 || STRNCMP(cmd, "global", p - cmd) == 0)
299 {
300 // skip over "!"
301 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200302 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200303 p++;
304 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100305 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200306 }
307 if (*cmd != 'g')
308 delim_optional = TRUE;
309 }
310 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100311 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200312
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200313 p = skipwhite(p);
314 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100315 *search_delim = delim;
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100316 end = skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200317
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200318 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200319
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200320 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100321 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200322
323 // Don't do 'hlsearch' highlighting if the pattern matches everything.
324 if (!use_last_pat)
325 {
326 char c = *end;
327 int empty;
328
329 *end = NUL;
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100330 empty = empty_pattern_magic(p, STRLEN(p), magic);
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200331 *end = c;
332 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100333 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200334 }
335
336 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200337 *skiplen = (int)(p - ccline.cmdbuff);
338 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200339
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200340 // parse the address range
341 save_cursor = curwin->w_cursor;
342 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200343 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200344 if (ea.addr_count > 0)
345 {
346 // Allow for reverse match.
347 if (ea.line2 < ea.line1)
348 {
349 search_first_line = ea.line2;
350 search_last_line = ea.line1;
351 }
352 else
353 {
354 search_first_line = ea.line1;
355 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200356 }
357 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200358 else if (cmd[0] == 's' && cmd[1] != 'o')
359 {
360 // :s defaults to the current line
361 search_first_line = curwin->w_cursor.lnum;
362 search_last_line = curwin->w_cursor.lnum;
363 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200364
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200365 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100366 retval = TRUE;
367theend:
368 --emsg_off;
369 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200370}
371
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200372 static void
373finish_incsearch_highlighting(
374 int gotesc,
375 incsearch_state_T *is_state,
376 int call_update_screen)
377{
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +0000378 if (!is_state->did_incsearch)
379 return;
380
381 is_state->did_incsearch = FALSE;
382 if (gotesc)
383 curwin->w_cursor = is_state->save_cursor;
384 else
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200385 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +0000386 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200387 {
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +0000388 // put the '" mark at the original position
389 curwin->w_cursor = is_state->save_cursor;
390 setpcmark();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200391 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +0000392 curwin->w_cursor = is_state->search_start;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200393 }
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +0000394 restore_viewstate(&is_state->old_viewstate);
395 highlight_match = FALSE;
396
397 // by default search all lines
398 search_first_line = 0;
399 search_last_line = MAXLNUM;
400
401 magic_overruled = is_state->magic_overruled_save;
402
403 validate_cursor(); // needed for TAB
404 status_redraw_all();
405 redraw_all_later(UPD_SOME_VALID);
406 if (call_update_screen)
407 update_screen(UPD_SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200408}
409
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200410/*
411 * Do 'incsearch' highlighting if desired.
412 */
413 static void
414may_do_incsearch_highlighting(
415 int firstc,
416 long count,
417 incsearch_state_T *is_state)
418{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200419 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200420 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200421 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200423 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200424#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200425 int next_char;
426 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200427 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100428 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200429
Bram Moolenaar198cb662018-09-06 21:44:17 +0200430 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100431 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200432 save_last_search_pattern();
433
Bram Moolenaara60053b2020-09-03 16:50:13 +0200434 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
435 &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200436 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200437 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200438 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200439 if (did_do_incsearch && vpeekc() == NUL)
440 // may have skipped a redraw, do it now
441 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200442 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200444
445 // If there is a character waiting, search and redraw later.
446 if (char_avail())
447 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200448 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200449 is_state->incsearch_postponed = TRUE;
450 return;
451 }
452 is_state->incsearch_postponed = FALSE;
453
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200454 if (search_first_line == 0)
455 // start at the original cursor position
456 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100457 else if (search_first_line > curbuf->b_ml.ml_line_count)
458 {
459 // start after the last line
460 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
461 curwin->w_cursor.col = MAXCOL;
462 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200463 else
464 {
465 // start at the first line in the range
466 curwin->w_cursor.lnum = search_first_line;
467 curwin->w_cursor.col = 0;
468 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 // Use the previous pattern for ":s//".
471 next_char = ccline.cmdbuff[skiplen + patlen];
472 use_last_pat = patlen == 0 && skiplen > 0
473 && ccline.cmdbuff[skiplen - 1] == next_char;
474
475 // If there is no pattern, don't do anything.
476 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200477 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200478 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 set_no_hlsearch(TRUE); // turn off previous highlight
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100480 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200481 }
482 else
483 {
484 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
485
486 cursor_off(); // so the user knows we're busy
487 out_flush();
488 ++emsg_off; // so it doesn't beep if bad expr
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200489 if (!p_hls)
490 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200491 if (search_first_line != 0)
492 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200493 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200494#ifdef FEAT_RELTIME
Bram Moolenaara80faa82020-04-12 19:37:17 +0200495 CLEAR_FIELD(sia);
Paul Ollis65745772022-06-05 16:55:54 +0100496 // Set the time limit to half a second.
497 sia.sa_tm = 500;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200498#endif
Bram Moolenaarc036e872020-02-21 21:30:52 +0100499 found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
John Marriott8c85a2a2024-05-20 19:18:26 +0200500 ccline.cmdbuff + skiplen, patlen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200501#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200502 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200503#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200504 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200505#endif
506 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200507 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200508 --emsg_off;
509
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200510 if (curwin->w_cursor.lnum < search_first_line
511 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200512 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200513 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200514 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200515 curwin->w_cursor = is_state->search_start;
516 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200517
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200518 // if interrupted while searching, behave like it failed
519 if (got_int)
520 {
521 (void)vpeekc(); // remove <C-C> from input stream
522 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200523 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200524 }
525 else if (char_avail())
526 // cancelled searching because a char was typed
527 is_state->incsearch_postponed = TRUE;
528 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200529 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200530 highlight_match = TRUE; // highlight position
531 else
532 highlight_match = FALSE; // remove highlight
533
534 // First restore the old curwin values, so the screen is positioned in the
535 // same way as the actual search command.
536 restore_viewstate(&is_state->old_viewstate);
537 changed_cline_bef_curs();
538 update_topline();
539
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200540 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200541 {
542 pos_T save_pos = curwin->w_cursor;
543
544 is_state->match_start = curwin->w_cursor;
545 set_search_match(&curwin->w_cursor);
546 validate_cursor();
547 end_pos = curwin->w_cursor;
548 is_state->match_end = end_pos;
549 curwin->w_cursor = save_pos;
550 }
551 else
552 end_pos = curwin->w_cursor; // shutup gcc 4
553
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200554 // Disable 'hlsearch' highlighting if the pattern matches everything.
555 // Avoids a flash when typing "foo\|".
556 if (!use_last_pat)
557 {
558 next_char = ccline.cmdbuff[skiplen + patlen];
559 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaard93a7fc2021-01-04 12:42:13 +0100560 if (empty_pattern(ccline.cmdbuff + skiplen, search_delim)
561 && !no_hlsearch)
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200562 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100563 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200564 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200565 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200566 ccline.cmdbuff[skiplen + patlen] = next_char;
567 }
568
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200569 validate_cursor();
Bram Moolenaarf32fb932022-11-17 11:34:38 +0000570
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200571 // May redraw the status line to show the cursor position.
572 if (p_ru && curwin->w_status_height > 0)
573 curwin->w_redr_status = TRUE;
574
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100575 update_screen(UPD_SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200576 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200577 restore_last_search_pattern();
578
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200579 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
580 // end of the pattern, e.g. for ":s/pat/".
581 if (ccline.cmdbuff[skiplen + patlen] != NUL)
582 curwin->w_cursor = is_state->search_start;
583 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200584 curwin->w_cursor = end_pos;
585
586 msg_starthere();
587 redrawcmdline();
588 is_state->did_incsearch = TRUE;
589}
590
591/*
592 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
593 * or previous match.
594 * Returns FAIL when jumping to cmdline_not_changed;
595 */
596 static int
597may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200598 int firstc,
599 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200600 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200601 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200603 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 pos_T t;
605 char_u *pat;
606 int search_flags = SEARCH_NOOF;
607 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200608 int save;
Bram Moolenaarc036e872020-02-21 21:30:52 +0100609 int search_delim;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610
Bram Moolenaar198cb662018-09-06 21:44:17 +0200611 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100612 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200613 save_last_search_pattern();
614
Bram Moolenaard8d957d2021-10-04 19:47:35 +0100615 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
616 &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200617 {
618 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200619 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200620 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200622 {
623 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200625 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200626
Bram Moolenaarc036e872020-02-21 21:30:52 +0100627 if (search_delim == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200628 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629 pat = last_search_pattern();
Bram Moolenaard8d957d2021-10-04 19:47:35 +0100630 if (pat == NULL)
631 {
632 restore_last_search_pattern();
633 return FAIL;
634 }
Bram Moolenaaref73a282018-08-11 19:02:22 +0200635 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200636 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200637 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200638 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200639 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200640
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200641 cursor_off();
642 out_flush();
643 if (c == Ctrl_G)
644 {
645 t = is_state->match_end;
646 if (LT_POS(is_state->match_start, is_state->match_end))
647 // Start searching at the end of the match not at the beginning of
648 // the next column.
649 (void)decl(&t);
650 search_flags += SEARCH_COL;
651 }
652 else
653 t = is_state->match_start;
654 if (!p_hls)
655 search_flags += SEARCH_KEEP;
656 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200657 save = pat[patlen];
658 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100659 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 c == Ctrl_G ? FORWARD : BACKWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +0200661 pat, patlen, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200662 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200663 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200664 if (i)
665 {
666 is_state->search_start = is_state->match_start;
667 is_state->match_end = t;
668 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200669 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200670 {
671 // Move just before the current match, so that when nv_search
672 // finishes the cursor will be put back on the match.
673 is_state->search_start = t;
674 (void)decl(&is_state->search_start);
675 }
676 else if (c == Ctrl_G && firstc == '?')
677 {
678 // Move just after the current match, so that when nv_search
679 // finishes the cursor will be put back on the match.
680 is_state->search_start = t;
681 (void)incl(&is_state->search_start);
682 }
683 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
684 {
685 // wrap around
686 is_state->search_start = t;
687 if (firstc == '?')
688 (void)incl(&is_state->search_start);
689 else
690 (void)decl(&is_state->search_start);
691 }
692
693 set_search_match(&is_state->match_end);
694 curwin->w_cursor = is_state->match_start;
695 changed_cline_bef_curs();
696 update_topline();
697 validate_cursor();
698 highlight_match = TRUE;
699 save_viewstate(&is_state->old_viewstate);
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100700 update_screen(UPD_NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200701 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200702 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200703 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200704 }
705 else
706 vim_beep(BO_ERROR);
707 restore_last_search_pattern();
708 return FAIL;
709}
710
711/*
712 * When CTRL-L typed: add character from the match to the pattern.
713 * May set "*c" to the added character.
714 * Return OK when jumping to cmdline_not_changed.
715 */
716 static int
717may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
718{
Bram Moolenaarc036e872020-02-21 21:30:52 +0100719 int skiplen, patlen, search_delim;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200720
Bram Moolenaar198cb662018-09-06 21:44:17 +0200721 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100722 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200723 save_last_search_pattern();
724
Bram Moolenaar9b7cce22020-06-06 15:14:08 +0200725 if (!do_incsearch_highlighting(firstc, &search_delim, is_state,
726 &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200727 {
728 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200729 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200730 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100731 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200732
733 // Add a character from under the cursor for 'incsearch'.
734 if (is_state->did_incsearch)
735 {
736 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200737 *c = gchar_cursor();
738 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200739 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200740 // If 'ignorecase' and 'smartcase' are set and the
741 // command line has no uppercase characters, convert
742 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200743 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200744 *c = MB_TOLOWER(*c);
Bram Moolenaarc036e872020-02-21 21:30:52 +0100745 if (*c == search_delim || vim_strchr((char_u *)(
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100746 magic_isset() ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200747 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200748 // put a backslash before special characters
749 stuffcharReadbuff(*c);
750 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200752 // add any composing characters
753 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
754 {
755 int save_c = *c;
756
757 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
758 {
759 curwin->w_cursor.col += mb_char2len(*c);
760 *c = gchar_cursor();
761 stuffcharReadbuff(*c);
762 }
763 *c = save_c;
764 }
765 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200766 }
767 }
768 return OK;
769}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200770#endif
771
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200772#ifdef FEAT_ARABIC
773/*
774 * Return TRUE if the command line has an Arabic character at or after "start"
775 * for "len" bytes.
776 */
777 static int
778cmdline_has_arabic(int start, int len)
779{
780 int j;
781 int mb_l;
782 int u8c;
783 char_u *p;
784 int u8cc[MAX_MCO];
785
786 if (!enc_utf8)
787 return FALSE;
788
789 for (j = start; j < start + len; j += mb_l)
790 {
791 p = ccline.cmdbuff + j;
792 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
793 mb_l = utfc_ptr2len_len(p, start + len - j);
794 if (ARABIC_CHAR(u8c))
795 return TRUE;
796 }
797 return FALSE;
798}
799#endif
800
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200801 void
802cmdline_init(void)
803{
Bram Moolenaara80faa82020-04-12 19:37:17 +0200804 CLEAR_FIELD(ccline);
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200805}
806
Bram Moolenaar66216052017-12-16 16:33:44 +0100807/*
zeertzjqffa4e9b2022-12-09 11:36:36 +0000808 * Handle CTRL-\ pressed in Command-line mode:
809 * - CTRL-\ CTRL-N goes to Normal mode
810 * - CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is set
811 * - CTRL-\ e prompts for an expression.
Bram Moolenaar9c929712020-09-07 22:05:28 +0200812 */
813 static int
zeertzjqffa4e9b2022-12-09 11:36:36 +0000814cmdline_handle_ctrl_bsl(int c, int *gotesc)
Bram Moolenaar9c929712020-09-07 22:05:28 +0200815{
816 ++no_mapping;
817 ++allow_keys;
818 c = plain_vgetc();
819 --no_mapping;
820 --allow_keys;
821
822 // CTRL-\ e doesn't work when obtaining an expression, unless it
823 // is in a mapping.
824 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
825 || (ccline.cmdfirstc == '=' && KeyTyped)
826#ifdef FEAT_EVAL
827 || cmdline_star > 0
828#endif
829 ))
830 {
831 vungetc(c);
832 return PROCESS_NEXT_KEY;
833 }
834
835#ifdef FEAT_EVAL
836 if (c == 'e')
837 {
838 char_u *p = NULL;
839 int len;
840
841 /*
842 * Replace the command line with the result of an expression.
zeertzjqffa4e9b2022-12-09 11:36:36 +0000843 * This will call getcmdline() recursively in get_expr_register().
Bram Moolenaar9c929712020-09-07 22:05:28 +0200844 */
845 if (ccline.cmdpos == ccline.cmdlen)
846 new_cmdpos = 99999; // keep it at the end
847 else
848 new_cmdpos = ccline.cmdpos;
849
850 c = get_expr_register();
851 if (c == '=')
852 {
zeertzjqffa4e9b2022-12-09 11:36:36 +0000853 // Evaluate the expression. Set "textlock" to avoid nasty things
854 // like going to another buffer.
zeertzjqcfe45652022-05-27 17:26:55 +0100855 ++textlock;
Bram Moolenaar9c929712020-09-07 22:05:28 +0200856 p = get_expr_line();
zeertzjqcfe45652022-05-27 17:26:55 +0100857 --textlock;
Bram Moolenaar9c929712020-09-07 22:05:28 +0200858
859 if (p != NULL)
860 {
861 len = (int)STRLEN(p);
862 if (realloc_cmdbuff(len + 1) == OK)
863 {
864 ccline.cmdlen = len;
865 STRCPY(ccline.cmdbuff, p);
866 vim_free(p);
867
868 // Restore the cursor or use the position set with
869 // set_cmdline_pos().
870 if (new_cmdpos > ccline.cmdlen)
871 ccline.cmdpos = ccline.cmdlen;
872 else
873 ccline.cmdpos = new_cmdpos;
874
875 KeyTyped = FALSE; // Don't do p_wc completion.
876 redrawcmd();
877 return CMDLINE_CHANGED;
878 }
879 vim_free(p);
880 }
881 }
882 beep_flush();
883 got_int = FALSE; // don't abandon the command line
884 did_emsg = FALSE;
885 emsg_on_display = FALSE;
886 redrawcmd();
887 return CMDLINE_NOT_CHANGED;
888 }
889#endif
890
891 if (c == Ctrl_G && p_im && restart_edit == 0)
892 restart_edit = 'a';
893 *gotesc = TRUE; // will free ccline.cmdbuff after putting it
894 // in history
895 return GOTO_NORMAL_MODE;
896}
897
898/*
899 * Completion for 'wildchar' or 'wildcharm' key.
900 * - hitting <ESC> twice means: abandon command line.
901 * - wildcard expansion is only done when the 'wildchar' key is really
902 * typed, not when it comes from a macro
903 * Returns CMDLINE_CHANGED if command line is changed or CMDLINE_NOT_CHANGED.
904 */
905 static int
906cmdline_wildchar_complete(
907 int c,
908 int escape,
909 int *did_wild_list,
910 int *wim_index_p,
911 expand_T *xp,
912 int *gotesc)
913{
914 int wim_index = *wim_index_p;
915 int res;
916 int j;
917 int options = WILD_NO_BEEP;
918
919 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
920 options |= WILD_BUFLASTUSED;
921 if (xp->xp_numfiles > 0) // typed p_wc at least twice
922 {
923 // if 'wildmode' contains "list" may still need to list
924 if (xp->xp_numfiles > 1
925 && !*did_wild_list
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000926 && ((wim_flags[wim_index] & WIM_LIST)
Bram Moolenaar54162322022-08-26 16:58:51 +0100927 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)))
Bram Moolenaar9c929712020-09-07 22:05:28 +0200928 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000929 (void)showmatches(xp,
930 p_wmnu && ((wim_flags[wim_index] & WIM_LIST) == 0));
Bram Moolenaar9c929712020-09-07 22:05:28 +0200931 redrawcmd();
932 *did_wild_list = TRUE;
933 }
934 if (wim_flags[wim_index] & WIM_LONGEST)
935 res = nextwild(xp, WILD_LONGEST, options, escape);
936 else if (wim_flags[wim_index] & WIM_FULL)
937 res = nextwild(xp, WILD_NEXT, options, escape);
938 else
939 res = OK; // don't insert 'wildchar' now
940 }
941 else // typed p_wc first time
942 {
943 wim_index = 0;
944 j = ccline.cmdpos;
945 // if 'wildmode' first contains "longest", get longest
946 // common part
947 if (wim_flags[0] & WIM_LONGEST)
948 res = nextwild(xp, WILD_LONGEST, options, escape);
949 else
950 res = nextwild(xp, WILD_EXPAND_KEEP, options, escape);
951
952 // if interrupted while completing, behave like it failed
953 if (got_int)
954 {
955 (void)vpeekc(); // remove <C-C> from input stream
956 got_int = FALSE; // don't abandon the command line
957 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
Bram Moolenaar9c929712020-09-07 22:05:28 +0200958 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaar9c929712020-09-07 22:05:28 +0200959 *wim_index_p = wim_index;
960 return CMDLINE_CHANGED;
961 }
962
963 // when more than one match, and 'wildmode' first contains
964 // "list", or no change and 'wildmode' contains "longest,list",
965 // list all matches
966 if (res == OK && xp->xp_numfiles > 1)
967 {
968 // a "longest" that didn't do anything is skipped (but not
969 // "list:longest")
970 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
971 wim_index = 1;
972 if ((wim_flags[wim_index] & WIM_LIST)
Bram Moolenaar54162322022-08-26 16:58:51 +0100973 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0))
Bram Moolenaar9c929712020-09-07 22:05:28 +0200974 {
975 if (!(wim_flags[0] & WIM_LONGEST))
976 {
Bram Moolenaar9c929712020-09-07 22:05:28 +0200977 int p_wmnu_save = p_wmnu;
Bram Moolenaar54162322022-08-26 16:58:51 +0100978
Bram Moolenaar9c929712020-09-07 22:05:28 +0200979 p_wmnu = 0;
Bram Moolenaar54162322022-08-26 16:58:51 +0100980
Bram Moolenaar9c929712020-09-07 22:05:28 +0200981 // remove match
982 nextwild(xp, WILD_PREV, 0, escape);
Bram Moolenaar9c929712020-09-07 22:05:28 +0200983 p_wmnu = p_wmnu_save;
Bram Moolenaar9c929712020-09-07 22:05:28 +0200984 }
Bram Moolenaar9c929712020-09-07 22:05:28 +0200985 (void)showmatches(xp, p_wmnu
986 && ((wim_flags[wim_index] & WIM_LIST) == 0));
Bram Moolenaar9c929712020-09-07 22:05:28 +0200987 redrawcmd();
988 *did_wild_list = TRUE;
989 if (wim_flags[wim_index] & WIM_LONGEST)
990 nextwild(xp, WILD_LONGEST, options, escape);
991 else if (wim_flags[wim_index] & WIM_FULL)
992 nextwild(xp, WILD_NEXT, options, escape);
993 }
994 else
995 vim_beep(BO_WILD);
996 }
Bram Moolenaar9c929712020-09-07 22:05:28 +0200997 else if (xp->xp_numfiles == -1)
998 xp->xp_context = EXPAND_NOTHING;
Bram Moolenaar9c929712020-09-07 22:05:28 +0200999 }
1000 if (wim_index < 3)
1001 ++wim_index;
1002 if (c == ESC)
1003 *gotesc = TRUE;
1004
1005 *wim_index_p = wim_index;
1006 return (res == OK) ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
1007}
1008
1009/*
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001010 * Handle backspace, delete and CTRL-W keys in the command-line mode.
1011 * Returns:
1012 * CMDLINE_NOT_CHANGED - if the command line is not changed
1013 * CMDLINE_CHANGED - if the command line is changed
1014 * GOTO_NORMAL_MODE - go back to normal mode
1015 */
1016 static int
1017cmdline_erase_chars(
1018 int c,
1019 int indent
1020#ifdef FEAT_SEARCH_EXTRA
1021 , incsearch_state_T *isp
1022#endif
1023 )
1024{
1025 int i;
1026 int j;
1027
1028 if (c == K_KDEL)
1029 c = K_DEL;
1030
1031 /*
1032 * Delete current character is the same as backspace on next
1033 * character, except at end of line.
1034 */
1035 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1036 ++ccline.cmdpos;
1037 if (has_mbyte && c == K_DEL)
1038 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1039 ccline.cmdbuff + ccline.cmdpos);
1040 if (ccline.cmdpos > 0)
1041 {
1042 char_u *p;
1043
1044 j = ccline.cmdpos;
1045 p = ccline.cmdbuff + j;
1046 if (has_mbyte)
1047 {
1048 p = mb_prevptr(ccline.cmdbuff, p);
1049 if (c == Ctrl_W)
1050 {
1051 while (p > ccline.cmdbuff && vim_isspace(*p))
1052 p = mb_prevptr(ccline.cmdbuff, p);
1053 i = mb_get_class(p);
1054 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1055 p = mb_prevptr(ccline.cmdbuff, p);
1056 if (mb_get_class(p) != i)
1057 p += (*mb_ptr2len)(p);
1058 }
1059 }
1060 else if (c == Ctrl_W)
1061 {
1062 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1063 --p;
Bram Moolenaaref02f162022-05-07 10:49:10 +01001064 if (p > ccline.cmdbuff)
1065 {
1066 i = vim_iswordc(p[-1]);
1067 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1068 && vim_iswordc(p[-1]) == i)
1069 --p;
1070 }
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001071 }
1072 else
1073 --p;
1074 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1075 ccline.cmdlen -= j - ccline.cmdpos;
1076 i = ccline.cmdpos;
1077 while (i < ccline.cmdlen)
1078 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1079
1080 // Truncate at the end, required for multi-byte chars.
1081 ccline.cmdbuff[ccline.cmdlen] = NUL;
1082#ifdef FEAT_SEARCH_EXTRA
1083 if (ccline.cmdlen == 0)
1084 {
1085 isp->search_start = isp->save_cursor;
1086 // save view settings, so that the screen
1087 // won't be restored at the wrong position
1088 isp->old_viewstate = isp->init_viewstate;
1089 }
1090#endif
1091 redrawcmd();
1092 }
1093 else if (ccline.cmdlen == 0 && c != Ctrl_W
1094 && ccline.cmdprompt == NULL && indent == 0)
1095 {
1096 // In ex and debug mode it doesn't make sense to return.
1097 if (exmode_active
1098#ifdef FEAT_EVAL
1099 || ccline.cmdfirstc == '>'
1100#endif
1101 )
1102 return CMDLINE_NOT_CHANGED;
1103
1104 VIM_CLEAR(ccline.cmdbuff); // no commandline to return
1105 if (!cmd_silent)
1106 {
1107#ifdef FEAT_RIGHTLEFT
1108 if (cmdmsg_rl)
1109 msg_col = Columns;
1110 else
1111#endif
1112 msg_col = 0;
1113 msg_putchar(' '); // delete ':'
1114 }
1115#ifdef FEAT_SEARCH_EXTRA
1116 if (ccline.cmdlen == 0)
1117 isp->search_start = isp->save_cursor;
1118#endif
1119 redraw_cmdline = TRUE;
1120 return GOTO_NORMAL_MODE;
1121 }
1122 return CMDLINE_CHANGED;
1123}
1124
1125/*
1126 * Handle the CTRL-^ key in the command-line mode and toggle the use of the
1127 * language :lmap mappings and/or Input Method.
1128 */
1129 static void
1130cmdline_toggle_langmap(long *b_im_ptr)
1131{
Bram Moolenaar24959102022-05-07 20:01:16 +01001132 if (map_to_exists_mode((char_u *)"", MODE_LANGMAP, FALSE))
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001133 {
1134 // ":lmap" mappings exists, toggle use of mappings.
Bram Moolenaar24959102022-05-07 20:01:16 +01001135 State ^= MODE_LANGMAP;
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001136#ifdef HAVE_INPUT_METHOD
1137 im_set_active(FALSE); // Disable input method
1138#endif
1139 if (b_im_ptr != NULL)
1140 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001141 if (State & MODE_LANGMAP)
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001142 *b_im_ptr = B_IMODE_LMAP;
1143 else
1144 *b_im_ptr = B_IMODE_NONE;
1145 }
1146 }
1147#ifdef HAVE_INPUT_METHOD
1148 else
1149 {
1150 // There are no ":lmap" mappings, toggle IM. When
1151 // 'imdisable' is set don't try getting the status, it's
1152 // always off.
1153 if ((p_imdisable && b_im_ptr != NULL)
1154 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1155 {
1156 im_set_active(FALSE); // Disable input method
1157 if (b_im_ptr != NULL)
1158 *b_im_ptr = B_IMODE_NONE;
1159 }
1160 else
1161 {
1162 im_set_active(TRUE); // Enable input method
1163 if (b_im_ptr != NULL)
1164 *b_im_ptr = B_IMODE_IM;
1165 }
1166 }
1167#endif
1168 if (b_im_ptr != NULL)
1169 {
1170 if (b_im_ptr == &curbuf->b_p_iminsert)
1171 set_iminsert_global();
1172 else
1173 set_imsearch_global();
1174 }
1175#ifdef CURSOR_SHAPE
1176 ui_cursor_shape(); // may show different cursor shape
1177#endif
1178#if defined(FEAT_KEYMAP)
1179 // Show/unshow value of 'keymap' in status lines later.
1180 status_redraw_curbuf();
1181#endif
1182}
1183
1184/*
1185 * Handle the CTRL-R key in the command-line mode and insert the contents of a
1186 * numbered or named register.
1187 */
1188 static int
1189cmdline_insert_reg(int *gotesc UNUSED)
1190{
1191 int i;
1192 int c;
K.Takatac4b7dec2023-02-10 21:38:44 +00001193 int literally = FALSE;
Bram Moolenaar6689df02022-06-22 18:14:29 +01001194#ifdef FEAT_EVAL
Bram Moolenaar6046ade2022-06-22 13:51:54 +01001195 int save_new_cmdpos = new_cmdpos;
Bram Moolenaar6689df02022-06-22 18:14:29 +01001196#endif
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001197
1198#ifdef USE_ON_FLY_SCROLL
1199 dont_scroll = TRUE; // disallow scrolling here
1200#endif
1201 putcmdline('"', TRUE);
1202 ++no_mapping;
1203 ++allow_keys;
1204 i = c = plain_vgetc(); // CTRL-R <char>
1205 if (i == Ctrl_O)
1206 i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
1207 if (i == Ctrl_R)
1208 c = plain_vgetc(); // CTRL-R CTRL-R <char>
1209 extra_char = NUL;
1210 --no_mapping;
1211 --allow_keys;
1212#ifdef FEAT_EVAL
1213 /*
1214 * Insert the result of an expression.
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001215 */
1216 new_cmdpos = -1;
1217 if (c == '=')
1218 {
1219 if (ccline.cmdfirstc == '=' // can't do this recursively
1220 || cmdline_star > 0) // or when typing a password
1221 {
1222 beep_flush();
1223 c = ESC;
1224 }
1225 else
1226 c = get_expr_register();
1227 }
1228#endif
1229 if (c != ESC) // use ESC to cancel inserting register
1230 {
K.Takata9cf6ab12023-05-29 16:08:08 +01001231 literally = i == Ctrl_R
1232#ifdef FEAT_CLIPBOARD
1233 || (clip_star.available && c == '*')
1234 || (clip_plus.available && c == '+')
1235#endif
1236 ;
K.Takatac4b7dec2023-02-10 21:38:44 +00001237 cmdline_paste(c, literally, FALSE);
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001238
1239#ifdef FEAT_EVAL
1240 // When there was a serious error abort getting the
1241 // command line.
1242 if (aborting())
1243 {
1244 *gotesc = TRUE; // will free ccline.cmdbuff after
1245 // putting it in history
1246 return GOTO_NORMAL_MODE;
1247 }
1248#endif
1249 KeyTyped = FALSE; // Don't do p_wc completion.
1250#ifdef FEAT_EVAL
1251 if (new_cmdpos >= 0)
1252 {
1253 // set_cmdline_pos() was used
1254 if (new_cmdpos > ccline.cmdlen)
1255 ccline.cmdpos = ccline.cmdlen;
1256 else
1257 ccline.cmdpos = new_cmdpos;
1258 }
1259#endif
1260 }
Bram Moolenaar6689df02022-06-22 18:14:29 +01001261#ifdef FEAT_EVAL
Bram Moolenaar6046ade2022-06-22 13:51:54 +01001262 new_cmdpos = save_new_cmdpos;
Bram Moolenaar6689df02022-06-22 18:14:29 +01001263#endif
Bram Moolenaar6046ade2022-06-22 13:51:54 +01001264
Bram Moolenaar796139a2021-05-18 21:38:45 +02001265 // remove the double quote
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001266 redrawcmd();
Bram Moolenaar796139a2021-05-18 21:38:45 +02001267
zeertzjq412e0e42023-02-11 10:34:07 +00001268 // With "literally": the command line has already changed.
1269 // Else: the text has been stuffed, but the command line didn't change yet.
1270 return literally ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02001271}
1272
1273/*
1274 * Handle the Left and Right mouse clicks in the command-line mode.
1275 */
1276 static void
1277cmdline_left_right_mouse(int c, int *ignore_drag_release)
1278{
1279 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1280 *ignore_drag_release = TRUE;
1281 else
1282 *ignore_drag_release = FALSE;
1283# ifdef FEAT_GUI
1284 // When GUI is active, also move when 'mouse' is empty
1285 if (!gui.in_use)
1286# endif
1287 if (!mouse_has(MOUSE_COMMAND))
1288 return;
1289# ifdef FEAT_CLIPBOARD
1290 if (mouse_row < cmdline_row && clip_star.available)
1291 {
1292 int button, is_click, is_drag;
1293
1294 /*
1295 * Handle modeless selection.
1296 */
1297 button = get_mouse_button(KEY2TERMCAP1(c),
1298 &is_click, &is_drag);
1299 if (mouse_model_popup() && button == MOUSE_LEFT
1300 && (mod_mask & MOD_MASK_SHIFT))
1301 {
1302 // Translate shift-left to right button.
1303 button = MOUSE_RIGHT;
1304 mod_mask &= ~MOD_MASK_SHIFT;
1305 }
1306 clip_modeless(button, is_click, is_drag);
1307 return;
1308 }
1309# endif
1310
1311 set_cmdspos();
1312 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1313 ++ccline.cmdpos)
1314 {
1315 int i;
1316
1317 i = cmdline_charsize(ccline.cmdpos);
1318 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1319 && mouse_col < ccline.cmdspos % Columns + i)
1320 break;
1321 if (has_mbyte)
1322 {
1323 // Count ">" for double-wide char that doesn't fit.
1324 correct_cmdspos(ccline.cmdpos, i);
1325 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
1326 + ccline.cmdpos) - 1;
1327 }
1328 ccline.cmdspos += i;
1329 }
1330}
1331
1332/*
1333 * Handle the Up, Down, Page Up, Page down, CTRL-N and CTRL-P key in the
1334 * command-line mode. The pressed key is in 'c'.
1335 * Returns:
1336 * CMDLINE_NOT_CHANGED - if the command line is not changed
1337 * CMDLINE_CHANGED - if the command line is changed
1338 * GOTO_NORMAL_MODE - go back to normal mode
1339 */
1340 static int
1341cmdline_browse_history(
1342 int c,
1343 int firstc,
1344 char_u **curcmdstr,
1345 int histype,
1346 int *hiscnt_p,
1347 expand_T *xp)
1348{
1349 int i;
1350 int j;
1351 char_u *lookfor = *curcmdstr;
1352 int hiscnt = *hiscnt_p;
1353 int res;
1354
1355 if (get_hislen() == 0 || firstc == NUL) // no history
1356 return CMDLINE_NOT_CHANGED;
1357
1358 i = hiscnt;
1359
1360 // save current command string so it can be restored later
1361 if (lookfor == NULL)
1362 {
1363 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1364 return CMDLINE_NOT_CHANGED;
1365 lookfor[ccline.cmdpos] = NUL;
1366 }
1367
1368 j = (int)STRLEN(lookfor);
1369 for (;;)
1370 {
1371 // one step backwards
1372 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
1373 || c == K_PAGEUP || c == K_KPAGEUP)
1374 {
1375 if (hiscnt == get_hislen()) // first time
1376 hiscnt = *get_hisidx(histype);
1377 else if (hiscnt == 0 && *get_hisidx(histype)
1378 != get_hislen() - 1)
1379 hiscnt = get_hislen() - 1;
1380 else if (hiscnt != *get_hisidx(histype) + 1)
1381 --hiscnt;
1382 else // at top of list
1383 {
1384 hiscnt = i;
1385 break;
1386 }
1387 }
1388 else // one step forwards
1389 {
1390 // on last entry, clear the line
1391 if (hiscnt == *get_hisidx(histype))
1392 {
1393 hiscnt = get_hislen();
1394 break;
1395 }
1396
1397 // not on a history line, nothing to do
1398 if (hiscnt == get_hislen())
1399 break;
1400 if (hiscnt == get_hislen() - 1) // wrap around
1401 hiscnt = 0;
1402 else
1403 ++hiscnt;
1404 }
1405 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr
1406 == NULL)
1407 {
1408 hiscnt = i;
1409 break;
1410 }
1411 if ((c != K_UP && c != K_DOWN)
1412 || hiscnt == i
1413 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
1414 lookfor, (size_t)j) == 0)
1415 break;
1416 }
1417
1418 if (hiscnt != i) // jumped to other entry
1419 {
1420 char_u *p;
1421 int len;
1422 int old_firstc;
1423
1424 VIM_CLEAR(ccline.cmdbuff);
1425 xp->xp_context = EXPAND_NOTHING;
1426 if (hiscnt == get_hislen())
1427 p = lookfor; // back to the old one
1428 else
1429 p = get_histentry(histype)[hiscnt].hisstr;
1430
1431 if (histype == HIST_SEARCH
1432 && p != lookfor
1433 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1434 {
1435 // Correct for the separator character used when
1436 // adding the history entry vs the one used now.
1437 // First loop: count length.
1438 // Second loop: copy the characters.
1439 for (i = 0; i <= 1; ++i)
1440 {
1441 len = 0;
1442 for (j = 0; p[j] != NUL; ++j)
1443 {
1444 // Replace old sep with new sep, unless it is
1445 // escaped.
1446 if (p[j] == old_firstc
1447 && (j == 0 || p[j - 1] != '\\'))
1448 {
1449 if (i > 0)
1450 ccline.cmdbuff[len] = firstc;
1451 }
1452 else
1453 {
1454 // Escape new sep, unless it is already
1455 // escaped.
1456 if (p[j] == firstc
1457 && (j == 0 || p[j - 1] != '\\'))
1458 {
1459 if (i > 0)
1460 ccline.cmdbuff[len] = '\\';
1461 ++len;
1462 }
1463 if (i > 0)
1464 ccline.cmdbuff[len] = p[j];
1465 }
1466 ++len;
1467 }
1468 if (i == 0)
1469 {
1470 alloc_cmdbuff(len);
1471 if (ccline.cmdbuff == NULL)
1472 {
1473 res = GOTO_NORMAL_MODE;
1474 goto done;
1475 }
1476 }
1477 }
1478 ccline.cmdbuff[len] = NUL;
1479 }
1480 else
1481 {
1482 alloc_cmdbuff((int)STRLEN(p));
1483 if (ccline.cmdbuff == NULL)
1484 {
1485 res = GOTO_NORMAL_MODE;
1486 goto done;
1487 }
1488 STRCPY(ccline.cmdbuff, p);
1489 }
1490
1491 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1492 redrawcmd();
1493 res = CMDLINE_CHANGED;
1494 goto done;
1495 }
1496 beep_flush();
1497 res = CMDLINE_NOT_CHANGED;
1498
1499done:
1500 *curcmdstr = lookfor;
1501 *hiscnt_p = hiscnt;
1502 return res;
1503}
1504
1505/*
Bram Moolenaar9c929712020-09-07 22:05:28 +02001506 * Initialize the current command-line info.
1507 */
1508 static int
1509init_ccline(int firstc, int indent)
1510{
1511 ccline.overstrike = FALSE; // always start in insert mode
1512
1513 /*
1514 * set some variables for redrawcmd()
1515 */
1516 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
1517 ccline.cmdindent = (firstc > 0 ? indent : 0);
1518
1519 // alloc initial ccline.cmdbuff
Bram Moolenaar85b67472022-01-25 11:55:02 +00001520 alloc_cmdbuff(indent + 50);
Bram Moolenaar9c929712020-09-07 22:05:28 +02001521 if (ccline.cmdbuff == NULL)
1522 return FAIL;
1523 ccline.cmdlen = ccline.cmdpos = 0;
1524 ccline.cmdbuff[0] = NUL;
1525 sb_text_start_cmdline();
1526
1527 // autoindent for :insert and :append
1528 if (firstc <= 0)
1529 {
1530 vim_memset(ccline.cmdbuff, ' ', indent);
1531 ccline.cmdbuff[indent] = NUL;
1532 ccline.cmdpos = indent;
1533 ccline.cmdspos = indent;
1534 ccline.cmdlen = indent;
1535 }
1536
1537 return OK;
1538}
1539
1540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 * getcmdline() - accept a command line starting with firstc.
1542 *
1543 * firstc == ':' get ":" command line.
1544 * firstc == '/' or '?' get search pattern
1545 * firstc == '=' get expression
1546 * firstc == '@' get text for input() function
1547 * firstc == '>' get text for debug mode
1548 * firstc == NUL get text for :insert command
1549 * firstc == -1 like NUL, and break on CTRL-C
1550 *
1551 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
1552 * command line.
1553 *
1554 * Careful: getcmdline() can be called recursively!
1555 *
1556 * Return pointer to allocated string if there is a commandline, NULL
1557 * otherwise.
1558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001560getcmdline(
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00001561 int firstc,
1562 long count, // only used for incremental search
1563 int indent, // indent for inside conditionals
1564 getline_opt_T do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001565{
1566 return getcmdline_int(firstc, count, indent, TRUE);
1567}
1568
1569 static char_u *
1570getcmdline_int(
1571 int firstc,
1572 long count UNUSED, // only used for incremental search
1573 int indent, // indent for inside conditionals
Bram Moolenaar9c929712020-09-07 22:05:28 +02001574 int clear_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar51f0bfb2022-05-17 20:11:02 +01001576 static int depth = 0; // call depth
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 int c;
1578 int i;
1579 int j;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001580 int gotesc = FALSE; // TRUE when <ESC> just typed
1581 int do_abbr; // when TRUE check for abbr.
1582 char_u *lookfor = NULL; // string to match
1583 int hiscnt; // current history line in use
1584 int histype; // history type to be used
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001586 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001588 int did_wild_list = FALSE; // did wild_list() recently
1589 int wim_index = 0; // index in wim_flags[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 int res;
1591 int save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001592 int save_State = State; // remember State when called
zeertzjqbc6f9672024-06-21 07:51:40 +02001593 int prev_cmdpos = -1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001594 int some_key_typed = FALSE; // one of the keys was typed
1595 // mouse drag and release events are ignored, unless they are
1596 // preceded with a mouse down event
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598#ifdef FEAT_EVAL
1599 int break_ctrl_c = FALSE;
1600#endif
1601 expand_T xpc;
1602 long *b_im_ptr = NULL;
Bram Moolenaar1c3dd8d2022-09-17 19:43:23 +01001603 buf_T *b_im_ptr_buf = NULL; // buffer where b_im_ptr is valid
Bram Moolenaar66b51422019-08-18 21:44:12 +02001604 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02001605 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001606 int cmdline_type;
Yee Cheng Chin209ec902023-10-17 10:56:25 +02001607 int wild_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar51f0bfb2022-05-17 20:11:02 +01001609 // one recursion level deeper
1610 ++depth;
1611
Bram Moolenaar438d1762018-09-30 17:11:48 +02001612 if (ccline.cmdbuff != NULL)
1613 {
1614 // Being called recursively. Since ccline is global, we need to save
1615 // the current buffer and restore it when returning.
1616 save_cmdline(&save_ccline);
1617 did_save_ccline = TRUE;
1618 }
Bram Moolenaar9c929712020-09-07 22:05:28 +02001619 if (clear_ccline)
Bram Moolenaara80faa82020-04-12 19:37:17 +02001620 CLEAR_FIELD(ccline);
Bram Moolenaar438d1762018-09-30 17:11:48 +02001621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#ifdef FEAT_EVAL
1623 if (firstc == -1)
1624 {
1625 firstc = NUL;
1626 break_ctrl_c = TRUE;
1627 }
1628#endif
1629#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001630 // start without Hebrew mapping for a command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (firstc == ':' || firstc == '=' || firstc == '>')
1632 cmd_hkmap = 0;
1633#endif
1634
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001636 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638
Bram Moolenaar9c929712020-09-07 22:05:28 +02001639 if (init_ccline(firstc, indent) != OK)
Bram Moolenaar438d1762018-09-30 17:11:48 +02001640 goto theend; // out of memory
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001641
Bram Moolenaar51f0bfb2022-05-17 20:11:02 +01001642 if (depth == 50)
1643 {
1644 // Somehow got into a loop recursively calling getcmdline(), bail out.
1645 emsg(_(e_command_too_recursive));
1646 goto theend;
1647 }
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001650 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
1652#ifdef FEAT_RIGHTLEFT
1653 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
1654 && (firstc == '/' || firstc == '?'))
1655 cmdmsg_rl = TRUE;
1656 else
1657 cmdmsg_rl = FALSE;
1658#endif
1659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001660 redir_off = TRUE; // don't redirect the typed command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (!cmd_silent)
1662 {
1663 i = msg_scrolled;
Bram Moolenaar13608d82022-08-29 15:06:50 +01001664 msg_scrolled = 0; // avoid wait_return() message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 gotocmdline(TRUE);
1666 msg_scrolled += i;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001667 redrawcmdprompt(); // draw prompt or indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 set_cmdspos();
1669 }
1670 xpc.xp_context = EXPAND_NOTHING;
1671 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001672#ifndef BACKSLASH_IN_FILENAME
1673 xpc.xp_shell = FALSE;
1674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001676#if defined(FEAT_EVAL)
1677 if (ccline.input_fn)
1678 {
1679 xpc.xp_context = ccline.xp_context;
1680 xpc.xp_pattern = ccline.cmdbuff;
1681 xpc.xp_arg = ccline.xp_arg;
1682 }
1683#endif
1684
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 /*
1686 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
1687 * doing ":@0" when register 0 doesn't contain a CR.
1688 */
1689 msg_scroll = FALSE;
1690
Bram Moolenaar24959102022-05-07 20:01:16 +01001691 State = MODE_CMDLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 if (firstc == '/' || firstc == '?' || firstc == '@')
1694 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001695 // Use ":lmap" mappings for search pattern and input().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
1697 b_im_ptr = &curbuf->b_p_iminsert;
1698 else
1699 b_im_ptr = &curbuf->b_p_imsearch;
Bram Moolenaar1c3dd8d2022-09-17 19:43:23 +01001700 b_im_ptr_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (*b_im_ptr == B_IMODE_LMAP)
Bram Moolenaar24959102022-05-07 20:01:16 +01001702 State |= MODE_LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001703#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 im_set_active(*b_im_ptr == B_IMODE_IM);
1705#endif
1706 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001707#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 else if (p_imcmdline)
1709 im_set_active(TRUE);
1710#endif
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001714 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001717 // When inside an autocommand for writing "exiting" may be set and
1718 // terminal mode set to cooked. Need to set raw mode here then.
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001719 settmode(TMODE_RAW);
1720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001721 // Trigger CmdlineEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001722 cmdline_type = firstc == NUL ? '-' : firstc;
1723 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001724#ifdef FEAT_EVAL
1725 if (!debug_mode)
LemonBoy2bf52dd2022-04-09 18:17:34 +01001726 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001727#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 init_history();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001730 hiscnt = get_hislen(); // set hiscnt to impossible history value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733#ifdef FEAT_DIGRAPHS
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001734 do_digraph(-1); // init digraph typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735#endif
1736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001737 // If something above caused an error, reset the flags, we do want to type
1738 // and execute commands. Display may be messed up a bit.
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001739 if (did_emsg)
1740 redrawcmd();
Bram Moolenaarce0b7572021-04-01 18:47:14 +02001741
Bram Moolenaar160a2b42021-04-01 21:57:46 +02001742#ifdef FEAT_STL_OPT
Bram Moolenaarce0b7572021-04-01 18:47:14 +02001743 // Redraw the statusline in case it uses the current mode using the mode()
1744 // function.
Bram Moolenaard8db8382021-04-08 18:27:53 +02001745 if (!cmd_silent && msg_scrolled == 0)
Bram Moolenaarce0b7572021-04-01 18:47:14 +02001746 {
Bram Moolenaard8db8382021-04-08 18:27:53 +02001747 int found_one = FALSE;
1748 win_T *wp;
1749
1750 FOR_ALL_WINDOWS(wp)
1751 if (*p_stl != NUL || *wp->w_p_stl != NUL)
1752 {
1753 wp->w_redr_status = TRUE;
1754 found_one = TRUE;
1755 }
zeertzjq6791adc2022-07-26 20:42:25 +01001756
1757 if (*p_tal != NUL)
1758 {
1759 redraw_tabline = TRUE;
1760 found_one = TRUE;
1761 }
1762
Bram Moolenaard8db8382021-04-08 18:27:53 +02001763 if (found_one)
1764 redraw_statuslines();
Bram Moolenaarce0b7572021-04-01 18:47:14 +02001765 }
Bram Moolenaar160a2b42021-04-01 21:57:46 +02001766#endif
Bram Moolenaarce0b7572021-04-01 18:47:14 +02001767
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001768 did_emsg = FALSE;
1769 got_int = FALSE;
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 /*
1772 * Collect the command string, handling editing keys.
1773 */
1774 for (;;)
1775 {
Bram Moolenaard832c3c2021-05-15 15:09:06 +02001776 int trigger_cmdlinechanged = TRUE;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001777 int end_wildmenu;
Bram Moolenaard832c3c2021-05-15 15:09:06 +02001778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001779 redir_off = TRUE; // Don't redirect the typed command.
1780 // Repeated, because a ":redir" inside
1781 // completion may switch it on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001783 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001785 quit_more = FALSE; // reset after CTRL-D which had a more-prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001787 did_emsg = FALSE; // There can't really be a reason why an error
1788 // that occurs while typing a command should
1789 // cause the command not to be executed.
Bram Moolenaar72532d32018-04-07 19:09:09 +02001790
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001791 // Trigger SafeState if nothing is pending.
1792 may_trigger_safestate(xpc.xp_numfiles <= 0);
1793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001794 // Get a character. Ignore K_IGNORE and K_NOP, they should not do
1795 // anything, such as stop completion.
Bram Moolenaar30405d32008-01-02 20:55:27 +00001796 do
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001797 {
1798 cursorcmd(); // set the cursor on the right spot
Bram Moolenaar30405d32008-01-02 20:55:27 +00001799 c = safe_vgetc();
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001800 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001801
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001802 if (c == K_COMMAND || c == K_SCRIPT_COMMAND)
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001803 {
Bram Moolenaar148bcd32022-12-09 12:41:32 +00001804 int clen = ccline.cmdlen;
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001805 int cc_count = aucmd_cmdline_changed_count;
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001806
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001807 if (do_cmdkey_command(c, DOCMD_NOWAIT) == OK)
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001808 {
Bram Moolenaar148bcd32022-12-09 12:41:32 +00001809 // Do not trigger CmdlineChanged below if:
1810 // - the length of the command line didn't change
1811 // - the <Cmd> mapping already triggered the event
1812 if (clen == ccline.cmdlen
1813 || cc_count != aucmd_cmdline_changed_count)
Bram Moolenaard832c3c2021-05-15 15:09:06 +02001814 trigger_cmdlinechanged = FALSE;
Bram Moolenaar847fe7d2021-05-15 13:19:16 +02001815 goto cmdline_changed;
1816 }
1817 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01001818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (KeyTyped)
1820 {
1821 some_key_typed = TRUE;
1822#ifdef FEAT_RIGHTLEFT
1823 if (cmd_hkmap)
1824 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (cmdmsg_rl && !KeyStuffed)
1826 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001827 // Invert horizontal movements and operations. Only when
1828 // typed by the user directly, not when the result of a
1829 // mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 switch (c)
1831 {
1832 case K_RIGHT: c = K_LEFT; break;
1833 case K_S_RIGHT: c = K_S_LEFT; break;
1834 case K_C_RIGHT: c = K_C_LEFT; break;
1835 case K_LEFT: c = K_RIGHT; break;
1836 case K_S_LEFT: c = K_S_RIGHT; break;
1837 case K_C_LEFT: c = K_C_RIGHT; break;
1838 }
1839 }
1840#endif
1841 }
1842
1843 /*
1844 * Ignore got_int when CTRL-C was typed here.
1845 * Don't ignore it in :global, we really need to break then, e.g., for
1846 * ":g/pat/normal /pat" (without the <CR>).
1847 * Don't ignore it for the input() function.
1848 */
1849 if ((c == Ctrl_C
1850#ifdef UNIX
1851 || c == intr_char
1852#endif
1853 )
1854#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1855 && firstc != '@'
1856#endif
1857#ifdef FEAT_EVAL
zeertzjq3cfae392022-07-26 17:48:13 +01001858 // do clear got_int in Ex mode to avoid infinite Ctrl-C loop
1859 && (!break_ctrl_c || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#endif
1861 && !global_busy)
1862 got_int = FALSE;
1863
Bram Moolenaard7663c22019-08-06 21:59:57 +02001864 // free old command line when finished moving around in the history
1865 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001867 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001868 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 && c != K_PAGEDOWN && c != K_PAGEUP
1870 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001871 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001873 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001876 * When there are matching completions to select <S-Tab> works like
1877 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001879 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 c = Ctrl_P;
1881
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001882 if (p_wmnu)
1883 c = wildmenu_translate_key(&ccline, c, &xpc, did_wild_list);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001884
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02001885 int key_is_wc = (c == p_wc && KeyTyped) || c == p_wcm;
Yee Cheng Chin209ec902023-10-17 10:56:25 +02001886 if ((cmdline_pum_active() || did_wild_list) && !key_is_wc)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001887 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001888 // Ctrl-Y: Accept the current selection and close the popup menu.
1889 // Ctrl-E: cancel the cmdline popup menu and return the original
1890 // text.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001891 if (c == Ctrl_E || c == Ctrl_Y)
1892 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001893 wild_type = (c == Ctrl_E) ? WILD_CANCEL : WILD_APPLY;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001894 if (nextwild(&xpc, wild_type, WILD_NO_BEEP,
1895 firstc != '@') == FAIL)
1896 break;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001897 }
1898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001900 // The wildmenu is cleared if the pressed key is not used for
1901 // navigating the wild menu (i.e. the key is not 'wildchar' or
1902 // 'wildcharm' or Ctrl-N or Ctrl-P or Ctrl-A or Ctrl-L).
1903 // If the popup menu is displayed, then PageDown and PageUp keys are
1904 // also used to navigate the menu.
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02001905 end_wildmenu = (!key_is_wc
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001906 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A && c != Ctrl_L);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001907 end_wildmenu = end_wildmenu && (!cmdline_pum_active() ||
1908 (c != K_PAGEDOWN && c != K_PAGEUP
1909 && c != K_KPAGEDOWN && c != K_KPAGEUP));
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001910
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001911 // free expanded names when finished walking through matches
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001912 if (end_wildmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar73a16c22022-02-08 17:40:36 +00001914 if (cmdline_pum_active())
1915 cmdline_pum_remove();
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001916 if (xpc.xp_numfiles != -1)
1917 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 did_wild_list = FALSE;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001919 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 xpc.xp_context = EXPAND_NOTHING;
1921 wim_index = 0;
Bram Moolenaareadee482020-09-04 15:37:31 +02001922 wildmenu_cleanup(&ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001925 if (p_wmnu)
1926 c = wildmenu_process_key(&ccline, c, &xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001928 // CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1929 // mode when 'insertmode' is set, CTRL-\ e prompts for an expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (c == Ctrl_BSL)
1931 {
zeertzjqffa4e9b2022-12-09 11:36:36 +00001932 res = cmdline_handle_ctrl_bsl(c, &gotesc);
Bram Moolenaar9c929712020-09-07 22:05:28 +02001933 if (res == CMDLINE_CHANGED)
1934 goto cmdline_changed;
1935 else if (res == CMDLINE_NOT_CHANGED)
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001936 goto cmdline_not_changed;
Bram Moolenaar9c929712020-09-07 22:05:28 +02001937 else if (res == GOTO_NORMAL_MODE)
1938 goto returncmd; // back to cmd mode
1939 c = Ctrl_BSL; // backslash key not processed by
zeertzjqffa4e9b2022-12-09 11:36:36 +00001940 // cmdline_handle_ctrl_bsl()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (c == cedit_key || c == K_CMDWIN)
1944 {
Bram Moolenaar85db5472019-12-04 15:11:08 +01001945 // TODO: why is ex_normal_busy checked here?
1946 if ((c == K_CMDWIN || ex_normal_busy == 0) && got_int == FALSE)
Bram Moolenaar58da7072014-09-09 18:45:49 +02001947 {
1948 /*
1949 * Open a window to edit the command line (and history).
1950 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001951 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001952 some_key_typed = TRUE;
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955#ifdef FEAT_DIGRAPHS
Martin Tournoij7904fa42022-10-04 16:28:45 +01001956 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 c = do_digraph(c);
1958#endif
1959
1960 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1961 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1962 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 // In Ex mode a backslash escapes a newline.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001964 if (exmode_active
1965 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001966 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001967 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001968 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001970 if (c == K_KENTER)
1971 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001973 else
1974 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001975 gotesc = FALSE; // Might have typed ESC previously, don't
1976 // truncate the cmdline now.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001977 if (ccheck_abbr(c + ABBR_OFF))
1978 goto cmdline_changed;
1979 if (!cmd_silent)
1980 {
1981 windgoto(msg_row, 0);
1982 out_flush();
1983 }
1984 break;
1985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987
Bram Moolenaar9c929712020-09-07 22:05:28 +02001988 // Completion for 'wildchar' or 'wildcharm' key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1990 {
Bram Moolenaar9c929712020-09-07 22:05:28 +02001991 res = cmdline_wildchar_complete(c, firstc != '@', &did_wild_list,
1992 &wim_index, &xpc, &gotesc);
1993 if (res == CMDLINE_CHANGED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 goto cmdline_changed;
1995 }
1996
1997 gotesc = FALSE;
1998
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001999 // <S-Tab> goes to last match, in a clumsy way
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (c == K_S_TAB && KeyTyped)
2001 {
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002002 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK)
2003 {
Bram Moolenaar300175f2022-08-21 18:38:21 +01002004 if (xpc.xp_numfiles > 1
2005 && ((!did_wild_list && (wim_flags[wim_index] & WIM_LIST))
Bram Moolenaar54162322022-08-26 16:58:51 +01002006 || p_wmnu))
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002007 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002008 // Trigger the popup menu when wildoptions=pum
2009 showmatches(&xpc, p_wmnu
2010 && ((wim_flags[wim_index] & WIM_LIST) == 0));
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002011 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002012 if (nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
2013 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
2014 goto cmdline_changed;
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002018 if (c == NUL || c == K_ZERO) // NUL is stored as NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 c = NL;
2020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002021 do_abbr = TRUE; // default: check for abbreviation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002023 // If already used to cancel/accept wildmenu, don't process the key
2024 // further.
2025 if (wild_type == WILD_CANCEL || wild_type == WILD_APPLY)
2026 {
2027 wild_type = 0;
2028 goto cmdline_not_changed;
2029 }
2030
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 /*
2032 * Big switch for a typed command line character.
2033 */
2034 switch (c)
2035 {
2036 case K_BS:
2037 case Ctrl_H:
2038 case K_DEL:
2039 case K_KDEL:
2040 case Ctrl_W:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002041 res = cmdline_erase_chars(c, indent
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002042#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002043 , &is_state
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002044#endif
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002045 );
2046 if (res == CMDLINE_NOT_CHANGED)
2047 goto cmdline_not_changed;
2048 else if (res == GOTO_NORMAL_MODE)
2049 goto returncmd; // back to cmd mode
2050 goto cmdline_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 case K_INS:
2053 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 ccline.overstrike = !ccline.overstrike;
2055#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002056 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#endif
Sam-programsd1c3ef12023-11-27 22:22:51 +01002058 may_trigger_modechanged();
2059 status_redraw_curbuf();
2060 redraw_statuslines();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 goto cmdline_not_changed;
2062
2063 case Ctrl_HAT:
Bram Moolenaar1c3dd8d2022-09-17 19:43:23 +01002064 cmdline_toggle_langmap(
2065 buf_valid(b_im_ptr_buf) ? b_im_ptr : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 goto cmdline_not_changed;
2067
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002068// case '@': only in very old vi
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 case Ctrl_U:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // delete all characters left of the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 j = ccline.cmdpos;
2072 ccline.cmdlen -= j;
2073 i = ccline.cmdpos = 0;
2074 while (i < ccline.cmdlen)
2075 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002076 // Truncate at the end, required for multi-byte chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002078#ifdef FEAT_SEARCH_EXTRA
2079 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002080 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 redrawcmd();
2083 goto cmdline_changed;
2084
2085#ifdef FEAT_CLIPBOARD
2086 case Ctrl_Y:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002087 // Copy the modeless selection, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (clip_star.state != SELECT_CLEARED)
2089 {
2090 if (clip_star.state == SELECT_DONE)
2091 clip_copy_modeless_selection(TRUE);
2092 goto cmdline_not_changed;
2093 }
2094 break;
2095#endif
2096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002097 case ESC: // get here if p_wc != ESC or when ESC typed twice
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 case Ctrl_C:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 // In exmode it doesn't make sense to return. Except when
2100 // ":normal" runs out of characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00002101 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01002102 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 goto cmdline_not_changed;
2104
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 gotesc = TRUE; // will free ccline.cmdbuff after
2106 // putting it in history
2107 goto returncmd; // back to cmd mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002109 case Ctrl_R: // insert register
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002110 res = cmdline_insert_reg(&gotesc);
K.Takatac4b7dec2023-02-10 21:38:44 +00002111 if (res == GOTO_NORMAL_MODE)
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002112 goto returncmd;
zeertzjq412e0e42023-02-11 10:34:07 +00002113 if (res == CMDLINE_CHANGED)
2114 goto cmdline_changed;
K.Takatac4b7dec2023-02-10 21:38:44 +00002115 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 case Ctrl_D:
2118 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002119 break; // Use ^D as normal char instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 redrawcmd();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 continue; // don't do incremental search now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 case K_RIGHT:
2125 case K_S_RIGHT:
2126 case K_C_RIGHT:
2127 do
2128 {
2129 if (ccline.cmdpos >= ccline.cmdlen)
2130 break;
2131 i = cmdline_charsize(ccline.cmdpos);
2132 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
2133 break;
2134 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002136 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 + ccline.cmdpos);
2138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 ++ccline.cmdpos;
2140 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002141 while ((c == K_S_RIGHT || c == K_C_RIGHT
2142 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (has_mbyte)
2145 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 goto cmdline_not_changed;
2147
2148 case K_LEFT:
2149 case K_S_LEFT:
2150 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002151 if (ccline.cmdpos == 0)
2152 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 do
2154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 --ccline.cmdpos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 if (has_mbyte) // move to first byte of char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
2158 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
2160 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00002161 while (ccline.cmdpos > 0
2162 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002163 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (has_mbyte)
2166 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 goto cmdline_not_changed;
2168
2169 case K_IGNORE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 // Ignore mouse event or open_cmdwin() result.
Bram Moolenaar30405d32008-01-02 20:55:27 +00002171 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaar4f974752019-02-17 17:44:42 +01002173#ifdef FEAT_GUI_MSWIN
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002174 // On MS-Windows ignore <M-F4>, we get it when closing the window
2175 // was cancelled.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002176 case K_F4:
2177 if (mod_mask == MOD_MASK_ALT)
2178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002179 redrawcmd(); // somehow the cmdline is cleared
Bram Moolenaar4770d092006-01-12 23:22:24 +00002180 goto cmdline_not_changed;
2181 }
2182 break;
2183#endif
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 case K_MIDDLEDRAG:
2186 case K_MIDDLERELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002187 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 case K_MIDDLEMOUSE:
2190# ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002191 // When GUI is active, also paste when 'mouse' is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if (!gui.in_use)
2193# endif
2194 if (!mouse_has(MOUSE_COMMAND))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002195 goto cmdline_not_changed; // Ignore mouse
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002196# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002198 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002200# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002201 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 redrawcmd();
2203 goto cmdline_changed;
2204
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002205# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002207 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 redrawcmd();
2209 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 case K_LEFTDRAG:
2213 case K_LEFTRELEASE:
2214 case K_RIGHTDRAG:
2215 case K_RIGHTRELEASE:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002216 // Ignore drag and release events when the button-down wasn't
2217 // seen before.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (ignore_drag_release)
2219 goto cmdline_not_changed;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002220 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 case K_LEFTMOUSE:
2222 case K_RIGHTMOUSE:
Bram Moolenaar2f3cd2e2020-09-06 15:54:00 +02002223 cmdline_left_right_mouse(c, &ignore_drag_release);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 goto cmdline_not_changed;
2225
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 // Mouse scroll wheel: ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 case K_MOUSEDOWN:
2228 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002229 case K_MOUSELEFT:
2230 case K_MOUSERIGHT:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002231 // Alternate buttons ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 case K_X1MOUSE:
2233 case K_X1DRAG:
2234 case K_X1RELEASE:
2235 case K_X2MOUSE:
2236 case K_X2DRAG:
2237 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002238 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 goto cmdline_not_changed;
2240
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002242 case K_LEFTMOUSE_NM: // mousefocus click, ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 case K_LEFTRELEASE_NM:
2244 goto cmdline_not_changed;
2245
2246 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002247 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 gui_do_scroll();
2250 redrawcmd();
2251 }
2252 goto cmdline_not_changed;
2253
2254 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002255 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Christopher Plewright44c22092022-11-15 17:43:36 +00002257 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 redrawcmd();
2259 }
2260 goto cmdline_not_changed;
2261#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002262#ifdef FEAT_GUI_TABLINE
2263 case K_TABLINE:
2264 case K_TABMENU:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002265 // Don't want to change any tabs here. Make sure the same tab
2266 // is still selected.
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002267 if (gui_use_tabline())
2268 gui_mch_set_curtab(tabpage_index(curtab));
2269 goto cmdline_not_changed;
2270#endif
2271
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002272 case K_SELECT: // end of Select mode mapping - ignore
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 goto cmdline_not_changed;
2274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002275 case Ctrl_B: // begin of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 case K_HOME:
2277 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 case K_S_HOME:
2279 case K_C_HOME:
2280 ccline.cmdpos = 0;
2281 set_cmdspos();
2282 goto cmdline_not_changed;
2283
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 case Ctrl_E: // end of command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 case K_END:
2286 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 case K_S_END:
2288 case K_C_END:
2289 ccline.cmdpos = ccline.cmdlen;
2290 set_cmdspos_cursor();
2291 goto cmdline_not_changed;
2292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 case Ctrl_A: // all matches
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002294 if (cmdline_pum_active())
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002295 // As Ctrl-A completes all the matches, close the popup
2296 // menu (if present)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002297 cmdline_pum_cleanup(&ccline);
Bram Moolenaar54162322022-08-26 16:58:51 +01002298
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00002299 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
2300 break;
2301 xpc.xp_context = EXPAND_NOTHING;
2302 did_wild_list = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 goto cmdline_changed;
2304
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002305 case Ctrl_L:
2306#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002307 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002308 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002309#endif
2310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // completion: longest common part
Bram Moolenaarb3479632012-11-28 16:49:58 +01002312 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 break;
2314 goto cmdline_changed;
2315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002316 case Ctrl_N: // next match
2317 case Ctrl_P: // previous match
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002318 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002320 wild_type = (c == Ctrl_P) ? WILD_PREV : WILD_NEXT;
2321 if (nextwild(&xpc, wild_type, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 break;
zeertzjqaf9e28a2023-02-06 20:58:09 +00002323 goto cmdline_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002325 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 case K_UP:
2327 case K_DOWN:
2328 case K_S_UP:
2329 case K_S_DOWN:
2330 case K_PAGEUP:
2331 case K_KPAGEUP:
2332 case K_PAGEDOWN:
2333 case K_KPAGEDOWN:
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00002334 if (cmdline_pum_active()
2335 && (c == K_PAGEUP || c == K_PAGEDOWN ||
2336 c == K_KPAGEUP || c == K_KPAGEDOWN))
2337 {
2338 // If the popup menu is displayed, then PageUp and PageDown
2339 // are used to scroll the menu.
Yegappan Lakshmanan155b0882022-03-17 13:03:09 +00002340 wild_type = WILD_PAGEUP;
2341 if (c == K_PAGEDOWN || c == K_KPAGEDOWN)
2342 wild_type = WILD_PAGEDOWN;
2343 if (nextwild(&xpc, wild_type, 0, firstc != '@') == FAIL)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00002344 break;
zeertzjqaf9e28a2023-02-06 20:58:09 +00002345 goto cmdline_changed;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00002346 }
2347 else
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00002348 {
2349 res = cmdline_browse_history(c, firstc, &lookfor, histype,
2350 &hiscnt, &xpc);
2351 if (res == CMDLINE_CHANGED)
2352 goto cmdline_changed;
2353 else if (res == GOTO_NORMAL_MODE)
2354 goto returncmd;
2355 }
Bram Moolenaar11956692016-08-27 16:26:56 +02002356 goto cmdline_not_changed;
2357
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002358#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 case Ctrl_G: // next match
2360 case Ctrl_T: // previous match
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002361 if (may_adjust_incsearch_highlighting(
2362 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002363 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
2367 case Ctrl_V:
2368 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002370 ignore_drag_release = TRUE;
2371 putcmdline('^', TRUE);
Bram Moolenaar0684e362020-12-03 19:54:42 +01002372
2373 // Get next (two) character(s). Do not change any
2374 // modifyOtherKeys ESC sequence to a normal key for
2375 // CTRL-SHIFT-V.
2376 c = get_literal(mod_mask & MOD_MASK_SHIFT);
2377
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002378 do_abbr = FALSE; // don't do abbreviation now
2379 extra_char = NUL;
2380 // may need to remove ^ when composing char was typed
2381 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2382 {
2383 draw_cmdline(ccline.cmdpos,
2384 ccline.cmdlen - ccline.cmdpos);
2385 msg_putchar(' ');
2386 cursorcmd();
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 break;
2391
2392#ifdef FEAT_DIGRAPHS
2393 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002396# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002397 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002400 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (c != NUL)
2402 break;
2403
2404 redrawcmd();
2405 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002406#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408#ifdef FEAT_RIGHTLEFT
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002409 case Ctrl__: // CTRL-_: switch language mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (!p_ari)
2411 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002412 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 goto cmdline_not_changed;
2414#endif
2415
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002416 case K_PS:
2417 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2418 goto cmdline_changed;
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 default:
2421#ifdef UNIX
2422 if (c == intr_char)
2423 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002424 gotesc = TRUE; // will free ccline.cmdbuff after
2425 // putting it in history
2426 goto returncmd; // back to Normal mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428#endif
2429 /*
2430 * Normal character with no special meaning. Just set mod_mask
2431 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2432 * the string <S-Space>. This should only happen after ^V.
2433 */
2434 if (!IS_SPECIAL(c))
2435 mod_mask = 0x0;
2436 break;
2437 }
2438 /*
2439 * End of switch on command line character.
2440 * We come here if we have a normal character.
2441 */
2442
Bram Moolenaar13505972019-01-24 15:04:48 +01002443 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2444 && (ccheck_abbr(
2445 // Add ABBR_OFF for characters above 0x100, this is
2446 // what check_abbr() expects.
2447 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2448 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 goto cmdline_changed;
2450
2451 /*
2452 * put the character in the command line
2453 */
2454 if (IS_SPECIAL(c) || mod_mask != 0)
2455 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2456 else
2457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (has_mbyte)
2459 {
2460 j = (*mb_char2bytes)(c, IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002461 IObuff[j] = NUL; // exclude composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 put_on_cmdline(IObuff, j, TRUE);
2463 }
2464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 IObuff[0] = c;
2467 put_on_cmdline(IObuff, 1, TRUE);
2468 }
2469 }
2470 goto cmdline_changed;
2471
2472/*
2473 * This part implements incremental searches for "/" and "?"
2474 * Jump to cmdline_not_changed when a character has been read but the command
2475 * line did not change. Then we only search and redraw if something changed in
2476 * the past.
2477 * Jump to cmdline_changed when the command line did change.
2478 * (Sorry for the goto's, I know it is ugly).
2479 */
2480cmdline_not_changed:
zeertzjqbc6f9672024-06-21 07:51:40 +02002481 // Trigger CursorMovedC autocommands.
2482 if (ccline.cmdpos != prev_cmdpos)
2483 {
Shougo Matsushitad0952142024-06-20 22:05:16 +02002484 trigger_cmd_autocmd(cmdline_type, EVENT_CURSORMOVEDC);
zeertzjqbc6f9672024-06-21 07:51:40 +02002485 prev_cmdpos = ccline.cmdpos;
2486 }
zeertzjq81456202024-07-07 20:48:25 +02002487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002489 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 continue;
2491#endif
2492
2493cmdline_changed:
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01002494#ifdef FEAT_SEARCH_EXTRA
2495 // If the window changed incremental search state is not valid.
2496 if (is_state.winid != curwin->w_id)
2497 init_incsearch_state(&is_state);
2498#endif
Shougo Matsushitad0952142024-06-20 22:05:16 +02002499 // Trigger CmdlineChanged autocommands.
Bram Moolenaard832c3c2021-05-15 15:09:06 +02002500 if (trigger_cmdlinechanged)
Bram Moolenaard832c3c2021-05-15 15:09:06 +02002501 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002502
zeertzjq81456202024-07-07 20:48:25 +02002503 // Trigger CursorMovedC autocommands.
2504 if (ccline.cmdpos != prev_cmdpos)
2505 {
2506 trigger_cmd_autocmd(cmdline_type, EVENT_CURSORMOVEDC);
2507 prev_cmdpos = ccline.cmdpos;
2508 }
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarccb148a2021-07-26 21:11:32 +02002511 if (xpc.xp_context == EXPAND_NOTHING && (KeyTyped || vpeekc() == NUL))
Bram Moolenaara60053b2020-09-03 16:50:13 +02002512 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513#endif
2514
2515#ifdef FEAT_RIGHTLEFT
2516 if (cmdmsg_rl
2517# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002518 || (p_arshape && !p_tbidi
2519 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520# endif
2521 )
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002522 // Always redraw the whole command line to fix shaping and
2523 // right-left typing. Not efficient, but it works.
2524 // Do it only when there are no characters left to read
2525 // to avoid useless intermediate redraws.
Bram Moolenaar58437e02012-02-22 17:58:04 +01002526 if (vpeekc() == NUL)
2527 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528#endif
2529 }
2530
2531returncmd:
2532
2533#ifdef FEAT_RIGHTLEFT
2534 cmdmsg_rl = FALSE;
2535#endif
2536
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02002537 // We could have reached here without having a chance to clean up wild menu
2538 // if certain special keys like <Esc> or <C-\> were used as wildchar. Make
2539 // sure to still clean up to avoid memory corruption.
2540 if (cmdline_pum_active())
2541 cmdline_pum_remove();
2542 wildmenu_cleanup(&ccline);
2543 did_wild_list = FALSE;
2544 wim_index = 0;
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002547 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002550 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#endif
2552
2553 if (ccline.cmdbuff != NULL)
2554 {
2555 /*
2556 * Put line in history buffer (":" and "=" only when it was typed).
2557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (ccline.cmdlen && firstc != NUL
2559 && (some_key_typed || histype == HIST_SEARCH))
2560 {
John Marriott8c85a2a2024-05-20 19:18:26 +02002561 size_t cmdbufflen = STRLEN(ccline.cmdbuff);
2562
2563 add_to_history(histype, ccline.cmdbuff, cmdbufflen, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 histype == HIST_SEARCH ? firstc : NUL);
2565 if (firstc == ':')
2566 {
2567 vim_free(new_last_cmdline);
John Marriott8c85a2a2024-05-20 19:18:26 +02002568 new_last_cmdline = vim_strnsave(ccline.cmdbuff, cmdbufflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002572 if (gotesc)
2573 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575
2576 /*
2577 * If the screen was shifted up, redraw the whole screen (later).
2578 * If the line is too long, clear it, so ruler and shown command do
2579 * not get printed in the middle of it.
2580 */
2581 msg_check();
2582 msg_scroll = save_msg_scroll;
2583 redir_off = FALSE;
2584
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002585 // When the command line was typed, no need for a wait-return prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (some_key_typed)
2587 need_wait_return = FALSE;
2588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002589 // Trigger CmdlineLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002590 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 State = save_State;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002593
2594#ifdef FEAT_EVAL
2595 if (!debug_mode)
LemonBoy2bf52dd2022-04-09 18:17:34 +01002596 may_trigger_modechanged();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002597#endif
2598
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002599#ifdef HAVE_INPUT_METHOD
Bram Moolenaar1c3dd8d2022-09-17 19:43:23 +01002600 if (b_im_ptr != NULL && buf_valid(b_im_ptr_buf)
2601 && *b_im_ptr != B_IMODE_LMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 im_save_status(b_im_ptr);
2603 im_set_active(FALSE);
2604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002607 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002609 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaar438d1762018-09-30 17:11:48 +02002611theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002612 {
2613 char_u *p = ccline.cmdbuff;
2614
Bram Moolenaar51f0bfb2022-05-17 20:11:02 +01002615 --depth;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002616 if (did_save_ccline)
2617 restore_cmdline(&save_ccline);
2618 else
2619 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002620 return p;
2621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622}
2623
2624#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2625/*
2626 * Get a command line with a prompt.
2627 * This is prepared to be called recursively from getcmdline() (e.g. by
2628 * f_input() when evaluating an expression from CTRL-R =).
2629 * Returns the command line in allocated memory, or NULL.
2630 */
2631 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002632getcmdline_prompt(
2633 int firstc,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002634 char_u *prompt, // command line prompt
2635 int attr, // attributes for prompt
2636 int xp_context, // type of expansion
2637 char_u *xp_arg) // user-defined expansion argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
2639 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002640 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002641 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002643 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar438d1762018-09-30 17:11:48 +02002645 if (ccline.cmdbuff != NULL)
2646 {
2647 // Save the values of the current cmdline and restore them below.
2648 save_cmdline(&save_ccline);
2649 did_save_ccline = TRUE;
2650 }
2651
Bram Moolenaara80faa82020-04-12 19:37:17 +02002652 CLEAR_FIELD(ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 ccline.cmdprompt = prompt;
2654 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002655# ifdef FEAT_EVAL
2656 ccline.xp_context = xp_context;
2657 ccline.xp_arg = xp_arg;
2658 ccline.input_fn = (firstc == '@');
2659# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002660 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002661 s = getcmdline_int(firstc, 1L, 0, FALSE);
2662
2663 if (did_save_ccline)
2664 restore_cmdline(&save_ccline);
2665
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002666 msg_silent = msg_silent_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002667 // Restore msg_col, the prompt from input() may have changed it.
2668 // But only if called recursively and the commandline is therefore being
2669 // restored to an old one; if not, the input() prompt stays on the screen,
2670 // so we need its modified msg_col left intact.
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002671 if (ccline.cmdbuff != NULL)
2672 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 return s;
2675}
2676#endif
2677
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002678/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002679 * Read the 'wildmode' option, fill wim_flags[].
2680 */
2681 int
2682check_opt_wim(void)
2683{
2684 char_u new_wim_flags[4];
2685 char_u *p;
2686 int i;
2687 int idx = 0;
2688
2689 for (i = 0; i < 4; ++i)
2690 new_wim_flags[i] = 0;
2691
2692 for (p = p_wim; *p; ++p)
2693 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002694 // Note: Keep this in sync with p_wim_values.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002695 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2696 ;
2697 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2698 return FAIL;
2699 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2700 new_wim_flags[idx] |= WIM_LONGEST;
2701 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2702 new_wim_flags[idx] |= WIM_FULL;
2703 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2704 new_wim_flags[idx] |= WIM_LIST;
2705 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2706 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2707 else
2708 return FAIL;
2709 p += i;
2710 if (*p == NUL)
2711 break;
2712 if (*p == ',')
2713 {
2714 if (idx == 3)
2715 return FAIL;
2716 ++idx;
2717 }
2718 }
2719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002720 // fill remaining entries with last flag
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002721 while (idx < 3)
2722 {
2723 new_wim_flags[idx + 1] = new_wim_flags[idx];
2724 ++idx;
2725 }
2726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002727 // only when there are no errors, wim_flags[] is changed
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002728 for (i = 0; i < 4; ++i)
2729 wim_flags[i] = new_wim_flags[i];
2730 return OK;
2731}
2732
2733/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002734 * Return TRUE when the text must not be changed and we can't switch to
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002735 * another window or buffer. TRUE when editing the command line, evaluating
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002736 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002737 */
2738 int
zeertzjqcfe45652022-05-27 17:26:55 +01002739text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002740{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002741 if (cmdwin_type != 0)
2742 return TRUE;
zeertzjqcfe45652022-05-27 17:26:55 +01002743 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002744}
2745
2746/*
2747 * Give an error message for a command that isn't allowed while the cmdline
2748 * window is open or editing the cmdline in another way.
2749 */
2750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002751text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002752{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002753 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002754}
2755
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002756 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002757get_text_locked_msg(void)
2758{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002759 if (cmdwin_type != 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002760 return e_invalid_in_cmdline_window;
zeertzjqcfe45652022-05-27 17:26:55 +01002761 return e_not_allowed_to_change_text_or_change_window;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02002762}
2763
2764/*
Bram Moolenaar71223e22022-05-30 15:23:09 +01002765 * Check for text, window or buffer locked.
2766 * Give an error message and return TRUE if something is locked.
2767 */
2768 int
2769text_or_buf_locked(void)
2770{
2771 if (text_locked())
2772 {
2773 text_locked_msg();
2774 return TRUE;
2775 }
2776 return curbuf_locked();
2777}
2778
2779/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002780 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2781 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002782 */
2783 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002784curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002785{
2786 if (curbuf_lock > 0)
2787 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002788 emsg(_(e_not_allowed_to_edit_another_buffer_now));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002789 return TRUE;
2790 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002791 return allbuf_locked();
2792}
2793
2794/*
2795 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2796 * message.
2797 */
2798 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002799allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002800{
2801 if (allbuf_lock > 0)
2802 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00002803 emsg(_(e_not_allowed_to_change_buffer_information_now));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002804 return TRUE;
2805 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002806 return FALSE;
2807}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002810cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
2812#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002813 if (cmdline_star > 0) // showing '*', always 1 position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return 1;
2815#endif
2816 return ptr2cells(ccline.cmdbuff + idx);
2817}
2818
2819/*
2820 * Compute the offset of the cursor on the command line for the prompt and
2821 * indent.
2822 */
2823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002824set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002826 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 ccline.cmdspos = 1 + ccline.cmdindent;
2828 else
2829 ccline.cmdspos = 0 + ccline.cmdindent;
2830}
2831
2832/*
2833 * Compute the screen position for the cursor on the command line.
2834 */
2835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002836set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
2838 int i, m, c;
2839
2840 set_cmdspos();
2841 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002844 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002845 m = MAXCOL;
2846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else
2848 m = MAXCOL;
2849 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2850 {
2851 c = cmdline_charsize(i);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002852 // Count ">" for double-wide multi-byte char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 if (has_mbyte)
2854 correct_cmdspos(i, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002855 // If the cmdline doesn't fit, show cursor on last visible char.
2856 // Don't move the cursor itself, so we can still append.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if ((ccline.cmdspos += c) >= m)
2858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 ccline.cmdspos -= c;
2860 break;
2861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002863 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865}
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867/*
2868 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2869 * character that doesn't fit, so that a ">" must be displayed.
2870 */
2871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002872correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002874 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2876 && ccline.cmdspos % Columns + cells > Columns)
2877 ccline.cmdspos++;
2878}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880/*
2881 * Get an Ex command line for the ":" command.
2882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002884getexline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002885 int c, // normally ':', NUL for ":append"
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002886 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002887 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002888 getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002890 // When executing a register, remove ':' that's in front of each line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (exec_from_reg && vpeekc() == ':')
2892 (void)vgetc();
Bram Moolenaar66250c92020-08-20 15:02:42 +02002893 return getcmdline(c, 1L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894}
2895
2896/*
2897 * Get an Ex command line for Ex mode.
2898 * In Ex mode we only use the OS supplied line editing features and no
2899 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002903getexmodeline(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002904 int promptc, // normally ':', NUL for ":append" and '?' for
2905 // :s prompt
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002906 void *cookie UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002907 int indent, // indent for inside conditionals
Bram Moolenaar66250c92020-08-20 15:02:42 +02002908 getline_opt_T options UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 garray_T line_ga;
2911 char_u *pend;
2912 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002913 int c1 = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002914 int escaped = FALSE; // CTRL-V typed
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 int vcol = 0;
2916 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002917 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002918 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002920 // Switch cursor on now. This avoids that it happens after the "\n", which
2921 // confuses the system function that computes tabstops.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 cursor_on();
2923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002924 // always start in column 0; write a newline if necessary
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002926 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002928 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002930 // indent that is only displayed, not in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002931 if (p_prompt)
2932 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 while (indent-- > 0)
2934 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
2937
2938 ga_init2(&line_ga, 1, 30);
2939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002940 // autoindent for :insert and :append is in the line itself
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002942 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002943 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002944 while (indent >= 8)
2945 {
2946 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002947 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002948 indent -= 8;
2949 }
2950 while (indent-- > 0)
2951 {
2952 ga_append(&line_ga, ' ');
2953 msg_putchar(' ');
2954 }
2955 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002956 ++no_mapping;
2957 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002958
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 /*
2960 * Get the line, one character at a time.
2961 */
2962 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002963 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002965 long sw;
2966 char_u *s;
2967
Bram Moolenaar733a69b2022-12-01 12:03:47 +00002968 // May request the keyboard protocol state now.
2969 may_send_t_RK();
2970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (ga_grow(&line_ga, 40) == FAIL)
2972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
Bram Moolenaarba748c82017-02-26 14:00:07 +01002974 /*
2975 * Get one character at a time.
2976 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002977 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002978
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002979 // Check for a ":normal" command and no more characters left.
Bram Moolenaarba748c82017-02-26 14:00:07 +01002980 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2981 c1 = '\n';
2982 else
2983 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002986 * Handle line editing.
2987 * Previously this was left to the system, putting the terminal in
2988 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002990 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002992 msg_putchar('\n');
2993 break;
2994 }
2995
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002996 if (c1 == K_PS)
2997 {
2998 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2999 goto redraw;
3000 }
3001
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003002 if (!escaped)
3003 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003004 // CR typed means "enter", which is NL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003005 if (c1 == '\r')
3006 c1 = '\n';
3007
3008 if (c1 == BS || c1 == K_BS
3009 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003011 if (line_ga.ga_len > 0)
3012 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003013 if (has_mbyte)
3014 {
3015 p = (char_u *)line_ga.ga_data;
3016 p[line_ga.ga_len] = NUL;
3017 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
3018 line_ga.ga_len -= len;
3019 }
3020 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003021 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003022 goto redraw;
3023 }
3024 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003027 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003029 msg_col = startcol;
3030 msg_clr_eos();
3031 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02003032 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003033 }
3034
3035 if (c1 == Ctrl_T)
3036 {
Bram Moolenaarda636572015-04-03 17:11:45 +02003037 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003038 p = (char_u *)line_ga.ga_data;
3039 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003040 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02003041 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003042add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02003043 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003045 (void)ga_grow(&line_ga, 2); // one more for the NUL
Bram Moolenaarda636572015-04-03 17:11:45 +02003046 p = (char_u *)line_ga.ga_data;
3047 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003048 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
3049 *s = ' ';
3050 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003052redraw:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003053 // redraw the line
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003054 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003055 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003056 p = (char_u *)line_ga.ga_data;
3057 p[line_ga.ga_len] = NUL;
3058 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003060 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003062 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003063 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003064 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003065 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02003069 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003070 msg_outtrans_len(p, len);
3071 vcol += ptr2cells(p);
3072 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003075 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00003076 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003077 continue;
3078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003080 if (c1 == Ctrl_D)
3081 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003082 // Delete one shiftwidth.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003083 p = (char_u *)line_ga.ga_data;
3084 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003086 if (prev_char == '^')
3087 ex_keep_indent = TRUE;
3088 indent = 0;
3089 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091 else
3092 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003093 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003094 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02003095 if (indent > 0)
3096 {
3097 --indent;
3098 indent -= indent % get_sw_value(curbuf);
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003101 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003102 {
Bram Moolenaarda636572015-04-03 17:11:45 +02003103 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003104 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
3105 --line_ga.ga_len;
3106 }
3107 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003109
3110 if (c1 == Ctrl_V || c1 == Ctrl_Q)
3111 {
3112 escaped = TRUE;
3113 continue;
3114 }
3115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003116 // Ignore special key codes: mouse movement, K_IGNORE, etc.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003117 if (IS_SPECIAL(c1))
3118 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003120
3121 if (IS_SPECIAL(c1))
3122 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003123 if (has_mbyte)
3124 len = (*mb_char2bytes)(c1,
3125 (char_u *)line_ga.ga_data + line_ga.ga_len);
3126 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003127 {
3128 len = 1;
3129 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
3130 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003131 if (c1 == '\n')
3132 msg_putchar('\n');
3133 else if (c1 == TAB)
3134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003135 // Don't use chartabsize(), 'ts' can be different
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003136 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003137 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003138 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003142 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003143 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003144 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02003146 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003147 escaped = FALSE;
3148
3149 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003150 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003151
Mohamed Akramf3daa452024-07-06 17:12:09 +02003152 // We are done when a NL is entered, but not when it comes after a
3153 // backslash in prompt mode.
3154 if (line_ga.ga_len > 0 && pend[-1] == '\n'
3155 && (line_ga.ga_len <= 1 || pend[-2] != '\\' || !promptc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
Mohamed Akramf3daa452024-07-06 17:12:09 +02003157 --line_ga.ga_len;
3158 --pend;
3159 *pend = NUL;
3160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162 }
3163
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003164 --no_mapping;
3165 --allow_keys;
3166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003167 // make following messages go to the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 msg_didout = FALSE;
3169 msg_col = 0;
3170 if (msg_row < Rows - 1)
3171 ++msg_row;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003172 emsg_on_display = FALSE; // don't want ui_delay()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 if (got_int)
3175 ga_clear(&line_ga);
3176
3177 return (char_u *)line_ga.ga_data;
3178}
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180/*
3181 * Return TRUE if ccline.overstrike is on.
3182 */
3183 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 return ccline.overstrike;
3187}
3188
Sam-programsd1c3ef12023-11-27 22:22:51 +01003189# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3190 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191/*
3192 * Return TRUE if the cursor is at the end of the cmdline.
3193 */
3194 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003195cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 return (ccline.cmdpos >= ccline.cmdlen);
3198}
3199#endif
3200
Bram Moolenaar9372a112005-12-06 19:59:18 +00003201#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202/*
3203 * Return the virtual column number at the current cursor position.
3204 * This is used by the IM code to obtain the start of the preedit string.
3205 */
3206 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003207cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
3209 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3210 return MAXCOL;
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (has_mbyte)
3213 {
3214 colnr_T col;
3215 int i = 0;
3216
3217 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003218 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 return col;
3221 }
3222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 return ccline.cmdpos;
3224}
3225#endif
3226
3227#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3228/*
3229 * If part of the command line is an IM preedit string, redraw it with
3230 * IM feedback attributes. The cursor position is restored after drawing.
3231 */
3232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003233redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
Bram Moolenaar24959102022-05-07 20:01:16 +01003235 if ((State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 && xic != NULL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003237 // && im_get_status() doesn't work when using SCIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 && !p_imdisable
3239 && im_is_preediting())
3240 {
3241 int cmdpos = 0;
3242 int cmdspos;
3243 int old_row;
3244 int old_col;
3245 colnr_T col;
3246
3247 old_row = msg_row;
3248 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003249 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (has_mbyte)
3252 {
3253 for (col = 0; col < preedit_start_col
3254 && cmdpos < ccline.cmdlen; ++col)
3255 {
3256 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003257 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 }
3260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 cmdspos += preedit_start_col;
3263 cmdpos += preedit_start_col;
3264 }
3265
3266 msg_row = cmdline_row + (cmdspos / (int)Columns);
3267 msg_col = cmdspos % (int)Columns;
3268 if (msg_row >= Rows)
3269 msg_row = Rows - 1;
3270
3271 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3272 {
3273 int char_len;
3274 int char_attr;
3275
3276 char_attr = im_get_feedback_attr(col);
3277 if (char_attr < 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003278 break; // end of preedit string
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003281 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 char_len = 1;
3284
3285 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3286 cmdpos += char_len;
3287 }
3288
3289 msg_row = old_row;
3290 msg_col = old_col;
3291 }
3292}
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003293#endif // FEAT_XIM && FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
3295/*
3296 * Allocate a new command line buffer.
3297 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
3299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003300alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 /*
3303 * give some extra space to avoid having to allocate all the time
3304 */
3305 if (len < 80)
3306 len = 100;
3307 else
3308 len += 20;
3309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003310 ccline.cmdbuff = alloc(len); // caller should check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 ccline.cmdbufflen = len;
3312}
3313
3314/*
3315 * Re-allocate the command line to length len + something extra.
3316 * return FAIL for failure, OK otherwise
3317 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003318 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003319realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 char_u *p;
3322
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003323 if (len < ccline.cmdbufflen)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003324 return OK; // no need to resize
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 p = ccline.cmdbuff;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003327 alloc_cmdbuff(len); // will get some more
3328 if (ccline.cmdbuff == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003330 ccline.cmdbuff = p; // keep the old one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 return FAIL;
3332 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003333 // There isn't always a NUL after the command, but it may need to be
3334 // there, thus copy up to the NUL and add a NUL.
Bram Moolenaar35a34232010-08-13 16:51:26 +02003335 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3336 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003337
3338 if (ccline.xpc != NULL
3339 && ccline.xpc->xp_pattern != NULL
3340 && ccline.xpc->xp_context != EXPAND_NOTHING
3341 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3342 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003343 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003345 // If xp_pattern points inside the old cmdbuff it needs to be adjusted
3346 // to point into the newly allocated memory.
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003347 if (i >= 0 && i <= ccline.cmdlen)
3348 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3349 }
3350
John Marriotta6de2872024-09-21 11:39:02 +02003351 vim_free(p);
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 return OK;
3354}
3355
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003356#if defined(FEAT_ARABIC) || defined(PROTO)
3357static char_u *arshape_buf = NULL;
3358
3359# if defined(EXITFREE) || defined(PROTO)
3360 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003361free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003362{
3363 vim_free(arshape_buf);
3364}
3365# endif
3366#endif
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368/*
3369 * Draw part of the cmdline at the current cursor position. But draw stars
3370 * when cmdline_star is TRUE.
3371 */
3372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003373draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3376 int i;
3377
3378 if (cmdline_star > 0)
3379 for (i = 0; i < len; ++i)
3380 {
3381 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003383 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else
3386#endif
3387#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003388 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 static int buflen = 0;
3391 char_u *p;
3392 int j;
3393 int newlen = 0;
3394 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003395 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 int prev_c = 0;
3397 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003398 int u8c;
3399 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
3402 /*
3403 * Do arabic shaping into a temporary buffer. This is very
3404 * inefficient!
3405 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003406 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003408 // Re-allocate the buffer. We keep it around to avoid a lot of
3409 // alloc()/free() calls.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003410 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003411 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003412 arshape_buf = alloc(buflen);
3413 if (arshape_buf == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003414 return; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003417 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003419 // Prepend a space to draw the leading composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003420 arshape_buf[0] = ' ';
3421 newlen = 1;
3422 }
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 for (j = start; j < start + len; j += mb_l)
3425 {
3426 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003427 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003428 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (ARABIC_CHAR(u8c))
3430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003431 // Do Arabic shaping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (cmdmsg_rl)
3433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003434 // displaying from right to left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 pc = prev_c;
3436 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003437 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (j + mb_l >= start + len)
3439 nc = NUL;
3440 else
3441 nc = utf_ptr2char(p + mb_l);
3442 }
3443 else
3444 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003445 // displaying from left to right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (j + mb_l >= start + len)
3447 pc = NUL;
3448 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003449 {
3450 int pcc[MAX_MCO];
3451
3452 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003454 pc1 = pcc[0];
3455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 nc = prev_c;
3457 }
3458 prev_c = u8c;
3459
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003460 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003462 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003463 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003465 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3466 if (u8cc[1] != 0)
3467 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003468 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 }
3471 else
3472 {
3473 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003474 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 newlen += mb_l;
3476 }
3477 }
3478
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003479 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 else
3482#endif
3483 msg_outtrans_len(ccline.cmdbuff + start, len);
3484}
3485
3486/*
3487 * Put a character on the command line. Shifts the following text to the
3488 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3489 * "c" must be printable (fit in one display cell)!
3490 */
3491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003492putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 if (cmd_silent)
3495 return;
3496 msg_no_more = TRUE;
3497 msg_putchar(c);
3498 if (shift)
3499 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3500 msg_no_more = FALSE;
3501 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003502 extra_char = c;
3503 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * Undo a putcmdline(c, FALSE).
3508 */
3509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003510unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 if (cmd_silent)
3513 return;
3514 msg_no_more = TRUE;
3515 if (ccline.cmdlen == ccline.cmdpos)
3516 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003517 else if (has_mbyte)
3518 draw_cmdline(ccline.cmdpos,
3519 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
3521 draw_cmdline(ccline.cmdpos, 1);
3522 msg_no_more = FALSE;
3523 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003524 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * Put the given string, of the given length, onto the command line.
3529 * If len is -1, then STRLEN() is used to calculate the length.
3530 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3531 * part will be redrawn, otherwise it will not. If this function is called
3532 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3533 * called afterwards.
3534 */
3535 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003536put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
3538 int retval;
3539 int i;
3540 int m;
3541 int c;
3542
3543 if (len < 0)
3544 len = (int)STRLEN(str);
3545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003546 // Check if ccline.cmdbuff needs to be longer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003548 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 else
3550 retval = OK;
3551 if (retval == OK)
3552 {
3553 if (!ccline.overstrike)
3554 {
3555 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3556 ccline.cmdbuff + ccline.cmdpos,
3557 (size_t)(ccline.cmdlen - ccline.cmdpos));
3558 ccline.cmdlen += len;
3559 }
3560 else
3561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (has_mbyte)
3563 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003564 // Count nr of characters in the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003566 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 ++m;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003568 // Count nr of bytes in cmdline that are overwritten by these
3569 // characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003571 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 --m;
3573 if (i < ccline.cmdlen)
3574 {
3575 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3576 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3577 ccline.cmdlen += ccline.cmdpos + len - i;
3578 }
3579 else
3580 ccline.cmdlen = ccline.cmdpos + len;
3581 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003582 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 ccline.cmdlen = ccline.cmdpos + len;
3584 }
3585 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3586 ccline.cmdbuff[ccline.cmdlen] = NUL;
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (enc_utf8)
3589 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003590 // When the inserted text starts with a composing character,
3591 // backup to the character before it. There could be two of them.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 i = 0;
3593 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3594 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3595 {
3596 i = (*mb_head_off)(ccline.cmdbuff,
3597 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3598 ccline.cmdpos -= i;
3599 len += i;
3600 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3601 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003602#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3604 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003605 // Check the previous character for Arabic combining pair.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 i = (*mb_head_off)(ccline.cmdbuff,
3607 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3608 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3609 + ccline.cmdpos - i), c))
3610 {
3611 ccline.cmdpos -= i;
3612 len += i;
3613 }
3614 else
3615 i = 0;
3616 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (i != 0)
3619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003620 // Also backup the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3622 ccline.cmdspos -= i;
3623 msg_col -= i;
3624 if (msg_col < 0)
3625 {
3626 msg_col += Columns;
3627 --msg_row;
3628 }
3629 }
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 if (redraw && !cmd_silent)
3633 {
3634 msg_no_more = TRUE;
3635 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003636 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003638 // Avoid clearing the rest of the line too often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (cmdline_row != i || ccline.overstrike)
3640 msg_clr_eos();
3641 msg_no_more = FALSE;
3642 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003643 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003645 m = Columns * Rows;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003646 if (m < 0) // overflow, Columns or Rows at weird value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003648 }
3649 else
3650 m = MAXCOL;
3651 for (i = 0; i < len; ++i)
3652 {
3653 c = cmdline_charsize(ccline.cmdpos);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003654 // count ">" for a double-wide char that doesn't fit.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003655 if (has_mbyte)
3656 correct_cmdspos(ccline.cmdpos, c);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003657 // Stop cursor at the end of the screen, but do increment the
3658 // insert position, so that entering a very long command
3659 // works, even though you can't see it.
Bram Moolenaar14184a32019-02-16 15:10:30 +01003660 if (ccline.cmdspos + c < m)
3661 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003662
Bram Moolenaar14184a32019-02-16 15:10:30 +01003663 if (has_mbyte)
3664 {
3665 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3666 if (c > len - i - 1)
3667 c = len - i - 1;
3668 ccline.cmdpos += c;
3669 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003671 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 }
3674 if (redraw)
3675 msg_check();
3676 return retval;
3677}
3678
Bram Moolenaar66b51422019-08-18 21:44:12 +02003679static cmdline_info_T prev_ccline;
3680static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003681
3682/*
3683 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3684 * and overwrite it. But get_cmdline_str() may need it, thus make it
3685 * available globally in prev_ccline.
3686 */
3687 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003689{
3690 if (!prev_ccline_used)
3691 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003692 CLEAR_FIELD(prev_ccline);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003693 prev_ccline_used = TRUE;
3694 }
3695 *ccp = prev_ccline;
3696 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003697 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003698}
3699
3700/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003701 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003702 */
3703 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003704restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003705{
3706 ccline = prev_ccline;
3707 prev_ccline = *ccp;
3708}
3709
Bram Moolenaar8299df92004-07-10 09:47:34 +00003710/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003711 * Paste a yank register into the command line.
3712 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003713 * insert_reg() can't be used here, because special characters from the
3714 * register contents will be interpreted as commands.
3715 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003716 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003717 */
3718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003719cmdline_paste(
3720 int regname,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003721 int literally, // Insert text literally instead of "as typed"
3722 int remcr) // remove trailing CR
Bram Moolenaar8299df92004-07-10 09:47:34 +00003723{
3724 long i;
3725 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003726 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003727 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003729 // check for valid regname; also accept special characters for CTRL-R in
3730 // the command line
Bram Moolenaar8299df92004-07-10 09:47:34 +00003731 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003732 && regname != Ctrl_A && regname != Ctrl_L
3733 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003734 return FAIL;
3735
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003736 // A register containing CTRL-R can cause an endless loop. Allow using
3737 // CTRL-C to break the loop.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003738 line_breakcheck();
3739 if (got_int)
3740 return FAIL;
3741
3742#ifdef FEAT_CLIPBOARD
3743 regname = may_get_selection(regname);
3744#endif
3745
zeertzjqcfe45652022-05-27 17:26:55 +01003746 // Need to set "textlock" to avoid nasty things like going to another
Bram Moolenaar438d1762018-09-30 17:11:48 +02003747 // buffer when evaluating an expression.
zeertzjqcfe45652022-05-27 17:26:55 +01003748 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003749 i = get_spec_reg(regname, &arg, &allocated, TRUE);
zeertzjqcfe45652022-05-27 17:26:55 +01003750 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003751
3752 if (i)
3753 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003754 // Got the value of a special register in "arg".
Bram Moolenaar8299df92004-07-10 09:47:34 +00003755 if (arg == NULL)
3756 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003757
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003758 // When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3759 // part of the word.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003760 p = arg;
3761 if (p_is && regname == Ctrl_W)
3762 {
3763 char_u *w;
3764 int len;
3765
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003766 // Locate start of last word in the cmd buffer.
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003767 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003768 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003769 if (has_mbyte)
3770 {
3771 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3772 if (!vim_iswordc(mb_ptr2char(w - len)))
3773 break;
3774 w -= len;
3775 }
3776 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003777 {
3778 if (!vim_iswordc(w[-1]))
3779 break;
3780 --w;
3781 }
3782 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003783 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003784 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3785 p += len;
3786 }
3787
3788 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003789 if (allocated)
3790 vim_free(arg);
3791 return OK;
3792 }
3793
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003794 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003795}
3796
3797/*
3798 * Put a string on the command line.
3799 * When "literally" is TRUE, insert literally.
3800 * When "literally" is FALSE, insert as typed, but don't leave the command
3801 * line.
3802 */
3803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003804cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003805{
3806 int c, cv;
3807
3808 if (literally)
3809 put_on_cmdline(s, -1, TRUE);
3810 else
3811 while (*s != NUL)
3812 {
3813 cv = *s;
3814 if (cv == Ctrl_V && s[1])
3815 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003816 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003817 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003818 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003819 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003820 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3821 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003822#ifdef UNIX
3823 || c == intr_char
3824#endif
3825 || (c == Ctrl_BSL && *s == Ctrl_N))
3826 stuffcharReadbuff(Ctrl_V);
3827 stuffcharReadbuff(c);
3828 }
3829}
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003832 * This function is called when the screen size changes and with incremental
3833 * search and in other situations where the command line may have been
3834 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
3836 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003837redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003839 redrawcmdline_ex(TRUE);
3840}
3841
Bram Moolenaare5050712021-12-09 10:51:05 +00003842/*
3843 * When "do_compute_cmdrow" is TRUE the command line is redrawn at the bottom.
3844 * If FALSE cmdline_row is used, which should redraw in the same place.
3845 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003846 void
3847redrawcmdline_ex(int do_compute_cmdrow)
3848{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (cmd_silent)
3850 return;
3851 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003852 if (do_compute_cmdrow)
3853 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 redrawcmd();
3855 cursorcmd();
3856}
3857
3858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003859redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 int i;
3862
3863 if (cmd_silent)
3864 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003865 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 msg_putchar(ccline.cmdfirstc);
3867 if (ccline.cmdprompt != NULL)
3868 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003869 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 // do the reverse of set_cmdspos()
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003872 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 --ccline.cmdindent;
3874 }
3875 else
3876 for (i = ccline.cmdindent; i > 0; --i)
3877 msg_putchar(' ');
3878}
3879
3880/*
3881 * Redraw what is currently on the command line.
3882 */
3883 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003884redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
Bram Moolenaar37fef162022-08-29 18:16:32 +01003886 int save_in_echowindow = in_echowindow;
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (cmd_silent)
3889 return;
3890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003891 // when 'incsearch' is set there may be no command line while redrawing
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003892 if (ccline.cmdbuff == NULL)
3893 {
3894 windgoto(cmdline_row, 0);
3895 msg_clr_eos();
3896 return;
3897 }
3898
Bram Moolenaar37fef162022-08-29 18:16:32 +01003899 // Do not put this in the message window.
3900 in_echowindow = FALSE;
3901
zeertzjq46af7bc2022-07-28 12:34:09 +01003902 sb_text_restart_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 msg_start();
3904 redrawcmdprompt();
3905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 // Don't use more prompt, truncate the cmdline if it doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 msg_no_more = TRUE;
3908 draw_cmdline(0, ccline.cmdlen);
3909 msg_clr_eos();
3910 msg_no_more = FALSE;
3911
3912 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003913 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003914 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 /*
3917 * An emsg() before may have set msg_scroll. This is used in normal mode,
3918 * in cmdline mode we can reset them now.
3919 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003920 msg_scroll = FALSE; // next message overwrites cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 // Typing ':' at the more prompt may set skip_redraw. We don't want this
3923 // in cmdline mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 skip_redraw = FALSE;
Bram Moolenaar37fef162022-08-29 18:16:32 +01003925
3926 in_echowindow = save_in_echowindow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927}
3928
3929 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003930compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01003932 // ignore "msg_scrolled" in update_screen(), it will be reset soon.
3933 if (exmode_active || (msg_scrolled != 0 && !updating_screen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 cmdline_row = Rows - 1;
3935 else
3936 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003937 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938}
3939
Bram Moolenaar66b51422019-08-18 21:44:12 +02003940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003941cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 if (cmd_silent)
3944 return;
3945
3946#ifdef FEAT_RIGHTLEFT
3947 if (cmdmsg_rl)
3948 {
3949 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3950 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3951 if (msg_row <= 0)
3952 msg_row = Rows - 1;
3953 }
3954 else
3955#endif
3956 {
3957 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3958 msg_col = ccline.cmdspos % (int)Columns;
3959 if (msg_row >= Rows)
3960 msg_row = Rows - 1;
3961 }
3962
3963 windgoto(msg_row, msg_col);
3964#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003965 if (p_imst == IM_ON_THE_SPOT)
3966 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967#endif
3968#ifdef MCH_CURSOR_SHAPE
3969 mch_update_cursor();
3970#endif
3971}
3972
3973 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003974gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 msg_start();
3977#ifdef FEAT_RIGHTLEFT
3978 if (cmdmsg_rl)
3979 msg_col = Columns - 1;
3980 else
3981#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003982 msg_col = 0; // always start in column 0
3983 if (clr) // clear the bottom line(s)
3984 msg_clr_eos(); // will reset clear_cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 windgoto(cmdline_row, 0);
3986}
3987
3988/*
3989 * Check the word in front of the cursor for an abbreviation.
3990 * Called when the non-id character "c" has been entered.
3991 * When an abbreviation is recognized it is removed from the text with
3992 * backspaces and the replacement string is inserted, followed by "c".
3993 */
3994 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003995ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003997 int spos = 0;
3998
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003999 if (p_paste || no_abbr) // no abbreviations or in paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return FALSE;
4001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004002 // Do not consider '<,'> be part of the mapping, skip leading whitespace.
4003 // Actually accepts any mark.
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02004004 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
4005 spos++;
4006 if (ccline.cmdlen - spos > 5
4007 && ccline.cmdbuff[spos] == '\''
4008 && ccline.cmdbuff[spos + 2] == ','
4009 && ccline.cmdbuff[spos + 3] == '\'')
4010 spos += 5;
4011 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004012 // check abbreviation from the beginning of the commandline
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02004013 spos = 0;
4014
4015 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016}
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018/*
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01004019 * Escape special characters in "fname", depending on "what":
4020 * VSE_NONE: for when used as a file name argument after a Vim command.
4021 * VSE_SHELL: for a shell command.
4022 * VSE_BUFFER: for the ":buffer" command.
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004023 * Returns the result in allocated memory.
4024 */
4025 char_u *
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01004026vim_strsave_fnameescape(char_u *fname, int what)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004027{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004028 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004029#ifdef BACKSLASH_IN_FILENAME
4030 char_u buf[20];
4031 int j = 0;
4032
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01004033 // Don't escape '[', '{' and '!' if they are in 'isfname' and for the
4034 // ":buffer" command.
4035 for (p = what == VSE_BUFFER ? BUFFER_ESC_CHARS : PATH_ESC_CHARS;
4036 *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004037 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004038 buf[j++] = *p;
4039 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004040 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004041#else
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01004042 p = vim_strsave_escaped(fname, what == VSE_SHELL ? SHELL_ESC_CHARS
4043 : what == VSE_BUFFER ? BUFFER_ESC_CHARS : PATH_ESC_CHARS);
4044 if (what == VSE_SHELL && csh_like_shell() && p != NULL)
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004045 {
4046 char_u *s;
4047
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004048 // For csh and similar shells need to put two backslashes before '!'.
4049 // One is taken by Vim, one by the shell.
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004050 s = vim_strsave_escaped(p, (char_u *)"!");
4051 vim_free(p);
4052 p = s;
4053 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004054#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004056 // '>' and '+' are special at the start of some commands, e.g. ":edit" and
4057 // ":write". "cd -" has a special meaning.
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004058 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004059 escape_fname(&p);
4060
4061 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004062}
4063
4064/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004065 * Put a backslash before the file name in "pp", which is in allocated memory.
4066 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02004067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004068escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004069{
4070 char_u *p;
4071
Bram Moolenaar964b3742019-05-24 18:54:09 +02004072 p = alloc(STRLEN(*pp) + 2);
Yegappan Lakshmananed0c1d52022-12-30 18:07:46 +00004073 if (p == NULL)
4074 return;
4075
4076 p[0] = '\\';
4077 STRCPY(p + 1, *pp);
4078 vim_free(*pp);
4079 *pp = p;
Bram Moolenaar45360022005-07-21 21:08:21 +00004080}
4081
4082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * For each file name in files[num_files]:
4084 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4085 */
4086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004087tilde_replace(
4088 char_u *orig_pat,
4089 int num_files,
4090 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 int i;
4093 char_u *p;
4094
4095 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4096 {
4097 for (i = 0; i < num_files; ++i)
4098 {
4099 p = home_replace_save(NULL, files[i]);
4100 if (p != NULL)
4101 {
4102 vim_free(files[i]);
4103 files[i] = p;
4104 }
4105 }
4106 }
4107}
4108
4109/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02004110 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02004112 cmdline_info_T *
4113get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004115 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116}
4117
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004118/*
zeertzjq46af7bc2022-07-28 12:34:09 +01004119 * Get pointer to the command line info to use. save_cmdline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004120 * ccline and put the previous value in prev_ccline.
4121 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02004122 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004123get_ccline_ptr(void)
4124{
Bram Moolenaar24959102022-05-07 20:01:16 +01004125 if ((State & MODE_CMDLINE) == 0)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004126 return NULL;
4127 if (ccline.cmdbuff != NULL)
4128 return &ccline;
4129 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4130 return &prev_ccline;
4131 return NULL;
4132}
4133
zeertzjq54acb902022-08-29 16:21:25 +01004134/*
4135 * Get the current command-line type.
4136 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4137 * Only works when the command line is being edited.
4138 * Returns NUL when something is wrong.
4139 */
4140 static int
4141get_cmdline_type(void)
4142{
4143 cmdline_info_T *p = get_ccline_ptr();
4144
4145 if (p == NULL)
4146 return NUL;
4147 if (p->cmdfirstc == NUL)
4148 return
4149# ifdef FEAT_EVAL
4150 (p->input_fn) ? '@' :
4151# endif
4152 '-';
4153 return p->cmdfirstc;
4154}
zeertzjq54acb902022-08-29 16:21:25 +01004155
Bram Moolenaar064154c2016-03-19 22:50:43 +01004156#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004157/*
4158 * Get the current command line in allocated memory.
4159 * Only works when the command line is being edited.
4160 * Returns NULL when something is wrong.
4161 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004162 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004163get_cmdline_str(void)
4164{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004165 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004166
Bram Moolenaaree91c332018-09-25 22:27:35 +02004167 if (cmdline_star > 0)
4168 return NULL;
4169 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004170 if (p == NULL)
4171 return NULL;
4172 return vim_strnsave(p->cmdbuff, p->cmdlen);
4173}
4174
4175/*
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004176 * Get the current command-line completion type.
4177 */
4178 static char_u *
4179get_cmdline_completion(void)
4180{
4181 cmdline_info_T *p;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004182 char_u *buffer;
zeertzjqa821b602024-06-18 20:31:08 +02004183 int xp_context;
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004184
4185 if (cmdline_star > 0)
4186 return NULL;
4187
4188 p = get_ccline_ptr();
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004189 if (p == NULL || p->xpc == NULL)
4190 return NULL;
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004191
zeertzjqa821b602024-06-18 20:31:08 +02004192 xp_context = p->xpc->xp_context;
4193 if (xp_context == EXPAND_NOTHING)
4194 {
4195 set_expand_context(p->xpc);
4196 xp_context = p->xpc->xp_context;
4197 p->xpc->xp_context = EXPAND_NOTHING;
4198 }
4199 if (xp_context == EXPAND_UNSUCCESSFUL)
zeertzjq961b2e52023-04-17 15:53:24 +01004200 return NULL;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00004201
zeertzjqa821b602024-06-18 20:31:08 +02004202 char_u *cmd_compl = cmdcomplete_type_to_str(xp_context);
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004203 if (cmd_compl == NULL)
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004204 return NULL;
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004205
zeertzjqa821b602024-06-18 20:31:08 +02004206 if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED)
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004207 {
4208 buffer = alloc(STRLEN(cmd_compl) + STRLEN(p->xpc->xp_arg) + 2);
4209 if (buffer == NULL)
4210 return NULL;
4211 sprintf((char *)buffer, "%s,%s", cmd_compl, p->xpc->xp_arg);
4212 return buffer;
4213 }
4214
4215 return vim_strsave(cmd_compl);
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004216}
4217
4218/*
4219 * "getcmdcompltype()" function
4220 */
4221 void
4222f_getcmdcompltype(typval_T *argvars UNUSED, typval_T *rettv)
4223{
4224 rettv->v_type = VAR_STRING;
4225 rettv->vval.v_string = get_cmdline_completion();
4226}
4227
4228/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004229 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004230 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004231 void
4232f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4233{
4234 rettv->v_type = VAR_STRING;
4235 rettv->vval.v_string = get_cmdline_str();
4236}
4237
4238/*
Shougo Matsushita69084282024-09-23 20:34:47 +02004239 * Get current command line prompt.
4240 */
4241 static char_u *
4242get_prompt(void)
4243{
4244 return current_prompt;
4245}
4246
4247/*
4248 * Set current command line prompt.
4249 */
4250 void
4251set_prompt(char_u* str)
4252{
4253 vim_strncpy(current_prompt, str, sizeof(current_prompt) - 1);
4254}
4255
4256/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004257 * "getcmdpos()" function
4258 */
4259 void
4260f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004261{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004262 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004263
zeertzjq54acb902022-08-29 16:21:25 +01004264 rettv->vval.v_number = p != NULL ? p->cmdpos + 1 : 0;
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004265}
4266
4267/*
Shougo Matsushita69084282024-09-23 20:34:47 +02004268 * "getcmdprompt()" function
4269 */
4270 void
4271f_getcmdprompt(typval_T *argvars UNUSED, typval_T *rettv)
4272{
4273 cmdline_info_T *p = get_ccline_ptr();
4274 rettv->v_type = VAR_STRING;
4275 rettv->vval.v_string = p != NULL ? vim_strsave(get_prompt()) : NULL;
4276}
4277
4278/*
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004279 * "getcmdscreenpos()" function
4280 */
4281 void
4282f_getcmdscreenpos(typval_T *argvars UNUSED, typval_T *rettv)
4283{
zeertzjq54acb902022-08-29 16:21:25 +01004284 cmdline_info_T *p = get_ccline_ptr();
4285
4286 rettv->vval.v_number = p != NULL ? p->cmdspos + 1 : 0;
4287}
4288
4289/*
4290 * "getcmdtype()" function
4291 */
4292 void
4293f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4294{
4295 rettv->v_type = VAR_STRING;
4296 rettv->vval.v_string = alloc(2);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004297 if (rettv->vval.v_string == NULL)
4298 return;
4299
4300 rettv->vval.v_string[0] = get_cmdline_type();
4301 rettv->vval.v_string[1] = NUL;
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004302}
4303
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004304// Set the command line str to "str".
4305// Returns 1 when failed, 0 when OK.
zeertzjq54acb902022-08-29 16:21:25 +01004306 static int
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004307set_cmdline_str(char_u *str, int pos)
4308{
4309 cmdline_info_T *p = get_ccline_ptr();
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004310 int len;
4311
4312 if (p == NULL)
4313 return 1;
4314
4315 len = (int)STRLEN(str);
4316 realloc_cmdbuff(len + 1);
4317 p->cmdlen = len;
4318 STRCPY(p->cmdbuff, str);
4319
4320 p->cmdpos = pos < 0 || pos > p->cmdlen ? p->cmdlen : pos;
4321 new_cmdpos = p->cmdpos;
4322
4323 redrawcmd();
4324
4325 // Trigger CmdlineChanged autocommands.
zeertzjq54acb902022-08-29 16:21:25 +01004326 trigger_cmd_autocmd(get_cmdline_type(), EVENT_CMDLINECHANGED);
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004327
4328 return 0;
4329}
4330
Shougo Matsushita79d599b2022-05-07 12:48:29 +01004331/*
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004332 * Set the command line byte position to "pos". Zero is the first position.
4333 * Only works when the command line is being edited.
4334 * Returns 1 when failed, 0 when OK.
4335 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004336 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004337set_cmdline_pos(
4338 int pos)
4339{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004340 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004341
4342 if (p == NULL)
4343 return 1;
4344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004345 // The position is not set directly but after CTRL-\ e or CTRL-R = has
4346 // changed the command line.
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004347 if (pos < 0)
4348 new_cmdpos = 0;
4349 else
4350 new_cmdpos = pos;
Shougo Matsushitad0952142024-06-20 22:05:16 +02004351
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004352 return 0;
4353}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004354
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004355// "setcmdline()" function
4356 void
4357f_setcmdline(typval_T *argvars, typval_T *rettv)
4358{
4359 int pos = -1;
4360
Yegappan Lakshmanan25f1e552022-08-28 17:25:04 +01004361 if (check_for_string_arg(argvars, 0) == FAIL
4362 || check_for_opt_number_arg(argvars, 1) == FAIL)
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004363 return;
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004364
4365 if (argvars[1].v_type != VAR_UNKNOWN)
4366 {
4367 int error = FALSE;
4368
4369 pos = (int)tv_get_number_chk(&argvars[1], &error) - 1;
4370 if (error)
4371 return;
4372 if (pos < 0)
4373 {
4374 emsg(_(e_argument_must_be_positive));
4375 return;
4376 }
4377 }
4378
zeertzjqac6cd312023-04-12 16:21:14 +01004379 // Use tv_get_string() to handle a NULL string like an empty string.
4380 rettv->vval.v_number = set_cmdline_str(tv_get_string(&argvars[0]), pos);
Shougo Matsushita07ea5f12022-08-27 12:22:25 +01004381}
4382
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004383/*
4384 * "setcmdpos()" function
4385 */
4386 void
4387f_setcmdpos(typval_T *argvars, typval_T *rettv)
4388{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004389 int pos;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004390
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004391 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
4392 return;
4393
4394 pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004395 if (pos >= 0)
4396 rettv->vval.v_number = set_cmdline_pos(pos);
4397}
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00004398#endif
Bram Moolenaar08c308a2019-09-04 17:48:15 +02004399
Bram Moolenaard7663c22019-08-06 21:59:57 +02004400/*
4401 * Return the first character of the current command line.
4402 */
4403 int
4404get_cmdline_firstc(void)
4405{
4406 return ccline.cmdfirstc;
4407}
4408
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409/*
4410 * Get indices "num1,num2" that specify a range within a list (not a range of
4411 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4412 * Returns OK if parsed successfully, otherwise FAIL.
4413 */
4414 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
4417 int len;
4418 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004419 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004422 if (**str == '-' || vim_isdigit(**str)) // parse "from" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00004424 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 *str += len;
Christian Brabandt9198c1f2023-10-26 21:29:32 +02004426 // overflow
4427 if (num > INT_MAX)
4428 return FAIL;
4429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 *num1 = (int)num;
4431 first = TRUE;
4432 }
4433 *str = skipwhite(*str);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004434 if (**str == ',') // parse "to" part of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
4436 *str = skipwhite(*str + 1);
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00004437 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (len > 0)
4439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 *str = skipwhite(*str + len);
Christian Brabandt9198c1f2023-10-26 21:29:32 +02004441 // overflow
4442 if (num > INT_MAX)
4443 return FAIL;
4444
4445 *num2 = (int)num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004447 else if (!first) // no number given at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 return FAIL;
4449 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004450 else if (first) // only one number given
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 *num2 = *num1;
4452 return OK;
4453}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004456 * Check value of 'cedit' and set cedit_key.
4457 * Returns NULL if value is OK, error message otherwise.
4458 */
4459 char *
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004460did_set_cedit(optset_T *args UNUSED)
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004461{
4462 int n;
4463
4464 if (*p_cedit == NUL)
4465 cedit_key = -1;
4466 else
4467 {
4468 n = string_to_key(p_cedit, FALSE);
4469 if (vim_isprintc(n))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004470 return e_invalid_argument;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004471 cedit_key = n;
4472 }
4473 return NULL;
4474}
4475
4476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 * Open a window on the current command line and history. Allow editing in
4478 * the window. Returns when the window is closed.
4479 * Returns:
4480 * CR if the command is to be executed
4481 * Ctrl_C if it is to be abandoned
4482 * K_IGNORE if editing continues
4483 */
4484 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004485open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004487 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004489 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 win_T *wp;
4491 int i;
4492 linenr_T lnum;
4493 int histtype;
4494 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 int save_restart_edit = restart_edit;
4496 int save_State = State;
4497 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004498#ifdef FEAT_RIGHTLEFT
4499 int save_cmdmsg_rl = cmdmsg_rl;
4500#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004501#ifdef FEAT_FOLDING
4502 int save_KeyTyped;
4503#endif
Sean Dewar43b395e2023-08-16 16:17:31 +01004504 int newbuf_status;
4505 int cmdwin_valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Bram Moolenaar71223e22022-05-30 15:23:09 +01004507 // Can't do this when text or buffer is locked.
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004508 // Can't do this recursively. Can't do it when typing a password.
Bram Moolenaarbe990422022-05-30 16:01:42 +01004509 if (text_or_buf_locked()
4510 || cmdwin_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4512 || cmdline_star > 0
4513# endif
4514 )
4515 {
4516 beep_flush();
4517 return K_IGNORE;
4518 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004519 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004521 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 win_size_save(&winsizes);
4523
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004524 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4525 // command line window, but we don't want the popup menu then.
4526 pum_undisplay();
4527
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004528 // don't use a new tab page
Bram Moolenaare1004402020-10-24 20:49:43 +02004529 cmdmod.cmod_tab = 0;
4530 cmdmod.cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004531
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004532 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4534 {
4535 beep_flush();
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004536 ga_clear(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 return K_IGNORE;
4538 }
Sean Dewar43b395e2023-08-16 16:17:31 +01004539 // win_split() autocommands may have messed with the old window or buffer.
4540 // Treat it as abandoning this command-line.
4541 if (!win_valid(old_curwin) || curwin == old_curwin
4542 || !bufref_valid(&old_curbuf)
4543 || old_curwin->w_buffer != old_curbuf.br_buf)
4544 {
4545 beep_flush();
4546 ga_clear(&winsizes);
4547 return Ctrl_C;
4548 }
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004549 // Don't let quitting the More prompt make this fail.
4550 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Sean Dewar988f7432023-08-16 14:17:36 +01004552 // Set "cmdwin_..." variables before any autocommands may mess things up.
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +01004553 cmdwin_type = get_cmdline_type();
Sean Dewar988f7432023-08-16 14:17:36 +01004554 cmdwin_win = curwin;
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +01004555
Sean Dewar43b395e2023-08-16 16:17:31 +01004556 // Create empty command-line buffer. Be especially cautious of BufLeave
4557 // autocommands from do_ecmd(), as cmdwin restrictions do not apply to them!
4558 newbuf_status = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE,
4559 NULL);
4560 cmdwin_valid = win_valid(cmdwin_win);
4561 if (newbuf_status == FAIL || !cmdwin_valid || curwin != cmdwin_win ||
4562 !win_valid(old_curwin) || !bufref_valid(&old_curbuf) ||
4563 old_curwin->w_buffer != old_curbuf.br_buf)
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004564 {
Sean Dewar43b395e2023-08-16 16:17:31 +01004565 if (newbuf_status == OK)
4566 set_bufref(&bufref, curbuf);
4567 if (cmdwin_valid && !last_window())
4568 win_close(cmdwin_win, TRUE);
4569
4570 // win_close() autocommands may have already deleted the buffer.
4571 if (newbuf_status == OK && bufref_valid(&bufref) &&
4572 bufref.br_buf != curbuf)
4573 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
4574
Bram Moolenaarbed72df2021-01-27 20:34:29 +01004575 cmdwin_type = 0;
Sean Dewar988f7432023-08-16 14:17:36 +01004576 cmdwin_win = NULL;
Sean Dewar43b395e2023-08-16 16:17:31 +01004577 beep_flush();
4578 ga_clear(&winsizes);
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004579 return Ctrl_C;
4580 }
Sean Dewar988f7432023-08-16 14:17:36 +01004581 cmdwin_buf = curbuf;
Bram Moolenaar9b7cce22020-06-06 15:14:08 +02004582
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004583 set_option_value_give_err((char_u *)"bt",
4584 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004586#ifdef FEAT_FOLDING
4587 curwin->w_p_fen = FALSE;
4588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004590 curwin->w_p_rl = cmdmsg_rl;
4591 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004593 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004595 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004596 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004598 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004599 need_wait_return = FALSE;
4600
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004601 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4603 {
4604 if (p_wc == TAB)
4605 {
Bram Moolenaar81f277f2023-05-13 13:55:09 +01004606 // Make Tab start command-line completion: CTRL-X CTRL-V
zeertzjq44068e92022-06-16 11:14:55 +01004607 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", MODE_INSERT, TRUE);
4608 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", MODE_NORMAL, TRUE);
Bram Moolenaar81f277f2023-05-13 13:55:09 +01004609
4610 // Make S-Tab work like CTRL-P in command-line completion
4611 add_map((char_u *)"<buffer> <S-Tab> <C-P>", MODE_INSERT, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004613 set_option_value_give_err((char_u *)"ft",
4614 0L, (char_u *)"vim", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004616 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004618 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4619 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004620 curbuf->b_p_tw = 0;
4621
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004622 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004624 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004626 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (i >= 0)
4628 {
4629 lnum = 0;
4630 do
4631 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004632 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004634 if (get_histentry(histtype)[i].hisstr != NULL)
4635 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 (colnr_T)0, FALSE);
4637 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004638 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 }
4641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004642 // Replace the empty last line with the current command-line and put the
4643 // cursor there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4645 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4646 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004647 changed_line_abv_curs();
4648 invalidate_botline();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004649 redraw_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Bram Moolenaar23324a02019-10-01 17:39:04 +02004651 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 exmode_active = 0;
4653
Bram Moolenaar24959102022-05-07 20:01:16 +01004654 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaar23324a02019-10-01 17:39:04 +02004657 // Reset here so it can be set by a CmdWinEnter autocommand.
4658 cmdwin_result = 0;
4659
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004660 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004661 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004662 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004663 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004665 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 RedrawingDisabled = 0;
4667
4668 /*
4669 * Call the main loop until <CR> or CTRL-C is typed.
4670 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004671 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004673 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004675# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004676 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004677# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004678
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004679 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004680 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004681
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004682# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004683 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004684 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685# endif
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 cmdwin_type = 0;
Sean Dewar988f7432023-08-16 14:17:36 +01004688 cmdwin_buf = NULL;
4689 cmdwin_win = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 exmode_active = save_exmode;
4691
Sean Dewar43b395e2023-08-16 16:17:31 +01004692 // Safety check: The old window or buffer was changed or deleted: It's a bug
4693 // when this happens!
4694 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)
4695 || old_curwin->w_buffer != old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
4697 cmdwin_result = Ctrl_C;
Sean Dewar43b395e2023-08-16 16:17:31 +01004698 emsg(_(e_active_window_or_buffer_changed_or_deleted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 else
4701 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004702# if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004703 // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (aborting() && cmdwin_result != K_IGNORE)
4705 cmdwin_result = Ctrl_C;
4706# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004707 // Set the new command line from the cmdline buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 vim_free(ccline.cmdbuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004709 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) // :qa[!] typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004711 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4712
4713 if (histtype == HIST_CMD)
4714 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004715 // Execute the command directly.
Bram Moolenaar46152342005-09-07 21:18:43 +00004716 ccline.cmdbuff = vim_strsave((char_u *)p);
4717 cmdwin_result = CAR;
4718 }
4719 else
4720 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004721 // First need to cancel what we were doing.
Bram Moolenaar46152342005-09-07 21:18:43 +00004722 ccline.cmdbuff = NULL;
4723 stuffcharReadbuff(':');
4724 stuffReadbuff((char_u *)p);
4725 stuffcharReadbuff(CAR);
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004728 else if (cmdwin_result == Ctrl_C)
4729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004730 // :q or :close, don't execute any command
4731 // and don't modify the cmd window.
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004732 ccline.cmdbuff = NULL;
4733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 else
4735 ccline.cmdbuff = vim_strsave(ml_get_curline());
4736 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004737 {
4738 ccline.cmdbuff = vim_strsave((char_u *)"");
4739 ccline.cmdlen = 0;
4740 ccline.cmdbufflen = 1;
4741 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 else
4745 {
4746 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4747 ccline.cmdbufflen = ccline.cmdlen + 1;
4748 ccline.cmdpos = curwin->w_cursor.col;
Bram Moolenaarb2f0ca82022-09-18 15:08:19 +01004749 // If the cursor is on the last character, it probably should be
4750 // after it.
4751 if (ccline.cmdpos == ccline.cmdlen - 1
4752 || ccline.cmdpos > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 ccline.cmdpos = ccline.cmdlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004756# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004757 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004758 curwin->w_p_cole = 0;
4759# endif
Bram Moolenaarb7e26702021-01-26 22:00:52 +01004760 // First go back to the original window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004762 set_bufref(&bufref, curbuf);
mityue697d482022-09-14 17:27:36 +01004763
Luuk van Baal3735f112022-09-15 12:43:26 +01004764 skip_win_fix_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 win_goto(old_curwin);
Bram Moolenaarb7e26702021-01-26 22:00:52 +01004766
4767 // win_goto() may trigger an autocommand that already closes the
4768 // cmdline window.
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01004769 if (win_valid(wp) && wp != curwin)
Bram Moolenaarb7e26702021-01-26 22:00:52 +01004770 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004771
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004772 // win_close() may have already wiped the buffer when 'bh' is
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01004773 // set to 'wipe', autocommands may have closed other windows
4774 if (bufref_valid(&bufref) && bufref.br_buf != curbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01004775 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004777 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 win_size_restore(&winsizes);
Luuk van Baal3735f112022-09-15 12:43:26 +01004779 skip_win_fix_cursor = FALSE;
Bram Moolenaarb2f0ca82022-09-18 15:08:19 +01004780
4781 if (cmdwin_result == K_IGNORE)
4782 {
4783 // It can be confusing that the cmdwin still shows, redraw the
4784 // screen.
4785 update_screen(UPD_VALID);
4786 set_cmdspos_cursor();
4787 redrawcmd();
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790
4791 ga_clear(&winsizes);
4792 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004793# ifdef FEAT_RIGHTLEFT
4794 cmdmsg_rl = save_cmdmsg_rl;
4795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 State = save_State;
zeertzjqc9e8fd62022-07-26 18:12:38 +01004798 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 return cmdwin_result;
4802}
mityua1198122021-11-20 19:13:39 +00004803
4804/*
4805 * Return TRUE if in the cmdwin, not editing the command line.
4806 */
4807 int
4808is_in_cmdwin(void)
4809{
4810 return cmdwin_type != 0 && get_cmdline_type() == NUL;
4811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813/*
4814 * Used for commands that either take a simple command string argument, or:
4815 * cmd << endmarker
4816 * {script}
4817 * endmarker
4818 * Returns a pointer to allocated memory with {script} or NULL.
4819 */
4820 char_u *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004821script_get(exarg_T *eap UNUSED, char_u *cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004823#ifdef FEAT_EVAL
4824 list_T *l;
4825 listitem_T *li;
4826 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 garray_T ga;
4828
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01004829 if (cmd[0] != '<' || cmd[1] != '<' || eap->ea_getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 return NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004831 cmd += 2;
4832
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01004833 l = heredoc_get(eap, cmd, TRUE, FALSE);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004834 if (l == NULL)
4835 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 ga_init2(&ga, 1, 0x400);
4838
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004839 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004841 s = tv_get_string(&li->li_tv);
4842 ga_concat(&ga, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004845 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004847 list_free(l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return (char_u *)ga.ga_data;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02004849#else
4850 return NULL;
4851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004853
4854#if defined(FEAT_EVAL) || defined(PROTO)
4855/*
4856 * This function is used by f_input() and f_inputdialog() functions. The third
4857 * argument to f_input() specifies the type of completion to use at the
4858 * prompt. The third argument to f_inputdialog() specifies the value to return
4859 * when the user cancels the prompt.
4860 */
4861 void
4862get_user_input(
4863 typval_T *argvars,
4864 typval_T *rettv,
4865 int inputdialog,
4866 int secret)
4867{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004868 char_u *prompt;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004869 char_u *p = NULL;
4870 int c;
4871 char_u buf[NUMBUFLEN];
4872 int cmd_silent_save = cmd_silent;
4873 char_u *defstr = (char_u *)"";
4874 int xp_type = EXPAND_NOTHING;
4875 char_u *xp_arg = NULL;
4876
4877 rettv->v_type = VAR_STRING;
4878 rettv->vval.v_string = NULL;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004879 if (input_busy)
4880 return; // this doesn't work recursively.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004881
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004882 if (in_vim9script()
4883 && (check_for_string_arg(argvars, 0) == FAIL
4884 || check_for_opt_string_arg(argvars, 1) == FAIL
4885 || (argvars[1].v_type != VAR_UNKNOWN
4886 && check_for_opt_string_arg(argvars, 2) == FAIL)))
4887 return;
4888
4889 prompt = tv_get_string_chk(&argvars[0]);
4890
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004891#ifdef NO_CONSOLE_INPUT
4892 // While starting up, there is no place to enter text. When running tests
4893 // with --not-a-term we assume feedkeys() will be used.
4894 if (no_console_input() && !is_not_a_term())
4895 return;
4896#endif
4897
4898 cmd_silent = FALSE; // Want to see the prompt.
4899 if (prompt != NULL)
4900 {
Shougo Matsushita69084282024-09-23 20:34:47 +02004901 set_prompt(prompt);
4902
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004903 // Only the part of the message after the last NL is considered as
4904 // prompt for the command line
4905 p = vim_strrchr(prompt, '\n');
4906 if (p == NULL)
4907 p = prompt;
4908 else
4909 {
4910 ++p;
4911 c = *p;
4912 *p = NUL;
4913 msg_start();
4914 msg_clr_eos();
4915 msg_puts_attr((char *)prompt, get_echo_attr());
4916 msg_didout = FALSE;
4917 msg_starthere();
4918 *p = c;
4919 }
4920 cmdline_row = msg_row;
4921
4922 if (argvars[1].v_type != VAR_UNKNOWN)
4923 {
4924 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4925 if (defstr != NULL)
4926 stuffReadbuffSpec(defstr);
4927
4928 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4929 {
4930 char_u *xp_name;
4931 int xp_namelen;
Bram Moolenaar05c17342022-02-25 20:57:11 +00004932 long argt = 0;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004933
4934 // input() with a third argument: completion
4935 rettv->vval.v_string = NULL;
4936
4937 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4938 if (xp_name == NULL)
4939 return;
4940
4941 xp_namelen = (int)STRLEN(xp_name);
4942
4943 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4944 &xp_arg) == FAIL)
4945 return;
4946 }
4947 }
4948
4949 if (defstr != NULL)
4950 {
4951 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004952 int save_vgetc_busy = vgetc_busy;
4953 int save_input_busy = input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004954
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004955 input_busy |= vgetc_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004956 ex_normal_busy = 0;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004957 vgetc_busy = 0;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004958 rettv->vval.v_string =
4959 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4960 xp_type, xp_arg);
4961 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaardfc33a62020-04-29 22:30:13 +02004962 vgetc_busy = save_vgetc_busy;
4963 input_busy = save_input_busy;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004964 }
4965 if (inputdialog && rettv->vval.v_string == NULL
4966 && argvars[1].v_type != VAR_UNKNOWN
4967 && argvars[2].v_type != VAR_UNKNOWN)
4968 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4969 &argvars[2], buf));
4970
4971 vim_free(xp_arg);
4972
4973 // since the user typed this, no need to wait for return
4974 need_wait_return = FALSE;
4975 msg_didout = FALSE;
4976 }
4977 cmd_silent = cmd_silent_save;
4978}
4979#endif