blob: 2de1798b18675d476a4fd160f8d51b9e49890f1c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 * ex_cmds2.c: some more functions for command line commands
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#include "version.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * If 'autowrite' option set, try to write the file.
19 * Careful: autocommands may make "buf" invalid!
20 *
21 * return FAIL for failure, OK otherwise
22 */
23 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010024autowrite(buf_T *buf, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025{
Bram Moolenaar373154b2007-02-13 05:19:30 +000026 int r;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020027 bufref_T bufref;
Bram Moolenaar373154b2007-02-13 05:19:30 +000028
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 if (!(p_aw || p_awa) || !p_write
30#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +010031 // never autowrite a "nofile" or "nowrite" buffer
Bram Moolenaar373154b2007-02-13 05:19:30 +000032 || bt_dontwrite(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
Bram Moolenaar373154b2007-02-13 05:19:30 +000034 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 return FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020036 set_bufref(&bufref, buf);
Bram Moolenaar373154b2007-02-13 05:19:30 +000037 r = buf_write_all(buf, forceit);
38
Bram Moolenaar217e1b82019-12-01 21:41:28 +010039 // Writing may succeed but the buffer still changed, e.g., when there is a
40 // conversion error. We do want to return FAIL then.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020041 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar373154b2007-02-13 05:19:30 +000042 r = FAIL;
43 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000044}
45
46/*
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +020047 * Flush all buffers, except the ones that are readonly or are never written.
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 */
49 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010050autowrite_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000051{
52 buf_T *buf;
53
54 if (!(p_aw || p_awa) || !p_write)
55 return;
Bram Moolenaar29323592016-07-24 22:04:11 +020056 FOR_ALL_BUFFERS(buf)
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +020057 if (bufIsChanged(buf) && !buf->b_p_ro && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020059 bufref_T bufref;
60
61 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010062
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 (void)buf_write_all(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010064
Bram Moolenaar217e1b82019-12-01 21:41:28 +010065 // an autocommand may have deleted the buffer
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020066 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 }
69}
70
71/*
Bram Moolenaar45d3b142013-11-09 03:31:51 +010072 * Return TRUE if buffer was changed and cannot be abandoned.
73 * For flags use the CCGD_ values.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010076check_changed(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020078 int forceit = (flags & CCGD_FORCEIT);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020079 bufref_T bufref;
80
81 set_bufref(&bufref, buf);
Bram Moolenaar45d3b142013-11-09 03:31:51 +010082
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 if ( !forceit
84 && bufIsChanged(buf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +010085 && ((flags & CCGD_MULTWIN) || buf->b_nwindows <= 1)
86 && (!(flags & CCGD_AW) || autowrite(buf, forceit) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 {
88#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +020089 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 {
91 buf_T *buf2;
92 int count = 0;
93
Bram Moolenaar45d3b142013-11-09 03:31:51 +010094 if (flags & CCGD_ALLBUF)
Bram Moolenaar29323592016-07-24 22:04:11 +020095 FOR_ALL_BUFFERS(buf2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 if (bufIsChanged(buf2)
97 && (buf2->b_ffname != NULL
98# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +020099 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100# endif
101 ))
102 ++count;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200103 if (!bufref_valid(&bufref))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100104 // Autocommand deleted buffer, oops! It's not changed now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100106
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 dialog_changed(buf, count > 1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100108
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200109 if (!bufref_valid(&bufref))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100110 // Autocommand deleted buffer, oops! It's not changed now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 return bufIsChanged(buf);
113 }
114#endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100115 if (flags & CCGD_EXCMD)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200116 no_write_message();
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100117 else
Bram Moolenaar7a760922018-02-19 23:10:02 +0100118 no_write_message_nobang(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 return TRUE;
120 }
121 return FALSE;
122}
123
124#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
125
126#if defined(FEAT_BROWSE) || defined(PROTO)
127/*
128 * When wanting to write a file without a file name, ask the user for a name.
129 */
130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100131browse_save_fname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 if (buf->b_fname == NULL)
134 {
135 char_u *fname;
136
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000137 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
138 NULL, NULL, NULL, NULL, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 if (fname != NULL)
140 {
141 if (setfname(buf, fname, NULL, TRUE) == OK)
142 buf->b_flags |= BF_NOTEDITED;
143 vim_free(fname);
144 }
145 }
146}
147#endif
148
149/*
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200150 * Ask the user what to do when abandoning a changed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 * Must check 'write' option first!
152 */
153 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100154dialog_changed(
155 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100156 int checkall) // may abandon all changed buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157{
Bram Moolenaard9462e32011-04-11 21:35:11 +0200158 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 int ret;
160 buf_T *buf2;
Bram Moolenaar8218f602012-04-25 17:32:18 +0200161 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Bram Moolenaar3f9a1ff2017-08-21 22:06:02 +0200163 dialog_msg(buff, _("Save changes to \"%s\"?"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 if (checkall)
165 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
166 else
167 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
168
Bram Moolenaar4ca41532019-05-09 21:48:37 +0200169 // Init ea pseudo-structure, this is needed for the check_overwrite()
170 // function.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200171 CLEAR_FIELD(ea);
Bram Moolenaar8218f602012-04-25 17:32:18 +0200172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (ret == VIM_YES)
174 {
175#ifdef FEAT_BROWSE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100176 // May get file name, when there is none
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 browse_save_fname(buf);
178#endif
Bram Moolenaar8218f602012-04-25 17:32:18 +0200179 if (buf->b_fname != NULL && check_overwrite(&ea, buf,
180 buf->b_fname, buf->b_ffname, FALSE) == OK)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100181 // didn't hit Cancel
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 (void)buf_write_all(buf, FALSE);
183 }
184 else if (ret == VIM_NO)
185 {
Bram Moolenaarc024b462019-06-08 18:07:21 +0200186 unchanged(buf, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 }
188 else if (ret == VIM_ALL)
189 {
190 /*
191 * Write all modified files that can be written.
192 * Skip readonly buffers, these need to be confirmed
193 * individually.
194 */
Bram Moolenaar29323592016-07-24 22:04:11 +0200195 FOR_ALL_BUFFERS(buf2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
197 if (bufIsChanged(buf2)
198 && (buf2->b_ffname != NULL
199#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200200 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202 )
203 && !buf2->b_p_ro)
204 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200205 bufref_T bufref;
206
207 set_bufref(&bufref, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#ifdef FEAT_BROWSE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100209 // May get file name, when there is none
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 browse_save_fname(buf2);
211#endif
Bram Moolenaar8218f602012-04-25 17:32:18 +0200212 if (buf2->b_fname != NULL && check_overwrite(&ea, buf2,
213 buf2->b_fname, buf2->b_ffname, FALSE) == OK)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100214 // didn't hit Cancel
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 (void)buf_write_all(buf2, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100216
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100217 // an autocommand may have deleted the buffer
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200218 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 buf2 = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 }
221 }
222 }
223 else if (ret == VIM_DISCARDALL)
224 {
225 /*
226 * mark all buffers as unchanged
227 */
Bram Moolenaar29323592016-07-24 22:04:11 +0200228 FOR_ALL_BUFFERS(buf2)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200229 unchanged(buf2, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
231}
232#endif
233
234/*
235 * Return TRUE if the buffer "buf" can be abandoned, either by making it
236 * hidden, autowriting it or unloading it.
237 */
238 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100239can_abandon(buf_T *buf, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
Bram Moolenaareb44a682017-08-03 22:44:55 +0200241 return ( buf_hide(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 || !bufIsChanged(buf)
243 || buf->b_nwindows > 1
244 || autowrite(buf, forceit) == OK
245 || forceit);
246}
247
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100248/*
249 * Add a buffer number to "bufnrs", unless it's already there.
250 */
251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100252add_bufnum(int *bufnrs, int *bufnump, int nr)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100253{
254 int i;
255
256 for (i = 0; i < *bufnump; ++i)
257 if (bufnrs[i] == nr)
258 return;
259 bufnrs[*bufnump] = nr;
260 *bufnump = *bufnump + 1;
261}
262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263/*
264 * Return TRUE if any buffer was changed and cannot be abandoned.
265 * That changed buffer becomes the current buffer.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100266 * When "unload" is TRUE the current buffer is unloaded instead of making it
Bram Moolenaar027387f2016-01-02 22:25:52 +0100267 * hidden. This is used for ":q!".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 */
269 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100270check_changed_any(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100271 int hidden, // Only check hidden buffers
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100272 int unload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100274 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 buf_T *buf;
276 int save;
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100277 int i;
278 int bufnum = 0;
279 int bufcount = 0;
280 int *bufnrs;
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100281 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100284 // Make a list of all buffers, with the most important ones first.
Bram Moolenaar29323592016-07-24 22:04:11 +0200285 FOR_ALL_BUFFERS(buf)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100286 ++bufcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100288 if (bufcount == 0)
289 return FALSE;
290
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200291 bufnrs = ALLOC_MULT(int, bufcount);
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100292 if (bufnrs == NULL)
293 return FALSE;
294
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100295 // curbuf
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100296 bufnrs[bufnum++] = curbuf->b_fnum;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100297
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100298 // buffers in current tab
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100299 FOR_ALL_WINDOWS(wp)
300 if (wp->w_buffer != curbuf)
301 add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
302
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100303 // buffers in other tabs
Bram Moolenaar29323592016-07-24 22:04:11 +0200304 FOR_ALL_TABPAGES(tp)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100305 if (tp != curtab)
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200306 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100307 add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100308
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100309 // any other buffer
Bram Moolenaar29323592016-07-24 22:04:11 +0200310 FOR_ALL_BUFFERS(buf)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100311 add_bufnum(bufnrs, &bufnum, buf->b_fnum);
312
313 for (i = 0; i < bufnum; ++i)
314 {
315 buf = buflist_findnr(bufnrs[i]);
316 if (buf == NULL)
317 continue;
318 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
319 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200320 bufref_T bufref;
321
322 set_bufref(&bufref, buf);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100323#ifdef FEAT_TERMINAL
324 if (term_job_running(buf->b_term))
325 {
326 if (term_try_stop_job(buf) == FAIL)
327 break;
328 }
329 else
330#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100331 // Try auto-writing the buffer. If this fails but the buffer no
332 // longer exists it's not changed, that's OK.
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100333 if (check_changed(buf, (p_awa ? CCGD_AW : 0)
334 | CCGD_MULTWIN
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200335 | CCGD_ALLBUF) && bufref_valid(&bufref))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100336 break; // didn't save - still changes
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 }
339
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100340 if (i >= bufnum)
341 goto theend;
342
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100343 // Get here if "buf" cannot be abandoned.
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100344 ret = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 exiting = FALSE;
346#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
347 /*
348 * When ":confirm" used, don't give an error message.
349 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200350 if (!(p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#endif
352 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100353 // There must be a wait_return for this message, do_buffer()
354 // may cause a redraw. But wait_return() is a no-op when vgetc()
355 // is busy (Quit used from window menu), then make sure we don't
356 // cause a scroll up.
Bram Moolenaar61660ea2006-04-07 21:40:07 +0000357 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
359 msg_row = cmdline_row;
360 msg_col = 0;
361 msg_didout = FALSE;
362 }
Bram Moolenaareb44a682017-08-03 22:44:55 +0200363 if (
364#ifdef FEAT_TERMINAL
365 term_job_running(buf->b_term)
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000366 ? semsg(_(e_job_still_running_in_buffer_str), buf->b_fname)
Bram Moolenaareb44a682017-08-03 22:44:55 +0200367 :
368#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +0000369 semsg(_(e_no_write_since_last_change_for_buffer_str),
Bram Moolenaare1704ba2012-10-03 18:25:00 +0200370 buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
372 save = no_wait_return;
373 no_wait_return = FALSE;
374 wait_return(FALSE);
375 no_wait_return = save;
376 }
377 }
378
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100379 // Try to find a window that contains the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (buf != curbuf)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100381 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 if (wp->w_buffer == buf)
383 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200384 bufref_T bufref;
385
386 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100387
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100388 goto_tabpage_win(tp, wp);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100389
Bram Moolenaarbdace832019-03-02 10:13:42 +0100390 // Paranoia: did autocmd wipe out the buffer with changes?
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200391 if (!bufref_valid(&bufref))
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100392 goto theend;
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100393 goto buf_found;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 }
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100395buf_found:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100397 // Open the changed buffer in the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (buf != curbuf)
Bram Moolenaar027387f2016-01-02 22:25:52 +0100399 set_curbuf(buf, unload ? DOBUF_UNLOAD : DOBUF_GOTO);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100401theend:
402 vim_free(bufnrs);
403 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404}
405
406/*
407 * return FAIL if there is no file name, OK if there is one
408 * give error message for FAIL
409 */
410 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100411check_fname(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 if (curbuf->b_ffname == NULL)
414 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200415 emsg(_(e_no_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return FAIL;
417 }
418 return OK;
419}
420
421/*
422 * flush the contents of a buffer, unless it has no file name
423 *
424 * return FAIL for failure, OK otherwise
425 */
426 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100427buf_write_all(buf_T *buf, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431
432 retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
433 (linenr_T)1, buf->b_ml.ml_line_count, NULL,
434 FALSE, forceit, TRUE, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 if (curbuf != old_curbuf)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000436 {
Bram Moolenaar8820b482017-03-16 17:23:31 +0100437 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +0100438 msg(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 return retval;
441}
442
443/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200444 * ":argdo", ":windo", ":bufdo", ":tabdo", ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 */
446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100447ex_listdo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 int i;
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000450 win_T *wp;
451 tabpage_T *tp;
Bram Moolenaare25bb902015-02-27 20:33:37 +0100452 buf_T *buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 int next_fnum = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100454#if defined(FEAT_SYN_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 char_u *save_ei = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000457 char_u *p_shm_save;
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200458#ifdef FEAT_QUICKFIX
Bram Moolenaared84b762015-09-09 22:35:29 +0200459 int qf_size = 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200460 int qf_idx;
461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100463#ifndef FEAT_QUICKFIX
464 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
465 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
466 {
467 ex_ni(eap);
468 return;
469 }
470#endif
471
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100472#if defined(FEAT_SYN_HL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000473 if (eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo)
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200474 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 // Don't do syntax HL autocommands. Skipping the syntax file is a
476 // great speed improvement.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000477 save_ei = au_event_disable(",Syntax");
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200478
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200479 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200480 buf->b_flags &= ~BF_SYN_SET;
481 buf = curbuf;
482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483#endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200484#ifdef FEAT_CLIPBOARD
485 start_global_changes();
486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 if (eap->cmdidx == CMD_windo
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000489 || eap->cmdidx == CMD_tabdo
Bram Moolenaareb44a682017-08-03 22:44:55 +0200490 || buf_hide(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100491 || !check_changed(curbuf, CCGD_AW
492 | (eap->forceit ? CCGD_FORCEIT : 0)
493 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 i = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // start at the eap->line1 argument/window/buffer
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000497 wp = firstwin;
498 tp = first_tabpage;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100499 switch (eap->cmdidx)
500 {
Bram Moolenaara162bc52015-01-07 16:54:21 +0100501 case CMD_windo:
502 for ( ; wp != NULL && i + 1 < eap->line1; wp = wp->w_next)
503 i++;
504 break;
505 case CMD_tabdo:
506 for( ; tp != NULL && i + 1 < eap->line1; tp = tp->tp_next)
507 i++;
508 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100509 case CMD_argdo:
510 i = eap->line1 - 1;
511 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100512 default:
513 break;
514 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100515 // set pcmark now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (eap->cmdidx == CMD_bufdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200517 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100518 // Advance to the first listed buffer after "eap->line1".
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200519 for (buf = firstbuf; buf != NULL && (buf->b_fnum < eap->line1
Bram Moolenaare25bb902015-02-27 20:33:37 +0100520 || !buf->b_p_bl); buf = buf->b_next)
521 if (buf->b_fnum > eap->line2)
522 {
523 buf = NULL;
524 break;
525 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200526 if (buf != NULL)
Bram Moolenaare25bb902015-02-27 20:33:37 +0100527 goto_buffer(eap, DOBUF_FIRST, FORWARD, buf->b_fnum);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200528 }
529#ifdef FEAT_QUICKFIX
530 else if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
531 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
532 {
Bram Moolenaar25190db2019-05-04 15:05:28 +0200533 qf_size = qf_get_valid_size(eap);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200534 if (qf_size <= 0 || eap->line1 > qf_size)
535 buf = NULL;
536 else
537 {
538 ex_cc(eap);
539
540 buf = curbuf;
541 i = eap->line1 - 1;
542 if (eap->addr_count <= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 // default is all the quickfix/location list entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200544 eap->line2 = qf_size;
545 }
546 }
547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 else
549 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100550 listcmd_busy = TRUE; // avoids setting pcmark below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaare25bb902015-02-27 20:33:37 +0100552 while (!got_int && buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
554 if (eap->cmdidx == CMD_argdo)
555 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100556 // go to argument "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (i == ARGCOUNT)
558 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100559 // Don't call do_argfile() when already there, it will try
560 // reloading the file.
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000561 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100563 // Clear 'shm' to avoid that the file message overwrites
564 // any output from the command.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000565 p_shm_save = vim_strsave(p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100566 set_option_value_give_err((char_u *)"shm",
567 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 do_argfile(eap, i);
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100569 set_option_value_give_err((char_u *)"shm",
570 0L, p_shm_save, 0);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000571 vim_free(p_shm_save);
572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (curwin->w_arg_idx != i)
574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 else if (eap->cmdidx == CMD_windo)
577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100578 // go to window "wp"
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000579 if (!win_valid(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 break;
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000581 win_goto(wp);
Bram Moolenaar41423242007-05-03 20:11:13 +0000582 if (curwin != wp)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100583 break; // something must be wrong
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000584 wp = curwin->w_next;
585 }
586 else if (eap->cmdidx == CMD_tabdo)
587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100588 // go to window "tp"
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000589 if (!valid_tabpage(tp))
590 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200591 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000592 tp = tp->tp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 else if (eap->cmdidx == CMD_bufdo)
595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100596 // Remember the number of the next listed buffer, in case
597 // ":bwipe" is used or autocommands do something strange.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 next_fnum = -1;
599 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
600 if (buf->b_p_bl)
601 {
602 next_fnum = buf->b_fnum;
603 break;
604 }
605 }
606
Bram Moolenaara162bc52015-01-07 16:54:21 +0100607 ++i;
608
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100609 // execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 do_cmdline(eap->arg, eap->getline, eap->cookie,
611 DOCMD_VERBOSE + DOCMD_NOWAIT);
612
613 if (eap->cmdidx == CMD_bufdo)
614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100615 // Done?
Bram Moolenaara162bc52015-01-07 16:54:21 +0100616 if (next_fnum < 0 || next_fnum > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100618 // Check if the buffer still exists.
Bram Moolenaar29323592016-07-24 22:04:11 +0200619 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (buf->b_fnum == next_fnum)
621 break;
622 if (buf == NULL)
623 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000624
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 // Go to the next buffer. Clear 'shm' to avoid that the file
626 // message overwrites any output from the command.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000627 p_shm_save = vim_strsave(p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100628 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100630 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000631 vim_free(p_shm_save);
632
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 // If autocommands took us elsewhere, quit here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (curbuf->b_fnum != next_fnum)
635 break;
636 }
637
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200638#ifdef FEAT_QUICKFIX
639 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
640 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
641 {
642 if (i >= qf_size || i >= eap->line2)
643 break;
644
645 qf_idx = qf_get_cur_idx(eap);
646
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200647 // Clear 'shm' to avoid that the file message overwrites
648 // any output from the command.
649 p_shm_save = vim_strsave(p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100650 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200651 ex_cnext(eap);
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100652 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200653 vim_free(p_shm_save);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // If jumping to the next quickfix entry fails, quit here
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200656 if (qf_get_cur_idx(eap) == qf_idx)
657 break;
658 }
659#endif
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (eap->cmdidx == CMD_windo)
662 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100663 validate_cursor(); // cursor may have moved
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // required when 'scrollbind' has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (curwin->w_p_scb)
667 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
Bram Moolenaara162bc52015-01-07 16:54:21 +0100669
Bram Moolenaara162bc52015-01-07 16:54:21 +0100670 if (eap->cmdidx == CMD_windo || eap->cmdidx == CMD_tabdo)
671 if (i+1 > eap->line2)
672 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100673 if (eap->cmdidx == CMD_argdo && i >= eap->line2)
674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
676 listcmd_busy = FALSE;
677 }
678
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100679#if defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000680 if (save_ei != NULL)
681 {
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200682 buf_T *bnext;
683 aco_save_T aco;
684
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000685 au_event_restore(save_ei);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200686
687 for (buf = firstbuf; buf != NULL; buf = bnext)
688 {
689 bnext = buf->b_next;
690 if (buf->b_nwindows > 0 && (buf->b_flags & BF_SYN_SET))
691 {
692 buf->b_flags &= ~BF_SYN_SET;
693
694 // buffer was opened while Syntax autocommands were disabled,
695 // need to trigger them now.
696 if (buf == curbuf)
697 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000698 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200699 else
700 {
701 aucmd_prepbuf(&aco, buf);
702 apply_autocmds(EVENT_SYNTAX, buf->b_p_syn,
703 buf->b_fname, TRUE, buf);
704 aucmd_restbuf(&aco);
705 }
706
707 // start over, in case autocommands messed things up.
708 bnext = firstbuf;
709 }
710 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200713#ifdef FEAT_CLIPBOARD
714 end_global_changes();
715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716}
717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_EVAL
719/*
720 * ":compiler[!] {name}"
721 */
722 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100723ex_compiler(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 char_u *buf;
726 char_u *old_cur_comp = NULL;
727 char_u *p;
728
729 if (*eap->arg == NUL)
730 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100731 // List all compiler scripts.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100733 // ) keep the indenter happy...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 else
736 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200737 buf = alloc(STRLEN(eap->arg) + 14);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (buf != NULL)
739 {
740 if (eap->forceit)
741 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100742 // ":compiler! {name}" sets global options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 do_cmdline_cmd((char_u *)
744 "command -nargs=* CompilerSet set <args>");
745 }
746 else
747 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100748 // ":compiler! {name}" sets local options.
749 // To remain backwards compatible "current_compiler" is always
750 // used. A user's compiler plugin may set it, the distributed
751 // plugin will then skip the settings. Afterwards set
752 // "b:current_compiler" and restore "current_compiler".
753 // Explicitly prepend "g:" to make it work in a function.
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100754 old_cur_comp = get_var_value((char_u *)"g:current_compiler");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (old_cur_comp != NULL)
756 old_cur_comp = vim_strsave(old_cur_comp);
757 do_cmdline_cmd((char_u *)
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000758 "command -nargs=* -keepscript CompilerSet setlocal <args>");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100760 do_unlet((char_u *)"g:current_compiler", TRUE);
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000761 do_unlet((char_u *)"b:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763 sprintf((char *)buf, "compiler/%s.vim", eap->arg);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100764 if (source_runtime(buf, DIP_ALL) == FAIL)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000765 semsg(_(e_compiler_not_supported_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 vim_free(buf);
767
768 do_cmdline_cmd((char_u *)":delcommand CompilerSet");
769
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100770 // Set "b:current_compiler" from "current_compiler".
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100771 p = get_var_value((char_u *)"g:current_compiler");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (p != NULL)
773 set_internal_string_var((char_u *)"b:current_compiler", p);
774
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100775 // Restore "current_compiler" for ":compiler {name}".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (!eap->forceit)
777 {
778 if (old_cur_comp != NULL)
779 {
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100780 set_internal_string_var((char_u *)"g:current_compiler",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 old_cur_comp);
782 vim_free(old_cur_comp);
783 }
784 else
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100785 do_unlet((char_u *)"g:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 }
788 }
789}
790#endif
791
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100792#if defined(FEAT_PYTHON3) || defined(FEAT_PYTHON) || defined(PROTO)
793
794# if (defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
795/*
796 * Detect Python 3 or 2, and initialize 'pyxversion'.
797 */
798 void
799init_pyxversion(void)
800{
801 if (p_pyx == 0)
802 {
803 if (python3_enabled(FALSE))
804 p_pyx = 3;
805 else if (python_enabled(FALSE))
806 p_pyx = 2;
807 }
808}
809# endif
810
811/*
812 * Does a file contain one of the following strings at the beginning of any
813 * line?
814 * "#!(any string)python2" => returns 2
815 * "#!(any string)python3" => returns 3
816 * "# requires python 2.x" => returns 2
817 * "# requires python 3.x" => returns 3
818 * otherwise return 0.
819 */
820 static int
821requires_py_version(char_u *filename)
822{
823 FILE *file;
824 int requires_py_version = 0;
825 int i, lines;
826
827 lines = (int)p_mls;
828 if (lines < 0)
829 lines = 5;
830
831 file = mch_fopen((char *)filename, "r");
832 if (file != NULL)
833 {
834 for (i = 0; i < lines; i++)
835 {
836 if (vim_fgets(IObuff, IOSIZE, file))
837 break;
838 if (i == 0 && IObuff[0] == '#' && IObuff[1] == '!')
839 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Check shebang.
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100841 if (strstr((char *)IObuff + 2, "python2") != NULL)
842 {
843 requires_py_version = 2;
844 break;
845 }
846 if (strstr((char *)IObuff + 2, "python3") != NULL)
847 {
848 requires_py_version = 3;
849 break;
850 }
851 }
852 IObuff[21] = '\0';
853 if (STRCMP("# requires python 2.x", IObuff) == 0)
854 {
855 requires_py_version = 2;
856 break;
857 }
858 if (STRCMP("# requires python 3.x", IObuff) == 0)
859 {
860 requires_py_version = 3;
861 break;
862 }
863 }
864 fclose(file);
865 }
866 return requires_py_version;
867}
868
869
870/*
871 * Source a python file using the requested python version.
872 */
873 static void
874source_pyx_file(exarg_T *eap, char_u *fname)
875{
876 exarg_T ex;
877 int v = requires_py_version(fname);
878
879# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
880 init_pyxversion();
881# endif
882 if (v == 0)
883 {
884# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100885 // user didn't choose a preference, 'pyx' is used
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100886 v = p_pyx;
887# elif defined(FEAT_PYTHON)
888 v = 2;
889# elif defined(FEAT_PYTHON3)
890 v = 3;
891# endif
892 }
893
894 /*
895 * now source, if required python version is not supported show
896 * unobtrusive message.
897 */
898 if (eap == NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +0200899 CLEAR_FIELD(ex);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100900 else
901 ex = *eap;
902 ex.arg = fname;
903 ex.cmd = (char_u *)(v == 2 ? "pyfile" : "pyfile3");
904
905 if (v == 2)
906 {
907# ifdef FEAT_PYTHON
908 ex_pyfile(&ex);
909# else
910 vim_snprintf((char *)IObuff, IOSIZE,
911 _("W20: Required python version 2.x not supported, ignoring file: %s"),
912 fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100913 msg((char *)IObuff);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100914# endif
915 return;
916 }
917 else
918 {
919# ifdef FEAT_PYTHON3
920 ex_py3file(&ex);
921# else
922 vim_snprintf((char *)IObuff, IOSIZE,
923 _("W21: Required python version 3.x not supported, ignoring file: %s"),
924 fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100925 msg((char *)IObuff);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100926# endif
927 return;
928 }
929}
930
931/*
932 * ":pyxfile {fname}"
933 */
934 void
935ex_pyxfile(exarg_T *eap)
936{
937 source_pyx_file(eap, eap->arg);
938}
939
940/*
941 * ":pyx"
942 */
943 void
944ex_pyx(exarg_T *eap)
945{
946# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
947 init_pyxversion();
948 if (p_pyx == 2)
949 ex_python(eap);
950 else
951 ex_py3(eap);
952# elif defined(FEAT_PYTHON)
953 ex_python(eap);
954# elif defined(FEAT_PYTHON3)
955 ex_py3(eap);
956# endif
957}
958
959/*
960 * ":pyxdo"
961 */
962 void
963ex_pyxdo(exarg_T *eap)
964{
965# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
966 init_pyxversion();
967 if (p_pyx == 2)
968 ex_pydo(eap);
969 else
970 ex_py3do(eap);
971# elif defined(FEAT_PYTHON)
972 ex_pydo(eap);
973# elif defined(FEAT_PYTHON3)
974 ex_py3do(eap);
975# endif
976}
977
978#endif
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 * ":checktime [buffer]"
982 */
983 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100984ex_checktime(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 buf_T *buf;
987 int save_no_check_timestamps = no_check_timestamps;
988
989 no_check_timestamps = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100990 if (eap->addr_count == 0) // default is all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 check_timestamps(FALSE);
992 else
993 {
994 buf = buflist_findnr((int)eap->line2);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100995 if (buf != NULL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 (void)buf_check_timestamp(buf, FALSE);
997 }
998 no_check_timestamps = save_no_check_timestamps;
999}