blob: 3bdbede1381bd4abef003bb8ff78318705b62f98 [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
Bram Moolenaar217e1b82019-12-01 21:41:28 +010030 // never autowrite a "nofile" or "nowrite" buffer
Bram Moolenaar373154b2007-02-13 05:19:30 +000031 || bt_dontwrite(buf)
Bram Moolenaar373154b2007-02-13 05:19:30 +000032 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 return FAIL;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020034 set_bufref(&bufref, buf);
Bram Moolenaar373154b2007-02-13 05:19:30 +000035 r = buf_write_all(buf, forceit);
36
Bram Moolenaar217e1b82019-12-01 21:41:28 +010037 // Writing may succeed but the buffer still changed, e.g., when there is a
38 // conversion error. We do want to return FAIL then.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020039 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar373154b2007-02-13 05:19:30 +000040 r = FAIL;
41 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000042}
43
44/*
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +020045 * Flush all buffers, except the ones that are readonly or are never written.
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 */
47 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010048autowrite_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000049{
50 buf_T *buf;
51
52 if (!(p_aw || p_awa) || !p_write)
53 return;
Bram Moolenaar29323592016-07-24 22:04:11 +020054 FOR_ALL_BUFFERS(buf)
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +020055 if (bufIsChanged(buf) && !buf->b_p_ro && !bt_dontwrite(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020057 bufref_T bufref;
58
59 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010060
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 (void)buf_write_all(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010062
Bram Moolenaar217e1b82019-12-01 21:41:28 +010063 // an autocommand may have deleted the buffer
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020064 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 }
67}
68
69/*
Bram Moolenaar45d3b142013-11-09 03:31:51 +010070 * Return TRUE if buffer was changed and cannot be abandoned.
71 * For flags use the CCGD_ values.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010074check_changed(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020076 int forceit = (flags & CCGD_FORCEIT);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020077 bufref_T bufref;
78
79 set_bufref(&bufref, buf);
Bram Moolenaar45d3b142013-11-09 03:31:51 +010080
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 if ( !forceit
82 && bufIsChanged(buf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +010083 && ((flags & CCGD_MULTWIN) || buf->b_nwindows <= 1)
84 && (!(flags & CCGD_AW) || autowrite(buf, forceit) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 {
86#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +020087 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +010089# ifdef FEAT_TERMINAL
90 if (term_job_running(buf->b_term))
91 {
92 return term_confirm_stop(buf) == FAIL;
93 }
94# endif
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 buf_T *buf2;
97 int count = 0;
98
Bram Moolenaar45d3b142013-11-09 03:31:51 +010099 if (flags & CCGD_ALLBUF)
Bram Moolenaar29323592016-07-24 22:04:11 +0200100 FOR_ALL_BUFFERS(buf2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 if (bufIsChanged(buf2)
102 && (buf2->b_ffname != NULL
103# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200104 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# endif
106 ))
107 ++count;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200108 if (!bufref_valid(&bufref))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100109 // Autocommand deleted buffer, oops! It's not changed now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100111
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 dialog_changed(buf, count > 1);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100113
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200114 if (!bufref_valid(&bufref))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100115 // Autocommand deleted buffer, oops! It's not changed now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 return bufIsChanged(buf);
118 }
119#endif
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100120 if (flags & CCGD_EXCMD)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200121 no_write_message();
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100122 else
Bram Moolenaar7a760922018-02-19 23:10:02 +0100123 no_write_message_nobang(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 return TRUE;
125 }
126 return FALSE;
127}
128
129#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
130
131#if defined(FEAT_BROWSE) || defined(PROTO)
132/*
133 * When wanting to write a file without a file name, ask the user for a name.
134 */
135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100136browse_save_fname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137{
138 if (buf->b_fname == NULL)
139 {
140 char_u *fname;
141
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000142 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
143 NULL, NULL, NULL, NULL, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 if (fname != NULL)
145 {
146 if (setfname(buf, fname, NULL, TRUE) == OK)
147 buf->b_flags |= BF_NOTEDITED;
148 vim_free(fname);
149 }
150 }
151}
152#endif
153
154/*
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200155 * Ask the user what to do when abandoning a changed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 * Must check 'write' option first!
157 */
158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100159dialog_changed(
160 buf_T *buf,
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100161 int checkall) // may abandon all changed buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162{
Bram Moolenaard9462e32011-04-11 21:35:11 +0200163 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 int ret;
165 buf_T *buf2;
Bram Moolenaar8218f602012-04-25 17:32:18 +0200166 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
Bram Moolenaar3f9a1ff2017-08-21 22:06:02 +0200168 dialog_msg(buff, _("Save changes to \"%s\"?"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 if (checkall)
170 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
171 else
172 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
173
Bram Moolenaar4ca41532019-05-09 21:48:37 +0200174 // Init ea pseudo-structure, this is needed for the check_overwrite()
175 // function.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200176 CLEAR_FIELD(ea);
Bram Moolenaar8218f602012-04-25 17:32:18 +0200177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 if (ret == VIM_YES)
179 {
180#ifdef FEAT_BROWSE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100181 // May get file name, when there is none
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 browse_save_fname(buf);
183#endif
Bram Moolenaar8218f602012-04-25 17:32:18 +0200184 if (buf->b_fname != NULL && check_overwrite(&ea, buf,
185 buf->b_fname, buf->b_ffname, FALSE) == OK)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100186 // didn't hit Cancel
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 (void)buf_write_all(buf, FALSE);
188 }
189 else if (ret == VIM_NO)
190 {
Bram Moolenaarc024b462019-06-08 18:07:21 +0200191 unchanged(buf, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 }
193 else if (ret == VIM_ALL)
194 {
195 /*
196 * Write all modified files that can be written.
197 * Skip readonly buffers, these need to be confirmed
198 * individually.
199 */
Bram Moolenaar29323592016-07-24 22:04:11 +0200200 FOR_ALL_BUFFERS(buf2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {
202 if (bufIsChanged(buf2)
203 && (buf2->b_ffname != NULL
204#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +0200205 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#endif
207 )
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100208 && !bt_dontwrite(buf2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 && !buf2->b_p_ro)
210 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200211 bufref_T bufref;
212
213 set_bufref(&bufref, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_BROWSE
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100215 // May get file name, when there is none
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 browse_save_fname(buf2);
217#endif
Bram Moolenaar8218f602012-04-25 17:32:18 +0200218 if (buf2->b_fname != NULL && check_overwrite(&ea, buf2,
219 buf2->b_fname, buf2->b_ffname, FALSE) == OK)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100220 // didn't hit Cancel
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 (void)buf_write_all(buf2, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100222
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100223 // an autocommand may have deleted the buffer
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200224 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 buf2 = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 }
227 }
228 }
229 else if (ret == VIM_DISCARDALL)
230 {
231 /*
232 * mark all buffers as unchanged
233 */
Bram Moolenaar29323592016-07-24 22:04:11 +0200234 FOR_ALL_BUFFERS(buf2)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200235 unchanged(buf2, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237}
238#endif
239
240/*
241 * Return TRUE if the buffer "buf" can be abandoned, either by making it
242 * hidden, autowriting it or unloading it.
243 */
244 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100245can_abandon(buf_T *buf, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
Bram Moolenaareb44a682017-08-03 22:44:55 +0200247 return ( buf_hide(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 || !bufIsChanged(buf)
249 || buf->b_nwindows > 1
250 || autowrite(buf, forceit) == OK
251 || forceit);
252}
253
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100254/*
255 * Add a buffer number to "bufnrs", unless it's already there.
256 */
257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100258add_bufnum(int *bufnrs, int *bufnump, int nr)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100259{
260 int i;
261
262 for (i = 0; i < *bufnump; ++i)
263 if (bufnrs[i] == nr)
264 return;
265 bufnrs[*bufnump] = nr;
266 *bufnump = *bufnump + 1;
267}
268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269/*
270 * Return TRUE if any buffer was changed and cannot be abandoned.
271 * That changed buffer becomes the current buffer.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100272 * When "unload" is TRUE the current buffer is unloaded instead of making it
Bram Moolenaar027387f2016-01-02 22:25:52 +0100273 * hidden. This is used for ":q!".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 */
275 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100276check_changed_any(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100277 int hidden, // Only check hidden buffers
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100278 int unload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100280 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 buf_T *buf;
282 int save;
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100283 int i;
284 int bufnum = 0;
285 int bufcount = 0;
286 int *bufnrs;
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100287 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100290 // Make a list of all buffers, with the most important ones first.
Bram Moolenaar29323592016-07-24 22:04:11 +0200291 FOR_ALL_BUFFERS(buf)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100292 ++bufcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100294 if (bufcount == 0)
295 return FALSE;
296
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200297 bufnrs = ALLOC_MULT(int, bufcount);
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100298 if (bufnrs == NULL)
299 return FALSE;
300
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100301 // curbuf
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100302 bufnrs[bufnum++] = curbuf->b_fnum;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100303
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100304 // buffers in current tab
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100305 FOR_ALL_WINDOWS(wp)
306 if (wp->w_buffer != curbuf)
307 add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
308
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100309 // buffers in other tabs
Bram Moolenaar29323592016-07-24 22:04:11 +0200310 FOR_ALL_TABPAGES(tp)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100311 if (tp != curtab)
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200312 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100313 add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100314
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100315 // any other buffer
Bram Moolenaar29323592016-07-24 22:04:11 +0200316 FOR_ALL_BUFFERS(buf)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100317 add_bufnum(bufnrs, &bufnum, buf->b_fnum);
318
319 for (i = 0; i < bufnum; ++i)
320 {
321 buf = buflist_findnr(bufnrs[i]);
322 if (buf == NULL)
323 continue;
324 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
325 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200326 bufref_T bufref;
327
328 set_bufref(&bufref, buf);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100329#ifdef FEAT_TERMINAL
330 if (term_job_running(buf->b_term))
331 {
332 if (term_try_stop_job(buf) == FAIL)
333 break;
334 }
335 else
336#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100337 // Try auto-writing the buffer. If this fails but the buffer no
338 // longer exists it's not changed, that's OK.
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100339 if (check_changed(buf, (p_awa ? CCGD_AW : 0)
340 | CCGD_MULTWIN
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200341 | CCGD_ALLBUF) && bufref_valid(&bufref))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100342 break; // didn't save - still changes
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 }
345
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100346 if (i >= bufnum)
347 goto theend;
348
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100349 // Get here if "buf" cannot be abandoned.
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100350 ret = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 exiting = FALSE;
352#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
353 /*
354 * When ":confirm" used, don't give an error message.
355 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200356 if (!(p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#endif
358 {
Bram Moolenaar13608d82022-08-29 15:06:50 +0100359 // There must be a wait_return() for this message, do_buffer()
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100360 // may cause a redraw. But wait_return() is a no-op when vgetc()
361 // is busy (Quit used from window menu), then make sure we don't
362 // cause a scroll up.
Bram Moolenaar61660ea2006-04-07 21:40:07 +0000363 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {
365 msg_row = cmdline_row;
366 msg_col = 0;
367 msg_didout = FALSE;
368 }
Bram Moolenaareb44a682017-08-03 22:44:55 +0200369 if (
370#ifdef FEAT_TERMINAL
371 term_job_running(buf->b_term)
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000372 ? semsg(_(e_job_still_running_in_buffer_str), buf->b_fname)
Bram Moolenaareb44a682017-08-03 22:44:55 +0200373 :
374#endif
Bram Moolenaar1a992222021-12-31 17:25:48 +0000375 semsg(_(e_no_write_since_last_change_for_buffer_str),
Bram Moolenaare1704ba2012-10-03 18:25:00 +0200376 buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {
378 save = no_wait_return;
379 no_wait_return = FALSE;
380 wait_return(FALSE);
381 no_wait_return = save;
382 }
383 }
384
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385 // Try to find a window that contains the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (buf != curbuf)
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100387 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (wp->w_buffer == buf)
389 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200390 bufref_T bufref;
391
392 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100393
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100394 goto_tabpage_win(tp, wp);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100395
Bram Moolenaarbdace832019-03-02 10:13:42 +0100396 // Paranoia: did autocmd wipe out the buffer with changes?
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200397 if (!bufref_valid(&bufref))
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100398 goto theend;
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100399 goto buf_found;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100401buf_found:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 // Open the changed buffer in the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 if (buf != curbuf)
Bram Moolenaar027387f2016-01-02 22:25:52 +0100405 set_curbuf(buf, unload ? DOBUF_UNLOAD : DOBUF_GOTO);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
Bram Moolenaar970a1b82012-03-23 18:39:18 +0100407theend:
408 vim_free(bufnrs);
409 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410}
411
412/*
413 * return FAIL if there is no file name, OK if there is one
414 * give error message for FAIL
415 */
416 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100417check_fname(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
419 if (curbuf->b_ffname == NULL)
420 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200421 emsg(_(e_no_file_name));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 return FAIL;
423 }
424 return OK;
425}
426
427/*
428 * flush the contents of a buffer, unless it has no file name
429 *
430 * return FAIL for failure, OK otherwise
431 */
432 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433buf_write_all(buf_T *buf, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434{
435 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
438 retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
439 (linenr_T)1, buf->b_ml.ml_line_count, NULL,
440 FALSE, forceit, TRUE, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 if (curbuf != old_curbuf)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000442 {
Bram Moolenaar8820b482017-03-16 17:23:31 +0100443 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +0100444 msg(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 return retval;
447}
448
449/*
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200450 * ":argdo", ":windo", ":bufdo", ":tabdo", ":cdo", ":ldo", ":cfdo" and ":lfdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 */
452 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453ex_listdo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 int i;
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000456 win_T *wp;
457 tabpage_T *tp;
Bram Moolenaare25bb902015-02-27 20:33:37 +0100458 buf_T *buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 int next_fnum = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100460#if defined(FEAT_SYN_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 char_u *save_ei = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#endif
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200463#ifdef FEAT_QUICKFIX
Bram Moolenaared84b762015-09-09 22:35:29 +0200464 int qf_size = 0;
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200465 int qf_idx;
466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
Bram Moolenaar0106e3d2016-02-23 18:55:43 +0100468#ifndef FEAT_QUICKFIX
469 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
470 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
471 {
472 ex_ni(eap);
473 return;
474 }
475#endif
476
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100477#if defined(FEAT_SYN_HL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000478 if (eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo)
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200479 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100480 // Don't do syntax HL autocommands. Skipping the syntax file is a
481 // great speed improvement.
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000482 save_ei = au_event_disable(",Syntax");
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200483
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200484 FOR_ALL_BUFFERS(buf)
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200485 buf->b_flags &= ~BF_SYN_SET;
486 buf = curbuf;
487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200489#ifdef FEAT_CLIPBOARD
490 start_global_changes();
491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 if (eap->cmdidx == CMD_windo
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000494 || eap->cmdidx == CMD_tabdo
Bram Moolenaareb44a682017-08-03 22:44:55 +0200495 || buf_hide(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +0100496 || !check_changed(curbuf, CCGD_AW
497 | (eap->forceit ? CCGD_FORCEIT : 0)
498 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 i = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // start at the eap->line1 argument/window/buffer
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000502 wp = firstwin;
503 tp = first_tabpage;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100504 switch (eap->cmdidx)
505 {
Bram Moolenaara162bc52015-01-07 16:54:21 +0100506 case CMD_windo:
507 for ( ; wp != NULL && i + 1 < eap->line1; wp = wp->w_next)
508 i++;
509 break;
510 case CMD_tabdo:
511 for( ; tp != NULL && i + 1 < eap->line1; tp = tp->tp_next)
512 i++;
513 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100514 case CMD_argdo:
515 i = eap->line1 - 1;
516 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100517 default:
518 break;
519 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100520 // set pcmark now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (eap->cmdidx == CMD_bufdo)
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200522 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100523 // Advance to the first listed buffer after "eap->line1".
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200524 for (buf = firstbuf; buf != NULL && (buf->b_fnum < eap->line1
Bram Moolenaare25bb902015-02-27 20:33:37 +0100525 || !buf->b_p_bl); buf = buf->b_next)
526 if (buf->b_fnum > eap->line2)
527 {
528 buf = NULL;
529 break;
530 }
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200531 if (buf != NULL)
Bram Moolenaare25bb902015-02-27 20:33:37 +0100532 goto_buffer(eap, DOBUF_FIRST, FORWARD, buf->b_fnum);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200533 }
534#ifdef FEAT_QUICKFIX
535 else if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
536 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
537 {
Bram Moolenaar25190db2019-05-04 15:05:28 +0200538 qf_size = qf_get_valid_size(eap);
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200539 if (qf_size <= 0 || eap->line1 > qf_size)
540 buf = NULL;
541 else
542 {
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000543 save_clear_shm_value();
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200544 ex_cc(eap);
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000545 restore_shm_value();
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200546
547 buf = curbuf;
548 i = eap->line1 - 1;
549 if (eap->addr_count <= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100550 // default is all the quickfix/location list entries
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200551 eap->line2 = qf_size;
552 }
553 }
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 else
556 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100557 listcmd_busy = TRUE; // avoids setting pcmark below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaare25bb902015-02-27 20:33:37 +0100559 while (!got_int && buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
561 if (eap->cmdidx == CMD_argdo)
562 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100563 // go to argument "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (i == ARGCOUNT)
565 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100566 // Don't call do_argfile() when already there, it will try
567 // reloading the file.
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000568 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100570 // Clear 'shm' to avoid that the file message overwrites
571 // any output from the command.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000572 save_clear_shm_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 do_argfile(eap, i);
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000574 restore_shm_value();
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (curwin->w_arg_idx != i)
577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 else if (eap->cmdidx == CMD_windo)
580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100581 // go to window "wp"
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000582 if (!win_valid(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 break;
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000584 win_goto(wp);
Bram Moolenaar41423242007-05-03 20:11:13 +0000585 if (curwin != wp)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100586 break; // something must be wrong
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000587 wp = curwin->w_next;
588 }
589 else if (eap->cmdidx == CMD_tabdo)
590 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100591 // go to window "tp"
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000592 if (!valid_tabpage(tp))
593 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200594 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000595 tp = tp->tp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 else if (eap->cmdidx == CMD_bufdo)
598 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100599 // Remember the number of the next listed buffer, in case
600 // ":bwipe" is used or autocommands do something strange.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 next_fnum = -1;
602 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
603 if (buf->b_p_bl)
604 {
605 next_fnum = buf->b_fnum;
606 break;
607 }
608 }
609
Bram Moolenaara162bc52015-01-07 16:54:21 +0100610 ++i;
611
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100612 // execute the command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 do_cmdline(eap->arg, eap->getline, eap->cookie,
614 DOCMD_VERBOSE + DOCMD_NOWAIT);
615
616 if (eap->cmdidx == CMD_bufdo)
617 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100618 // Done?
Bram Moolenaara162bc52015-01-07 16:54:21 +0100619 if (next_fnum < 0 || next_fnum > eap->line2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 // Check if the buffer still exists.
Bram Moolenaar29323592016-07-24 22:04:11 +0200622 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 if (buf->b_fnum == next_fnum)
624 break;
625 if (buf == NULL)
626 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000627
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100628 // Go to the next buffer. Clear 'shm' to avoid that the file
629 // message overwrites any output from the command.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000630 save_clear_shm_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000632 restore_shm_value();
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000633
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 // If autocommands took us elsewhere, quit here.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (curbuf->b_fnum != next_fnum)
636 break;
637 }
638
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200639#ifdef FEAT_QUICKFIX
640 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
641 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
642 {
643 if (i >= qf_size || i >= eap->line2)
644 break;
645
646 qf_idx = qf_get_cur_idx(eap);
647
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000648 save_clear_shm_value();
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200649 ex_cnext(eap);
Christian Brabandt9aee8ec2022-12-16 16:41:23 +0000650 restore_shm_value();
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200651
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100652 // If jumping to the next quickfix entry fails, quit here
Bram Moolenaaraa23b372015-09-08 18:46:31 +0200653 if (qf_get_cur_idx(eap) == qf_idx)
654 break;
655 }
656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (eap->cmdidx == CMD_windo)
659 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100660 validate_cursor(); // cursor may have moved
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100661
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100662 // required when 'scrollbind' has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (curwin->w_p_scb)
664 do_check_scrollbind(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
Bram Moolenaara162bc52015-01-07 16:54:21 +0100666
Bram Moolenaara162bc52015-01-07 16:54:21 +0100667 if (eap->cmdidx == CMD_windo || eap->cmdidx == CMD_tabdo)
668 if (i+1 > eap->line2)
669 break;
Bram Moolenaara162bc52015-01-07 16:54:21 +0100670 if (eap->cmdidx == CMD_argdo && i >= eap->line2)
671 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673 listcmd_busy = FALSE;
674 }
675
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100676#if defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000677 if (save_ei != NULL)
678 {
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200679 buf_T *bnext;
680 aco_save_T aco;
681
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000682 au_event_restore(save_ei);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200683
684 for (buf = firstbuf; buf != NULL; buf = bnext)
685 {
686 bnext = buf->b_next;
687 if (buf->b_nwindows > 0 && (buf->b_flags & BF_SYN_SET))
688 {
689 buf->b_flags &= ~BF_SYN_SET;
690
691 // buffer was opened while Syntax autocommands were disabled,
692 // need to trigger them now.
693 if (buf == curbuf)
694 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000695 curbuf->b_fname, TRUE, curbuf);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200696 else
697 {
698 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +0000699 if (curbuf == buf)
700 {
701 apply_autocmds(EVENT_SYNTAX, buf->b_p_syn,
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200702 buf->b_fname, TRUE, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +0000703 aucmd_restbuf(&aco);
704 }
Bram Moolenaarc7f1e402019-08-03 13:29:46 +0200705 }
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}