blob: d159f3f7cc831f71ae8d323c5d060a49c4cee755 [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{
54 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
55}
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
108 // Don't use 'suffixes' here. This should work like the shell did the
109 // expansion. Also, the vimrc file isn't read yet, thus the user
110 // can't set the options.
111 p_su = empty_option;
112 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
113 if (old_arg_files != NULL)
114 {
115 for (i = 0; i < GARGCOUNT; ++i)
116 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
117 old_arg_count = GARGCOUNT;
118 if (expand_wildcards(old_arg_count, old_arg_files,
119 &new_arg_file_count, &new_arg_files,
120 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
121 && new_arg_file_count > 0)
122 {
123 alist_set(&global_alist, new_arg_file_count, new_arg_files,
124 TRUE, fnum_list, fnum_len);
125 FreeWild(old_arg_count, old_arg_files);
126 }
127 }
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{
278 ga_init2(gap, (int)sizeof(char_u *), 20);
279 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/*
387 * "what" == AL_SET: Redefine the argument list to 'str'.
388 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
389 * "what" == AL_DEL: remove files in 'str' from the argument list.
390 *
391 * Return FAIL for failure, OK otherwise.
392 */
393 static int
394do_arglist(
395 char_u *str,
396 int what,
397 int after UNUSED, // 0 means before first one
398 int will_edit) // will edit added argument
399{
400 garray_T new_ga;
401 int exp_count;
402 char_u **exp_files;
403 int i;
404 char_u *p;
405 int match;
406 int arg_escaped = TRUE;
407
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100408 if (check_arglist_locked() == FAIL)
409 return FAIL;
410
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200411 // Set default argument for ":argadd" command.
412 if (what == AL_ADD && *str == NUL)
413 {
414 if (curbuf->b_ffname == NULL)
415 return FAIL;
416 str = curbuf->b_fname;
417 arg_escaped = FALSE;
418 }
419
420 // Collect all file name arguments in "new_ga".
421 if (get_arglist(&new_ga, str, arg_escaped) == FAIL)
422 return FAIL;
423
424 if (what == AL_DEL)
425 {
426 regmatch_T regmatch;
427 int didone;
428
429 // Delete the items: use each item as a regexp and find a match in the
430 // argument list.
431 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
432 for (i = 0; i < new_ga.ga_len && !got_int; ++i)
433 {
434 p = ((char_u **)new_ga.ga_data)[i];
435 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
436 if (p == NULL)
437 break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100438 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200439 if (regmatch.regprog == NULL)
440 {
441 vim_free(p);
442 break;
443 }
444
445 didone = FALSE;
446 for (match = 0; match < ARGCOUNT; ++match)
447 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
448 (colnr_T)0))
449 {
450 didone = TRUE;
451 vim_free(ARGLIST[match].ae_fname);
452 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
453 (ARGCOUNT - match - 1) * sizeof(aentry_T));
454 --ALIST(curwin)->al_ga.ga_len;
455 if (curwin->w_arg_idx > match)
456 --curwin->w_arg_idx;
457 --match;
458 }
459
460 vim_regfree(regmatch.regprog);
461 vim_free(p);
462 if (!didone)
463 semsg(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
464 }
465 ga_clear(&new_ga);
466 }
467 else
468 {
469 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
470 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
471 ga_clear(&new_ga);
472 if (i == FAIL || exp_count == 0)
473 {
474 emsg(_(e_nomatch));
475 return FAIL;
476 }
477
478 if (what == AL_ADD)
479 {
480 alist_add_list(exp_count, exp_files, after, will_edit);
481 vim_free(exp_files);
482 }
483 else // what == AL_SET
484 alist_set(ALIST(curwin), exp_count, exp_files, will_edit, NULL, 0);
485 }
486
487 alist_check_arg_idx();
488
489 return OK;
490}
491
492/*
493 * Redefine the argument list.
494 */
495 void
496set_arglist(char_u *str)
497{
498 do_arglist(str, AL_SET, 0, FALSE);
499}
500
501/*
502 * Return TRUE if window "win" is editing the file at the current argument
503 * index.
504 */
505 int
506editing_arg_idx(win_T *win)
507{
508 return !(win->w_arg_idx >= WARGCOUNT(win)
509 || (win->w_buffer->b_fnum
510 != WARGLIST(win)[win->w_arg_idx].ae_fnum
511 && (win->w_buffer->b_ffname == NULL
512 || !(fullpathcmp(
513 alist_name(&WARGLIST(win)[win->w_arg_idx]),
514 win->w_buffer->b_ffname, TRUE, TRUE) & FPC_SAME))));
515}
516
517/*
518 * Check if window "win" is editing the w_arg_idx file in its argument list.
519 */
520 void
521check_arg_idx(win_T *win)
522{
523 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
524 {
525 // We are not editing the current entry in the argument list.
526 // Set "arg_had_last" if we are editing the last one.
527 win->w_arg_idx_invalid = TRUE;
528 if (win->w_arg_idx != WARGCOUNT(win) - 1
529 && arg_had_last == FALSE
530 && ALIST(win) == &global_alist
531 && GARGCOUNT > 0
532 && win->w_arg_idx < GARGCOUNT
533 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
534 || (win->w_buffer->b_ffname != NULL
535 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
536 win->w_buffer->b_ffname, TRUE, TRUE) & FPC_SAME))))
537 arg_had_last = TRUE;
538 }
539 else
540 {
541 // We are editing the current entry in the argument list.
542 // Set "arg_had_last" if it's also the last one
543 win->w_arg_idx_invalid = FALSE;
544 if (win->w_arg_idx == WARGCOUNT(win) - 1
545 && win->w_alist == &global_alist)
546 arg_had_last = TRUE;
547 }
548}
549
550/*
551 * ":args", ":argslocal" and ":argsglobal".
552 */
553 void
554ex_args(exarg_T *eap)
555{
556 int i;
557
558 if (eap->cmdidx != CMD_args)
559 {
Bram Moolenaar6bcb8772021-02-03 21:23:29 +0100560 if (check_arglist_locked() == FAIL)
561 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200562 alist_unlink(ALIST(curwin));
563 if (eap->cmdidx == CMD_argglobal)
564 ALIST(curwin) = &global_alist;
565 else // eap->cmdidx == CMD_arglocal
566 alist_new();
567 }
568
569 if (*eap->arg != NUL)
570 {
Bram Moolenaar6bcb8772021-02-03 21:23:29 +0100571 if (check_arglist_locked() == FAIL)
572 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200573 // ":args file ..": define new argument list, handle like ":next"
574 // Also for ":argslocal file .." and ":argsglobal file ..".
575 ex_next(eap);
576 }
577 else if (eap->cmdidx == CMD_args)
578 {
579 // ":args": list arguments.
580 if (ARGCOUNT > 0)
581 {
582 char_u **items = ALLOC_MULT(char_u *, ARGCOUNT);
583
584 if (items != NULL)
585 {
586 // Overwrite the command, for a short list there is no
587 // scrolling required and no wait_return().
588 gotocmdline(TRUE);
589
590 for (i = 0; i < ARGCOUNT; ++i)
591 items[i] = alist_name(&ARGLIST[i]);
592 list_in_columns(items, ARGCOUNT, curwin->w_arg_idx);
593 vim_free(items);
594 }
595 }
596 }
597 else if (eap->cmdidx == CMD_arglocal)
598 {
599 garray_T *gap = &curwin->w_alist->al_ga;
600
601 // ":argslocal": make a local copy of the global argument list.
Bram Moolenaar35578162021-08-02 19:10:38 +0200602 if (GA_GROW_OK(gap, GARGCOUNT))
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200603 for (i = 0; i < GARGCOUNT; ++i)
604 if (GARGLIST[i].ae_fname != NULL)
605 {
606 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
607 vim_strsave(GARGLIST[i].ae_fname);
608 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
609 GARGLIST[i].ae_fnum;
610 ++gap->ga_len;
611 }
612 }
613}
614
615/*
616 * ":previous", ":sprevious", ":Next" and ":sNext".
617 */
618 void
619ex_previous(exarg_T *eap)
620{
621 // If past the last one already, go to the last one.
622 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
623 do_argfile(eap, ARGCOUNT - 1);
624 else
625 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
626}
627
628/*
629 * ":rewind", ":first", ":sfirst" and ":srewind".
630 */
631 void
632ex_rewind(exarg_T *eap)
633{
634 do_argfile(eap, 0);
635}
636
637/*
638 * ":last" and ":slast".
639 */
640 void
641ex_last(exarg_T *eap)
642{
643 do_argfile(eap, ARGCOUNT - 1);
644}
645
646/*
647 * ":argument" and ":sargument".
648 */
649 void
650ex_argument(exarg_T *eap)
651{
652 int i;
653
654 if (eap->addr_count > 0)
655 i = eap->line2 - 1;
656 else
657 i = curwin->w_arg_idx;
658 do_argfile(eap, i);
659}
660
661/*
662 * Edit file "argn" of the argument lists.
663 */
664 void
665do_argfile(exarg_T *eap, int argn)
666{
667 int other;
668 char_u *p;
669 int old_arg_idx = curwin->w_arg_idx;
670
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +0100671 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200672 return;
673 if (argn < 0 || argn >= ARGCOUNT)
674 {
675 if (ARGCOUNT <= 1)
Bram Moolenaar1a992222021-12-31 17:25:48 +0000676 emsg(_(e_there_is_only_one_file_to_edit));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200677 else if (argn < 0)
Bram Moolenaar1a992222021-12-31 17:25:48 +0000678 emsg(_(e_cannot_go_before_first_file));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200679 else
Bram Moolenaar1a992222021-12-31 17:25:48 +0000680 emsg(_(e_cannot_go_beyond_last_file));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200681 }
682 else
683 {
684 setpcmark();
685#ifdef FEAT_GUI
686 need_mouse_correct = TRUE;
687#endif
688
689 // split window or create new tab page first
Bram Moolenaare1004402020-10-24 20:49:43 +0200690 if (*eap->cmd == 's' || cmdmod.cmod_tab != 0)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200691 {
692 if (win_split(0, 0) == FAIL)
693 return;
694 RESET_BINDING(curwin);
695 }
696 else
697 {
698 // if 'hidden' set, only check for changed file when re-editing
699 // the same buffer
700 other = TRUE;
701 if (buf_hide(curbuf))
702 {
703 p = fix_fname(alist_name(&ARGLIST[argn]));
704 other = otherfile(p);
705 vim_free(p);
706 }
707 if ((!buf_hide(curbuf) || !other)
708 && check_changed(curbuf, CCGD_AW
709 | (other ? 0 : CCGD_MULTWIN)
710 | (eap->forceit ? CCGD_FORCEIT : 0)
711 | CCGD_EXCMD))
712 return;
713 }
714
715 curwin->w_arg_idx = argn;
716 if (argn == ARGCOUNT - 1 && curwin->w_alist == &global_alist)
717 arg_had_last = TRUE;
718
719 // Edit the file; always use the last known line number.
720 // When it fails (e.g. Abort for already edited file) restore the
721 // argument index.
722 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
723 eap, ECMD_LAST,
724 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
725 + (eap->forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
726 curwin->w_arg_idx = old_arg_idx;
727 // like Vi: set the mark where the cursor is in the file.
728 else if (eap->cmdidx != CMD_argdo)
729 setmark('\'');
730 }
731}
732
733/*
734 * ":next", and commands that behave like it.
735 */
736 void
737ex_next(exarg_T *eap)
738{
739 int i;
740
741 // check for changed buffer now, if this fails the argument list is not
742 // redefined.
743 if ( buf_hide(curbuf)
744 || eap->cmdidx == CMD_snext
745 || !check_changed(curbuf, CCGD_AW
746 | (eap->forceit ? CCGD_FORCEIT : 0)
747 | CCGD_EXCMD))
748 {
749 if (*eap->arg != NUL) // redefine file list
750 {
751 if (do_arglist(eap->arg, AL_SET, 0, TRUE) == FAIL)
752 return;
753 i = 0;
754 }
755 else
756 i = curwin->w_arg_idx + (int)eap->line2;
757 do_argfile(eap, i);
758 }
759}
760
761/*
Nir Lichtman73a02422021-12-24 20:28:03 +0000762 * ":argdedupe"
763 */
764 void
765ex_argdedupe(exarg_T *eap UNUSED)
766{
767 int i;
768 int j;
769
770 for (i = 0; i < ARGCOUNT; ++i)
771 for (j = i + 1; j < ARGCOUNT; ++j)
772 if (fnamecmp(ARGLIST[i].ae_fname, ARGLIST[j].ae_fname) == 0)
773 {
774 vim_free(ARGLIST[j].ae_fname);
775 mch_memmove(ARGLIST + j, ARGLIST + j + 1,
776 (ARGCOUNT - j - 1) * sizeof(aentry_T));
777 --ARGCOUNT;
778
779 if (curwin->w_arg_idx == j)
780 curwin->w_arg_idx = i;
781 else if (curwin->w_arg_idx > j)
782 --curwin->w_arg_idx;
783
784 --j;
785 }
786}
787
788/*
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200789 * ":argedit"
790 */
791 void
792ex_argedit(exarg_T *eap)
793{
794 int i = eap->addr_count ? (int)eap->line2 : curwin->w_arg_idx + 1;
795 // Whether curbuf will be reused, curbuf->b_ffname will be set.
796 int curbuf_is_reusable = curbuf_reusable();
797
798 if (do_arglist(eap->arg, AL_ADD, i, TRUE) == FAIL)
799 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200800 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200801
802 if (curwin->w_arg_idx == 0
803 && (curbuf->b_ml.ml_flags & ML_EMPTY)
804 && (curbuf->b_ffname == NULL || curbuf_is_reusable))
805 i = 0;
806 // Edit the argument.
807 if (i < ARGCOUNT)
808 do_argfile(eap, i);
809}
810
811/*
812 * ":argadd"
813 */
814 void
815ex_argadd(exarg_T *eap)
816{
817 do_arglist(eap->arg, AL_ADD,
818 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1,
819 FALSE);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200820 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200821}
822
823/*
824 * ":argdelete"
825 */
826 void
827ex_argdelete(exarg_T *eap)
828{
829 int i;
830 int n;
831
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100832 if (check_arglist_locked() == FAIL)
833 return;
834
Bram Moolenaar7b221172020-08-17 19:34:10 +0200835 if (eap->addr_count > 0 || *eap->arg == NUL)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200836 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +0200837 // ":argdel" works like ":.argdel"
Bram Moolenaar7b221172020-08-17 19:34:10 +0200838 if (eap->addr_count == 0)
839 {
840 if (curwin->w_arg_idx >= ARGCOUNT)
841 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000842 emsg(_(e_no_argument_to_delete));
Bram Moolenaar7b221172020-08-17 19:34:10 +0200843 return;
844 }
845 eap->line1 = eap->line2 = curwin->w_arg_idx + 1;
846 }
847 else if (eap->line2 > ARGCOUNT)
848 // ":1,4argdel": Delete all arguments in the range.
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200849 eap->line2 = ARGCOUNT;
850 n = eap->line2 - eap->line1 + 1;
851 if (*eap->arg != NUL)
852 // Can't have both a range and an argument.
853 emsg(_(e_invarg));
854 else if (n <= 0)
855 {
856 // Don't give an error for ":%argdel" if the list is empty.
857 if (eap->line1 != 1 || eap->line2 != 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200858 emsg(_(e_invalid_range));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200859 }
860 else
861 {
862 for (i = eap->line1; i <= eap->line2; ++i)
863 vim_free(ARGLIST[i - 1].ae_fname);
864 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
865 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
866 ALIST(curwin)->al_ga.ga_len -= n;
867 if (curwin->w_arg_idx >= eap->line2)
868 curwin->w_arg_idx -= n;
869 else if (curwin->w_arg_idx > eap->line1)
870 curwin->w_arg_idx = eap->line1;
871 if (ARGCOUNT == 0)
872 curwin->w_arg_idx = 0;
873 else if (curwin->w_arg_idx >= ARGCOUNT)
874 curwin->w_arg_idx = ARGCOUNT - 1;
875 }
876 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200877 else
878 do_arglist(eap->arg, AL_DEL, 0, FALSE);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200879 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200880}
881
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200882/*
883 * Function given to ExpandGeneric() to obtain the possible arguments of the
884 * argedit and argdelete commands.
885 */
886 char_u *
887get_arglist_name(expand_T *xp UNUSED, int idx)
888{
889 if (idx >= ARGCOUNT)
890 return NULL;
891
892 return alist_name(&ARGLIST[idx]);
893}
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200894
895/*
896 * Get the file name for an argument list entry.
897 */
898 char_u *
899alist_name(aentry_T *aep)
900{
901 buf_T *bp;
902
903 // Use the name from the associated buffer if it exists.
904 bp = buflist_findnr(aep->ae_fnum);
905 if (bp == NULL || bp->b_fname == NULL)
906 return aep->ae_fname;
907 return bp->b_fname;
908}
909
910/*
911 * do_arg_all(): Open up to 'count' windows, one for each argument.
912 */
913 static void
914do_arg_all(
915 int count,
916 int forceit, // hide buffers in current windows
917 int keep_tabs) // keep current tabs, for ":tab drop file"
918{
919 int i;
920 win_T *wp, *wpnext;
921 char_u *opened; // Array of weight for which args are open:
922 // 0: not opened
923 // 1: opened in other tab
924 // 2: opened in curtab
925 // 3: opened in curtab and curwin
926 //
927 int opened_len; // length of opened[]
928 int use_firstwin = FALSE; // use first window for arglist
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100929 int tab_drop_empty_window = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200930 int split_ret = OK;
931 int p_ea_save;
932 alist_T *alist; // argument list to be used
933 buf_T *buf;
934 tabpage_T *tpnext;
Bram Moolenaare1004402020-10-24 20:49:43 +0200935 int had_tab = cmdmod.cmod_tab;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200936 win_T *old_curwin, *last_curwin;
937 tabpage_T *old_curtab, *last_curtab;
938 win_T *new_curwin = NULL;
939 tabpage_T *new_curtab = NULL;
Bram Moolenaar6f983712021-12-24 18:11:27 +0000940 int prev_arglist_locked = arglist_locked;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200941
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100942#ifdef FEAT_CMDWIN
943 if (cmdwin_type != 0)
944 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200945 emsg(_(e_invalid_in_cmdline_window));
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100946 return;
947 }
948#endif
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200949 if (ARGCOUNT <= 0)
950 {
951 // Don't give an error message. We don't want it when the ":all"
952 // command is in the .vimrc.
953 return;
954 }
955 setpcmark();
956
957 opened_len = ARGCOUNT;
958 opened = alloc_clear(opened_len);
959 if (opened == NULL)
960 return;
961
962 // Autocommands may do anything to the argument list. Make sure it's not
963 // freed while we are working here by "locking" it. We still have to
964 // watch out for its size to be changed.
965 alist = curwin->w_alist;
966 ++alist->al_refcount;
Bram Moolenaar6f983712021-12-24 18:11:27 +0000967 arglist_locked = TRUE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200968
969 old_curwin = curwin;
970 old_curtab = curtab;
971
972# ifdef FEAT_GUI
973 need_mouse_correct = TRUE;
974# endif
975
976 // Try closing all windows that are not in the argument list.
977 // Also close windows that are not full width;
978 // When 'hidden' or "forceit" set the buffer becomes hidden.
979 // Windows that have a changed buffer and can't be hidden won't be closed.
980 // When the ":tab" modifier was used do this for all tab pages.
981 if (had_tab > 0)
982 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
983 for (;;)
984 {
985 tpnext = curtab->tp_next;
986 for (wp = firstwin; wp != NULL; wp = wpnext)
987 {
988 wpnext = wp->w_next;
989 buf = wp->w_buffer;
990 if (buf->b_ffname == NULL
991 || (!keep_tabs && (buf->b_nwindows > 1
992 || wp->w_width != Columns)))
993 i = opened_len;
994 else
995 {
996 // check if the buffer in this window is in the arglist
997 for (i = 0; i < opened_len; ++i)
998 {
999 if (i < alist->al_ga.ga_len
1000 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
1001 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
1002 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
1003 {
1004 int weight = 1;
1005
1006 if (old_curtab == curtab)
1007 {
1008 ++weight;
1009 if (old_curwin == wp)
1010 ++weight;
1011 }
1012
1013 if (weight > (int)opened[i])
1014 {
1015 opened[i] = (char_u)weight;
1016 if (i == 0)
1017 {
1018 if (new_curwin != NULL)
1019 new_curwin->w_arg_idx = opened_len;
1020 new_curwin = wp;
1021 new_curtab = curtab;
1022 }
1023 }
1024 else if (keep_tabs)
1025 i = opened_len;
1026
1027 if (wp->w_alist != alist)
1028 {
1029 // Use the current argument list for all windows
1030 // containing a file from it.
1031 alist_unlink(wp->w_alist);
1032 wp->w_alist = alist;
1033 ++wp->w_alist->al_refcount;
1034 }
1035 break;
1036 }
1037 }
1038 }
1039 wp->w_arg_idx = i;
1040
1041 if (i == opened_len && !keep_tabs)// close this window
1042 {
1043 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
1044 || !bufIsChanged(buf))
1045 {
1046 // If the buffer was changed, and we would like to hide it,
1047 // try autowriting.
1048 if (!buf_hide(buf) && buf->b_nwindows <= 1
1049 && bufIsChanged(buf))
1050 {
1051 bufref_T bufref;
1052
1053 set_bufref(&bufref, buf);
1054
1055 (void)autowrite(buf, FALSE);
1056
1057 // check if autocommands removed the window
1058 if (!win_valid(wp) || !bufref_valid(&bufref))
1059 {
1060 wpnext = firstwin; // start all over...
1061 continue;
1062 }
1063 }
1064 // don't close last window
1065 if (ONE_WINDOW
1066 && (first_tabpage->tp_next == NULL || !had_tab))
1067 use_firstwin = TRUE;
1068 else
1069 {
1070 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
1071
1072 // check if autocommands removed the next window
1073 if (!win_valid(wpnext))
1074 wpnext = firstwin; // start all over...
1075 }
1076 }
1077 }
1078 }
1079
1080 // Without the ":tab" modifier only do the current tab page.
1081 if (had_tab == 0 || tpnext == NULL)
1082 break;
1083
1084 // check if autocommands removed the next tab page
1085 if (!valid_tabpage(tpnext))
1086 tpnext = first_tabpage; // start all over...
1087
1088 goto_tabpage_tp(tpnext, TRUE, TRUE);
1089 }
1090
1091 // Open a window for files in the argument list that don't have one.
1092 // ARGCOUNT may change while doing this, because of autocommands.
1093 if (count > opened_len || count <= 0)
1094 count = opened_len;
1095
1096 // Don't execute Win/Buf Enter/Leave autocommands here.
1097 ++autocmd_no_enter;
1098 ++autocmd_no_leave;
1099 last_curwin = curwin;
1100 last_curtab = curtab;
1101 win_enter(lastwin, FALSE);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001102 // ":tab drop file" should re-use an empty window to avoid "--remote-tab"
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001103 // leaving an empty tab page when executed locally.
1104 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
1105 && curbuf->b_ffname == NULL && !curbuf->b_changed)
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001106 {
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001107 use_firstwin = TRUE;
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001108 tab_drop_empty_window = TRUE;
1109 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001110
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001111 for (i = 0; i < count && !got_int; ++i)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001112 {
1113 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
1114 arg_had_last = TRUE;
1115 if (opened[i] > 0)
1116 {
1117 // Move the already present window to below the current window
1118 if (curwin->w_arg_idx != i)
1119 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001120 FOR_ALL_WINDOWS(wpnext)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001121 {
1122 if (wpnext->w_arg_idx == i)
1123 {
1124 if (keep_tabs)
1125 {
1126 new_curwin = wpnext;
1127 new_curtab = curtab;
1128 }
1129 else if (wpnext->w_frame->fr_parent
1130 != curwin->w_frame->fr_parent)
1131 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001132 emsg(_(e_window_layout_changed_unexpectedly));
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001133 i = count;
1134 break;
1135 }
1136 else
1137 win_move_after(wpnext, curwin);
1138 break;
1139 }
1140 }
1141 }
1142 }
1143 else if (split_ret == OK)
1144 {
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001145 // trigger events for tab drop
1146 if (tab_drop_empty_window && i == count - 1)
1147 --autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001148 if (!use_firstwin) // split current window
1149 {
1150 p_ea_save = p_ea;
1151 p_ea = TRUE; // use space from all windows
1152 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
1153 p_ea = p_ea_save;
1154 if (split_ret == FAIL)
1155 continue;
1156 }
1157 else // first window: do autocmd for leaving this buffer
1158 --autocmd_no_leave;
1159
1160 // edit file "i"
1161 curwin->w_arg_idx = i;
1162 if (i == 0)
1163 {
1164 new_curwin = curwin;
1165 new_curtab = curtab;
1166 }
1167 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
1168 ECMD_ONE,
1169 ((buf_hide(curwin->w_buffer)
1170 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
1171 + ECMD_OLDBUF, curwin);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001172 if (tab_drop_empty_window && i == count - 1)
1173 ++autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001174 if (use_firstwin)
1175 ++autocmd_no_leave;
1176 use_firstwin = FALSE;
1177 }
1178 ui_breakcheck();
1179
1180 // When ":tab" was used open a new tab for a new window repeatedly.
1181 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02001182 cmdmod.cmod_tab = 9999;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001183 }
1184
1185 // Remove the "lock" on the argument list.
1186 alist_unlink(alist);
Bram Moolenaar6f983712021-12-24 18:11:27 +00001187 arglist_locked = prev_arglist_locked;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001188
1189 --autocmd_no_enter;
1190
1191 // restore last referenced tabpage's curwin
1192 if (last_curtab != new_curtab)
1193 {
1194 if (valid_tabpage(last_curtab))
1195 goto_tabpage_tp(last_curtab, TRUE, TRUE);
1196 if (win_valid(last_curwin))
1197 win_enter(last_curwin, FALSE);
1198 }
1199 // to window with first arg
1200 if (valid_tabpage(new_curtab))
1201 goto_tabpage_tp(new_curtab, TRUE, TRUE);
1202 if (win_valid(new_curwin))
1203 win_enter(new_curwin, FALSE);
1204
1205 --autocmd_no_leave;
1206 vim_free(opened);
1207}
1208
1209/*
1210 * ":all" and ":sall".
1211 * Also used for ":tab drop file ..." after setting the argument list.
1212 */
1213 void
1214ex_all(exarg_T *eap)
1215{
1216 if (eap->addr_count == 0)
1217 eap->line2 = 9999;
1218 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
1219}
1220
1221/*
1222 * Concatenate all files in the argument list, separated by spaces, and return
1223 * it in one allocated string.
1224 * Spaces and backslashes in the file names are escaped with a backslash.
1225 * Returns NULL when out of memory.
1226 */
1227 char_u *
1228arg_all(void)
1229{
1230 int len;
1231 int idx;
1232 char_u *retval = NULL;
1233 char_u *p;
1234
1235 // Do this loop two times:
1236 // first time: compute the total length
1237 // second time: concatenate the names
1238 for (;;)
1239 {
1240 len = 0;
1241 for (idx = 0; idx < ARGCOUNT; ++idx)
1242 {
1243 p = alist_name(&ARGLIST[idx]);
1244 if (p != NULL)
1245 {
1246 if (len > 0)
1247 {
1248 // insert a space in between names
1249 if (retval != NULL)
1250 retval[len] = ' ';
1251 ++len;
1252 }
1253 for ( ; *p != NUL; ++p)
1254 {
1255 if (*p == ' '
1256#ifndef BACKSLASH_IN_FILENAME
1257 || *p == '\\'
1258#endif
1259 || *p == '`')
1260 {
1261 // insert a backslash
1262 if (retval != NULL)
1263 retval[len] = '\\';
1264 ++len;
1265 }
1266 if (retval != NULL)
1267 retval[len] = *p;
1268 ++len;
1269 }
1270 }
1271 }
1272
1273 // second time: break here
1274 if (retval != NULL)
1275 {
1276 retval[len] = NUL;
1277 break;
1278 }
1279
1280 // allocate memory
1281 retval = alloc(len + 1);
1282 if (retval == NULL)
1283 break;
1284 }
1285
1286 return retval;
1287}
1288
1289#if defined(FEAT_EVAL) || defined(PROTO)
1290/*
1291 * "argc([window id])" function
1292 */
1293 void
1294f_argc(typval_T *argvars, typval_T *rettv)
1295{
1296 win_T *wp;
1297
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001298 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1299 return;
1300
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001301 if (argvars[0].v_type == VAR_UNKNOWN)
1302 // use the current window
1303 rettv->vval.v_number = ARGCOUNT;
1304 else if (argvars[0].v_type == VAR_NUMBER
1305 && tv_get_number(&argvars[0]) == -1)
1306 // use the global argument list
1307 rettv->vval.v_number = GARGCOUNT;
1308 else
1309 {
1310 // use the argument list of the specified window
1311 wp = find_win_by_nr_or_id(&argvars[0]);
1312 if (wp != NULL)
1313 rettv->vval.v_number = WARGCOUNT(wp);
1314 else
1315 rettv->vval.v_number = -1;
1316 }
1317}
1318
1319/*
1320 * "argidx()" function
1321 */
1322 void
1323f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1324{
1325 rettv->vval.v_number = curwin->w_arg_idx;
1326}
1327
1328/*
1329 * "arglistid()" function
1330 */
1331 void
1332f_arglistid(typval_T *argvars, typval_T *rettv)
1333{
1334 win_T *wp;
1335
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001336 if (in_vim9script()
1337 && (check_for_opt_number_arg(argvars, 0) == FAIL
1338 || (argvars[0].v_type != VAR_UNKNOWN
1339 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1340 return;
1341
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001342 rettv->vval.v_number = -1;
1343 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
1344 if (wp != NULL)
1345 rettv->vval.v_number = wp->w_alist->id;
1346}
1347
1348/*
1349 * Get the argument list for a given window
1350 */
1351 static void
1352get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1353{
1354 int idx;
1355
1356 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1357 for (idx = 0; idx < argcount; ++idx)
1358 list_append_string(rettv->vval.v_list,
1359 alist_name(&arglist[idx]), -1);
1360}
1361
1362/*
1363 * "argv(nr)" function
1364 */
1365 void
1366f_argv(typval_T *argvars, typval_T *rettv)
1367{
1368 int idx;
1369 aentry_T *arglist = NULL;
1370 int argcount = -1;
1371
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001372 if (in_vim9script()
1373 && (check_for_opt_number_arg(argvars, 0) == FAIL
1374 || (argvars[0].v_type != VAR_UNKNOWN
1375 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1376 return;
1377
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001378 if (argvars[0].v_type != VAR_UNKNOWN)
1379 {
1380 if (argvars[1].v_type == VAR_UNKNOWN)
1381 {
1382 arglist = ARGLIST;
1383 argcount = ARGCOUNT;
1384 }
1385 else if (argvars[1].v_type == VAR_NUMBER
1386 && tv_get_number(&argvars[1]) == -1)
1387 {
1388 arglist = GARGLIST;
1389 argcount = GARGCOUNT;
1390 }
1391 else
1392 {
1393 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1394
1395 if (wp != NULL)
1396 {
1397 // Use the argument list of the specified window
1398 arglist = WARGLIST(wp);
1399 argcount = WARGCOUNT(wp);
1400 }
1401 }
1402
1403 rettv->v_type = VAR_STRING;
1404 rettv->vval.v_string = NULL;
1405 idx = tv_get_number_chk(&argvars[0], NULL);
1406 if (arglist != NULL && idx >= 0 && idx < argcount)
1407 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1408 else if (idx == -1)
1409 get_arglist_as_rettv(arglist, argcount, rettv);
1410 }
1411 else
1412 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
1413}
1414#endif