blob: 117116cd5d0a63c31ad73ae2c3dd093e30f5a694 [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);
151 if (ga_grow(&al->al_ga, count) == OK)
152 {
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,
318 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
319 else
320 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
321 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
322
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
358 && ga_grow(&ALIST(curwin)->al_ga, count) == OK)
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 {
560 alist_unlink(ALIST(curwin));
561 if (eap->cmdidx == CMD_argglobal)
562 ALIST(curwin) = &global_alist;
563 else // eap->cmdidx == CMD_arglocal
564 alist_new();
565 }
566
567 if (*eap->arg != NUL)
568 {
569 // ":args file ..": define new argument list, handle like ":next"
570 // Also for ":argslocal file .." and ":argsglobal file ..".
571 ex_next(eap);
572 }
573 else if (eap->cmdidx == CMD_args)
574 {
575 // ":args": list arguments.
576 if (ARGCOUNT > 0)
577 {
578 char_u **items = ALLOC_MULT(char_u *, ARGCOUNT);
579
580 if (items != NULL)
581 {
582 // Overwrite the command, for a short list there is no
583 // scrolling required and no wait_return().
584 gotocmdline(TRUE);
585
586 for (i = 0; i < ARGCOUNT; ++i)
587 items[i] = alist_name(&ARGLIST[i]);
588 list_in_columns(items, ARGCOUNT, curwin->w_arg_idx);
589 vim_free(items);
590 }
591 }
592 }
593 else if (eap->cmdidx == CMD_arglocal)
594 {
595 garray_T *gap = &curwin->w_alist->al_ga;
596
597 // ":argslocal": make a local copy of the global argument list.
598 if (ga_grow(gap, GARGCOUNT) == OK)
599 for (i = 0; i < GARGCOUNT; ++i)
600 if (GARGLIST[i].ae_fname != NULL)
601 {
602 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
603 vim_strsave(GARGLIST[i].ae_fname);
604 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
605 GARGLIST[i].ae_fnum;
606 ++gap->ga_len;
607 }
608 }
609}
610
611/*
612 * ":previous", ":sprevious", ":Next" and ":sNext".
613 */
614 void
615ex_previous(exarg_T *eap)
616{
617 // If past the last one already, go to the last one.
618 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
619 do_argfile(eap, ARGCOUNT - 1);
620 else
621 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
622}
623
624/*
625 * ":rewind", ":first", ":sfirst" and ":srewind".
626 */
627 void
628ex_rewind(exarg_T *eap)
629{
630 do_argfile(eap, 0);
631}
632
633/*
634 * ":last" and ":slast".
635 */
636 void
637ex_last(exarg_T *eap)
638{
639 do_argfile(eap, ARGCOUNT - 1);
640}
641
642/*
643 * ":argument" and ":sargument".
644 */
645 void
646ex_argument(exarg_T *eap)
647{
648 int i;
649
650 if (eap->addr_count > 0)
651 i = eap->line2 - 1;
652 else
653 i = curwin->w_arg_idx;
654 do_argfile(eap, i);
655}
656
657/*
658 * Edit file "argn" of the argument lists.
659 */
660 void
661do_argfile(exarg_T *eap, int argn)
662{
663 int other;
664 char_u *p;
665 int old_arg_idx = curwin->w_arg_idx;
666
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +0100667 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200668 return;
669 if (argn < 0 || argn >= ARGCOUNT)
670 {
671 if (ARGCOUNT <= 1)
672 emsg(_("E163: There is only one file to edit"));
673 else if (argn < 0)
674 emsg(_("E164: Cannot go before first file"));
675 else
676 emsg(_("E165: Cannot go beyond last file"));
677 }
678 else
679 {
680 setpcmark();
681#ifdef FEAT_GUI
682 need_mouse_correct = TRUE;
683#endif
684
685 // split window or create new tab page first
Bram Moolenaare1004402020-10-24 20:49:43 +0200686 if (*eap->cmd == 's' || cmdmod.cmod_tab != 0)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200687 {
688 if (win_split(0, 0) == FAIL)
689 return;
690 RESET_BINDING(curwin);
691 }
692 else
693 {
694 // if 'hidden' set, only check for changed file when re-editing
695 // the same buffer
696 other = TRUE;
697 if (buf_hide(curbuf))
698 {
699 p = fix_fname(alist_name(&ARGLIST[argn]));
700 other = otherfile(p);
701 vim_free(p);
702 }
703 if ((!buf_hide(curbuf) || !other)
704 && check_changed(curbuf, CCGD_AW
705 | (other ? 0 : CCGD_MULTWIN)
706 | (eap->forceit ? CCGD_FORCEIT : 0)
707 | CCGD_EXCMD))
708 return;
709 }
710
711 curwin->w_arg_idx = argn;
712 if (argn == ARGCOUNT - 1 && curwin->w_alist == &global_alist)
713 arg_had_last = TRUE;
714
715 // Edit the file; always use the last known line number.
716 // When it fails (e.g. Abort for already edited file) restore the
717 // argument index.
718 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
719 eap, ECMD_LAST,
720 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
721 + (eap->forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
722 curwin->w_arg_idx = old_arg_idx;
723 // like Vi: set the mark where the cursor is in the file.
724 else if (eap->cmdidx != CMD_argdo)
725 setmark('\'');
726 }
727}
728
729/*
730 * ":next", and commands that behave like it.
731 */
732 void
733ex_next(exarg_T *eap)
734{
735 int i;
736
737 // check for changed buffer now, if this fails the argument list is not
738 // redefined.
739 if ( buf_hide(curbuf)
740 || eap->cmdidx == CMD_snext
741 || !check_changed(curbuf, CCGD_AW
742 | (eap->forceit ? CCGD_FORCEIT : 0)
743 | CCGD_EXCMD))
744 {
745 if (*eap->arg != NUL) // redefine file list
746 {
747 if (do_arglist(eap->arg, AL_SET, 0, TRUE) == FAIL)
748 return;
749 i = 0;
750 }
751 else
752 i = curwin->w_arg_idx + (int)eap->line2;
753 do_argfile(eap, i);
754 }
755}
756
757/*
758 * ":argedit"
759 */
760 void
761ex_argedit(exarg_T *eap)
762{
763 int i = eap->addr_count ? (int)eap->line2 : curwin->w_arg_idx + 1;
764 // Whether curbuf will be reused, curbuf->b_ffname will be set.
765 int curbuf_is_reusable = curbuf_reusable();
766
767 if (do_arglist(eap->arg, AL_ADD, i, TRUE) == FAIL)
768 return;
769#ifdef FEAT_TITLE
770 maketitle();
771#endif
772
773 if (curwin->w_arg_idx == 0
774 && (curbuf->b_ml.ml_flags & ML_EMPTY)
775 && (curbuf->b_ffname == NULL || curbuf_is_reusable))
776 i = 0;
777 // Edit the argument.
778 if (i < ARGCOUNT)
779 do_argfile(eap, i);
780}
781
782/*
783 * ":argadd"
784 */
785 void
786ex_argadd(exarg_T *eap)
787{
788 do_arglist(eap->arg, AL_ADD,
789 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1,
790 FALSE);
791#ifdef FEAT_TITLE
792 maketitle();
793#endif
794}
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)
831 emsg(_(e_invrange));
832 }
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);
852#ifdef FEAT_TITLE
853 maketitle();
854#endif
855}
856
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200857/*
858 * Function given to ExpandGeneric() to obtain the possible arguments of the
859 * argedit and argdelete commands.
860 */
861 char_u *
862get_arglist_name(expand_T *xp UNUSED, int idx)
863{
864 if (idx >= ARGCOUNT)
865 return NULL;
866
867 return alist_name(&ARGLIST[idx]);
868}
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200869
870/*
871 * Get the file name for an argument list entry.
872 */
873 char_u *
874alist_name(aentry_T *aep)
875{
876 buf_T *bp;
877
878 // Use the name from the associated buffer if it exists.
879 bp = buflist_findnr(aep->ae_fnum);
880 if (bp == NULL || bp->b_fname == NULL)
881 return aep->ae_fname;
882 return bp->b_fname;
883}
884
885/*
886 * do_arg_all(): Open up to 'count' windows, one for each argument.
887 */
888 static void
889do_arg_all(
890 int count,
891 int forceit, // hide buffers in current windows
892 int keep_tabs) // keep current tabs, for ":tab drop file"
893{
894 int i;
895 win_T *wp, *wpnext;
896 char_u *opened; // Array of weight for which args are open:
897 // 0: not opened
898 // 1: opened in other tab
899 // 2: opened in curtab
900 // 3: opened in curtab and curwin
901 //
902 int opened_len; // length of opened[]
903 int use_firstwin = FALSE; // use first window for arglist
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100904 int tab_drop_empty_window = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200905 int split_ret = OK;
906 int p_ea_save;
907 alist_T *alist; // argument list to be used
908 buf_T *buf;
909 tabpage_T *tpnext;
Bram Moolenaare1004402020-10-24 20:49:43 +0200910 int had_tab = cmdmod.cmod_tab;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200911 win_T *old_curwin, *last_curwin;
912 tabpage_T *old_curtab, *last_curtab;
913 win_T *new_curwin = NULL;
914 tabpage_T *new_curtab = NULL;
915
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100916#ifdef FEAT_CMDWIN
917 if (cmdwin_type != 0)
918 {
919 emsg(_(e_cmdwin));
920 return;
921 }
922#endif
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200923 if (ARGCOUNT <= 0)
924 {
925 // Don't give an error message. We don't want it when the ":all"
926 // command is in the .vimrc.
927 return;
928 }
929 setpcmark();
930
931 opened_len = ARGCOUNT;
932 opened = alloc_clear(opened_len);
933 if (opened == NULL)
934 return;
935
936 // Autocommands may do anything to the argument list. Make sure it's not
937 // freed while we are working here by "locking" it. We still have to
938 // watch out for its size to be changed.
939 alist = curwin->w_alist;
940 ++alist->al_refcount;
941
942 old_curwin = curwin;
943 old_curtab = curtab;
944
945# ifdef FEAT_GUI
946 need_mouse_correct = TRUE;
947# endif
948
949 // Try closing all windows that are not in the argument list.
950 // Also close windows that are not full width;
951 // When 'hidden' or "forceit" set the buffer becomes hidden.
952 // Windows that have a changed buffer and can't be hidden won't be closed.
953 // When the ":tab" modifier was used do this for all tab pages.
954 if (had_tab > 0)
955 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
956 for (;;)
957 {
958 tpnext = curtab->tp_next;
959 for (wp = firstwin; wp != NULL; wp = wpnext)
960 {
961 wpnext = wp->w_next;
962 buf = wp->w_buffer;
963 if (buf->b_ffname == NULL
964 || (!keep_tabs && (buf->b_nwindows > 1
965 || wp->w_width != Columns)))
966 i = opened_len;
967 else
968 {
969 // check if the buffer in this window is in the arglist
970 for (i = 0; i < opened_len; ++i)
971 {
972 if (i < alist->al_ga.ga_len
973 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
974 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
975 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
976 {
977 int weight = 1;
978
979 if (old_curtab == curtab)
980 {
981 ++weight;
982 if (old_curwin == wp)
983 ++weight;
984 }
985
986 if (weight > (int)opened[i])
987 {
988 opened[i] = (char_u)weight;
989 if (i == 0)
990 {
991 if (new_curwin != NULL)
992 new_curwin->w_arg_idx = opened_len;
993 new_curwin = wp;
994 new_curtab = curtab;
995 }
996 }
997 else if (keep_tabs)
998 i = opened_len;
999
1000 if (wp->w_alist != alist)
1001 {
1002 // Use the current argument list for all windows
1003 // containing a file from it.
1004 alist_unlink(wp->w_alist);
1005 wp->w_alist = alist;
1006 ++wp->w_alist->al_refcount;
1007 }
1008 break;
1009 }
1010 }
1011 }
1012 wp->w_arg_idx = i;
1013
1014 if (i == opened_len && !keep_tabs)// close this window
1015 {
1016 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
1017 || !bufIsChanged(buf))
1018 {
1019 // If the buffer was changed, and we would like to hide it,
1020 // try autowriting.
1021 if (!buf_hide(buf) && buf->b_nwindows <= 1
1022 && bufIsChanged(buf))
1023 {
1024 bufref_T bufref;
1025
1026 set_bufref(&bufref, buf);
1027
1028 (void)autowrite(buf, FALSE);
1029
1030 // check if autocommands removed the window
1031 if (!win_valid(wp) || !bufref_valid(&bufref))
1032 {
1033 wpnext = firstwin; // start all over...
1034 continue;
1035 }
1036 }
1037 // don't close last window
1038 if (ONE_WINDOW
1039 && (first_tabpage->tp_next == NULL || !had_tab))
1040 use_firstwin = TRUE;
1041 else
1042 {
1043 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
1044
1045 // check if autocommands removed the next window
1046 if (!win_valid(wpnext))
1047 wpnext = firstwin; // start all over...
1048 }
1049 }
1050 }
1051 }
1052
1053 // Without the ":tab" modifier only do the current tab page.
1054 if (had_tab == 0 || tpnext == NULL)
1055 break;
1056
1057 // check if autocommands removed the next tab page
1058 if (!valid_tabpage(tpnext))
1059 tpnext = first_tabpage; // start all over...
1060
1061 goto_tabpage_tp(tpnext, TRUE, TRUE);
1062 }
1063
1064 // Open a window for files in the argument list that don't have one.
1065 // ARGCOUNT may change while doing this, because of autocommands.
1066 if (count > opened_len || count <= 0)
1067 count = opened_len;
1068
1069 // Don't execute Win/Buf Enter/Leave autocommands here.
1070 ++autocmd_no_enter;
1071 ++autocmd_no_leave;
1072 last_curwin = curwin;
1073 last_curtab = curtab;
1074 win_enter(lastwin, FALSE);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001075 // ":tab drop file" should re-use an empty window to avoid "--remote-tab"
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001076 // leaving an empty tab page when executed locally.
1077 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
1078 && curbuf->b_ffname == NULL && !curbuf->b_changed)
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001079 {
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001080 use_firstwin = TRUE;
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001081 tab_drop_empty_window = TRUE;
1082 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001083
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001084 for (i = 0; i < count && !got_int; ++i)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001085 {
1086 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
1087 arg_had_last = TRUE;
1088 if (opened[i] > 0)
1089 {
1090 // Move the already present window to below the current window
1091 if (curwin->w_arg_idx != i)
1092 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001093 FOR_ALL_WINDOWS(wpnext)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001094 {
1095 if (wpnext->w_arg_idx == i)
1096 {
1097 if (keep_tabs)
1098 {
1099 new_curwin = wpnext;
1100 new_curtab = curtab;
1101 }
1102 else if (wpnext->w_frame->fr_parent
1103 != curwin->w_frame->fr_parent)
1104 {
1105 emsg(_("E249: window layout changed unexpectedly"));
1106 i = count;
1107 break;
1108 }
1109 else
1110 win_move_after(wpnext, curwin);
1111 break;
1112 }
1113 }
1114 }
1115 }
1116 else if (split_ret == OK)
1117 {
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001118 // trigger events for tab drop
1119 if (tab_drop_empty_window && i == count - 1)
1120 --autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001121 if (!use_firstwin) // split current window
1122 {
1123 p_ea_save = p_ea;
1124 p_ea = TRUE; // use space from all windows
1125 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
1126 p_ea = p_ea_save;
1127 if (split_ret == FAIL)
1128 continue;
1129 }
1130 else // first window: do autocmd for leaving this buffer
1131 --autocmd_no_leave;
1132
1133 // edit file "i"
1134 curwin->w_arg_idx = i;
1135 if (i == 0)
1136 {
1137 new_curwin = curwin;
1138 new_curtab = curtab;
1139 }
1140 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
1141 ECMD_ONE,
1142 ((buf_hide(curwin->w_buffer)
1143 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
1144 + ECMD_OLDBUF, curwin);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001145 if (tab_drop_empty_window && i == count - 1)
1146 ++autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001147 if (use_firstwin)
1148 ++autocmd_no_leave;
1149 use_firstwin = FALSE;
1150 }
1151 ui_breakcheck();
1152
1153 // When ":tab" was used open a new tab for a new window repeatedly.
1154 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02001155 cmdmod.cmod_tab = 9999;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001156 }
1157
1158 // Remove the "lock" on the argument list.
1159 alist_unlink(alist);
1160
1161 --autocmd_no_enter;
1162
1163 // restore last referenced tabpage's curwin
1164 if (last_curtab != new_curtab)
1165 {
1166 if (valid_tabpage(last_curtab))
1167 goto_tabpage_tp(last_curtab, TRUE, TRUE);
1168 if (win_valid(last_curwin))
1169 win_enter(last_curwin, FALSE);
1170 }
1171 // to window with first arg
1172 if (valid_tabpage(new_curtab))
1173 goto_tabpage_tp(new_curtab, TRUE, TRUE);
1174 if (win_valid(new_curwin))
1175 win_enter(new_curwin, FALSE);
1176
1177 --autocmd_no_leave;
1178 vim_free(opened);
1179}
1180
1181/*
1182 * ":all" and ":sall".
1183 * Also used for ":tab drop file ..." after setting the argument list.
1184 */
1185 void
1186ex_all(exarg_T *eap)
1187{
1188 if (eap->addr_count == 0)
1189 eap->line2 = 9999;
1190 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
1191}
1192
1193/*
1194 * Concatenate all files in the argument list, separated by spaces, and return
1195 * it in one allocated string.
1196 * Spaces and backslashes in the file names are escaped with a backslash.
1197 * Returns NULL when out of memory.
1198 */
1199 char_u *
1200arg_all(void)
1201{
1202 int len;
1203 int idx;
1204 char_u *retval = NULL;
1205 char_u *p;
1206
1207 // Do this loop two times:
1208 // first time: compute the total length
1209 // second time: concatenate the names
1210 for (;;)
1211 {
1212 len = 0;
1213 for (idx = 0; idx < ARGCOUNT; ++idx)
1214 {
1215 p = alist_name(&ARGLIST[idx]);
1216 if (p != NULL)
1217 {
1218 if (len > 0)
1219 {
1220 // insert a space in between names
1221 if (retval != NULL)
1222 retval[len] = ' ';
1223 ++len;
1224 }
1225 for ( ; *p != NUL; ++p)
1226 {
1227 if (*p == ' '
1228#ifndef BACKSLASH_IN_FILENAME
1229 || *p == '\\'
1230#endif
1231 || *p == '`')
1232 {
1233 // insert a backslash
1234 if (retval != NULL)
1235 retval[len] = '\\';
1236 ++len;
1237 }
1238 if (retval != NULL)
1239 retval[len] = *p;
1240 ++len;
1241 }
1242 }
1243 }
1244
1245 // second time: break here
1246 if (retval != NULL)
1247 {
1248 retval[len] = NUL;
1249 break;
1250 }
1251
1252 // allocate memory
1253 retval = alloc(len + 1);
1254 if (retval == NULL)
1255 break;
1256 }
1257
1258 return retval;
1259}
1260
1261#if defined(FEAT_EVAL) || defined(PROTO)
1262/*
1263 * "argc([window id])" function
1264 */
1265 void
1266f_argc(typval_T *argvars, typval_T *rettv)
1267{
1268 win_T *wp;
1269
1270 if (argvars[0].v_type == VAR_UNKNOWN)
1271 // use the current window
1272 rettv->vval.v_number = ARGCOUNT;
1273 else if (argvars[0].v_type == VAR_NUMBER
1274 && tv_get_number(&argvars[0]) == -1)
1275 // use the global argument list
1276 rettv->vval.v_number = GARGCOUNT;
1277 else
1278 {
1279 // use the argument list of the specified window
1280 wp = find_win_by_nr_or_id(&argvars[0]);
1281 if (wp != NULL)
1282 rettv->vval.v_number = WARGCOUNT(wp);
1283 else
1284 rettv->vval.v_number = -1;
1285 }
1286}
1287
1288/*
1289 * "argidx()" function
1290 */
1291 void
1292f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1293{
1294 rettv->vval.v_number = curwin->w_arg_idx;
1295}
1296
1297/*
1298 * "arglistid()" function
1299 */
1300 void
1301f_arglistid(typval_T *argvars, typval_T *rettv)
1302{
1303 win_T *wp;
1304
1305 rettv->vval.v_number = -1;
1306 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
1307 if (wp != NULL)
1308 rettv->vval.v_number = wp->w_alist->id;
1309}
1310
1311/*
1312 * Get the argument list for a given window
1313 */
1314 static void
1315get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1316{
1317 int idx;
1318
1319 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1320 for (idx = 0; idx < argcount; ++idx)
1321 list_append_string(rettv->vval.v_list,
1322 alist_name(&arglist[idx]), -1);
1323}
1324
1325/*
1326 * "argv(nr)" function
1327 */
1328 void
1329f_argv(typval_T *argvars, typval_T *rettv)
1330{
1331 int idx;
1332 aentry_T *arglist = NULL;
1333 int argcount = -1;
1334
1335 if (argvars[0].v_type != VAR_UNKNOWN)
1336 {
1337 if (argvars[1].v_type == VAR_UNKNOWN)
1338 {
1339 arglist = ARGLIST;
1340 argcount = ARGCOUNT;
1341 }
1342 else if (argvars[1].v_type == VAR_NUMBER
1343 && tv_get_number(&argvars[1]) == -1)
1344 {
1345 arglist = GARGLIST;
1346 argcount = GARGCOUNT;
1347 }
1348 else
1349 {
1350 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1351
1352 if (wp != NULL)
1353 {
1354 // Use the argument list of the specified window
1355 arglist = WARGLIST(wp);
1356 argcount = WARGCOUNT(wp);
1357 }
1358 }
1359
1360 rettv->v_type = VAR_STRING;
1361 rettv->vval.v_string = NULL;
1362 idx = tv_get_number_chk(&argvars[0], NULL);
1363 if (arglist != NULL && idx >= 0 && idx < argcount)
1364 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1365 else if (idx == -1)
1366 get_arglist_as_rettv(arglist, argcount, rettv);
1367 }
1368 else
1369 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
1370}
1371#endif