blob: db82277aca197beb0d3599a32f3907082c01a87d [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);
566 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 do_argfile(eap, i);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000568 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
569 vim_free(p_shm_save);
570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (curwin->w_arg_idx != i)
572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 else if (eap->cmdidx == CMD_windo)
575 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100576 // go to window "wp"
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000577 if (!win_valid(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 break;
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000579 win_goto(wp);
Bram Moolenaar41423242007-05-03 20:11:13 +0000580 if (curwin != wp)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100581 break; // something must be wrong
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000582 wp = curwin->w_next;
583 }
584 else if (eap->cmdidx == CMD_tabdo)
585 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100586 // go to window "tp"
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000587 if (!valid_tabpage(tp))
588 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200589 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000590 tp = tp->tp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 else if (eap->cmdidx == CMD_bufdo)
593 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100594 // Remember the number of the next listed buffer, in case
595 // ":bwipe" is used or autocommands do something strange.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 next_fnum = -1;
597 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
598 if (buf->b_p_bl)
599 {
600 next_fnum = buf->b_fnum;
601 break;
602 }
603 }
604
Bram Moolenaara162bc52015-01-07 16:54:21 +0100605 ++i;
606
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100607 // execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 do_cmdline(eap->arg, eap->getline, eap->cookie,
609 DOCMD_VERBOSE + DOCMD_NOWAIT);
610
611 if (eap->cmdidx == CMD_bufdo)
612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100613 // Done?
Bram Moolenaara162bc52015-01-07 16:54:21 +0100614 if (next_fnum < 0 || next_fnum > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 // Check if the buffer still exists.
Bram Moolenaar29323592016-07-24 22:04:11 +0200617 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 if (buf->b_fnum == next_fnum)
619 break;
620 if (buf == NULL)
621 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000622
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100623 // Go to the next buffer. Clear 'shm' to avoid that the file
624 // message overwrites any output from the command.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000625 p_shm_save = vim_strsave(p_shm);
626 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000628 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
629 vim_free(p_shm_save);
630
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 // If autocommands took us elsewhere, quit here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (curbuf->b_fnum != next_fnum)
633 break;
634 }
635
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200636#ifdef FEAT_QUICKFIX
637 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
638 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
639 {
640 if (i >= qf_size || i >= eap->line2)
641 break;
642
643 qf_idx = qf_get_cur_idx(eap);
644
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200645 // Clear 'shm' to avoid that the file message overwrites
646 // any output from the command.
647 p_shm_save = vim_strsave(p_shm);
648 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200649 ex_cnext(eap);
Bram Moolenaar14798ab2020-05-28 21:30:11 +0200650 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
651 vim_free(p_shm_save);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200652
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 // If jumping to the next quickfix entry fails, quit here
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200654 if (qf_get_cur_idx(eap) == qf_idx)
655 break;
656 }
657#endif
658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (eap->cmdidx == CMD_windo)
660 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100661 validate_cursor(); // cursor may have moved
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100662
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100663 // required when 'scrollbind' has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (curwin->w_p_scb)
665 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
Bram Moolenaara162bc52015-01-07 16:54:21 +0100667
Bram Moolenaara162bc52015-01-07 16:54:21 +0100668 if (eap->cmdidx == CMD_windo || eap->cmdidx == CMD_tabdo)
669 if (i+1 > eap->line2)
670 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100671 if (eap->cmdidx == CMD_argdo && i >= eap->line2)
672 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674 listcmd_busy = FALSE;
675 }
676
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100677#if defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000678 if (save_ei != NULL)
679 {
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200680 buf_T *bnext;
681 aco_save_T aco;
682
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000683 au_event_restore(save_ei);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200684
685 for (buf = firstbuf; buf != NULL; buf = bnext)
686 {
687 bnext = buf->b_next;
688 if (buf->b_nwindows > 0 && (buf->b_flags & BF_SYN_SET))
689 {
690 buf->b_flags &= ~BF_SYN_SET;
691
692 // buffer was opened while Syntax autocommands were disabled,
693 // need to trigger them now.
694 if (buf == curbuf)
695 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000696 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200697 else
698 {
699 aucmd_prepbuf(&aco, buf);
700 apply_autocmds(EVENT_SYNTAX, buf->b_p_syn,
701 buf->b_fname, TRUE, buf);
702 aucmd_restbuf(&aco);
703 }
704
705 // start over, in case autocommands messed things up.
706 bnext = firstbuf;
707 }
708 }
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200711#ifdef FEAT_CLIPBOARD
712 end_global_changes();
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714}
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_EVAL
717/*
718 * ":compiler[!] {name}"
719 */
720 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100721ex_compiler(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 char_u *buf;
724 char_u *old_cur_comp = NULL;
725 char_u *p;
726
727 if (*eap->arg == NUL)
728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100729 // List all compiler scripts.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100731 // ) keep the indenter happy...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 else
734 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200735 buf = alloc(STRLEN(eap->arg) + 14);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 if (buf != NULL)
737 {
738 if (eap->forceit)
739 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100740 // ":compiler! {name}" sets global options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 do_cmdline_cmd((char_u *)
742 "command -nargs=* CompilerSet set <args>");
743 }
744 else
745 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100746 // ":compiler! {name}" sets local options.
747 // To remain backwards compatible "current_compiler" is always
748 // used. A user's compiler plugin may set it, the distributed
749 // plugin will then skip the settings. Afterwards set
750 // "b:current_compiler" and restore "current_compiler".
751 // Explicitly prepend "g:" to make it work in a function.
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100752 old_cur_comp = get_var_value((char_u *)"g:current_compiler");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (old_cur_comp != NULL)
754 old_cur_comp = vim_strsave(old_cur_comp);
755 do_cmdline_cmd((char_u *)
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000756 "command -nargs=* -keepscript CompilerSet setlocal <args>");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100758 do_unlet((char_u *)"g:current_compiler", TRUE);
Bram Moolenaar2ce06f62005-01-31 19:19:04 +0000759 do_unlet((char_u *)"b:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
761 sprintf((char *)buf, "compiler/%s.vim", eap->arg);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100762 if (source_runtime(buf, DIP_ALL) == FAIL)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000763 semsg(_(e_compiler_not_supported_str), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 vim_free(buf);
765
766 do_cmdline_cmd((char_u *)":delcommand CompilerSet");
767
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100768 // Set "b:current_compiler" from "current_compiler".
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100769 p = get_var_value((char_u *)"g:current_compiler");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (p != NULL)
771 set_internal_string_var((char_u *)"b:current_compiler", p);
772
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100773 // Restore "current_compiler" for ":compiler {name}".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (!eap->forceit)
775 {
776 if (old_cur_comp != NULL)
777 {
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100778 set_internal_string_var((char_u *)"g:current_compiler",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 old_cur_comp);
780 vim_free(old_cur_comp);
781 }
782 else
Bram Moolenaar3d63e3f2010-01-19 16:13:50 +0100783 do_unlet((char_u *)"g:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 }
786 }
787}
788#endif
789
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100790#if defined(FEAT_PYTHON3) || defined(FEAT_PYTHON) || defined(PROTO)
791
792# if (defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
793/*
794 * Detect Python 3 or 2, and initialize 'pyxversion'.
795 */
796 void
797init_pyxversion(void)
798{
799 if (p_pyx == 0)
800 {
801 if (python3_enabled(FALSE))
802 p_pyx = 3;
803 else if (python_enabled(FALSE))
804 p_pyx = 2;
805 }
806}
807# endif
808
809/*
810 * Does a file contain one of the following strings at the beginning of any
811 * line?
812 * "#!(any string)python2" => returns 2
813 * "#!(any string)python3" => returns 3
814 * "# requires python 2.x" => returns 2
815 * "# requires python 3.x" => returns 3
816 * otherwise return 0.
817 */
818 static int
819requires_py_version(char_u *filename)
820{
821 FILE *file;
822 int requires_py_version = 0;
823 int i, lines;
824
825 lines = (int)p_mls;
826 if (lines < 0)
827 lines = 5;
828
829 file = mch_fopen((char *)filename, "r");
830 if (file != NULL)
831 {
832 for (i = 0; i < lines; i++)
833 {
834 if (vim_fgets(IObuff, IOSIZE, file))
835 break;
836 if (i == 0 && IObuff[0] == '#' && IObuff[1] == '!')
837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Check shebang.
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100839 if (strstr((char *)IObuff + 2, "python2") != NULL)
840 {
841 requires_py_version = 2;
842 break;
843 }
844 if (strstr((char *)IObuff + 2, "python3") != NULL)
845 {
846 requires_py_version = 3;
847 break;
848 }
849 }
850 IObuff[21] = '\0';
851 if (STRCMP("# requires python 2.x", IObuff) == 0)
852 {
853 requires_py_version = 2;
854 break;
855 }
856 if (STRCMP("# requires python 3.x", IObuff) == 0)
857 {
858 requires_py_version = 3;
859 break;
860 }
861 }
862 fclose(file);
863 }
864 return requires_py_version;
865}
866
867
868/*
869 * Source a python file using the requested python version.
870 */
871 static void
872source_pyx_file(exarg_T *eap, char_u *fname)
873{
874 exarg_T ex;
875 int v = requires_py_version(fname);
876
877# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
878 init_pyxversion();
879# endif
880 if (v == 0)
881 {
882# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100883 // user didn't choose a preference, 'pyx' is used
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100884 v = p_pyx;
885# elif defined(FEAT_PYTHON)
886 v = 2;
887# elif defined(FEAT_PYTHON3)
888 v = 3;
889# endif
890 }
891
892 /*
893 * now source, if required python version is not supported show
894 * unobtrusive message.
895 */
896 if (eap == NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +0200897 CLEAR_FIELD(ex);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100898 else
899 ex = *eap;
900 ex.arg = fname;
901 ex.cmd = (char_u *)(v == 2 ? "pyfile" : "pyfile3");
902
903 if (v == 2)
904 {
905# ifdef FEAT_PYTHON
906 ex_pyfile(&ex);
907# else
908 vim_snprintf((char *)IObuff, IOSIZE,
909 _("W20: Required python version 2.x not supported, ignoring file: %s"),
910 fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100911 msg((char *)IObuff);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100912# endif
913 return;
914 }
915 else
916 {
917# ifdef FEAT_PYTHON3
918 ex_py3file(&ex);
919# else
920 vim_snprintf((char *)IObuff, IOSIZE,
921 _("W21: Required python version 3.x not supported, ignoring file: %s"),
922 fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100923 msg((char *)IObuff);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100924# endif
925 return;
926 }
927}
928
929/*
930 * ":pyxfile {fname}"
931 */
932 void
933ex_pyxfile(exarg_T *eap)
934{
935 source_pyx_file(eap, eap->arg);
936}
937
938/*
939 * ":pyx"
940 */
941 void
942ex_pyx(exarg_T *eap)
943{
944# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
945 init_pyxversion();
946 if (p_pyx == 2)
947 ex_python(eap);
948 else
949 ex_py3(eap);
950# elif defined(FEAT_PYTHON)
951 ex_python(eap);
952# elif defined(FEAT_PYTHON3)
953 ex_py3(eap);
954# endif
955}
956
957/*
958 * ":pyxdo"
959 */
960 void
961ex_pyxdo(exarg_T *eap)
962{
963# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
964 init_pyxversion();
965 if (p_pyx == 2)
966 ex_pydo(eap);
967 else
968 ex_py3do(eap);
969# elif defined(FEAT_PYTHON)
970 ex_pydo(eap);
971# elif defined(FEAT_PYTHON3)
972 ex_py3do(eap);
973# endif
974}
975
976#endif
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * ":checktime [buffer]"
980 */
981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100982ex_checktime(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 buf_T *buf;
985 int save_no_check_timestamps = no_check_timestamps;
986
987 no_check_timestamps = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100988 if (eap->addr_count == 0) // default is all buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 check_timestamps(FALSE);
990 else
991 {
992 buf = buflist_findnr((int)eap->line2);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100993 if (buf != NULL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 (void)buf_check_timestamp(buf, FALSE);
995 }
996 no_check_timestamps = save_no_check_timestamps;
997}