blob: 06d94280b0d3dfdd0ad69fc74d15dc28f1d1d303 [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
20/*
21 * Clear an argument list: free all file names and reset it to zero entries.
22 */
23 void
24alist_clear(alist_T *al)
25{
26 while (--al->al_ga.ga_len >= 0)
27 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
28 ga_clear(&al->al_ga);
29}
30
31/*
32 * Init an argument list.
33 */
34 void
35alist_init(alist_T *al)
36{
37 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
38}
39
40/*
41 * Remove a reference from an argument list.
42 * Ignored when the argument list is the global one.
43 * If the argument list is no longer used by any window, free it.
44 */
45 void
46alist_unlink(alist_T *al)
47{
48 if (al != &global_alist && --al->al_refcount <= 0)
49 {
50 alist_clear(al);
51 vim_free(al);
52 }
53}
54
55/*
56 * Create a new argument list and use it for the current window.
57 */
58 void
59alist_new(void)
60{
61 curwin->w_alist = ALLOC_ONE(alist_T);
62 if (curwin->w_alist == NULL)
63 {
64 curwin->w_alist = &global_alist;
65 ++global_alist.al_refcount;
66 }
67 else
68 {
69 curwin->w_alist->al_refcount = 1;
70 curwin->w_alist->id = ++max_alist_id;
71 alist_init(curwin->w_alist);
72 }
73}
74
75#if !defined(UNIX) || defined(PROTO)
76/*
77 * Expand the file names in the global argument list.
78 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
79 * numbers to be re-used.
80 */
81 void
82alist_expand(int *fnum_list, int fnum_len)
83{
84 char_u **old_arg_files;
85 int old_arg_count;
86 char_u **new_arg_files;
87 int new_arg_file_count;
88 char_u *save_p_su = p_su;
89 int i;
90
91 // Don't use 'suffixes' here. This should work like the shell did the
92 // expansion. Also, the vimrc file isn't read yet, thus the user
93 // can't set the options.
94 p_su = empty_option;
95 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
96 if (old_arg_files != NULL)
97 {
98 for (i = 0; i < GARGCOUNT; ++i)
99 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
100 old_arg_count = GARGCOUNT;
101 if (expand_wildcards(old_arg_count, old_arg_files,
102 &new_arg_file_count, &new_arg_files,
103 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
104 && new_arg_file_count > 0)
105 {
106 alist_set(&global_alist, new_arg_file_count, new_arg_files,
107 TRUE, fnum_list, fnum_len);
108 FreeWild(old_arg_count, old_arg_files);
109 }
110 }
111 p_su = save_p_su;
112}
113#endif
114
115/*
116 * Set the argument list for the current window.
117 * Takes over the allocated files[] and the allocated fnames in it.
118 */
119 void
120alist_set(
121 alist_T *al,
122 int count,
123 char_u **files,
124 int use_curbuf,
125 int *fnum_list,
126 int fnum_len)
127{
128 int i;
129 static int recursive = 0;
130
131 if (recursive)
132 {
133 emsg(_(e_au_recursive));
134 return;
135 }
136 ++recursive;
137
138 alist_clear(al);
139 if (ga_grow(&al->al_ga, count) == OK)
140 {
141 for (i = 0; i < count; ++i)
142 {
143 if (got_int)
144 {
145 // When adding many buffers this can take a long time. Allow
146 // interrupting here.
147 while (i < count)
148 vim_free(files[i++]);
149 break;
150 }
151
152 // May set buffer name of a buffer previously used for the
153 // argument list, so that it's re-used by alist_add.
154 if (fnum_list != NULL && i < fnum_len)
155 buf_set_name(fnum_list[i], files[i]);
156
157 alist_add(al, files[i], use_curbuf ? 2 : 1);
158 ui_breakcheck();
159 }
160 vim_free(files);
161 }
162 else
163 FreeWild(count, files);
164 if (al == &global_alist)
165 arg_had_last = FALSE;
166
167 --recursive;
168}
169
170/*
171 * Add file "fname" to argument list "al".
172 * "fname" must have been allocated and "al" must have been checked for room.
173 */
174 void
175alist_add(
176 alist_T *al,
177 char_u *fname,
178 int set_fnum) // 1: set buffer number; 2: re-use curbuf
179{
180 if (fname == NULL) // don't add NULL file names
181 return;
182#ifdef BACKSLASH_IN_FILENAME
183 slash_adjust(fname);
184#endif
185 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
186 if (set_fnum > 0)
187 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
188 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
189 ++al->al_ga.ga_len;
190}
191
192#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
193/*
194 * Adjust slashes in file names. Called after 'shellslash' was set.
195 */
196 void
197alist_slash_adjust(void)
198{
199 int i;
200 win_T *wp;
201 tabpage_T *tp;
202
203 for (i = 0; i < GARGCOUNT; ++i)
204 if (GARGLIST[i].ae_fname != NULL)
205 slash_adjust(GARGLIST[i].ae_fname);
206 FOR_ALL_TAB_WINDOWS(tp, wp)
207 if (wp->w_alist != &global_alist)
208 for (i = 0; i < WARGCOUNT(wp); ++i)
209 if (WARGLIST(wp)[i].ae_fname != NULL)
210 slash_adjust(WARGLIST(wp)[i].ae_fname);
211}
212#endif
213
214/*
215 * Isolate one argument, taking backticks.
216 * Changes the argument in-place, puts a NUL after it. Backticks remain.
217 * Return a pointer to the start of the next argument.
218 */
219 static char_u *
220do_one_arg(char_u *str)
221{
222 char_u *p;
223 int inbacktick;
224
225 inbacktick = FALSE;
226 for (p = str; *str; ++str)
227 {
228 // When the backslash is used for escaping the special meaning of a
229 // character we need to keep it until wildcard expansion.
230 if (rem_backslash(str))
231 {
232 *p++ = *str++;
233 *p++ = *str;
234 }
235 else
236 {
237 // An item ends at a space not in backticks
238 if (!inbacktick && vim_isspace(*str))
239 break;
240 if (*str == '`')
241 inbacktick ^= TRUE;
242 *p++ = *str;
243 }
244 }
245 str = skipwhite(str);
246 *p = NUL;
247
248 return str;
249}
250
251/*
252 * Separate the arguments in "str" and return a list of pointers in the
253 * growarray "gap".
254 */
255 static int
256get_arglist(garray_T *gap, char_u *str, int escaped)
257{
258 ga_init2(gap, (int)sizeof(char_u *), 20);
259 while (*str != NUL)
260 {
261 if (ga_grow(gap, 1) == FAIL)
262 {
263 ga_clear(gap);
264 return FAIL;
265 }
266 ((char_u **)gap->ga_data)[gap->ga_len++] = str;
267
268 // If str is escaped, don't handle backslashes or spaces
269 if (!escaped)
270 return OK;
271
272 // Isolate one argument, change it in-place, put a NUL after it.
273 str = do_one_arg(str);
274 }
275 return OK;
276}
277
278#if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
279/*
280 * Parse a list of arguments (file names), expand them and return in
281 * "fnames[fcountp]". When "wig" is TRUE, removes files matching 'wildignore'.
282 * Return FAIL or OK.
283 */
284 int
285get_arglist_exp(
286 char_u *str,
287 int *fcountp,
288 char_u ***fnamesp,
289 int wig)
290{
291 garray_T ga;
292 int i;
293
294 if (get_arglist(&ga, str, TRUE) == FAIL)
295 return FAIL;
296 if (wig == TRUE)
297 i = expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
298 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
299 else
300 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
301 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
302
303 ga_clear(&ga);
304 return i;
305}
306#endif
307
308/*
309 * Check the validity of the arg_idx for each other window.
310 */
311 static void
312alist_check_arg_idx(void)
313{
314 win_T *win;
315 tabpage_T *tp;
316
317 FOR_ALL_TAB_WINDOWS(tp, win)
318 if (win->w_alist == curwin->w_alist)
319 check_arg_idx(win);
320}
321
322/*
323 * Add files[count] to the arglist of the current window after arg "after".
324 * The file names in files[count] must have been allocated and are taken over.
325 * Files[] itself is not taken over.
326 */
327 static void
328alist_add_list(
329 int count,
330 char_u **files,
331 int after, // where to add: 0 = before first one
332 int will_edit) // will edit adding argument
333{
334 int i;
335 int old_argcount = ARGCOUNT;
336
337 if (ga_grow(&ALIST(curwin)->al_ga, count) == OK)
338 {
339 if (after < 0)
340 after = 0;
341 if (after > ARGCOUNT)
342 after = ARGCOUNT;
343 if (after < ARGCOUNT)
344 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]),
345 (ARGCOUNT - after) * sizeof(aentry_T));
346 for (i = 0; i < count; ++i)
347 {
348 int flags = BLN_LISTED | (will_edit ? BLN_CURBUF : 0);
349
350 ARGLIST[after + i].ae_fname = files[i];
351 ARGLIST[after + i].ae_fnum = buflist_add(files[i], flags);
352 }
353 ALIST(curwin)->al_ga.ga_len += count;
354 if (old_argcount > 0 && curwin->w_arg_idx >= after)
355 curwin->w_arg_idx += count;
356 return;
357 }
358
359 for (i = 0; i < count; ++i)
360 vim_free(files[i]);
361}
362
363/*
364 * "what" == AL_SET: Redefine the argument list to 'str'.
365 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
366 * "what" == AL_DEL: remove files in 'str' from the argument list.
367 *
368 * Return FAIL for failure, OK otherwise.
369 */
370 static int
371do_arglist(
372 char_u *str,
373 int what,
374 int after UNUSED, // 0 means before first one
375 int will_edit) // will edit added argument
376{
377 garray_T new_ga;
378 int exp_count;
379 char_u **exp_files;
380 int i;
381 char_u *p;
382 int match;
383 int arg_escaped = TRUE;
384
385 // Set default argument for ":argadd" command.
386 if (what == AL_ADD && *str == NUL)
387 {
388 if (curbuf->b_ffname == NULL)
389 return FAIL;
390 str = curbuf->b_fname;
391 arg_escaped = FALSE;
392 }
393
394 // Collect all file name arguments in "new_ga".
395 if (get_arglist(&new_ga, str, arg_escaped) == FAIL)
396 return FAIL;
397
398 if (what == AL_DEL)
399 {
400 regmatch_T regmatch;
401 int didone;
402
403 // Delete the items: use each item as a regexp and find a match in the
404 // argument list.
405 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
406 for (i = 0; i < new_ga.ga_len && !got_int; ++i)
407 {
408 p = ((char_u **)new_ga.ga_data)[i];
409 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
410 if (p == NULL)
411 break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100412 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200413 if (regmatch.regprog == NULL)
414 {
415 vim_free(p);
416 break;
417 }
418
419 didone = FALSE;
420 for (match = 0; match < ARGCOUNT; ++match)
421 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
422 (colnr_T)0))
423 {
424 didone = TRUE;
425 vim_free(ARGLIST[match].ae_fname);
426 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
427 (ARGCOUNT - match - 1) * sizeof(aentry_T));
428 --ALIST(curwin)->al_ga.ga_len;
429 if (curwin->w_arg_idx > match)
430 --curwin->w_arg_idx;
431 --match;
432 }
433
434 vim_regfree(regmatch.regprog);
435 vim_free(p);
436 if (!didone)
437 semsg(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
438 }
439 ga_clear(&new_ga);
440 }
441 else
442 {
443 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
444 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
445 ga_clear(&new_ga);
446 if (i == FAIL || exp_count == 0)
447 {
448 emsg(_(e_nomatch));
449 return FAIL;
450 }
451
452 if (what == AL_ADD)
453 {
454 alist_add_list(exp_count, exp_files, after, will_edit);
455 vim_free(exp_files);
456 }
457 else // what == AL_SET
458 alist_set(ALIST(curwin), exp_count, exp_files, will_edit, NULL, 0);
459 }
460
461 alist_check_arg_idx();
462
463 return OK;
464}
465
466/*
467 * Redefine the argument list.
468 */
469 void
470set_arglist(char_u *str)
471{
472 do_arglist(str, AL_SET, 0, FALSE);
473}
474
475/*
476 * Return TRUE if window "win" is editing the file at the current argument
477 * index.
478 */
479 int
480editing_arg_idx(win_T *win)
481{
482 return !(win->w_arg_idx >= WARGCOUNT(win)
483 || (win->w_buffer->b_fnum
484 != WARGLIST(win)[win->w_arg_idx].ae_fnum
485 && (win->w_buffer->b_ffname == NULL
486 || !(fullpathcmp(
487 alist_name(&WARGLIST(win)[win->w_arg_idx]),
488 win->w_buffer->b_ffname, TRUE, TRUE) & FPC_SAME))));
489}
490
491/*
492 * Check if window "win" is editing the w_arg_idx file in its argument list.
493 */
494 void
495check_arg_idx(win_T *win)
496{
497 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
498 {
499 // We are not editing the current entry in the argument list.
500 // Set "arg_had_last" if we are editing the last one.
501 win->w_arg_idx_invalid = TRUE;
502 if (win->w_arg_idx != WARGCOUNT(win) - 1
503 && arg_had_last == FALSE
504 && ALIST(win) == &global_alist
505 && GARGCOUNT > 0
506 && win->w_arg_idx < GARGCOUNT
507 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
508 || (win->w_buffer->b_ffname != NULL
509 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
510 win->w_buffer->b_ffname, TRUE, TRUE) & FPC_SAME))))
511 arg_had_last = TRUE;
512 }
513 else
514 {
515 // We are editing the current entry in the argument list.
516 // Set "arg_had_last" if it's also the last one
517 win->w_arg_idx_invalid = FALSE;
518 if (win->w_arg_idx == WARGCOUNT(win) - 1
519 && win->w_alist == &global_alist)
520 arg_had_last = TRUE;
521 }
522}
523
524/*
525 * ":args", ":argslocal" and ":argsglobal".
526 */
527 void
528ex_args(exarg_T *eap)
529{
530 int i;
531
532 if (eap->cmdidx != CMD_args)
533 {
534 alist_unlink(ALIST(curwin));
535 if (eap->cmdidx == CMD_argglobal)
536 ALIST(curwin) = &global_alist;
537 else // eap->cmdidx == CMD_arglocal
538 alist_new();
539 }
540
541 if (*eap->arg != NUL)
542 {
543 // ":args file ..": define new argument list, handle like ":next"
544 // Also for ":argslocal file .." and ":argsglobal file ..".
545 ex_next(eap);
546 }
547 else if (eap->cmdidx == CMD_args)
548 {
549 // ":args": list arguments.
550 if (ARGCOUNT > 0)
551 {
552 char_u **items = ALLOC_MULT(char_u *, ARGCOUNT);
553
554 if (items != NULL)
555 {
556 // Overwrite the command, for a short list there is no
557 // scrolling required and no wait_return().
558 gotocmdline(TRUE);
559
560 for (i = 0; i < ARGCOUNT; ++i)
561 items[i] = alist_name(&ARGLIST[i]);
562 list_in_columns(items, ARGCOUNT, curwin->w_arg_idx);
563 vim_free(items);
564 }
565 }
566 }
567 else if (eap->cmdidx == CMD_arglocal)
568 {
569 garray_T *gap = &curwin->w_alist->al_ga;
570
571 // ":argslocal": make a local copy of the global argument list.
572 if (ga_grow(gap, GARGCOUNT) == OK)
573 for (i = 0; i < GARGCOUNT; ++i)
574 if (GARGLIST[i].ae_fname != NULL)
575 {
576 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
577 vim_strsave(GARGLIST[i].ae_fname);
578 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
579 GARGLIST[i].ae_fnum;
580 ++gap->ga_len;
581 }
582 }
583}
584
585/*
586 * ":previous", ":sprevious", ":Next" and ":sNext".
587 */
588 void
589ex_previous(exarg_T *eap)
590{
591 // If past the last one already, go to the last one.
592 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
593 do_argfile(eap, ARGCOUNT - 1);
594 else
595 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
596}
597
598/*
599 * ":rewind", ":first", ":sfirst" and ":srewind".
600 */
601 void
602ex_rewind(exarg_T *eap)
603{
604 do_argfile(eap, 0);
605}
606
607/*
608 * ":last" and ":slast".
609 */
610 void
611ex_last(exarg_T *eap)
612{
613 do_argfile(eap, ARGCOUNT - 1);
614}
615
616/*
617 * ":argument" and ":sargument".
618 */
619 void
620ex_argument(exarg_T *eap)
621{
622 int i;
623
624 if (eap->addr_count > 0)
625 i = eap->line2 - 1;
626 else
627 i = curwin->w_arg_idx;
628 do_argfile(eap, i);
629}
630
631/*
632 * Edit file "argn" of the argument lists.
633 */
634 void
635do_argfile(exarg_T *eap, int argn)
636{
637 int other;
638 char_u *p;
639 int old_arg_idx = curwin->w_arg_idx;
640
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +0100641 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200642 return;
643 if (argn < 0 || argn >= ARGCOUNT)
644 {
645 if (ARGCOUNT <= 1)
646 emsg(_("E163: There is only one file to edit"));
647 else if (argn < 0)
648 emsg(_("E164: Cannot go before first file"));
649 else
650 emsg(_("E165: Cannot go beyond last file"));
651 }
652 else
653 {
654 setpcmark();
655#ifdef FEAT_GUI
656 need_mouse_correct = TRUE;
657#endif
658
659 // split window or create new tab page first
Bram Moolenaare1004402020-10-24 20:49:43 +0200660 if (*eap->cmd == 's' || cmdmod.cmod_tab != 0)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200661 {
662 if (win_split(0, 0) == FAIL)
663 return;
664 RESET_BINDING(curwin);
665 }
666 else
667 {
668 // if 'hidden' set, only check for changed file when re-editing
669 // the same buffer
670 other = TRUE;
671 if (buf_hide(curbuf))
672 {
673 p = fix_fname(alist_name(&ARGLIST[argn]));
674 other = otherfile(p);
675 vim_free(p);
676 }
677 if ((!buf_hide(curbuf) || !other)
678 && check_changed(curbuf, CCGD_AW
679 | (other ? 0 : CCGD_MULTWIN)
680 | (eap->forceit ? CCGD_FORCEIT : 0)
681 | CCGD_EXCMD))
682 return;
683 }
684
685 curwin->w_arg_idx = argn;
686 if (argn == ARGCOUNT - 1 && curwin->w_alist == &global_alist)
687 arg_had_last = TRUE;
688
689 // Edit the file; always use the last known line number.
690 // When it fails (e.g. Abort for already edited file) restore the
691 // argument index.
692 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
693 eap, ECMD_LAST,
694 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
695 + (eap->forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
696 curwin->w_arg_idx = old_arg_idx;
697 // like Vi: set the mark where the cursor is in the file.
698 else if (eap->cmdidx != CMD_argdo)
699 setmark('\'');
700 }
701}
702
703/*
704 * ":next", and commands that behave like it.
705 */
706 void
707ex_next(exarg_T *eap)
708{
709 int i;
710
711 // check for changed buffer now, if this fails the argument list is not
712 // redefined.
713 if ( buf_hide(curbuf)
714 || eap->cmdidx == CMD_snext
715 || !check_changed(curbuf, CCGD_AW
716 | (eap->forceit ? CCGD_FORCEIT : 0)
717 | CCGD_EXCMD))
718 {
719 if (*eap->arg != NUL) // redefine file list
720 {
721 if (do_arglist(eap->arg, AL_SET, 0, TRUE) == FAIL)
722 return;
723 i = 0;
724 }
725 else
726 i = curwin->w_arg_idx + (int)eap->line2;
727 do_argfile(eap, i);
728 }
729}
730
731/*
732 * ":argedit"
733 */
734 void
735ex_argedit(exarg_T *eap)
736{
737 int i = eap->addr_count ? (int)eap->line2 : curwin->w_arg_idx + 1;
738 // Whether curbuf will be reused, curbuf->b_ffname will be set.
739 int curbuf_is_reusable = curbuf_reusable();
740
741 if (do_arglist(eap->arg, AL_ADD, i, TRUE) == FAIL)
742 return;
743#ifdef FEAT_TITLE
744 maketitle();
745#endif
746
747 if (curwin->w_arg_idx == 0
748 && (curbuf->b_ml.ml_flags & ML_EMPTY)
749 && (curbuf->b_ffname == NULL || curbuf_is_reusable))
750 i = 0;
751 // Edit the argument.
752 if (i < ARGCOUNT)
753 do_argfile(eap, i);
754}
755
756/*
757 * ":argadd"
758 */
759 void
760ex_argadd(exarg_T *eap)
761{
762 do_arglist(eap->arg, AL_ADD,
763 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1,
764 FALSE);
765#ifdef FEAT_TITLE
766 maketitle();
767#endif
768}
769
770/*
771 * ":argdelete"
772 */
773 void
774ex_argdelete(exarg_T *eap)
775{
776 int i;
777 int n;
778
Bram Moolenaar7b221172020-08-17 19:34:10 +0200779 if (eap->addr_count > 0 || *eap->arg == NUL)
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200780 {
Bram Moolenaar02c037a2020-08-30 19:26:45 +0200781 // ":argdel" works like ":.argdel"
Bram Moolenaar7b221172020-08-17 19:34:10 +0200782 if (eap->addr_count == 0)
783 {
784 if (curwin->w_arg_idx >= ARGCOUNT)
785 {
786 emsg(_("E610: No argument to delete"));
787 return;
788 }
789 eap->line1 = eap->line2 = curwin->w_arg_idx + 1;
790 }
791 else if (eap->line2 > ARGCOUNT)
792 // ":1,4argdel": Delete all arguments in the range.
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200793 eap->line2 = ARGCOUNT;
794 n = eap->line2 - eap->line1 + 1;
795 if (*eap->arg != NUL)
796 // Can't have both a range and an argument.
797 emsg(_(e_invarg));
798 else if (n <= 0)
799 {
800 // Don't give an error for ":%argdel" if the list is empty.
801 if (eap->line1 != 1 || eap->line2 != 0)
802 emsg(_(e_invrange));
803 }
804 else
805 {
806 for (i = eap->line1; i <= eap->line2; ++i)
807 vim_free(ARGLIST[i - 1].ae_fname);
808 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
809 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
810 ALIST(curwin)->al_ga.ga_len -= n;
811 if (curwin->w_arg_idx >= eap->line2)
812 curwin->w_arg_idx -= n;
813 else if (curwin->w_arg_idx > eap->line1)
814 curwin->w_arg_idx = eap->line1;
815 if (ARGCOUNT == 0)
816 curwin->w_arg_idx = 0;
817 else if (curwin->w_arg_idx >= ARGCOUNT)
818 curwin->w_arg_idx = ARGCOUNT - 1;
819 }
820 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200821 else
822 do_arglist(eap->arg, AL_DEL, 0, FALSE);
823#ifdef FEAT_TITLE
824 maketitle();
825#endif
826}
827
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200828/*
829 * Function given to ExpandGeneric() to obtain the possible arguments of the
830 * argedit and argdelete commands.
831 */
832 char_u *
833get_arglist_name(expand_T *xp UNUSED, int idx)
834{
835 if (idx >= ARGCOUNT)
836 return NULL;
837
838 return alist_name(&ARGLIST[idx]);
839}
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200840
841/*
842 * Get the file name for an argument list entry.
843 */
844 char_u *
845alist_name(aentry_T *aep)
846{
847 buf_T *bp;
848
849 // Use the name from the associated buffer if it exists.
850 bp = buflist_findnr(aep->ae_fnum);
851 if (bp == NULL || bp->b_fname == NULL)
852 return aep->ae_fname;
853 return bp->b_fname;
854}
855
856/*
857 * do_arg_all(): Open up to 'count' windows, one for each argument.
858 */
859 static void
860do_arg_all(
861 int count,
862 int forceit, // hide buffers in current windows
863 int keep_tabs) // keep current tabs, for ":tab drop file"
864{
865 int i;
866 win_T *wp, *wpnext;
867 char_u *opened; // Array of weight for which args are open:
868 // 0: not opened
869 // 1: opened in other tab
870 // 2: opened in curtab
871 // 3: opened in curtab and curwin
872 //
873 int opened_len; // length of opened[]
874 int use_firstwin = FALSE; // use first window for arglist
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100875 int tab_drop_empty_window = FALSE;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200876 int split_ret = OK;
877 int p_ea_save;
878 alist_T *alist; // argument list to be used
879 buf_T *buf;
880 tabpage_T *tpnext;
Bram Moolenaare1004402020-10-24 20:49:43 +0200881 int had_tab = cmdmod.cmod_tab;
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200882 win_T *old_curwin, *last_curwin;
883 tabpage_T *old_curtab, *last_curtab;
884 win_T *new_curwin = NULL;
885 tabpage_T *new_curtab = NULL;
886
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100887#ifdef FEAT_CMDWIN
888 if (cmdwin_type != 0)
889 {
890 emsg(_(e_cmdwin));
891 return;
892 }
893#endif
Bram Moolenaar4ad62152019-08-17 14:38:55 +0200894 if (ARGCOUNT <= 0)
895 {
896 // Don't give an error message. We don't want it when the ":all"
897 // command is in the .vimrc.
898 return;
899 }
900 setpcmark();
901
902 opened_len = ARGCOUNT;
903 opened = alloc_clear(opened_len);
904 if (opened == NULL)
905 return;
906
907 // Autocommands may do anything to the argument list. Make sure it's not
908 // freed while we are working here by "locking" it. We still have to
909 // watch out for its size to be changed.
910 alist = curwin->w_alist;
911 ++alist->al_refcount;
912
913 old_curwin = curwin;
914 old_curtab = curtab;
915
916# ifdef FEAT_GUI
917 need_mouse_correct = TRUE;
918# endif
919
920 // Try closing all windows that are not in the argument list.
921 // Also close windows that are not full width;
922 // When 'hidden' or "forceit" set the buffer becomes hidden.
923 // Windows that have a changed buffer and can't be hidden won't be closed.
924 // When the ":tab" modifier was used do this for all tab pages.
925 if (had_tab > 0)
926 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
927 for (;;)
928 {
929 tpnext = curtab->tp_next;
930 for (wp = firstwin; wp != NULL; wp = wpnext)
931 {
932 wpnext = wp->w_next;
933 buf = wp->w_buffer;
934 if (buf->b_ffname == NULL
935 || (!keep_tabs && (buf->b_nwindows > 1
936 || wp->w_width != Columns)))
937 i = opened_len;
938 else
939 {
940 // check if the buffer in this window is in the arglist
941 for (i = 0; i < opened_len; ++i)
942 {
943 if (i < alist->al_ga.ga_len
944 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
945 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
946 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
947 {
948 int weight = 1;
949
950 if (old_curtab == curtab)
951 {
952 ++weight;
953 if (old_curwin == wp)
954 ++weight;
955 }
956
957 if (weight > (int)opened[i])
958 {
959 opened[i] = (char_u)weight;
960 if (i == 0)
961 {
962 if (new_curwin != NULL)
963 new_curwin->w_arg_idx = opened_len;
964 new_curwin = wp;
965 new_curtab = curtab;
966 }
967 }
968 else if (keep_tabs)
969 i = opened_len;
970
971 if (wp->w_alist != alist)
972 {
973 // Use the current argument list for all windows
974 // containing a file from it.
975 alist_unlink(wp->w_alist);
976 wp->w_alist = alist;
977 ++wp->w_alist->al_refcount;
978 }
979 break;
980 }
981 }
982 }
983 wp->w_arg_idx = i;
984
985 if (i == opened_len && !keep_tabs)// close this window
986 {
987 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
988 || !bufIsChanged(buf))
989 {
990 // If the buffer was changed, and we would like to hide it,
991 // try autowriting.
992 if (!buf_hide(buf) && buf->b_nwindows <= 1
993 && bufIsChanged(buf))
994 {
995 bufref_T bufref;
996
997 set_bufref(&bufref, buf);
998
999 (void)autowrite(buf, FALSE);
1000
1001 // check if autocommands removed the window
1002 if (!win_valid(wp) || !bufref_valid(&bufref))
1003 {
1004 wpnext = firstwin; // start all over...
1005 continue;
1006 }
1007 }
1008 // don't close last window
1009 if (ONE_WINDOW
1010 && (first_tabpage->tp_next == NULL || !had_tab))
1011 use_firstwin = TRUE;
1012 else
1013 {
1014 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
1015
1016 // check if autocommands removed the next window
1017 if (!win_valid(wpnext))
1018 wpnext = firstwin; // start all over...
1019 }
1020 }
1021 }
1022 }
1023
1024 // Without the ":tab" modifier only do the current tab page.
1025 if (had_tab == 0 || tpnext == NULL)
1026 break;
1027
1028 // check if autocommands removed the next tab page
1029 if (!valid_tabpage(tpnext))
1030 tpnext = first_tabpage; // start all over...
1031
1032 goto_tabpage_tp(tpnext, TRUE, TRUE);
1033 }
1034
1035 // Open a window for files in the argument list that don't have one.
1036 // ARGCOUNT may change while doing this, because of autocommands.
1037 if (count > opened_len || count <= 0)
1038 count = opened_len;
1039
1040 // Don't execute Win/Buf Enter/Leave autocommands here.
1041 ++autocmd_no_enter;
1042 ++autocmd_no_leave;
1043 last_curwin = curwin;
1044 last_curtab = curtab;
1045 win_enter(lastwin, FALSE);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001046 // ":tab drop file" should re-use an empty window to avoid "--remote-tab"
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001047 // leaving an empty tab page when executed locally.
1048 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
1049 && curbuf->b_ffname == NULL && !curbuf->b_changed)
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001050 {
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001051 use_firstwin = TRUE;
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001052 tab_drop_empty_window = TRUE;
1053 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001054
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001055 for (i = 0; i < count && !got_int; ++i)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001056 {
1057 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
1058 arg_had_last = TRUE;
1059 if (opened[i] > 0)
1060 {
1061 // Move the already present window to below the current window
1062 if (curwin->w_arg_idx != i)
1063 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001064 FOR_ALL_WINDOWS(wpnext)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001065 {
1066 if (wpnext->w_arg_idx == i)
1067 {
1068 if (keep_tabs)
1069 {
1070 new_curwin = wpnext;
1071 new_curtab = curtab;
1072 }
1073 else if (wpnext->w_frame->fr_parent
1074 != curwin->w_frame->fr_parent)
1075 {
1076 emsg(_("E249: window layout changed unexpectedly"));
1077 i = count;
1078 break;
1079 }
1080 else
1081 win_move_after(wpnext, curwin);
1082 break;
1083 }
1084 }
1085 }
1086 }
1087 else if (split_ret == OK)
1088 {
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001089 // trigger events for tab drop
1090 if (tab_drop_empty_window && i == count - 1)
1091 --autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001092 if (!use_firstwin) // split current window
1093 {
1094 p_ea_save = p_ea;
1095 p_ea = TRUE; // use space from all windows
1096 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
1097 p_ea = p_ea_save;
1098 if (split_ret == FAIL)
1099 continue;
1100 }
1101 else // first window: do autocmd for leaving this buffer
1102 --autocmd_no_leave;
1103
1104 // edit file "i"
1105 curwin->w_arg_idx = i;
1106 if (i == 0)
1107 {
1108 new_curwin = curwin;
1109 new_curtab = curtab;
1110 }
1111 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
1112 ECMD_ONE,
1113 ((buf_hide(curwin->w_buffer)
1114 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
1115 + ECMD_OLDBUF, curwin);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001116 if (tab_drop_empty_window && i == count - 1)
1117 ++autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001118 if (use_firstwin)
1119 ++autocmd_no_leave;
1120 use_firstwin = FALSE;
1121 }
1122 ui_breakcheck();
1123
1124 // When ":tab" was used open a new tab for a new window repeatedly.
1125 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02001126 cmdmod.cmod_tab = 9999;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001127 }
1128
1129 // Remove the "lock" on the argument list.
1130 alist_unlink(alist);
1131
1132 --autocmd_no_enter;
1133
1134 // restore last referenced tabpage's curwin
1135 if (last_curtab != new_curtab)
1136 {
1137 if (valid_tabpage(last_curtab))
1138 goto_tabpage_tp(last_curtab, TRUE, TRUE);
1139 if (win_valid(last_curwin))
1140 win_enter(last_curwin, FALSE);
1141 }
1142 // to window with first arg
1143 if (valid_tabpage(new_curtab))
1144 goto_tabpage_tp(new_curtab, TRUE, TRUE);
1145 if (win_valid(new_curwin))
1146 win_enter(new_curwin, FALSE);
1147
1148 --autocmd_no_leave;
1149 vim_free(opened);
1150}
1151
1152/*
1153 * ":all" and ":sall".
1154 * Also used for ":tab drop file ..." after setting the argument list.
1155 */
1156 void
1157ex_all(exarg_T *eap)
1158{
1159 if (eap->addr_count == 0)
1160 eap->line2 = 9999;
1161 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
1162}
1163
1164/*
1165 * Concatenate all files in the argument list, separated by spaces, and return
1166 * it in one allocated string.
1167 * Spaces and backslashes in the file names are escaped with a backslash.
1168 * Returns NULL when out of memory.
1169 */
1170 char_u *
1171arg_all(void)
1172{
1173 int len;
1174 int idx;
1175 char_u *retval = NULL;
1176 char_u *p;
1177
1178 // Do this loop two times:
1179 // first time: compute the total length
1180 // second time: concatenate the names
1181 for (;;)
1182 {
1183 len = 0;
1184 for (idx = 0; idx < ARGCOUNT; ++idx)
1185 {
1186 p = alist_name(&ARGLIST[idx]);
1187 if (p != NULL)
1188 {
1189 if (len > 0)
1190 {
1191 // insert a space in between names
1192 if (retval != NULL)
1193 retval[len] = ' ';
1194 ++len;
1195 }
1196 for ( ; *p != NUL; ++p)
1197 {
1198 if (*p == ' '
1199#ifndef BACKSLASH_IN_FILENAME
1200 || *p == '\\'
1201#endif
1202 || *p == '`')
1203 {
1204 // insert a backslash
1205 if (retval != NULL)
1206 retval[len] = '\\';
1207 ++len;
1208 }
1209 if (retval != NULL)
1210 retval[len] = *p;
1211 ++len;
1212 }
1213 }
1214 }
1215
1216 // second time: break here
1217 if (retval != NULL)
1218 {
1219 retval[len] = NUL;
1220 break;
1221 }
1222
1223 // allocate memory
1224 retval = alloc(len + 1);
1225 if (retval == NULL)
1226 break;
1227 }
1228
1229 return retval;
1230}
1231
1232#if defined(FEAT_EVAL) || defined(PROTO)
1233/*
1234 * "argc([window id])" function
1235 */
1236 void
1237f_argc(typval_T *argvars, typval_T *rettv)
1238{
1239 win_T *wp;
1240
1241 if (argvars[0].v_type == VAR_UNKNOWN)
1242 // use the current window
1243 rettv->vval.v_number = ARGCOUNT;
1244 else if (argvars[0].v_type == VAR_NUMBER
1245 && tv_get_number(&argvars[0]) == -1)
1246 // use the global argument list
1247 rettv->vval.v_number = GARGCOUNT;
1248 else
1249 {
1250 // use the argument list of the specified window
1251 wp = find_win_by_nr_or_id(&argvars[0]);
1252 if (wp != NULL)
1253 rettv->vval.v_number = WARGCOUNT(wp);
1254 else
1255 rettv->vval.v_number = -1;
1256 }
1257}
1258
1259/*
1260 * "argidx()" function
1261 */
1262 void
1263f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1264{
1265 rettv->vval.v_number = curwin->w_arg_idx;
1266}
1267
1268/*
1269 * "arglistid()" function
1270 */
1271 void
1272f_arglistid(typval_T *argvars, typval_T *rettv)
1273{
1274 win_T *wp;
1275
1276 rettv->vval.v_number = -1;
1277 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
1278 if (wp != NULL)
1279 rettv->vval.v_number = wp->w_alist->id;
1280}
1281
1282/*
1283 * Get the argument list for a given window
1284 */
1285 static void
1286get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1287{
1288 int idx;
1289
1290 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1291 for (idx = 0; idx < argcount; ++idx)
1292 list_append_string(rettv->vval.v_list,
1293 alist_name(&arglist[idx]), -1);
1294}
1295
1296/*
1297 * "argv(nr)" function
1298 */
1299 void
1300f_argv(typval_T *argvars, typval_T *rettv)
1301{
1302 int idx;
1303 aentry_T *arglist = NULL;
1304 int argcount = -1;
1305
1306 if (argvars[0].v_type != VAR_UNKNOWN)
1307 {
1308 if (argvars[1].v_type == VAR_UNKNOWN)
1309 {
1310 arglist = ARGLIST;
1311 argcount = ARGCOUNT;
1312 }
1313 else if (argvars[1].v_type == VAR_NUMBER
1314 && tv_get_number(&argvars[1]) == -1)
1315 {
1316 arglist = GARGLIST;
1317 argcount = GARGCOUNT;
1318 }
1319 else
1320 {
1321 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1322
1323 if (wp != NULL)
1324 {
1325 // Use the argument list of the specified window
1326 arglist = WARGLIST(wp);
1327 argcount = WARGCOUNT(wp);
1328 }
1329 }
1330
1331 rettv->v_type = VAR_STRING;
1332 rettv->vval.v_string = NULL;
1333 idx = tv_get_number_chk(&argvars[0], NULL);
1334 if (arglist != NULL && idx >= 0 && idx < argcount)
1335 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1336 else if (idx == -1)
1337 get_arglist_as_rettv(arglist, argcount, rettv);
1338 }
1339 else
1340 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
1341}
1342#endif