blob: d6e613f0b59d119ad9489b360ccda8e9876dcc27 [file] [log] [blame]
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
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 * arglist.c: functions for dealing with the argument list
12 */
13
14#include "vim.h"
15
16#define AL_SET 1
17#define AL_ADD 2
18#define AL_DEL 3
19
Bram Moolenaar5ed58c72021-01-28 14:24:55 +010020// This flag is set whenever the argument list is being changed and calling a
21// function that might trigger an autocommand.
22static int arglist_locked = FALSE;
23
24 static int
25check_arglist_locked(void)
26{
27 if (arglist_locked)
28 {
29 emsg(_(e_cannot_change_arglist_recursively));
30 return FAIL;
31 }
32 return OK;
33}
34
Bram Moolenaar4ad62152019-08-17 14:38:55 +020035/*
36 * Clear an argument list: free all file names and reset it to zero entries.
37 */
38 void
39alist_clear(alist_T *al)
40{
Bram Moolenaar5ed58c72021-01-28 14:24:55 +010041 if (check_arglist_locked() == FAIL)
42 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +020043 while (--al->al_ga.ga_len >= 0)
44 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
45 ga_clear(&al->al_ga);
46}
47
48/*
49 * Init an argument list.
50 */
51 void
52alist_init(alist_T *al)
53{
Bram Moolenaar04935fb2022-01-08 16:19:22 +000054 ga_init2(&al->al_ga, sizeof(aentry_T), 5);
Bram Moolenaar4ad62152019-08-17 14:38:55 +020055}
56
57/*
58 * Remove a reference from an argument list.
59 * Ignored when the argument list is the global one.
60 * If the argument list is no longer used by any window, free it.
61 */
62 void
63alist_unlink(alist_T *al)
64{
65 if (al != &global_alist && --al->al_refcount <= 0)
66 {
67 alist_clear(al);
68 vim_free(al);
69 }
70}
71
72/*
73 * Create a new argument list and use it for the current window.
74 */
75 void
76alist_new(void)
77{
78 curwin->w_alist = ALLOC_ONE(alist_T);
79 if (curwin->w_alist == NULL)
80 {
81 curwin->w_alist = &global_alist;
82 ++global_alist.al_refcount;
83 }
84 else
85 {
86 curwin->w_alist->al_refcount = 1;
87 curwin->w_alist->id = ++max_alist_id;
88 alist_init(curwin->w_alist);
89 }
90}
91
92#if !defined(UNIX) || defined(PROTO)
93/*
94 * Expand the file names in the global argument list.
95 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
96 * numbers to be re-used.
97 */
98 void
99alist_expand(int *fnum_list, int fnum_len)
100{
101 char_u **old_arg_files;
102 int old_arg_count;
103 char_u **new_arg_files;
104 int new_arg_file_count;
105 char_u *save_p_su = p_su;
106 int i;
107
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100108 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
109 if (old_arg_files == NULL)
110 return;
111
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200112 // Don't use 'suffixes' here. This should work like the shell did the
113 // expansion. Also, the vimrc file isn't read yet, thus the user
114 // can't set the options.
115 p_su = empty_option;
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100116 for (i = 0; i < GARGCOUNT; ++i)
117 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
118 old_arg_count = GARGCOUNT;
119 if (expand_wildcards(old_arg_count, old_arg_files,
120 &new_arg_file_count, &new_arg_files,
121 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
122 && new_arg_file_count > 0)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200123 {
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100124 alist_set(&global_alist, new_arg_file_count, new_arg_files,
125 TRUE, fnum_list, fnum_len);
126 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200127 }
128 p_su = save_p_su;
129}
130#endif
131
132/*
133 * Set the argument list for the current window.
134 * Takes over the allocated files[] and the allocated fnames in it.
135 */
136 void
137alist_set(
138 alist_T *al,
139 int count,
140 char_u **files,
141 int use_curbuf,
142 int *fnum_list,
143 int fnum_len)
144{
145 int i;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200146
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100147 if (check_arglist_locked() == FAIL)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200148 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200149
150 alist_clear(al);
Bram Moolenaar35578162021-08-02 19:10:38 +0200151 if (GA_GROW_OK(&al->al_ga, count))
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200152 {
153 for (i = 0; i < count; ++i)
154 {
155 if (got_int)
156 {
157 // When adding many buffers this can take a long time. Allow
158 // interrupting here.
159 while (i < count)
160 vim_free(files[i++]);
161 break;
162 }
163
164 // May set buffer name of a buffer previously used for the
165 // argument list, so that it's re-used by alist_add.
166 if (fnum_list != NULL && i < fnum_len)
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100167 {
168 arglist_locked = TRUE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200169 buf_set_name(fnum_list[i], files[i]);
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100170 arglist_locked = FALSE;
171 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200172
173 alist_add(al, files[i], use_curbuf ? 2 : 1);
174 ui_breakcheck();
175 }
176 vim_free(files);
177 }
178 else
179 FreeWild(count, files);
180 if (al == &global_alist)
181 arg_had_last = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200182}
183
184/*
185 * Add file "fname" to argument list "al".
186 * "fname" must have been allocated and "al" must have been checked for room.
187 */
188 void
189alist_add(
190 alist_T *al,
191 char_u *fname,
192 int set_fnum) // 1: set buffer number; 2: re-use curbuf
193{
194 if (fname == NULL) // don't add NULL file names
195 return;
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100196 if (check_arglist_locked() == FAIL)
197 return;
198 arglist_locked = TRUE;
199
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200200#ifdef BACKSLASH_IN_FILENAME
201 slash_adjust(fname);
202#endif
203 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
204 if (set_fnum > 0)
205 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
206 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
207 ++al->al_ga.ga_len;
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100208
209 arglist_locked = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200210}
211
212#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
213/*
214 * Adjust slashes in file names. Called after 'shellslash' was set.
215 */
216 void
217alist_slash_adjust(void)
218{
219 int i;
220 win_T *wp;
221 tabpage_T *tp;
222
223 for (i = 0; i < GARGCOUNT; ++i)
224 if (GARGLIST[i].ae_fname != NULL)
225 slash_adjust(GARGLIST[i].ae_fname);
226 FOR_ALL_TAB_WINDOWS(tp, wp)
227 if (wp->w_alist != &global_alist)
228 for (i = 0; i < WARGCOUNT(wp); ++i)
229 if (WARGLIST(wp)[i].ae_fname != NULL)
230 slash_adjust(WARGLIST(wp)[i].ae_fname);
231}
232#endif
233
234/*
235 * Isolate one argument, taking backticks.
236 * Changes the argument in-place, puts a NUL after it. Backticks remain.
237 * Return a pointer to the start of the next argument.
238 */
239 static char_u *
240do_one_arg(char_u *str)
241{
242 char_u *p;
243 int inbacktick;
244
245 inbacktick = FALSE;
246 for (p = str; *str; ++str)
247 {
248 // When the backslash is used for escaping the special meaning of a
249 // character we need to keep it until wildcard expansion.
250 if (rem_backslash(str))
251 {
252 *p++ = *str++;
253 *p++ = *str;
254 }
255 else
256 {
257 // An item ends at a space not in backticks
258 if (!inbacktick && vim_isspace(*str))
259 break;
260 if (*str == '`')
261 inbacktick ^= TRUE;
262 *p++ = *str;
263 }
264 }
265 str = skipwhite(str);
266 *p = NUL;
267
268 return str;
269}
270
271/*
272 * Separate the arguments in "str" and return a list of pointers in the
273 * growarray "gap".
274 */
275 static int
276get_arglist(garray_T *gap, char_u *str, int escaped)
277{
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000278 ga_init2(gap, sizeof(char_u *), 20);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200279 while (*str != NUL)
280 {
281 if (ga_grow(gap, 1) == FAIL)
282 {
283 ga_clear(gap);
284 return FAIL;
285 }
286 ((char_u **)gap->ga_data)[gap->ga_len++] = str;
287
288 // If str is escaped, don't handle backslashes or spaces
289 if (!escaped)
290 return OK;
291
292 // Isolate one argument, change it in-place, put a NUL after it.
293 str = do_one_arg(str);
294 }
295 return OK;
296}
297
298#if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
299/*
300 * Parse a list of arguments (file names), expand them and return in
301 * "fnames[fcountp]". When "wig" is TRUE, removes files matching 'wildignore'.
302 * Return FAIL or OK.
303 */
304 int
305get_arglist_exp(
306 char_u *str,
307 int *fcountp,
308 char_u ***fnamesp,
309 int wig)
310{
311 garray_T ga;
312 int i;
313
314 if (get_arglist(&ga, str, TRUE) == FAIL)
315 return FAIL;
316 if (wig == TRUE)
317 i = expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
Bram Moolenaarf8c6a172021-01-30 18:09:06 +0100318 fcountp, fnamesp, EW_FILE|EW_NOTFOUND|EW_NOTWILD);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200319 else
320 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
Bram Moolenaarf8c6a172021-01-30 18:09:06 +0100321 fcountp, fnamesp, EW_FILE|EW_NOTFOUND|EW_NOTWILD);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200322
323 ga_clear(&ga);
324 return i;
325}
326#endif
327
328/*
329 * Check the validity of the arg_idx for each other window.
330 */
331 static void
332alist_check_arg_idx(void)
333{
334 win_T *win;
335 tabpage_T *tp;
336
337 FOR_ALL_TAB_WINDOWS(tp, win)
338 if (win->w_alist == curwin->w_alist)
339 check_arg_idx(win);
340}
341
342/*
343 * Add files[count] to the arglist of the current window after arg "after".
344 * The file names in files[count] must have been allocated and are taken over.
345 * Files[] itself is not taken over.
346 */
347 static void
348alist_add_list(
349 int count,
350 char_u **files,
351 int after, // where to add: 0 = before first one
352 int will_edit) // will edit adding argument
353{
354 int i;
355 int old_argcount = ARGCOUNT;
356
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100357 if (check_arglist_locked() != FAIL
Bram Moolenaar35578162021-08-02 19:10:38 +0200358 && GA_GROW_OK(&ALIST(curwin)->al_ga, count))
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200359 {
360 if (after < 0)
361 after = 0;
362 if (after > ARGCOUNT)
363 after = ARGCOUNT;
364 if (after < ARGCOUNT)
365 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]),
366 (ARGCOUNT - after) * sizeof(aentry_T));
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100367 arglist_locked = TRUE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200368 for (i = 0; i < count; ++i)
369 {
370 int flags = BLN_LISTED | (will_edit ? BLN_CURBUF : 0);
371
372 ARGLIST[after + i].ae_fname = files[i];
373 ARGLIST[after + i].ae_fnum = buflist_add(files[i], flags);
374 }
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100375 arglist_locked = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200376 ALIST(curwin)->al_ga.ga_len += count;
377 if (old_argcount > 0 && curwin->w_arg_idx >= after)
378 curwin->w_arg_idx += count;
379 return;
380 }
381
382 for (i = 0; i < count; ++i)
383 vim_free(files[i]);
384}
385
386/*
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100387 * Delete the file names in 'alist_ga' from the argument list.
388 */
389 static void
390arglist_del_files(garray_T *alist_ga)
391{
392 regmatch_T regmatch;
393 int didone;
394 int i;
395 char_u *p;
396 int match;
397
398 // Delete the items: use each item as a regexp and find a match in the
399 // argument list.
400 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
401 for (i = 0; i < alist_ga->ga_len && !got_int; ++i)
402 {
403 p = ((char_u **)alist_ga->ga_data)[i];
404 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
405 if (p == NULL)
406 break;
407 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
408 if (regmatch.regprog == NULL)
409 {
410 vim_free(p);
411 break;
412 }
413
414 didone = FALSE;
415 for (match = 0; match < ARGCOUNT; ++match)
416 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
417 (colnr_T)0))
418 {
419 didone = TRUE;
420 vim_free(ARGLIST[match].ae_fname);
421 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
422 (ARGCOUNT - match - 1) * sizeof(aentry_T));
423 --ALIST(curwin)->al_ga.ga_len;
424 if (curwin->w_arg_idx > match)
425 --curwin->w_arg_idx;
426 --match;
427 }
428
429 vim_regfree(regmatch.regprog);
430 vim_free(p);
431 if (!didone)
432 semsg(_(e_no_match_str_2), ((char_u **)alist_ga->ga_data)[i]);
433 }
434 ga_clear(alist_ga);
435}
436
437/*
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200438 * "what" == AL_SET: Redefine the argument list to 'str'.
439 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
440 * "what" == AL_DEL: remove files in 'str' from the argument list.
441 *
442 * Return FAIL for failure, OK otherwise.
443 */
444 static int
445do_arglist(
446 char_u *str,
447 int what,
448 int after UNUSED, // 0 means before first one
449 int will_edit) // will edit added argument
450{
451 garray_T new_ga;
452 int exp_count;
453 char_u **exp_files;
454 int i;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200455 int arg_escaped = TRUE;
456
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100457 if (check_arglist_locked() == FAIL)
458 return FAIL;
459
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200460 // Set default argument for ":argadd" command.
461 if (what == AL_ADD && *str == NUL)
462 {
463 if (curbuf->b_ffname == NULL)
464 return FAIL;
465 str = curbuf->b_fname;
466 arg_escaped = FALSE;
467 }
468
469 // Collect all file name arguments in "new_ga".
470 if (get_arglist(&new_ga, str, arg_escaped) == FAIL)
471 return FAIL;
472
473 if (what == AL_DEL)
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100474 arglist_del_files(&new_ga);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200475 else
476 {
477 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
478 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
479 ga_clear(&new_ga);
480 if (i == FAIL || exp_count == 0)
481 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000482 emsg(_(e_no_match));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200483 return FAIL;
484 }
485
486 if (what == AL_ADD)
487 {
488 alist_add_list(exp_count, exp_files, after, will_edit);
489 vim_free(exp_files);
490 }
491 else // what == AL_SET
492 alist_set(ALIST(curwin), exp_count, exp_files, will_edit, NULL, 0);
493 }
494
495 alist_check_arg_idx();
496
497 return OK;
498}
499
500/*
501 * Redefine the argument list.
502 */
503 void
504set_arglist(char_u *str)
505{
506 do_arglist(str, AL_SET, 0, FALSE);
507}
508
509/*
510 * Return TRUE if window "win" is editing the file at the current argument
511 * index.
512 */
513 int
514editing_arg_idx(win_T *win)
515{
516 return !(win->w_arg_idx >= WARGCOUNT(win)
517 || (win->w_buffer->b_fnum
518 != WARGLIST(win)[win->w_arg_idx].ae_fnum
519 && (win->w_buffer->b_ffname == NULL
520 || !(fullpathcmp(
521 alist_name(&WARGLIST(win)[win->w_arg_idx]),
522 win->w_buffer->b_ffname, TRUE, TRUE) & FPC_SAME))));
523}
524
525/*
526 * Check if window "win" is editing the w_arg_idx file in its argument list.
527 */
528 void
529check_arg_idx(win_T *win)
530{
531 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
532 {
533 // We are not editing the current entry in the argument list.
534 // Set "arg_had_last" if we are editing the last one.
535 win->w_arg_idx_invalid = TRUE;
536 if (win->w_arg_idx != WARGCOUNT(win) - 1
537 && arg_had_last == FALSE
538 && ALIST(win) == &global_alist
539 && GARGCOUNT > 0
540 && win->w_arg_idx < GARGCOUNT
541 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
542 || (win->w_buffer->b_ffname != NULL
543 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
544 win->w_buffer->b_ffname, TRUE, TRUE) & FPC_SAME))))
545 arg_had_last = TRUE;
546 }
547 else
548 {
549 // We are editing the current entry in the argument list.
550 // Set "arg_had_last" if it's also the last one
551 win->w_arg_idx_invalid = FALSE;
552 if (win->w_arg_idx == WARGCOUNT(win) - 1
553 && win->w_alist == &global_alist)
554 arg_had_last = TRUE;
555 }
556}
557
558/*
559 * ":args", ":argslocal" and ":argsglobal".
560 */
561 void
562ex_args(exarg_T *eap)
563{
564 int i;
565
566 if (eap->cmdidx != CMD_args)
567 {
Bram Moolenaar6bcb8772021-02-03 21:23:29 +0100568 if (check_arglist_locked() == FAIL)
569 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200570 alist_unlink(ALIST(curwin));
571 if (eap->cmdidx == CMD_argglobal)
572 ALIST(curwin) = &global_alist;
573 else // eap->cmdidx == CMD_arglocal
574 alist_new();
575 }
576
577 if (*eap->arg != NUL)
578 {
Bram Moolenaar6bcb8772021-02-03 21:23:29 +0100579 if (check_arglist_locked() == FAIL)
580 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200581 // ":args file ..": define new argument list, handle like ":next"
582 // Also for ":argslocal file .." and ":argsglobal file ..".
583 ex_next(eap);
584 }
585 else if (eap->cmdidx == CMD_args)
586 {
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100587 char_u **items;
588
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200589 // ":args": list arguments.
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100590 if (ARGCOUNT <= 0)
591 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200592
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100593 items = ALLOC_MULT(char_u *, ARGCOUNT);
594 if (items == NULL)
595 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200596
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100597 // Overwrite the command, for a short list there is no scrolling
598 // required and no wait_return().
599 gotocmdline(TRUE);
600
601 for (i = 0; i < ARGCOUNT; ++i)
602 items[i] = alist_name(&ARGLIST[i]);
603 list_in_columns(items, ARGCOUNT, curwin->w_arg_idx);
604 vim_free(items);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200605 }
606 else if (eap->cmdidx == CMD_arglocal)
607 {
608 garray_T *gap = &curwin->w_alist->al_ga;
609
610 // ":argslocal": make a local copy of the global argument list.
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +0100611 if (GA_GROW_FAILS(gap, GARGCOUNT))
612 return;
613
614 for (i = 0; i < GARGCOUNT; ++i)
615 if (GARGLIST[i].ae_fname != NULL)
616 {
617 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
618 vim_strsave(GARGLIST[i].ae_fname);
619 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
620 GARGLIST[i].ae_fnum;
621 ++gap->ga_len;
622 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200623 }
624}
625
626/*
627 * ":previous", ":sprevious", ":Next" and ":sNext".
628 */
629 void
630ex_previous(exarg_T *eap)
631{
632 // If past the last one already, go to the last one.
633 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
634 do_argfile(eap, ARGCOUNT - 1);
635 else
636 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
637}
638
639/*
640 * ":rewind", ":first", ":sfirst" and ":srewind".
641 */
642 void
643ex_rewind(exarg_T *eap)
644{
645 do_argfile(eap, 0);
646}
647
648/*
649 * ":last" and ":slast".
650 */
651 void
652ex_last(exarg_T *eap)
653{
654 do_argfile(eap, ARGCOUNT - 1);
655}
656
657/*
658 * ":argument" and ":sargument".
659 */
660 void
661ex_argument(exarg_T *eap)
662{
663 int i;
664
665 if (eap->addr_count > 0)
666 i = eap->line2 - 1;
667 else
668 i = curwin->w_arg_idx;
669 do_argfile(eap, i);
670}
671
672/*
673 * Edit file "argn" of the argument lists.
674 */
675 void
676do_argfile(exarg_T *eap, int argn)
677{
678 int other;
679 char_u *p;
680 int old_arg_idx = curwin->w_arg_idx;
681
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +0100682 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200683 return;
684 if (argn < 0 || argn >= ARGCOUNT)
685 {
686 if (ARGCOUNT <= 1)
Bram Moolenaar1a992222021-12-31 17:25:48 +0000687 emsg(_(e_there_is_only_one_file_to_edit));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200688 else if (argn < 0)
Bram Moolenaar1a992222021-12-31 17:25:48 +0000689 emsg(_(e_cannot_go_before_first_file));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200690 else
Bram Moolenaar1a992222021-12-31 17:25:48 +0000691 emsg(_(e_cannot_go_beyond_last_file));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200692 }
693 else
694 {
695 setpcmark();
696#ifdef FEAT_GUI
697 need_mouse_correct = TRUE;
698#endif
699
700 // split window or create new tab page first
Bram Moolenaare1004402020-10-24 20:49:43 +0200701 if (*eap->cmd == 's' || cmdmod.cmod_tab != 0)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200702 {
703 if (win_split(0, 0) == FAIL)
704 return;
705 RESET_BINDING(curwin);
706 }
707 else
708 {
709 // if 'hidden' set, only check for changed file when re-editing
710 // the same buffer
711 other = TRUE;
712 if (buf_hide(curbuf))
713 {
714 p = fix_fname(alist_name(&ARGLIST[argn]));
715 other = otherfile(p);
716 vim_free(p);
717 }
718 if ((!buf_hide(curbuf) || !other)
719 && check_changed(curbuf, CCGD_AW
720 | (other ? 0 : CCGD_MULTWIN)
721 | (eap->forceit ? CCGD_FORCEIT : 0)
722 | CCGD_EXCMD))
723 return;
724 }
725
726 curwin->w_arg_idx = argn;
727 if (argn == ARGCOUNT - 1 && curwin->w_alist == &global_alist)
728 arg_had_last = TRUE;
729
730 // Edit the file; always use the last known line number.
731 // When it fails (e.g. Abort for already edited file) restore the
732 // argument index.
733 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
734 eap, ECMD_LAST,
735 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
736 + (eap->forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
737 curwin->w_arg_idx = old_arg_idx;
738 // like Vi: set the mark where the cursor is in the file.
739 else if (eap->cmdidx != CMD_argdo)
740 setmark('\'');
741 }
742}
743
744/*
745 * ":next", and commands that behave like it.
746 */
747 void
748ex_next(exarg_T *eap)
749{
750 int i;
751
752 // check for changed buffer now, if this fails the argument list is not
753 // redefined.
754 if ( buf_hide(curbuf)
755 || eap->cmdidx == CMD_snext
756 || !check_changed(curbuf, CCGD_AW
757 | (eap->forceit ? CCGD_FORCEIT : 0)
758 | CCGD_EXCMD))
759 {
760 if (*eap->arg != NUL) // redefine file list
761 {
762 if (do_arglist(eap->arg, AL_SET, 0, TRUE) == FAIL)
763 return;
764 i = 0;
765 }
766 else
767 i = curwin->w_arg_idx + (int)eap->line2;
768 do_argfile(eap, i);
769 }
770}
771
772/*
Nir Lichtman73a02422021-12-24 20:28:03 +0000773 * ":argdedupe"
774 */
775 void
776ex_argdedupe(exarg_T *eap UNUSED)
777{
778 int i;
779 int j;
780
781 for (i = 0; i < ARGCOUNT; ++i)
782 for (j = i + 1; j < ARGCOUNT; ++j)
783 if (fnamecmp(ARGLIST[i].ae_fname, ARGLIST[j].ae_fname) == 0)
784 {
785 vim_free(ARGLIST[j].ae_fname);
786 mch_memmove(ARGLIST + j, ARGLIST + j + 1,
787 (ARGCOUNT - j - 1) * sizeof(aentry_T));
788 --ARGCOUNT;
789
790 if (curwin->w_arg_idx == j)
791 curwin->w_arg_idx = i;
792 else if (curwin->w_arg_idx > j)
793 --curwin->w_arg_idx;
794
795 --j;
796 }
797}
798
799/*
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200800 * ":argedit"
801 */
802 void
803ex_argedit(exarg_T *eap)
804{
805 int i = eap->addr_count ? (int)eap->line2 : curwin->w_arg_idx + 1;
806 // Whether curbuf will be reused, curbuf->b_ffname will be set.
807 int curbuf_is_reusable = curbuf_reusable();
808
809 if (do_arglist(eap->arg, AL_ADD, i, TRUE) == FAIL)
810 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200811 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200812
813 if (curwin->w_arg_idx == 0
814 && (curbuf->b_ml.ml_flags & ML_EMPTY)
815 && (curbuf->b_ffname == NULL || curbuf_is_reusable))
816 i = 0;
817 // Edit the argument.
818 if (i < ARGCOUNT)
819 do_argfile(eap, i);
820}
821
822/*
823 * ":argadd"
824 */
825 void
826ex_argadd(exarg_T *eap)
827{
828 do_arglist(eap->arg, AL_ADD,
829 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1,
830 FALSE);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200831 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200832}
833
834/*
835 * ":argdelete"
836 */
837 void
838ex_argdelete(exarg_T *eap)
839{
840 int i;
841 int n;
842
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100843 if (check_arglist_locked() == FAIL)
844 return;
845
Bram Moolenaar7b221172020-08-17 19:34:10 +0200846 if (eap->addr_count > 0 || *eap->arg == NUL)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200847 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +0200848 // ":argdel" works like ":.argdel"
Bram Moolenaar7b221172020-08-17 19:34:10 +0200849 if (eap->addr_count == 0)
850 {
851 if (curwin->w_arg_idx >= ARGCOUNT)
852 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000853 emsg(_(e_no_argument_to_delete));
Bram Moolenaar7b221172020-08-17 19:34:10 +0200854 return;
855 }
856 eap->line1 = eap->line2 = curwin->w_arg_idx + 1;
857 }
858 else if (eap->line2 > ARGCOUNT)
859 // ":1,4argdel": Delete all arguments in the range.
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200860 eap->line2 = ARGCOUNT;
861 n = eap->line2 - eap->line1 + 1;
862 if (*eap->arg != NUL)
863 // Can't have both a range and an argument.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000864 emsg(_(e_invalid_argument));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200865 else if (n <= 0)
866 {
867 // Don't give an error for ":%argdel" if the list is empty.
868 if (eap->line1 != 1 || eap->line2 != 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200869 emsg(_(e_invalid_range));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200870 }
871 else
872 {
873 for (i = eap->line1; i <= eap->line2; ++i)
874 vim_free(ARGLIST[i - 1].ae_fname);
875 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
876 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
877 ALIST(curwin)->al_ga.ga_len -= n;
878 if (curwin->w_arg_idx >= eap->line2)
879 curwin->w_arg_idx -= n;
880 else if (curwin->w_arg_idx > eap->line1)
881 curwin->w_arg_idx = eap->line1;
882 if (ARGCOUNT == 0)
883 curwin->w_arg_idx = 0;
884 else if (curwin->w_arg_idx >= ARGCOUNT)
885 curwin->w_arg_idx = ARGCOUNT - 1;
886 }
887 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200888 else
889 do_arglist(eap->arg, AL_DEL, 0, FALSE);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200890 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200891}
892
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200893/*
894 * Function given to ExpandGeneric() to obtain the possible arguments of the
895 * argedit and argdelete commands.
896 */
897 char_u *
898get_arglist_name(expand_T *xp UNUSED, int idx)
899{
900 if (idx >= ARGCOUNT)
901 return NULL;
902
903 return alist_name(&ARGLIST[idx]);
904}
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200905
906/*
907 * Get the file name for an argument list entry.
908 */
909 char_u *
910alist_name(aentry_T *aep)
911{
912 buf_T *bp;
913
914 // Use the name from the associated buffer if it exists.
915 bp = buflist_findnr(aep->ae_fnum);
916 if (bp == NULL || bp->b_fname == NULL)
917 return aep->ae_fname;
918 return bp->b_fname;
919}
920
921/*
922 * do_arg_all(): Open up to 'count' windows, one for each argument.
923 */
924 static void
925do_arg_all(
926 int count,
927 int forceit, // hide buffers in current windows
928 int keep_tabs) // keep current tabs, for ":tab drop file"
929{
930 int i;
931 win_T *wp, *wpnext;
932 char_u *opened; // Array of weight for which args are open:
933 // 0: not opened
934 // 1: opened in other tab
935 // 2: opened in curtab
936 // 3: opened in curtab and curwin
937 //
938 int opened_len; // length of opened[]
939 int use_firstwin = FALSE; // use first window for arglist
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100940 int tab_drop_empty_window = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200941 int split_ret = OK;
942 int p_ea_save;
943 alist_T *alist; // argument list to be used
944 buf_T *buf;
945 tabpage_T *tpnext;
Bram Moolenaare1004402020-10-24 20:49:43 +0200946 int had_tab = cmdmod.cmod_tab;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200947 win_T *old_curwin, *last_curwin;
948 tabpage_T *old_curtab, *last_curtab;
949 win_T *new_curwin = NULL;
950 tabpage_T *new_curtab = NULL;
Bram Moolenaar6f983712021-12-24 18:11:27 +0000951 int prev_arglist_locked = arglist_locked;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200952
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100953#ifdef FEAT_CMDWIN
954 if (cmdwin_type != 0)
955 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200956 emsg(_(e_invalid_in_cmdline_window));
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100957 return;
958 }
959#endif
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200960 if (ARGCOUNT <= 0)
961 {
962 // Don't give an error message. We don't want it when the ":all"
963 // command is in the .vimrc.
964 return;
965 }
966 setpcmark();
967
968 opened_len = ARGCOUNT;
969 opened = alloc_clear(opened_len);
970 if (opened == NULL)
971 return;
972
973 // Autocommands may do anything to the argument list. Make sure it's not
974 // freed while we are working here by "locking" it. We still have to
975 // watch out for its size to be changed.
976 alist = curwin->w_alist;
977 ++alist->al_refcount;
Bram Moolenaar6f983712021-12-24 18:11:27 +0000978 arglist_locked = TRUE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200979
980 old_curwin = curwin;
981 old_curtab = curtab;
982
K.Takata6e1d31e2022-02-03 13:05:32 +0000983#ifdef FEAT_GUI
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200984 need_mouse_correct = TRUE;
K.Takata6e1d31e2022-02-03 13:05:32 +0000985#endif
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200986
987 // Try closing all windows that are not in the argument list.
988 // Also close windows that are not full width;
989 // When 'hidden' or "forceit" set the buffer becomes hidden.
990 // Windows that have a changed buffer and can't be hidden won't be closed.
991 // When the ":tab" modifier was used do this for all tab pages.
992 if (had_tab > 0)
993 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
994 for (;;)
995 {
996 tpnext = curtab->tp_next;
997 for (wp = firstwin; wp != NULL; wp = wpnext)
998 {
999 wpnext = wp->w_next;
1000 buf = wp->w_buffer;
1001 if (buf->b_ffname == NULL
1002 || (!keep_tabs && (buf->b_nwindows > 1
1003 || wp->w_width != Columns)))
1004 i = opened_len;
1005 else
1006 {
1007 // check if the buffer in this window is in the arglist
1008 for (i = 0; i < opened_len; ++i)
1009 {
1010 if (i < alist->al_ga.ga_len
1011 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
1012 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
1013 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
1014 {
1015 int weight = 1;
1016
1017 if (old_curtab == curtab)
1018 {
1019 ++weight;
1020 if (old_curwin == wp)
1021 ++weight;
1022 }
1023
1024 if (weight > (int)opened[i])
1025 {
1026 opened[i] = (char_u)weight;
1027 if (i == 0)
1028 {
1029 if (new_curwin != NULL)
1030 new_curwin->w_arg_idx = opened_len;
1031 new_curwin = wp;
1032 new_curtab = curtab;
1033 }
1034 }
1035 else if (keep_tabs)
1036 i = opened_len;
1037
1038 if (wp->w_alist != alist)
1039 {
1040 // Use the current argument list for all windows
1041 // containing a file from it.
1042 alist_unlink(wp->w_alist);
1043 wp->w_alist = alist;
1044 ++wp->w_alist->al_refcount;
1045 }
1046 break;
1047 }
1048 }
1049 }
1050 wp->w_arg_idx = i;
1051
1052 if (i == opened_len && !keep_tabs)// close this window
1053 {
1054 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
1055 || !bufIsChanged(buf))
1056 {
1057 // If the buffer was changed, and we would like to hide it,
1058 // try autowriting.
1059 if (!buf_hide(buf) && buf->b_nwindows <= 1
1060 && bufIsChanged(buf))
1061 {
1062 bufref_T bufref;
1063
1064 set_bufref(&bufref, buf);
1065
1066 (void)autowrite(buf, FALSE);
1067
1068 // check if autocommands removed the window
1069 if (!win_valid(wp) || !bufref_valid(&bufref))
1070 {
1071 wpnext = firstwin; // start all over...
1072 continue;
1073 }
1074 }
1075 // don't close last window
1076 if (ONE_WINDOW
1077 && (first_tabpage->tp_next == NULL || !had_tab))
1078 use_firstwin = TRUE;
1079 else
1080 {
1081 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
1082
1083 // check if autocommands removed the next window
1084 if (!win_valid(wpnext))
1085 wpnext = firstwin; // start all over...
1086 }
1087 }
1088 }
1089 }
1090
1091 // Without the ":tab" modifier only do the current tab page.
1092 if (had_tab == 0 || tpnext == NULL)
1093 break;
1094
1095 // check if autocommands removed the next tab page
1096 if (!valid_tabpage(tpnext))
1097 tpnext = first_tabpage; // start all over...
1098
1099 goto_tabpage_tp(tpnext, TRUE, TRUE);
1100 }
1101
1102 // Open a window for files in the argument list that don't have one.
1103 // ARGCOUNT may change while doing this, because of autocommands.
1104 if (count > opened_len || count <= 0)
1105 count = opened_len;
1106
1107 // Don't execute Win/Buf Enter/Leave autocommands here.
1108 ++autocmd_no_enter;
1109 ++autocmd_no_leave;
1110 last_curwin = curwin;
1111 last_curtab = curtab;
1112 win_enter(lastwin, FALSE);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001113 // ":tab drop file" should re-use an empty window to avoid "--remote-tab"
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001114 // leaving an empty tab page when executed locally.
1115 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
1116 && curbuf->b_ffname == NULL && !curbuf->b_changed)
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001117 {
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001118 use_firstwin = TRUE;
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001119 tab_drop_empty_window = TRUE;
1120 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001121
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001122 for (i = 0; i < count && !got_int; ++i)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001123 {
1124 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
1125 arg_had_last = TRUE;
1126 if (opened[i] > 0)
1127 {
1128 // Move the already present window to below the current window
1129 if (curwin->w_arg_idx != i)
1130 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001131 FOR_ALL_WINDOWS(wpnext)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001132 {
1133 if (wpnext->w_arg_idx == i)
1134 {
1135 if (keep_tabs)
1136 {
1137 new_curwin = wpnext;
1138 new_curtab = curtab;
1139 }
1140 else if (wpnext->w_frame->fr_parent
1141 != curwin->w_frame->fr_parent)
1142 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001143 emsg(_(e_window_layout_changed_unexpectedly));
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001144 i = count;
1145 break;
1146 }
1147 else
1148 win_move_after(wpnext, curwin);
1149 break;
1150 }
1151 }
1152 }
1153 }
1154 else if (split_ret == OK)
1155 {
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001156 // trigger events for tab drop
1157 if (tab_drop_empty_window && i == count - 1)
1158 --autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001159 if (!use_firstwin) // split current window
1160 {
1161 p_ea_save = p_ea;
1162 p_ea = TRUE; // use space from all windows
1163 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
1164 p_ea = p_ea_save;
1165 if (split_ret == FAIL)
1166 continue;
1167 }
1168 else // first window: do autocmd for leaving this buffer
1169 --autocmd_no_leave;
1170
1171 // edit file "i"
1172 curwin->w_arg_idx = i;
1173 if (i == 0)
1174 {
1175 new_curwin = curwin;
1176 new_curtab = curtab;
1177 }
1178 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
1179 ECMD_ONE,
1180 ((buf_hide(curwin->w_buffer)
1181 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
1182 + ECMD_OLDBUF, curwin);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001183 if (tab_drop_empty_window && i == count - 1)
1184 ++autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001185 if (use_firstwin)
1186 ++autocmd_no_leave;
1187 use_firstwin = FALSE;
1188 }
1189 ui_breakcheck();
1190
1191 // When ":tab" was used open a new tab for a new window repeatedly.
1192 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02001193 cmdmod.cmod_tab = 9999;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001194 }
1195
1196 // Remove the "lock" on the argument list.
1197 alist_unlink(alist);
Bram Moolenaar6f983712021-12-24 18:11:27 +00001198 arglist_locked = prev_arglist_locked;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001199
1200 --autocmd_no_enter;
1201
1202 // restore last referenced tabpage's curwin
1203 if (last_curtab != new_curtab)
1204 {
1205 if (valid_tabpage(last_curtab))
1206 goto_tabpage_tp(last_curtab, TRUE, TRUE);
1207 if (win_valid(last_curwin))
1208 win_enter(last_curwin, FALSE);
1209 }
1210 // to window with first arg
1211 if (valid_tabpage(new_curtab))
1212 goto_tabpage_tp(new_curtab, TRUE, TRUE);
1213 if (win_valid(new_curwin))
1214 win_enter(new_curwin, FALSE);
1215
1216 --autocmd_no_leave;
1217 vim_free(opened);
1218}
1219
1220/*
1221 * ":all" and ":sall".
1222 * Also used for ":tab drop file ..." after setting the argument list.
1223 */
1224 void
1225ex_all(exarg_T *eap)
1226{
1227 if (eap->addr_count == 0)
1228 eap->line2 = 9999;
1229 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
1230}
1231
1232/*
1233 * Concatenate all files in the argument list, separated by spaces, and return
1234 * it in one allocated string.
1235 * Spaces and backslashes in the file names are escaped with a backslash.
1236 * Returns NULL when out of memory.
1237 */
1238 char_u *
1239arg_all(void)
1240{
1241 int len;
1242 int idx;
1243 char_u *retval = NULL;
1244 char_u *p;
1245
1246 // Do this loop two times:
1247 // first time: compute the total length
1248 // second time: concatenate the names
1249 for (;;)
1250 {
1251 len = 0;
1252 for (idx = 0; idx < ARGCOUNT; ++idx)
1253 {
1254 p = alist_name(&ARGLIST[idx]);
zeertzjq101d57b2022-07-31 18:34:32 +01001255 if (p == NULL)
1256 continue;
1257 if (len > 0)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001258 {
zeertzjq101d57b2022-07-31 18:34:32 +01001259 // insert a space in between names
1260 if (retval != NULL)
1261 retval[len] = ' ';
1262 ++len;
1263 }
1264 for ( ; *p != NUL; ++p)
1265 {
1266 if (*p == ' '
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001267#ifndef BACKSLASH_IN_FILENAME
zeertzjq101d57b2022-07-31 18:34:32 +01001268 || *p == '\\'
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001269#endif
zeertzjq101d57b2022-07-31 18:34:32 +01001270 || *p == '`')
1271 {
1272 // insert a backslash
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001273 if (retval != NULL)
zeertzjq101d57b2022-07-31 18:34:32 +01001274 retval[len] = '\\';
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001275 ++len;
1276 }
zeertzjq101d57b2022-07-31 18:34:32 +01001277 if (retval != NULL)
1278 retval[len] = *p;
1279 ++len;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001280 }
1281 }
1282
1283 // second time: break here
1284 if (retval != NULL)
1285 {
1286 retval[len] = NUL;
1287 break;
1288 }
1289
1290 // allocate memory
1291 retval = alloc(len + 1);
1292 if (retval == NULL)
1293 break;
1294 }
1295
1296 return retval;
1297}
1298
1299#if defined(FEAT_EVAL) || defined(PROTO)
1300/*
1301 * "argc([window id])" function
1302 */
1303 void
1304f_argc(typval_T *argvars, typval_T *rettv)
1305{
1306 win_T *wp;
1307
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001308 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1309 return;
1310
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001311 if (argvars[0].v_type == VAR_UNKNOWN)
1312 // use the current window
1313 rettv->vval.v_number = ARGCOUNT;
1314 else if (argvars[0].v_type == VAR_NUMBER
1315 && tv_get_number(&argvars[0]) == -1)
1316 // use the global argument list
1317 rettv->vval.v_number = GARGCOUNT;
1318 else
1319 {
1320 // use the argument list of the specified window
1321 wp = find_win_by_nr_or_id(&argvars[0]);
1322 if (wp != NULL)
1323 rettv->vval.v_number = WARGCOUNT(wp);
1324 else
1325 rettv->vval.v_number = -1;
1326 }
1327}
1328
1329/*
1330 * "argidx()" function
1331 */
1332 void
1333f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1334{
1335 rettv->vval.v_number = curwin->w_arg_idx;
1336}
1337
1338/*
1339 * "arglistid()" function
1340 */
1341 void
1342f_arglistid(typval_T *argvars, typval_T *rettv)
1343{
1344 win_T *wp;
1345
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001346 if (in_vim9script()
1347 && (check_for_opt_number_arg(argvars, 0) == FAIL
1348 || (argvars[0].v_type != VAR_UNKNOWN
1349 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1350 return;
1351
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001352 rettv->vval.v_number = -1;
1353 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
1354 if (wp != NULL)
1355 rettv->vval.v_number = wp->w_alist->id;
1356}
1357
1358/*
1359 * Get the argument list for a given window
1360 */
1361 static void
1362get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1363{
1364 int idx;
1365
1366 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1367 for (idx = 0; idx < argcount; ++idx)
1368 list_append_string(rettv->vval.v_list,
1369 alist_name(&arglist[idx]), -1);
1370}
1371
1372/*
1373 * "argv(nr)" function
1374 */
1375 void
1376f_argv(typval_T *argvars, typval_T *rettv)
1377{
1378 int idx;
1379 aentry_T *arglist = NULL;
1380 int argcount = -1;
1381
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001382 if (in_vim9script()
1383 && (check_for_opt_number_arg(argvars, 0) == FAIL
1384 || (argvars[0].v_type != VAR_UNKNOWN
1385 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1386 return;
1387
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +01001388 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001389 {
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +01001390 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
1391 return;
1392 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001393
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +01001394 if (argvars[1].v_type == VAR_UNKNOWN)
1395 {
1396 arglist = ARGLIST;
1397 argcount = ARGCOUNT;
1398 }
1399 else if (argvars[1].v_type == VAR_NUMBER
1400 && tv_get_number(&argvars[1]) == -1)
1401 {
1402 arglist = GARGLIST;
1403 argcount = GARGCOUNT;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001404 }
1405 else
Yegappan Lakshmananb1f471e2022-09-05 14:33:47 +01001406 {
1407 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1408
1409 if (wp != NULL)
1410 {
1411 // Use the argument list of the specified window
1412 arglist = WARGLIST(wp);
1413 argcount = WARGCOUNT(wp);
1414 }
1415 }
1416
1417 rettv->v_type = VAR_STRING;
1418 rettv->vval.v_string = NULL;
1419 idx = tv_get_number_chk(&argvars[0], NULL);
1420 if (arglist != NULL && idx >= 0 && idx < argcount)
1421 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1422 else if (idx == -1)
1423 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001424}
1425#endif