blob: 24b0a870e215ec0bfc0a6bd706e6df3073820115 [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)
676 emsg(_("E163: There is only one file to edit"));
677 else if (argn < 0)
678 emsg(_("E164: Cannot go before first file"));
679 else
680 emsg(_("E165: Cannot go beyond last file"));
681 }
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/*
762 * ":argedit"
763 */
764 void
765ex_argedit(exarg_T *eap)
766{
767 int i = eap->addr_count ? (int)eap->line2 : curwin->w_arg_idx + 1;
768 // Whether curbuf will be reused, curbuf->b_ffname will be set.
769 int curbuf_is_reusable = curbuf_reusable();
770
771 if (do_arglist(eap->arg, AL_ADD, i, TRUE) == FAIL)
772 return;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200773 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200774
775 if (curwin->w_arg_idx == 0
776 && (curbuf->b_ml.ml_flags & ML_EMPTY)
777 && (curbuf->b_ffname == NULL || curbuf_is_reusable))
778 i = 0;
779 // Edit the argument.
780 if (i < ARGCOUNT)
781 do_argfile(eap, i);
782}
783
784/*
785 * ":argadd"
786 */
787 void
788ex_argadd(exarg_T *eap)
789{
790 do_arglist(eap->arg, AL_ADD,
791 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1,
792 FALSE);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200793 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200794}
795
796/*
797 * ":argdelete"
798 */
799 void
800ex_argdelete(exarg_T *eap)
801{
802 int i;
803 int n;
804
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100805 if (check_arglist_locked() == FAIL)
806 return;
807
Bram Moolenaar7b221172020-08-17 19:34:10 +0200808 if (eap->addr_count > 0 || *eap->arg == NUL)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200809 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +0200810 // ":argdel" works like ":.argdel"
Bram Moolenaar7b221172020-08-17 19:34:10 +0200811 if (eap->addr_count == 0)
812 {
813 if (curwin->w_arg_idx >= ARGCOUNT)
814 {
815 emsg(_("E610: No argument to delete"));
816 return;
817 }
818 eap->line1 = eap->line2 = curwin->w_arg_idx + 1;
819 }
820 else if (eap->line2 > ARGCOUNT)
821 // ":1,4argdel": Delete all arguments in the range.
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200822 eap->line2 = ARGCOUNT;
823 n = eap->line2 - eap->line1 + 1;
824 if (*eap->arg != NUL)
825 // Can't have both a range and an argument.
826 emsg(_(e_invarg));
827 else if (n <= 0)
828 {
829 // Don't give an error for ":%argdel" if the list is empty.
830 if (eap->line1 != 1 || eap->line2 != 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200831 emsg(_(e_invalid_range));
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200832 }
833 else
834 {
835 for (i = eap->line1; i <= eap->line2; ++i)
836 vim_free(ARGLIST[i - 1].ae_fname);
837 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
838 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
839 ALIST(curwin)->al_ga.ga_len -= n;
840 if (curwin->w_arg_idx >= eap->line2)
841 curwin->w_arg_idx -= n;
842 else if (curwin->w_arg_idx > eap->line1)
843 curwin->w_arg_idx = eap->line1;
844 if (ARGCOUNT == 0)
845 curwin->w_arg_idx = 0;
846 else if (curwin->w_arg_idx >= ARGCOUNT)
847 curwin->w_arg_idx = ARGCOUNT - 1;
848 }
849 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200850 else
851 do_arglist(eap->arg, AL_DEL, 0, FALSE);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200852 maketitle();
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200853}
854
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200855/*
856 * Function given to ExpandGeneric() to obtain the possible arguments of the
857 * argedit and argdelete commands.
858 */
859 char_u *
860get_arglist_name(expand_T *xp UNUSED, int idx)
861{
862 if (idx >= ARGCOUNT)
863 return NULL;
864
865 return alist_name(&ARGLIST[idx]);
866}
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200867
868/*
869 * Get the file name for an argument list entry.
870 */
871 char_u *
872alist_name(aentry_T *aep)
873{
874 buf_T *bp;
875
876 // Use the name from the associated buffer if it exists.
877 bp = buflist_findnr(aep->ae_fnum);
878 if (bp == NULL || bp->b_fname == NULL)
879 return aep->ae_fname;
880 return bp->b_fname;
881}
882
883/*
884 * do_arg_all(): Open up to 'count' windows, one for each argument.
885 */
886 static void
887do_arg_all(
888 int count,
889 int forceit, // hide buffers in current windows
890 int keep_tabs) // keep current tabs, for ":tab drop file"
891{
892 int i;
893 win_T *wp, *wpnext;
894 char_u *opened; // Array of weight for which args are open:
895 // 0: not opened
896 // 1: opened in other tab
897 // 2: opened in curtab
898 // 3: opened in curtab and curwin
899 //
900 int opened_len; // length of opened[]
901 int use_firstwin = FALSE; // use first window for arglist
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100902 int tab_drop_empty_window = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200903 int split_ret = OK;
904 int p_ea_save;
905 alist_T *alist; // argument list to be used
906 buf_T *buf;
907 tabpage_T *tpnext;
Bram Moolenaare1004402020-10-24 20:49:43 +0200908 int had_tab = cmdmod.cmod_tab;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200909 win_T *old_curwin, *last_curwin;
910 tabpage_T *old_curtab, *last_curtab;
911 win_T *new_curwin = NULL;
912 tabpage_T *new_curtab = NULL;
913
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100914#ifdef FEAT_CMDWIN
915 if (cmdwin_type != 0)
916 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200917 emsg(_(e_invalid_in_cmdline_window));
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100918 return;
919 }
920#endif
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200921 if (ARGCOUNT <= 0)
922 {
923 // Don't give an error message. We don't want it when the ":all"
924 // command is in the .vimrc.
925 return;
926 }
927 setpcmark();
928
929 opened_len = ARGCOUNT;
930 opened = alloc_clear(opened_len);
931 if (opened == NULL)
932 return;
933
934 // Autocommands may do anything to the argument list. Make sure it's not
935 // freed while we are working here by "locking" it. We still have to
936 // watch out for its size to be changed.
937 alist = curwin->w_alist;
938 ++alist->al_refcount;
939
940 old_curwin = curwin;
941 old_curtab = curtab;
942
943# ifdef FEAT_GUI
944 need_mouse_correct = TRUE;
945# endif
946
947 // Try closing all windows that are not in the argument list.
948 // Also close windows that are not full width;
949 // When 'hidden' or "forceit" set the buffer becomes hidden.
950 // Windows that have a changed buffer and can't be hidden won't be closed.
951 // When the ":tab" modifier was used do this for all tab pages.
952 if (had_tab > 0)
953 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
954 for (;;)
955 {
956 tpnext = curtab->tp_next;
957 for (wp = firstwin; wp != NULL; wp = wpnext)
958 {
959 wpnext = wp->w_next;
960 buf = wp->w_buffer;
961 if (buf->b_ffname == NULL
962 || (!keep_tabs && (buf->b_nwindows > 1
963 || wp->w_width != Columns)))
964 i = opened_len;
965 else
966 {
967 // check if the buffer in this window is in the arglist
968 for (i = 0; i < opened_len; ++i)
969 {
970 if (i < alist->al_ga.ga_len
971 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
972 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
973 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
974 {
975 int weight = 1;
976
977 if (old_curtab == curtab)
978 {
979 ++weight;
980 if (old_curwin == wp)
981 ++weight;
982 }
983
984 if (weight > (int)opened[i])
985 {
986 opened[i] = (char_u)weight;
987 if (i == 0)
988 {
989 if (new_curwin != NULL)
990 new_curwin->w_arg_idx = opened_len;
991 new_curwin = wp;
992 new_curtab = curtab;
993 }
994 }
995 else if (keep_tabs)
996 i = opened_len;
997
998 if (wp->w_alist != alist)
999 {
1000 // Use the current argument list for all windows
1001 // containing a file from it.
1002 alist_unlink(wp->w_alist);
1003 wp->w_alist = alist;
1004 ++wp->w_alist->al_refcount;
1005 }
1006 break;
1007 }
1008 }
1009 }
1010 wp->w_arg_idx = i;
1011
1012 if (i == opened_len && !keep_tabs)// close this window
1013 {
1014 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
1015 || !bufIsChanged(buf))
1016 {
1017 // If the buffer was changed, and we would like to hide it,
1018 // try autowriting.
1019 if (!buf_hide(buf) && buf->b_nwindows <= 1
1020 && bufIsChanged(buf))
1021 {
1022 bufref_T bufref;
1023
1024 set_bufref(&bufref, buf);
1025
1026 (void)autowrite(buf, FALSE);
1027
1028 // check if autocommands removed the window
1029 if (!win_valid(wp) || !bufref_valid(&bufref))
1030 {
1031 wpnext = firstwin; // start all over...
1032 continue;
1033 }
1034 }
1035 // don't close last window
1036 if (ONE_WINDOW
1037 && (first_tabpage->tp_next == NULL || !had_tab))
1038 use_firstwin = TRUE;
1039 else
1040 {
1041 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
1042
1043 // check if autocommands removed the next window
1044 if (!win_valid(wpnext))
1045 wpnext = firstwin; // start all over...
1046 }
1047 }
1048 }
1049 }
1050
1051 // Without the ":tab" modifier only do the current tab page.
1052 if (had_tab == 0 || tpnext == NULL)
1053 break;
1054
1055 // check if autocommands removed the next tab page
1056 if (!valid_tabpage(tpnext))
1057 tpnext = first_tabpage; // start all over...
1058
1059 goto_tabpage_tp(tpnext, TRUE, TRUE);
1060 }
1061
1062 // Open a window for files in the argument list that don't have one.
1063 // ARGCOUNT may change while doing this, because of autocommands.
1064 if (count > opened_len || count <= 0)
1065 count = opened_len;
1066
1067 // Don't execute Win/Buf Enter/Leave autocommands here.
1068 ++autocmd_no_enter;
1069 ++autocmd_no_leave;
1070 last_curwin = curwin;
1071 last_curtab = curtab;
1072 win_enter(lastwin, FALSE);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001073 // ":tab drop file" should re-use an empty window to avoid "--remote-tab"
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001074 // leaving an empty tab page when executed locally.
1075 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
1076 && curbuf->b_ffname == NULL && !curbuf->b_changed)
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001077 {
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001078 use_firstwin = TRUE;
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001079 tab_drop_empty_window = TRUE;
1080 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001081
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001082 for (i = 0; i < count && !got_int; ++i)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001083 {
1084 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
1085 arg_had_last = TRUE;
1086 if (opened[i] > 0)
1087 {
1088 // Move the already present window to below the current window
1089 if (curwin->w_arg_idx != i)
1090 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001091 FOR_ALL_WINDOWS(wpnext)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001092 {
1093 if (wpnext->w_arg_idx == i)
1094 {
1095 if (keep_tabs)
1096 {
1097 new_curwin = wpnext;
1098 new_curtab = curtab;
1099 }
1100 else if (wpnext->w_frame->fr_parent
1101 != curwin->w_frame->fr_parent)
1102 {
1103 emsg(_("E249: window layout changed unexpectedly"));
1104 i = count;
1105 break;
1106 }
1107 else
1108 win_move_after(wpnext, curwin);
1109 break;
1110 }
1111 }
1112 }
1113 }
1114 else if (split_ret == OK)
1115 {
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001116 // trigger events for tab drop
1117 if (tab_drop_empty_window && i == count - 1)
1118 --autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001119 if (!use_firstwin) // split current window
1120 {
1121 p_ea_save = p_ea;
1122 p_ea = TRUE; // use space from all windows
1123 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
1124 p_ea = p_ea_save;
1125 if (split_ret == FAIL)
1126 continue;
1127 }
1128 else // first window: do autocmd for leaving this buffer
1129 --autocmd_no_leave;
1130
1131 // edit file "i"
1132 curwin->w_arg_idx = i;
1133 if (i == 0)
1134 {
1135 new_curwin = curwin;
1136 new_curtab = curtab;
1137 }
1138 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
1139 ECMD_ONE,
1140 ((buf_hide(curwin->w_buffer)
1141 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
1142 + ECMD_OLDBUF, curwin);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001143 if (tab_drop_empty_window && i == count - 1)
1144 ++autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001145 if (use_firstwin)
1146 ++autocmd_no_leave;
1147 use_firstwin = FALSE;
1148 }
1149 ui_breakcheck();
1150
1151 // When ":tab" was used open a new tab for a new window repeatedly.
1152 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02001153 cmdmod.cmod_tab = 9999;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001154 }
1155
1156 // Remove the "lock" on the argument list.
1157 alist_unlink(alist);
1158
1159 --autocmd_no_enter;
1160
1161 // restore last referenced tabpage's curwin
1162 if (last_curtab != new_curtab)
1163 {
1164 if (valid_tabpage(last_curtab))
1165 goto_tabpage_tp(last_curtab, TRUE, TRUE);
1166 if (win_valid(last_curwin))
1167 win_enter(last_curwin, FALSE);
1168 }
1169 // to window with first arg
1170 if (valid_tabpage(new_curtab))
1171 goto_tabpage_tp(new_curtab, TRUE, TRUE);
1172 if (win_valid(new_curwin))
1173 win_enter(new_curwin, FALSE);
1174
1175 --autocmd_no_leave;
1176 vim_free(opened);
1177}
1178
1179/*
1180 * ":all" and ":sall".
1181 * Also used for ":tab drop file ..." after setting the argument list.
1182 */
1183 void
1184ex_all(exarg_T *eap)
1185{
1186 if (eap->addr_count == 0)
1187 eap->line2 = 9999;
1188 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
1189}
1190
1191/*
1192 * Concatenate all files in the argument list, separated by spaces, and return
1193 * it in one allocated string.
1194 * Spaces and backslashes in the file names are escaped with a backslash.
1195 * Returns NULL when out of memory.
1196 */
1197 char_u *
1198arg_all(void)
1199{
1200 int len;
1201 int idx;
1202 char_u *retval = NULL;
1203 char_u *p;
1204
1205 // Do this loop two times:
1206 // first time: compute the total length
1207 // second time: concatenate the names
1208 for (;;)
1209 {
1210 len = 0;
1211 for (idx = 0; idx < ARGCOUNT; ++idx)
1212 {
1213 p = alist_name(&ARGLIST[idx]);
1214 if (p != NULL)
1215 {
1216 if (len > 0)
1217 {
1218 // insert a space in between names
1219 if (retval != NULL)
1220 retval[len] = ' ';
1221 ++len;
1222 }
1223 for ( ; *p != NUL; ++p)
1224 {
1225 if (*p == ' '
1226#ifndef BACKSLASH_IN_FILENAME
1227 || *p == '\\'
1228#endif
1229 || *p == '`')
1230 {
1231 // insert a backslash
1232 if (retval != NULL)
1233 retval[len] = '\\';
1234 ++len;
1235 }
1236 if (retval != NULL)
1237 retval[len] = *p;
1238 ++len;
1239 }
1240 }
1241 }
1242
1243 // second time: break here
1244 if (retval != NULL)
1245 {
1246 retval[len] = NUL;
1247 break;
1248 }
1249
1250 // allocate memory
1251 retval = alloc(len + 1);
1252 if (retval == NULL)
1253 break;
1254 }
1255
1256 return retval;
1257}
1258
1259#if defined(FEAT_EVAL) || defined(PROTO)
1260/*
1261 * "argc([window id])" function
1262 */
1263 void
1264f_argc(typval_T *argvars, typval_T *rettv)
1265{
1266 win_T *wp;
1267
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001268 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1269 return;
1270
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001271 if (argvars[0].v_type == VAR_UNKNOWN)
1272 // use the current window
1273 rettv->vval.v_number = ARGCOUNT;
1274 else if (argvars[0].v_type == VAR_NUMBER
1275 && tv_get_number(&argvars[0]) == -1)
1276 // use the global argument list
1277 rettv->vval.v_number = GARGCOUNT;
1278 else
1279 {
1280 // use the argument list of the specified window
1281 wp = find_win_by_nr_or_id(&argvars[0]);
1282 if (wp != NULL)
1283 rettv->vval.v_number = WARGCOUNT(wp);
1284 else
1285 rettv->vval.v_number = -1;
1286 }
1287}
1288
1289/*
1290 * "argidx()" function
1291 */
1292 void
1293f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1294{
1295 rettv->vval.v_number = curwin->w_arg_idx;
1296}
1297
1298/*
1299 * "arglistid()" function
1300 */
1301 void
1302f_arglistid(typval_T *argvars, typval_T *rettv)
1303{
1304 win_T *wp;
1305
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001306 if (in_vim9script()
1307 && (check_for_opt_number_arg(argvars, 0) == FAIL
1308 || (argvars[0].v_type != VAR_UNKNOWN
1309 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1310 return;
1311
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001312 rettv->vval.v_number = -1;
1313 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
1314 if (wp != NULL)
1315 rettv->vval.v_number = wp->w_alist->id;
1316}
1317
1318/*
1319 * Get the argument list for a given window
1320 */
1321 static void
1322get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1323{
1324 int idx;
1325
1326 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1327 for (idx = 0; idx < argcount; ++idx)
1328 list_append_string(rettv->vval.v_list,
1329 alist_name(&arglist[idx]), -1);
1330}
1331
1332/*
1333 * "argv(nr)" function
1334 */
1335 void
1336f_argv(typval_T *argvars, typval_T *rettv)
1337{
1338 int idx;
1339 aentry_T *arglist = NULL;
1340 int argcount = -1;
1341
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001342 if (in_vim9script()
1343 && (check_for_opt_number_arg(argvars, 0) == FAIL
1344 || (argvars[0].v_type != VAR_UNKNOWN
1345 && check_for_opt_number_arg(argvars, 1) == FAIL)))
1346 return;
1347
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001348 if (argvars[0].v_type != VAR_UNKNOWN)
1349 {
1350 if (argvars[1].v_type == VAR_UNKNOWN)
1351 {
1352 arglist = ARGLIST;
1353 argcount = ARGCOUNT;
1354 }
1355 else if (argvars[1].v_type == VAR_NUMBER
1356 && tv_get_number(&argvars[1]) == -1)
1357 {
1358 arglist = GARGLIST;
1359 argcount = GARGCOUNT;
1360 }
1361 else
1362 {
1363 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1364
1365 if (wp != NULL)
1366 {
1367 // Use the argument list of the specified window
1368 arglist = WARGLIST(wp);
1369 argcount = WARGCOUNT(wp);
1370 }
1371 }
1372
1373 rettv->v_type = VAR_STRING;
1374 rettv->vval.v_string = NULL;
1375 idx = tv_get_number_chk(&argvars[0], NULL);
1376 if (arglist != NULL && idx >= 0 && idx < argcount)
1377 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1378 else if (idx == -1)
1379 get_arglist_as_rettv(arglist, argcount, rettv);
1380 }
1381 else
1382 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
1383}
1384#endif