blob: ab23880a5e4fb2d2cc07c804705bf733f1a8816e [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;
412 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
413 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
660 if (*eap->cmd == 's' || cmdmod.tab != 0)
661 {
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;
881 int had_tab = cmdmod.tab;
882 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
887 if (ARGCOUNT <= 0)
888 {
889 // Don't give an error message. We don't want it when the ":all"
890 // command is in the .vimrc.
891 return;
892 }
893 setpcmark();
894
895 opened_len = ARGCOUNT;
896 opened = alloc_clear(opened_len);
897 if (opened == NULL)
898 return;
899
900 // Autocommands may do anything to the argument list. Make sure it's not
901 // freed while we are working here by "locking" it. We still have to
902 // watch out for its size to be changed.
903 alist = curwin->w_alist;
904 ++alist->al_refcount;
905
906 old_curwin = curwin;
907 old_curtab = curtab;
908
909# ifdef FEAT_GUI
910 need_mouse_correct = TRUE;
911# endif
912
913 // Try closing all windows that are not in the argument list.
914 // Also close windows that are not full width;
915 // When 'hidden' or "forceit" set the buffer becomes hidden.
916 // Windows that have a changed buffer and can't be hidden won't be closed.
917 // When the ":tab" modifier was used do this for all tab pages.
918 if (had_tab > 0)
919 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
920 for (;;)
921 {
922 tpnext = curtab->tp_next;
923 for (wp = firstwin; wp != NULL; wp = wpnext)
924 {
925 wpnext = wp->w_next;
926 buf = wp->w_buffer;
927 if (buf->b_ffname == NULL
928 || (!keep_tabs && (buf->b_nwindows > 1
929 || wp->w_width != Columns)))
930 i = opened_len;
931 else
932 {
933 // check if the buffer in this window is in the arglist
934 for (i = 0; i < opened_len; ++i)
935 {
936 if (i < alist->al_ga.ga_len
937 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
938 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
939 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
940 {
941 int weight = 1;
942
943 if (old_curtab == curtab)
944 {
945 ++weight;
946 if (old_curwin == wp)
947 ++weight;
948 }
949
950 if (weight > (int)opened[i])
951 {
952 opened[i] = (char_u)weight;
953 if (i == 0)
954 {
955 if (new_curwin != NULL)
956 new_curwin->w_arg_idx = opened_len;
957 new_curwin = wp;
958 new_curtab = curtab;
959 }
960 }
961 else if (keep_tabs)
962 i = opened_len;
963
964 if (wp->w_alist != alist)
965 {
966 // Use the current argument list for all windows
967 // containing a file from it.
968 alist_unlink(wp->w_alist);
969 wp->w_alist = alist;
970 ++wp->w_alist->al_refcount;
971 }
972 break;
973 }
974 }
975 }
976 wp->w_arg_idx = i;
977
978 if (i == opened_len && !keep_tabs)// close this window
979 {
980 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
981 || !bufIsChanged(buf))
982 {
983 // If the buffer was changed, and we would like to hide it,
984 // try autowriting.
985 if (!buf_hide(buf) && buf->b_nwindows <= 1
986 && bufIsChanged(buf))
987 {
988 bufref_T bufref;
989
990 set_bufref(&bufref, buf);
991
992 (void)autowrite(buf, FALSE);
993
994 // check if autocommands removed the window
995 if (!win_valid(wp) || !bufref_valid(&bufref))
996 {
997 wpnext = firstwin; // start all over...
998 continue;
999 }
1000 }
1001 // don't close last window
1002 if (ONE_WINDOW
1003 && (first_tabpage->tp_next == NULL || !had_tab))
1004 use_firstwin = TRUE;
1005 else
1006 {
1007 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
1008
1009 // check if autocommands removed the next window
1010 if (!win_valid(wpnext))
1011 wpnext = firstwin; // start all over...
1012 }
1013 }
1014 }
1015 }
1016
1017 // Without the ":tab" modifier only do the current tab page.
1018 if (had_tab == 0 || tpnext == NULL)
1019 break;
1020
1021 // check if autocommands removed the next tab page
1022 if (!valid_tabpage(tpnext))
1023 tpnext = first_tabpage; // start all over...
1024
1025 goto_tabpage_tp(tpnext, TRUE, TRUE);
1026 }
1027
1028 // Open a window for files in the argument list that don't have one.
1029 // ARGCOUNT may change while doing this, because of autocommands.
1030 if (count > opened_len || count <= 0)
1031 count = opened_len;
1032
1033 // Don't execute Win/Buf Enter/Leave autocommands here.
1034 ++autocmd_no_enter;
1035 ++autocmd_no_leave;
1036 last_curwin = curwin;
1037 last_curtab = curtab;
1038 win_enter(lastwin, FALSE);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001039 // ":tab drop file" should re-use an empty window to avoid "--remote-tab"
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001040 // leaving an empty tab page when executed locally.
1041 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
1042 && curbuf->b_ffname == NULL && !curbuf->b_changed)
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001043 {
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001044 use_firstwin = TRUE;
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001045 tab_drop_empty_window = TRUE;
1046 }
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001047
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001048 for (i = 0; i < count && !got_int; ++i)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001049 {
1050 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
1051 arg_had_last = TRUE;
1052 if (opened[i] > 0)
1053 {
1054 // Move the already present window to below the current window
1055 if (curwin->w_arg_idx != i)
1056 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001057 FOR_ALL_WINDOWS(wpnext)
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001058 {
1059 if (wpnext->w_arg_idx == i)
1060 {
1061 if (keep_tabs)
1062 {
1063 new_curwin = wpnext;
1064 new_curtab = curtab;
1065 }
1066 else if (wpnext->w_frame->fr_parent
1067 != curwin->w_frame->fr_parent)
1068 {
1069 emsg(_("E249: window layout changed unexpectedly"));
1070 i = count;
1071 break;
1072 }
1073 else
1074 win_move_after(wpnext, curwin);
1075 break;
1076 }
1077 }
1078 }
1079 }
1080 else if (split_ret == OK)
1081 {
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001082 // trigger events for tab drop
1083 if (tab_drop_empty_window && i == count - 1)
1084 --autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001085 if (!use_firstwin) // split current window
1086 {
1087 p_ea_save = p_ea;
1088 p_ea = TRUE; // use space from all windows
1089 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
1090 p_ea = p_ea_save;
1091 if (split_ret == FAIL)
1092 continue;
1093 }
1094 else // first window: do autocmd for leaving this buffer
1095 --autocmd_no_leave;
1096
1097 // edit file "i"
1098 curwin->w_arg_idx = i;
1099 if (i == 0)
1100 {
1101 new_curwin = curwin;
1102 new_curtab = curtab;
1103 }
1104 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
1105 ECMD_ONE,
1106 ((buf_hide(curwin->w_buffer)
1107 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
1108 + ECMD_OLDBUF, curwin);
Bram Moolenaarc10b5212020-01-13 20:54:51 +01001109 if (tab_drop_empty_window && i == count - 1)
1110 ++autocmd_no_enter;
Bram Moolenaar4ad62152019-08-17 14:38:55 +02001111 if (use_firstwin)
1112 ++autocmd_no_leave;
1113 use_firstwin = FALSE;
1114 }
1115 ui_breakcheck();
1116
1117 // When ":tab" was used open a new tab for a new window repeatedly.
1118 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
1119 cmdmod.tab = 9999;
1120 }
1121
1122 // Remove the "lock" on the argument list.
1123 alist_unlink(alist);
1124
1125 --autocmd_no_enter;
1126
1127 // restore last referenced tabpage's curwin
1128 if (last_curtab != new_curtab)
1129 {
1130 if (valid_tabpage(last_curtab))
1131 goto_tabpage_tp(last_curtab, TRUE, TRUE);
1132 if (win_valid(last_curwin))
1133 win_enter(last_curwin, FALSE);
1134 }
1135 // to window with first arg
1136 if (valid_tabpage(new_curtab))
1137 goto_tabpage_tp(new_curtab, TRUE, TRUE);
1138 if (win_valid(new_curwin))
1139 win_enter(new_curwin, FALSE);
1140
1141 --autocmd_no_leave;
1142 vim_free(opened);
1143}
1144
1145/*
1146 * ":all" and ":sall".
1147 * Also used for ":tab drop file ..." after setting the argument list.
1148 */
1149 void
1150ex_all(exarg_T *eap)
1151{
1152 if (eap->addr_count == 0)
1153 eap->line2 = 9999;
1154 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
1155}
1156
1157/*
1158 * Concatenate all files in the argument list, separated by spaces, and return
1159 * it in one allocated string.
1160 * Spaces and backslashes in the file names are escaped with a backslash.
1161 * Returns NULL when out of memory.
1162 */
1163 char_u *
1164arg_all(void)
1165{
1166 int len;
1167 int idx;
1168 char_u *retval = NULL;
1169 char_u *p;
1170
1171 // Do this loop two times:
1172 // first time: compute the total length
1173 // second time: concatenate the names
1174 for (;;)
1175 {
1176 len = 0;
1177 for (idx = 0; idx < ARGCOUNT; ++idx)
1178 {
1179 p = alist_name(&ARGLIST[idx]);
1180 if (p != NULL)
1181 {
1182 if (len > 0)
1183 {
1184 // insert a space in between names
1185 if (retval != NULL)
1186 retval[len] = ' ';
1187 ++len;
1188 }
1189 for ( ; *p != NUL; ++p)
1190 {
1191 if (*p == ' '
1192#ifndef BACKSLASH_IN_FILENAME
1193 || *p == '\\'
1194#endif
1195 || *p == '`')
1196 {
1197 // insert a backslash
1198 if (retval != NULL)
1199 retval[len] = '\\';
1200 ++len;
1201 }
1202 if (retval != NULL)
1203 retval[len] = *p;
1204 ++len;
1205 }
1206 }
1207 }
1208
1209 // second time: break here
1210 if (retval != NULL)
1211 {
1212 retval[len] = NUL;
1213 break;
1214 }
1215
1216 // allocate memory
1217 retval = alloc(len + 1);
1218 if (retval == NULL)
1219 break;
1220 }
1221
1222 return retval;
1223}
1224
1225#if defined(FEAT_EVAL) || defined(PROTO)
1226/*
1227 * "argc([window id])" function
1228 */
1229 void
1230f_argc(typval_T *argvars, typval_T *rettv)
1231{
1232 win_T *wp;
1233
1234 if (argvars[0].v_type == VAR_UNKNOWN)
1235 // use the current window
1236 rettv->vval.v_number = ARGCOUNT;
1237 else if (argvars[0].v_type == VAR_NUMBER
1238 && tv_get_number(&argvars[0]) == -1)
1239 // use the global argument list
1240 rettv->vval.v_number = GARGCOUNT;
1241 else
1242 {
1243 // use the argument list of the specified window
1244 wp = find_win_by_nr_or_id(&argvars[0]);
1245 if (wp != NULL)
1246 rettv->vval.v_number = WARGCOUNT(wp);
1247 else
1248 rettv->vval.v_number = -1;
1249 }
1250}
1251
1252/*
1253 * "argidx()" function
1254 */
1255 void
1256f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1257{
1258 rettv->vval.v_number = curwin->w_arg_idx;
1259}
1260
1261/*
1262 * "arglistid()" function
1263 */
1264 void
1265f_arglistid(typval_T *argvars, typval_T *rettv)
1266{
1267 win_T *wp;
1268
1269 rettv->vval.v_number = -1;
1270 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
1271 if (wp != NULL)
1272 rettv->vval.v_number = wp->w_alist->id;
1273}
1274
1275/*
1276 * Get the argument list for a given window
1277 */
1278 static void
1279get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1280{
1281 int idx;
1282
1283 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1284 for (idx = 0; idx < argcount; ++idx)
1285 list_append_string(rettv->vval.v_list,
1286 alist_name(&arglist[idx]), -1);
1287}
1288
1289/*
1290 * "argv(nr)" function
1291 */
1292 void
1293f_argv(typval_T *argvars, typval_T *rettv)
1294{
1295 int idx;
1296 aentry_T *arglist = NULL;
1297 int argcount = -1;
1298
1299 if (argvars[0].v_type != VAR_UNKNOWN)
1300 {
1301 if (argvars[1].v_type == VAR_UNKNOWN)
1302 {
1303 arglist = ARGLIST;
1304 argcount = ARGCOUNT;
1305 }
1306 else if (argvars[1].v_type == VAR_NUMBER
1307 && tv_get_number(&argvars[1]) == -1)
1308 {
1309 arglist = GARGLIST;
1310 argcount = GARGCOUNT;
1311 }
1312 else
1313 {
1314 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1315
1316 if (wp != NULL)
1317 {
1318 // Use the argument list of the specified window
1319 arglist = WARGLIST(wp);
1320 argcount = WARGCOUNT(wp);
1321 }
1322 }
1323
1324 rettv->v_type = VAR_STRING;
1325 rettv->vval.v_string = NULL;
1326 idx = tv_get_number_chk(&argvars[0], NULL);
1327 if (arglist != NULL && idx >= 0 && idx < argcount)
1328 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1329 else if (idx == -1)
1330 get_arglist_as_rettv(arglist, argcount, rettv);
1331 }
1332 else
1333 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
1334}
1335#endif