robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
25.4 KiB932 linesraw
1/// @file ex_cmds2.c
2///
3/// Some more functions for command line commands
4
5#include <assert.h>
6#include <inttypes.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <string.h>
10
11#include "nvim/arglist.h"
12#include "nvim/ascii_defs.h"
13#include "nvim/autocmd.h"
14#include "nvim/autocmd_defs.h"
15#include "nvim/buffer.h"
16#include "nvim/buffer_defs.h"
17#include "nvim/bufwrite.h"
18#include "nvim/change.h"
19#include "nvim/channel.h"
20#include "nvim/context.h"
21#include "nvim/dialog.h"
22#include "nvim/errors.h"
23#include "nvim/eval.h"
24#include "nvim/eval/typval.h"
25#include "nvim/eval/typval_defs.h"
26#include "nvim/eval/vars.h"
27#include "nvim/ex_cmds.h"
28#include "nvim/ex_cmds2.h"
29#include "nvim/ex_cmds_defs.h"
30#include "nvim/ex_docmd.h"
31#include "nvim/ex_getln.h"
32#include "nvim/fileio.h"
33#include "nvim/gettext_defs.h"
34#include "nvim/globals.h"
35#include "nvim/highlight_defs.h"
36#include "nvim/macros_defs.h"
37#include "nvim/mark.h"
38#include "nvim/memline_defs.h"
39#include "nvim/memory.h"
40#include "nvim/message.h"
41#include "nvim/move.h"
42#include "nvim/normal.h"
43#include "nvim/option_vars.h"
44#include "nvim/os/os_defs.h"
45#include "nvim/path.h"
46#include "nvim/pos_defs.h"
47#include "nvim/quickfix.h"
48#include "nvim/runtime.h"
49#include "nvim/types_defs.h"
50#include "nvim/ui.h"
51#include "nvim/undo.h"
52#include "nvim/vim_defs.h"
53#include "nvim/window.h"
54
55#include "ex_cmds2.c.generated.h"
56
57static const char e_compiler_not_supported_str[]
58  = N_("E666: Compiler not supported: %s");
59
60void ex_ruby(exarg_T *eap)
61{
62  script_host_execute("ruby", eap);
63}
64
65void ex_rubyfile(exarg_T *eap)
66{
67  script_host_execute_file("ruby", eap);
68}
69
70void ex_rubydo(exarg_T *eap)
71{
72  script_host_do_range("ruby", eap);
73}
74
75void ex_python3(exarg_T *eap)
76{
77  script_host_execute("python3", eap);
78}
79
80void ex_py3file(exarg_T *eap)
81{
82  script_host_execute_file("python3", eap);
83}
84
85void ex_pydo3(exarg_T *eap)
86{
87  script_host_do_range("python3", eap);
88}
89
90void ex_perl(exarg_T *eap)
91{
92  script_host_execute("perl", eap);
93}
94
95void ex_perlfile(exarg_T *eap)
96{
97  script_host_execute_file("perl", eap);
98}
99
100void ex_perldo(exarg_T *eap)
101{
102  script_host_do_range("perl", eap);
103}
104
105/// If 'autowrite' option set, try to write the file.
106/// Careful: autocommands may make "buf" invalid!
107///
108/// @return FAIL for failure, OK otherwise
109int autowrite(buf_T *buf, bool forceit)
110{
111  bufref_T bufref;
112
113  if (!(p_aw || p_awa) || !p_write
114      // never autowrite a "nofile" or "nowrite" buffer
115      || bt_dontwrite(buf)
116      || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL) {
117    return FAIL;
118  }
119  set_bufref(&bufref, buf);
120  int r = buf_write_all(buf, forceit);
121
122  // Writing may succeed but the buffer still changed, e.g., when there is a
123  // conversion error.  We do want to return FAIL then.
124  if (bufref_valid(&bufref) && bufIsChanged(buf)) {
125    r = FAIL;
126  }
127  return r;
128}
129
130/// Flush all buffers, except the ones that are readonly or are never written.
131void autowrite_all(void)
132{
133  if (!(p_aw || p_awa) || !p_write) {
134    return;
135  }
136
137  FOR_ALL_BUFFERS(buf) {
138    if (bufIsChanged(buf) && !buf->b_p_ro && !bt_dontwrite(buf)) {
139      bufref_T bufref;
140      set_bufref(&bufref, buf);
141      buf_write_all(buf, false);
142      // an autocommand may have deleted the buffer
143      if (!bufref_valid(&bufref)) {
144        buf = firstbuf;
145      }
146    }
147  }
148}
149
150/// @return  true if buffer was changed and cannot be abandoned.
151/// For flags use the CCGD_ values.
152bool check_changed(buf_T *buf, int flags)
153{
154  bool forceit = (flags & CCGD_FORCEIT);
155  bufref_T bufref;
156  set_bufref(&bufref, buf);
157
158  if (!forceit
159      && bufIsChanged(buf)
160      && ((flags & CCGD_MULTWIN) || buf->b_nwindows <= 1)
161      && (!(flags & CCGD_AW) || autowrite(buf, forceit) == FAIL)) {
162    if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write) {
163      int count = 0;
164
165      if (flags & CCGD_ALLBUF) {
166        FOR_ALL_BUFFERS(buf2) {
167          if (bufIsChanged(buf2) && (buf2->b_ffname != NULL)) {
168            count++;
169          }
170        }
171      }
172      if (!bufref_valid(&bufref)) {
173        // Autocommand deleted buffer, oops!  It's not changed now.
174        return false;
175      }
176      dialog_changed(buf, count > 1);
177      if (!bufref_valid(&bufref)) {
178        // Autocommand deleted buffer, oops!  It's not changed now.
179        return false;
180      }
181      return bufIsChanged(buf);
182    }
183    if (flags & CCGD_EXCMD) {
184      no_write_message();
185    } else {
186      no_write_message_nobang(curbuf);
187    }
188    return true;
189  }
190  return false;
191}
192
193/// Ask the user what to do when abandoning a changed buffer.
194/// Must check 'write' option first!
195///
196/// @param buf
197/// @param checkall may abandon all changed buffers
198void dialog_changed(buf_T *buf, bool checkall)
199{
200  char buff[DIALOG_MSG_SIZE];
201  int ret;
202  // Init ea pseudo-structure, this is needed for the check_overwrite()
203  // function.
204  exarg_T ea = {
205    .append = false,
206    .forceit = false,
207  };
208
209  const char *fname = buf->b_fname ? buf->b_fname : _("Untitled");
210  snprintf(buff, sizeof buff, _("Save changes to \"%s\"?"), fname);
211  if (checkall) {
212    ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
213  } else {
214    ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
215  }
216
217  if (ret == VIM_YES) {
218    bool empty_bufname = buf->b_fname == NULL;
219    if (empty_bufname) {
220      buf_set_name(buf->b_fnum, "Untitled");
221    }
222
223    if (check_overwrite(&ea, buf, buf->b_fname, buf->b_ffname, false) == OK) {
224      // didn't hit Cancel
225      if (buf_write_all(buf, false) == OK) {
226        return;
227      }
228    }
229
230    // restore to empty when write failed or was cancelled
231    if (empty_bufname) {
232      buf->b_fname = NULL;
233      XFREE_CLEAR(buf->b_ffname);
234      XFREE_CLEAR(buf->b_sfname);
235    }
236  } else if (ret == VIM_NO) {
237    unchanged(buf, true, false);
238  } else if (ret == VIM_ALL) {
239    // Write all modified files that can be written.
240    // Skip readonly buffers, these need to be confirmed
241    // individually.
242    FOR_ALL_BUFFERS(buf2) {
243      if (bufIsChanged(buf2) && (buf2->b_ffname != NULL) && !buf2->b_p_ro) {
244        bufref_T bufref;
245        set_bufref(&bufref, buf2);
246
247        if (buf2->b_fname != NULL
248            && check_overwrite(&ea, buf2, buf2->b_fname, buf2->b_ffname, false) == OK) {
249          // didn't hit Cancel
250          buf_write_all(buf2, false);
251        }
252        // an autocommand may have deleted the buffer
253        if (!bufref_valid(&bufref)) {
254          buf2 = firstbuf;
255        }
256      }
257    }
258  } else if (ret == VIM_DISCARDALL) {
259    // mark all buffers as unchanged
260    FOR_ALL_BUFFERS(buf2) {
261      unchanged(buf2, true, false);
262    }
263  }
264}
265
266/// Ask the user whether to close the terminal buffer or not.
267///
268/// @param buf The terminal buffer.
269/// @return bool Whether to close the buffer or not.
270bool dialog_close_terminal(buf_T *buf)
271{
272  char buff[DIALOG_MSG_SIZE];
273
274  snprintf(buff, sizeof buff, _("Close \"%s\"?"),
275           (buf->b_fname != NULL) ? buf->b_fname : "?");
276
277  int ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
278
279  return ret == VIM_YES;
280}
281
282/// @return true if the buffer "buf" can be abandoned, either by making it
283/// hidden, autowriting it or unloading it.
284bool can_abandon(buf_T *buf, bool forceit)
285{
286  return buf_hide(buf)
287         || !bufIsChanged(buf)
288         || buf->b_nwindows > 1
289         || autowrite(buf, forceit) == OK
290         || forceit;
291}
292
293/// Add a buffer number to "bufnrs", unless it's already there.
294static void add_bufnum(int *bufnrs, int *bufnump, int nr)
295{
296  for (int i = 0; i < *bufnump; i++) {
297    if (bufnrs[i] == nr) {
298      return;
299    }
300  }
301  bufnrs[*bufnump] = nr;
302  *bufnump = *bufnump + 1;
303}
304
305/// Check if any buffer was changed and cannot be abandoned.
306/// That changed buffer becomes the current buffer.
307/// When "unload" is true the current buffer is unloaded instead of making it
308/// hidden.  This is used for ":q!".
309///
310/// @param[in] hidden specifies whether to check only hidden buffers.
311/// @param[in] unload specifies whether to unload, instead of hide, the buffer.
312///
313/// @returns          true if any buffer is changed and cannot be abandoned
314bool check_changed_any(bool hidden, bool unload)
315{
316  bool ret = false;
317  int i;
318  int bufnum = 0;
319  size_t bufcount = 0;
320
321  // Make a list of all buffers, with the most important ones first.
322  FOR_ALL_BUFFERS(buf) {
323    bufcount++;
324  }
325
326  if (bufcount == 0) {
327    return false;
328  }
329
330  int *bufnrs = xmalloc(sizeof(*bufnrs) * bufcount);
331
332  // curbuf
333  bufnrs[bufnum++] = curbuf->b_fnum;
334
335  // buffers in current tab
336  FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
337    if (wp->w_buffer != curbuf) {
338      add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
339    }
340  }
341
342  // buffers in other tabs
343  FOR_ALL_TABS(tp) {
344    if (tp != curtab) {
345      FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
346        add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
347      }
348    }
349  }
350
351  // any other buffer
352  FOR_ALL_BUFFERS(buf) {
353    add_bufnum(bufnrs, &bufnum, buf->b_fnum);
354  }
355
356  buf_T *buf = NULL;
357  for (i = 0; i < bufnum; i++) {
358    buf = buflist_findnr(bufnrs[i]);
359    if (buf == NULL) {
360      continue;
361    }
362    if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf)) {
363      bufref_T bufref;
364      set_bufref(&bufref, buf);
365
366      // Try auto-writing the buffer.  If this fails but the buffer no
367      // longer exists it's not changed, that's OK.
368      if (check_changed(buf, (p_awa ? CCGD_AW : 0)
369                        | CCGD_MULTWIN
370                        | CCGD_ALLBUF) && bufref_valid(&bufref)) {
371        break;    // didn't save - still changes
372      }
373    }
374  }
375
376  if (i >= bufnum) {
377    goto theend;
378  }
379
380  // Get here if "buf" cannot be abandoned.
381  ret = true;
382  exiting = false;
383  // When ":confirm" used, don't give an error message.
384  if (!(p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))) {
385    // There must be a wait_return() for this message, do_buffer()
386    // may cause a redraw.  But wait_return() is a no-op when vgetc()
387    // is busy (Quit used from window menu), then make sure we don't
388    // cause a scroll up.
389    if (vgetc_busy > 0) {
390      msg_row = cmdline_row;
391      msg_col = 0;
392      msg_didout = false;
393    }
394    if (((buf->terminal && channel_job_running((uint64_t)buf->b_p_channel))
395         ? semsg(_("E947: Job still running in buffer \"%s\""), buf->b_fname)
396         : semsg(_("E162: No write since last change for buffer \"%s\""),
397                 buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname))
398        // Only makes sense if error is shown, which cause_errthrow() may prevent.
399        && msg_didany) {
400      int save = no_wait_return;
401      no_wait_return = false;
402      if (!ui_has(kUIMessages)) {
403        wait_return(false);
404      }
405      no_wait_return = save;
406    }
407  }
408
409  // Try to find a window that contains the buffer.
410  if (buf != curbuf) {
411    FOR_ALL_TAB_WINDOWS(tp, wp) {
412      if (wp->w_buffer == buf) {
413        bufref_T bufref;
414        set_bufref(&bufref, buf);
415        goto_tabpage_win(tp, wp);
416        // Paranoia: did autocmds wipe out the buffer with changes?
417        if (!bufref_valid(&bufref)) {
418          goto theend;
419        }
420        goto buf_found;
421      }
422    }
423  }
424buf_found:
425
426  // Open the changed buffer in the current window.
427  if (buf != curbuf) {
428    set_curbuf(buf, unload ? DOBUF_UNLOAD : DOBUF_GOTO, true);
429  }
430
431theend:
432  xfree(bufnrs);
433  return ret;
434}
435
436/// @return  FAIL if there is no file name, OK if there is one.
437///          Give error message for FAIL.
438int check_fname(void)
439{
440  if (curbuf->b_ffname == NULL) {
441    emsg(_(e_noname));
442    return FAIL;
443  }
444  return OK;
445}
446
447/// Flush the contents of a buffer, unless it has no file name.
448///
449/// @return  FAIL for failure, OK otherwise
450int buf_write_all(buf_T *buf, bool forceit)
451{
452  buf_T *old_curbuf = curbuf;
453
454  int retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
455                          1, buf->b_ml.ml_line_count, NULL,
456                          false, forceit, true, false));
457  if (curbuf != old_curbuf) {
458    msg_source(HLF_W);
459    msg(_("Warning: Entered other buffer unexpectedly (check autocommands)"), 0);
460  }
461  return retval;
462}
463
464/// ":argdo", ":windo", ":bufdo", ":tabdo", ":cdo", ":ldo", ":cfdo" and ":lfdo"
465void ex_listdo(exarg_T *eap)
466{
467  // ":windo" and ":tabdo" only visit existing windows/tabpages, they don't
468  // change the current window's buffer, so they can't escape a 'winfixbuf'
469  // window (which would create a split).
470  if (curwin->w_p_wfb && eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo) {
471    if ((eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) && !eap->forceit) {
472      // Disallow :ldo if 'winfixbuf' is applied
473      emsg(_(e_winfixbuf_cannot_go_to_buffer));
474      return;
475    }
476
477    if (win_valid(prevwin) && !prevwin->w_p_wfb) {
478      // 'winfixbuf' is set; attempt to change to a window without it.
479      win_goto(prevwin);
480    }
481    if (curwin->w_p_wfb) {
482      // Split the window, which will be 'nowinfixbuf', and set curwin to that
483      (void)win_split(0, 0);
484
485      if (curwin->w_p_wfb) {
486        // Autocommands set 'winfixbuf' or sent us to another window
487        // with it set, or we failed to split the window. Give up.
488        emsg(_(e_winfixbuf_cannot_go_to_buffer));
489        return;
490      }
491    }
492  }
493
494  char *save_ei = NULL;
495
496  // Temporarily override kShmOver and kShmOverall to avoid that file
497  // message overwrites output from the command.
498  msg_listdo_overwrite++;
499
500  if (eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo) {
501    // Don't do syntax HL autocommands.  Skipping the syntax file is a
502    // great speed improvement.
503    save_ei = au_event_disable(",Syntax");
504
505    FOR_ALL_BUFFERS(buf) {
506      buf->b_flags &= ~BF_SYN_SET;
507    }
508  }
509
510  if (eap->cmdidx == CMD_windo
511      || eap->cmdidx == CMD_tabdo
512      || buf_hide(curbuf)
513      || !check_changed(curbuf, CCGD_AW
514                        | (eap->forceit ? CCGD_FORCEIT : 0)
515                        | CCGD_EXCMD)) {
516    int next_fnum = 0;
517    int i = 0;
518    // start at the eap->line1 argument/window/buffer
519    win_T *wp = firstwin;
520    tabpage_T *tp = first_tabpage;
521    switch (eap->cmdidx) {
522    case CMD_windo:
523      for (; wp != NULL && i + 1 < eap->line1; wp = wp->w_next) {
524        i++;
525      }
526      break;
527    case CMD_tabdo:
528      for (; tp != NULL && i + 1 < eap->line1; tp = tp->tp_next) {
529        i++;
530      }
531      break;
532    case CMD_argdo:
533      i = (int)eap->line1 - 1;
534      break;
535    default:
536      break;
537    }
538
539    buf_T *buf = curbuf;
540    size_t qf_size = 0;
541
542    // set pcmark now
543    if (eap->cmdidx == CMD_bufdo) {
544      // Advance to the first listed buffer after "eap->line1".
545      for (buf = firstbuf;
546           buf != NULL && (buf->b_fnum < eap->line1 || !buf->b_p_bl);
547           buf = buf->b_next) {
548        if (buf->b_fnum > eap->line2) {
549          buf = NULL;
550          break;
551        }
552      }
553      if (buf != NULL) {
554        goto_buffer(eap, DOBUF_FIRST, FORWARD, buf->b_fnum);
555      }
556    } else if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
557               || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) {
558      qf_size = qf_get_valid_size(eap);
559      assert(eap->line1 >= 0);
560      if (qf_size == 0 || (size_t)eap->line1 > qf_size) {
561        buf = NULL;
562      } else {
563        ex_cc(eap);
564
565        buf = curbuf;
566        i = (int)eap->line1 - 1;
567        if (eap->addr_count <= 0) {
568          // Default to all quickfix/location list entries.
569          assert(qf_size < MAXLNUM);
570          eap->line2 = (linenr_T)qf_size;
571        }
572      }
573    } else {
574      setpcmark();
575    }
576    listcmd_busy = true;            // avoids setting pcmark below
577
578    while (!got_int && buf != NULL) {
579      bool execute = true;
580      if (eap->cmdidx == CMD_argdo) {
581        // go to argument "i"
582        if (i == ARGCOUNT) {
583          break;
584        }
585        // Don't call do_argfile() when already there, it will try
586        // reloading the file.
587        if (curwin->w_arg_idx != i || !editing_arg_idx(curwin)) {
588          // Clear 'shm' to avoid that the file message overwrites
589          // any output from the command.
590          do_argfile(eap, i);
591        }
592        if (curwin->w_arg_idx != i) {
593          break;
594        }
595      } else if (eap->cmdidx == CMD_windo) {
596        // go to window "wp"
597        if (!win_valid(wp)) {
598          break;
599        }
600        assert(wp);
601        execute = !wp->w_floating || (!wp->w_config.hide && wp->w_config.focusable);
602        if (execute) {
603          win_goto(wp);
604          if (curwin != wp) {
605            break;    // something must be wrong
606          }
607        }
608        wp = wp->w_next;
609      } else if (eap->cmdidx == CMD_tabdo) {
610        // go to window "tp"
611        if (!valid_tabpage(tp)) {
612          break;
613        }
614        assert(tp);
615        goto_tabpage_tp(tp, true, true);
616        tp = tp->tp_next;
617      } else if (eap->cmdidx == CMD_bufdo) {
618        // Remember the number of the next listed buffer, in case
619        // ":bwipe" is used or autocommands do something strange.
620        next_fnum = -1;
621        for (buf_T *bp = curbuf->b_next; bp != NULL; bp = bp->b_next) {
622          if (bp->b_p_bl) {
623            next_fnum = bp->b_fnum;
624            break;
625          }
626        }
627      }
628
629      i++;
630      // execute the command
631      if (execute) {
632        do_cmdline(eap->arg, eap->ea_getline, eap->cookie, DOCMD_VERBOSE + DOCMD_NOWAIT);
633      }
634
635      if (eap->cmdidx == CMD_bufdo) {
636        // Done?
637        if (next_fnum < 0 || next_fnum > eap->line2) {
638          break;
639        }
640
641        // Check if the buffer still exists.
642        bool buf_still_exists = false;
643        FOR_ALL_BUFFERS(bp) {
644          if (bp->b_fnum == next_fnum) {
645            buf_still_exists = true;
646            break;
647          }
648        }
649        if (!buf_still_exists) {
650          break;
651        }
652
653        // Go to the next buffer.
654        goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
655
656        // If autocommands took us elsewhere, quit here.
657        if (curbuf->b_fnum != next_fnum) {
658          break;
659        }
660      }
661
662      if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
663          || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) {
664        assert(i >= 0);
665        if ((size_t)i >= qf_size || i >= eap->line2) {
666          break;
667        }
668
669        size_t qf_idx = qf_get_cur_idx(eap);
670
671        ex_cnext(eap);
672
673        // If jumping to the next quickfix entry fails, quit here.
674        if (qf_get_cur_idx(eap) == qf_idx) {
675          break;
676        }
677      }
678
679      if (eap->cmdidx == CMD_windo && execute) {
680        validate_cursor(curwin);              // cursor may have moved
681        // required when 'scrollbind' has been set
682        if (curwin->w_p_scb) {
683          do_check_scrollbind(true);
684        }
685      }
686      if (eap->cmdidx == CMD_windo || eap->cmdidx == CMD_tabdo) {
687        if (i + 1 > eap->line2) {
688          break;
689        }
690      }
691      if (eap->cmdidx == CMD_argdo && i >= eap->line2) {
692        break;
693      }
694    }
695    listcmd_busy = false;
696  }
697
698  msg_listdo_overwrite--;
699  if (save_ei != NULL) {
700    buf_T *bnext;
701    CtxSwitch aco = { 0 };
702
703    au_event_restore(save_ei);
704
705    for (buf_T *buf = firstbuf; buf != NULL; buf = bnext) {
706      bnext = buf->b_next;
707      if (buf->b_nwindows > 0 && (buf->b_flags & BF_SYN_SET)) {
708        buf->b_flags &= ~BF_SYN_SET;
709
710        // buffer was opened while Syntax autocommands were disabled,
711        // need to trigger them now.
712        if (buf == curbuf) {
713          apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname, true,
714                         curbuf);
715        } else {
716          ctx_switch(&aco, NULL, NULL, buf, 0);
717          apply_autocmds(EVENT_SYNTAX, buf->b_p_syn, buf->b_fname, true, buf);
718          ctx_restore(&aco);
719        }
720
721        // start over, in case autocommands messed things up.
722        bnext = firstbuf;
723      }
724    }
725  }
726}
727
728/// ":compiler[!] {name}"
729void ex_compiler(exarg_T *eap)
730{
731  char *old_cur_comp = NULL;
732
733  if (*eap->arg == NUL) {
734    // List all compiler scripts.
735    do_cmdline_cmd("echo globpath(&rtp, 'compiler/*.vim')");  // NOLINT
736    do_cmdline_cmd("echo globpath(&rtp, 'compiler/*.lua')");  // NOLINT
737    return;
738  }
739
740  size_t bufsize = strlen(eap->arg) + 14;
741  char *buf = xmalloc(bufsize);
742
743  if (eap->forceit) {
744    // ":compiler! {name}" sets global options
745    do_cmdline_cmd("command -nargs=* -keepscript CompilerSet set <args>");
746  } else {
747    // ":compiler! {name}" sets local options.
748    // To remain backwards compatible "current_compiler" is always
749    // used.  A user's compiler plugin may set it, the distributed
750    // plugin will then skip the settings.  Afterwards set
751    // "b:current_compiler" and restore "current_compiler".
752    // Explicitly prepend "g:" to make it work in a function.
753    old_cur_comp = get_var_value("g:current_compiler");
754    if (old_cur_comp != NULL) {
755      old_cur_comp = xstrdup(old_cur_comp);
756    }
757    do_cmdline_cmd("command -nargs=* -keepscript CompilerSet setlocal <args>");
758  }
759  do_unlet(S_LEN("g:current_compiler"), true);
760  do_unlet(S_LEN("b:current_compiler"), true);
761
762  snprintf(buf, bufsize, "compiler/%s.*", eap->arg);
763  if (source_runtime_vim_lua(buf, DIP_ALL) == FAIL) {
764    semsg(_(e_compiler_not_supported_str), eap->arg);
765  }
766  xfree(buf);
767
768  do_cmdline_cmd(":delcommand CompilerSet");
769
770  // Set "b:current_compiler" from "current_compiler".
771  char *p = get_var_value("g:current_compiler");
772  if (p != NULL) {
773    set_internal_string_var("b:current_compiler", p);
774  }
775
776  // Restore "current_compiler" for ":compiler {name}".
777  if (!eap->forceit) {
778    if (old_cur_comp != NULL) {
779      set_internal_string_var("g:current_compiler", old_cur_comp);
780      xfree(old_cur_comp);
781    } else {
782      do_unlet(S_LEN("g:current_compiler"), true);
783    }
784  }
785}
786
787/// ":checktime [buffer]"
788void ex_checktime(exarg_T *eap)
789{
790  int save_no_check_timestamps = no_check_timestamps;
791
792  no_check_timestamps = 0;
793  if (eap->addr_count == 0) {    // default is all buffers
794    check_timestamps(false);
795  } else {
796    buf_T *buf = buflist_findnr((int)eap->line2);
797    if (buf != NULL) {           // cannot happen?
798      buf_check_timestamp(buf);
799    }
800  }
801  no_check_timestamps = save_no_check_timestamps;
802}
803
804static void script_host_execute(char *name, exarg_T *eap)
805{
806  size_t len;
807  char *const script = script_get(eap, &len);
808
809  if (script != NULL) {
810    list_T *const args = tv_list_alloc(3);
811    // script
812    tv_list_append_allocated_string(args, script);
813    // current range
814    tv_list_append_number(args, (int)eap->line1);
815    tv_list_append_number(args, (int)eap->line2);
816
817    eval_call_provider(name, "execute", args, true);
818  }
819}
820
821static void script_host_execute_file(char *name, exarg_T *eap)
822{
823  if (!eap->skip) {
824    uint8_t buffer[MAXPATHL];
825    vim_FullName(eap->arg, (char *)buffer, sizeof(buffer), false);
826
827    list_T *args = tv_list_alloc(3);
828    // filename
829    tv_list_append_string(args, (const char *)buffer, -1);
830    // current range
831    tv_list_append_number(args, (int)eap->line1);
832    tv_list_append_number(args, (int)eap->line2);
833    eval_call_provider(name, "execute_file", args, true);
834  }
835}
836
837static void script_host_do_range(char *name, exarg_T *eap)
838{
839  if (!eap->skip) {
840    list_T *args = tv_list_alloc(3);
841    tv_list_append_number(args, (int)eap->line1);
842    tv_list_append_number(args, (int)eap->line2);
843    tv_list_append_string(args, eap->arg, -1);
844    eval_call_provider(name, "do_range", args, true);
845  }
846}
847
848/// ":drop"
849/// Opens the first argument in a window, and the argument list is redefined.
850void ex_drop(exarg_T *eap)
851{
852  bool split = false;
853
854  // Check if the first argument is already being edited in a window.  If
855  // so, jump to that window.
856  // We would actually need to check all arguments, but that's complicated
857  // and mostly only one file is dropped.
858  // This also ignores wildcards, since it is very unlikely the user is
859  // editing a file name with a wildcard character.
860  set_arglist(eap->arg);
861
862  // Expanding wildcards may result in an empty argument list.  E.g. when
863  // editing "foo.pyc" and ".pyc" is in 'wildignore'.  Assume that we
864  // already did an error message for this.
865  if (ARGCOUNT == 0) {
866    return;
867  }
868
869  if (cmdmod.cmod_tab) {
870    // ":tab drop file ...": open a tab for each argument that isn't
871    // edited in a window yet.  It's like ":tab all" but without closing
872    // windows or tabs.
873    ex_all(eap);
874    cmdmod.cmod_tab = 0;
875    ex_rewind(eap);
876    return;
877  }
878
879  // ":drop file ...": Edit the first argument.  Jump to an existing
880  // window if possible, edit in current window if the current buffer
881  // can be abandoned, otherwise open a new window.
882  buf_T *buf = buflist_findnr(ARGLIST[0].ae_fnum);
883
884  FOR_ALL_TAB_WINDOWS(tp, wp) {
885    if (wp->w_buffer == buf) {
886      goto_tabpage_win(tp, wp);
887      curwin->w_arg_idx = 0;
888      if (!bufIsChanged(curbuf)) {
889        const int save_ar = curbuf->b_p_ar;
890
891        // reload the file if it is newer
892        curbuf->b_p_ar = true;
893        buf_check_timestamp(curbuf);
894        curbuf->b_p_ar = save_ar;
895      }
896      if (curbuf->b_ml.ml_flags & ML_EMPTY) {
897        ex_rewind(eap);
898      }
899
900      // execute [+cmd]
901      if (eap->do_ecmd_cmd) {
902        bool did_set_swapcommand = set_swapcommand(eap->do_ecmd_cmd, 0);
903        do_cmdline(eap->do_ecmd_cmd, NULL, NULL, DOCMD_VERBOSE);
904        if (did_set_swapcommand) {
905          set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
906        }
907      }
908
909      // no need to execute [++opts] - they only apply for newly loaded buffers.
910      return;
911    }
912  }
913
914  // Check whether the current buffer is changed. If so, we will need
915  // to split the current window or data could be lost.
916  // Skip the check if the 'hidden' option is set, as in this case the
917  // buffer won't be lost.
918  if (!buf_hide(curbuf)) {
919    emsg_off++;
920    split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD);
921    emsg_off--;
922  }
923
924  // Fake a ":sfirst" or ":first" command edit the first argument.
925  if (split) {
926    eap->cmdidx = CMD_sfirst;
927    eap->cmd[0] = 's';
928  } else {
929    eap->cmdidx = CMD_first;
930  }
931  ex_rewind(eap);
932}