blob: cec33b0a45029840701fcdf21c71329ddf385956 [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 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void enter_buffer(buf_T *buf);
31static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020035static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
36static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
37static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
41#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020042static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
45static void free_buffer(buf_T *);
46static void free_buffer_stuff(buf_T *buf, int free_options);
47static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49#ifdef UNIX
50# define dev_T dev_t
51#else
52# define dev_T unsigned
53#endif
54
Bram Moolenaar4033c552017-09-16 20:54:51 +020055#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020056static char *msg_loclist = N_("[Location List]");
57static char *msg_qflist = N_("[Quickfix List]");
58#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010059static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020061// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020062static int buf_free_count = 0;
63
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020064static int top_file_num = 1; // highest file number
65static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
66
67/*
68 * Return the highest possible buffer number.
69 */
70 int
71get_highest_fnum(void)
72{
73 return top_file_num - 1;
74}
75
Bram Moolenaarc024b462019-06-08 18:07:21 +020076/*
77 * Read data from buffer for retrying.
78 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020079 static int
80read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010081 int read_stdin, // read file from stdin, otherwise fifo
82 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
83 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020084{
85 int retval = OK;
86 linenr_T line_count;
87
88 /*
89 * Read from the buffer which the text is already filled in and append at
90 * the end. This makes it possible to retry when 'fileformat' or
91 * 'fileencoding' was guessed wrong.
92 */
93 line_count = curbuf->b_ml.ml_line_count;
94 retval = readfile(
95 read_stdin ? NULL : curbuf->b_ffname,
96 read_stdin ? NULL : curbuf->b_fname,
97 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
98 flags | READ_BUFFER);
99 if (retval == OK)
100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100101 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200102 while (--line_count >= 0)
103 ml_delete((linenr_T)1, FALSE);
104 }
105 else
106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100107 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200108 while (curbuf->b_ml.ml_line_count > line_count)
109 ml_delete(line_count, FALSE);
110 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100111 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200112 curwin->w_cursor.lnum = 1;
113 curwin->w_cursor.col = 0;
114
115 if (read_stdin)
116 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100117 // Set or reset 'modified' before executing autocommands, so that
118 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100119 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200120 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100121 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200122 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200123
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100124 if (retval == OK)
125 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100126#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100127 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100128 curbuf, &retval);
129#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100130 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200131#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200133 }
134 return retval;
135}
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200138 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
139 */
140 void
141buffer_ensure_loaded(buf_T *buf)
142{
143 if (buf->b_ml.ml_mfp == NULL)
144 {
145 aco_save_T aco;
146
147 aucmd_prepbuf(&aco, buf);
148 swap_exists_action = SEA_NONE;
149 open_buffer(FALSE, NULL, 0);
150 aucmd_restbuf(&aco);
151 }
152}
153
154/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200155 * Open current buffer, that is: open the memfile and read the file into
156 * memory.
157 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 */
159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100160open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100161 int read_stdin, // read file from stdin
162 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
163 int flags) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200166 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100167#ifdef FEAT_SYN_HL
168 long old_tw = curbuf->b_p_tw;
169#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200170 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172 /*
173 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
174 * When re-entering the same buffer, it should not change, because the
175 * user may have reset the flag by hand.
176 */
177 if (readonlymode && curbuf->b_ffname != NULL
178 && (curbuf->b_flags & BF_NEVERLOADED))
179 curbuf->b_p_ro = TRUE;
180
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 {
183 /*
184 * There MUST be a memfile, otherwise we can't do anything
185 * If we can't create one for the current buffer, take another buffer
186 */
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100187 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200188 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (curbuf->b_ml.ml_mfp != NULL)
190 break;
191 /*
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200192 * If there is no memfile at all, exit.
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000193 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 */
195 if (curbuf == NULL)
196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100197 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200198
199 // Don't try to do any saving, with "curbuf" NULL almost nothing
200 // will work.
201 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 getout(2);
203 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200204
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100205 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100207#ifdef FEAT_SYN_HL
208 if (old_tw != curbuf->b_p_tw)
209 check_colorcolumn(curwin);
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 return FAIL;
212 }
213
Bram Moolenaarc667da52019-11-30 20:52:27 +0100214 // The autocommands in readfile() may change the buffer, but only AFTER
215 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200216 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaarc667da52019-11-30 20:52:27 +0100219 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100220 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
222 if (curbuf->b_ffname != NULL
223#ifdef FEAT_NETBEANS_INTG
224 && netbeansReadFile
225#endif
226 )
227 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100228 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200229#ifdef UNIX
230 int save_bin = curbuf->b_p_bin;
231 int perm;
232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_NETBEANS_INTG
234 int oldFire = netbeansFireChanges;
235
236 netbeansFireChanges = 0;
237#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238#ifdef UNIX
239 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200240 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200241 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200242# ifdef OPEN_CHR_FILES
243 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
244# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200245 ))
246 read_fifo = TRUE;
247 if (read_fifo)
248 curbuf->b_p_bin = TRUE;
249#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100250 if (shortmess(SHM_FILEINFO))
251 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200253 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
255#ifdef UNIX
256 if (read_fifo)
257 {
258 curbuf->b_p_bin = save_bin;
259 if (retval == OK)
260 retval = read_buffer(FALSE, eap, flags);
261 }
262#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100263 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_NETBEANS_INTG
265 netbeansFireChanges = oldFire;
266#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100267 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200268 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 fix_help_buffer();
270 }
271 else if (read_stdin)
272 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200273 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 /*
276 * First read the text in binary mode into the buffer.
277 * Then read from that same buffer and append at the end. This makes
278 * it possible to retry when 'fileformat' or 'fileencoding' was
279 * guessed wrong.
280 */
281 curbuf->b_p_bin = TRUE;
282 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200283 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
284 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 curbuf->b_p_bin = save_bin;
286 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200287 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289
Bram Moolenaarc667da52019-11-30 20:52:27 +0100290 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100294#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100295 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100296#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
299 /*
300 * Set/reset the Changed flag first, autocmds may change the buffer.
301 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200302 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100304 // When reading stdin, the buffer contents always needs writing, so set
305 // the changed flag. Unless in readonly mode: "ls | gview -".
306 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000307 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100308 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100309#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000312 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100314 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200315 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100316 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
Bram Moolenaarc667da52019-11-30 20:52:27 +0100318 // Set last_changedtick to avoid triggering a TextChanged autocommand right
319 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100320 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100321 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100322
Bram Moolenaarc667da52019-11-30 20:52:27 +0100323 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_EVAL
325 if (aborting())
326#else
327 if (got_int)
328#endif
329 curbuf->b_flags |= BF_READERR;
330
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000331#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100332 // Need to update automatic folding. Do this before the autocommands,
333 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000334 foldUpdateAll(curwin);
335#endif
336
Bram Moolenaarc667da52019-11-30 20:52:27 +0100337 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 if (!(curwin->w_valid & VALID_TOPLINE))
339 {
340 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100341#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100347#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
350
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100351 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * The autocommands may have changed the current buffer. Apply the
355 * modelines to the correct buffer, if it still exists and is loaded.
356 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200357 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
359 aco_save_T aco;
360
Bram Moolenaarc667da52019-11-30 20:52:27 +0100361 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200362 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000363 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
365
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100366#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100368 &retval);
369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaarc667da52019-11-30 20:52:27 +0100373 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 aucmd_restbuf(&aco);
375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 return retval;
379}
380
381/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200382 * Store "buf" in "bufref" and set the free count.
383 */
384 void
385set_bufref(bufref_T *bufref, buf_T *buf)
386{
387 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200388 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200389 bufref->br_buf_free_count = buf_free_count;
390}
391
392/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200393 * Return TRUE if "bufref->br_buf" points to the same buffer as when
394 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200395 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200396 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
397 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200398 */
399 int
400bufref_valid(bufref_T *bufref)
401{
402 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200403 ? TRUE : buf_valid(bufref->br_buf)
404 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200405}
406
407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200409 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 */
411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100412buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
414 buf_T *bp;
415
Bram Moolenaarc667da52019-11-30 20:52:27 +0100416 // Assume that we more often have a recent buffer, start with the last
417 // one.
Bram Moolenaar82404332016-07-10 17:00:38 +0200418 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 if (bp == buf)
420 return TRUE;
421 return FALSE;
422}
423
424/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200425 * A hash table used to quickly lookup a buffer by its number.
426 */
427static hashtab_T buf_hashtab;
428
429 static void
430buf_hashtab_add(buf_T *buf)
431{
432 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
433 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100434 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200435}
436
437 static void
438buf_hashtab_remove(buf_T *buf)
439{
440 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
441
442 if (!HASHITEM_EMPTY(hi))
443 hash_remove(&buf_hashtab, hi);
444}
445
Bram Moolenaar94f01952018-09-01 15:30:03 +0200446/*
447 * Return TRUE when buffer "buf" can be unloaded.
448 * Give an error message and return FALSE when the buffer is locked or the
449 * screen is being redrawn and the buffer is in a window.
450 */
451 static int
452can_unload_buffer(buf_T *buf)
453{
454 int can_unload = !buf->b_locked;
455
456 if (can_unload && updating_screen)
457 {
458 win_T *wp;
459
460 FOR_ALL_WINDOWS(wp)
461 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200462 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200463 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200464 break;
465 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200466 }
467 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200468 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
469 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200470 return can_unload;
471}
Bram Moolenaara997b452018-04-17 23:24:06 +0200472
Bram Moolenaar480778b2016-07-14 22:09:39 +0200473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Close the link to a buffer.
475 * "action" is used when there is no longer a window for the buffer.
476 * It can be:
477 * 0 buffer becomes hidden
478 * DOBUF_UNLOAD buffer is unloaded
479 * DOBUF_DELETE buffer is unloaded and removed from buffer list
480 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200481 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * When doing all but the first one on the current buffer, the caller should
483 * get a new buffer very soon!
484 *
485 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100486 *
487 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
488 * cause there to be only one window with this buffer. e.g. when ":quit" is
489 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100490 *
491 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 */
493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100494close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100495 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100496 buf_T *buf,
497 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100498 int abort_if_last,
499 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100502 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200503 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100504 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200505 win_T *the_curwin = curwin;
506 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200508 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
509 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510
Bram Moolenaarcee52202020-03-11 14:19:58 +0100511 CHECK_CURBUF;
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200512 /*
513 * Force unloading or deleting when 'bufhidden' says so.
514 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
515 * "hide" (otherwise we could never free or delete a buffer).
516 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100517 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200518 {
519 del_buf = TRUE;
520 unload_buf = TRUE;
521 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100522 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200523 {
524 del_buf = TRUE;
525 unload_buf = TRUE;
526 wipe_buf = TRUE;
527 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100528 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200529 unload_buf = TRUE;
530
Bram Moolenaar94053a52017-08-01 21:44:33 +0200531#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200532 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200533 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100534 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200535 if (term_job_running(buf->b_term))
536 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200537 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200538 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200539 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200540 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200541
Bram Moolenaarc667da52019-11-30 20:52:27 +0100542 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200544 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200545 else
546 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100547 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200548 del_buf = FALSE;
549 unload_buf = FALSE;
550 }
551 }
552 else
553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100554 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200555 del_buf = TRUE;
556 unload_buf = TRUE;
557 wipe_buf = TRUE;
558 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100559 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200560 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaarc667da52019-11-30 20:52:27 +0100563 // Disallow deleting the buffer when it is locked (already being closed or
564 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200565 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200566 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200567
Bram Moolenaarc667da52019-11-30 20:52:27 +0100568 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200569 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // Set b_last_cursor when closing the last window for the buffer.
572 // Remember the last cursor position and window options of the buffer.
573 // This used to be only for the current window, but then options like
574 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (buf->b_nwindows == 1)
576 set_last_cursor(win);
577 buflist_setfpos(buf, win,
578 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
579 win->w_cursor.col, TRUE);
580 }
581
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200582 set_bufref(&bufref, buf);
583
Bram Moolenaarc667da52019-11-30 20:52:27 +0100584 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (buf->b_nwindows == 1)
586 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200587 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200588 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
589 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200590 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100591 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100592 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200593aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100594 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100596 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200597 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200598 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100599 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200600 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
Bram Moolenaarc667da52019-11-30 20:52:27 +0100602 // When the buffer becomes hidden, but is not unloaded, trigger
603 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (!unload_buf)
605 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200606 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200607 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
608 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200609 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100610 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200611 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200612 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200613 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200615 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100617#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100618 // autocmds may abort script processing
619 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200623
Bram Moolenaarc667da52019-11-30 20:52:27 +0100624 // If the buffer was in curwin and the window has changed, go back to that
625 // window, if it still exists. This avoids that ":edit x" triggering a
626 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200627 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
628 {
629 block_autocmds();
630 goto_tabpage_win(the_curtab, the_curwin);
631 unblock_autocmds();
632 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
Bram Moolenaarc667da52019-11-30 20:52:27 +0100636 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 if (buf->b_nwindows > 0)
638 --buf->b_nwindows;
639
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100640#ifdef FEAT_DIFF
641 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100642 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100643#endif
644
Bram Moolenaarc667da52019-11-30 20:52:27 +0100645 // Return when a window is displaying the buffer or when it's not
646 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaarc667da52019-11-30 20:52:27 +0100650 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (buf->b_ffname == NULL)
652 del_buf = TRUE;
653
Bram Moolenaarc667da52019-11-30 20:52:27 +0100654 // When closing the current buffer stop Visual mode before freeing
655 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200656 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200657#if defined(EXITFREE)
658 && !entered_free_all_mem
659#endif
660 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200661 end_visual_mode();
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Free all things allocated for this buffer.
665 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
666 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 // Remember if we are closing the current buffer. Restore the number of
668 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 is_curbuf = (buf == curbuf);
670 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100672 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
673 + (wipe_buf ? BFA_WIPE : 0)
674 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
Bram Moolenaarc667da52019-11-30 20:52:27 +0100676 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200677 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100679#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100680 // autocmds may abort script processing
681 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 /*
686 * It's possible that autocommands change curbuf to the one being deleted.
687 * This might cause the previous curbuf to be deleted unexpectedly. But
688 * in some cases it's OK to delete the curbuf, because a new one is
689 * obtained anyway. Therefore only return if curbuf changed to the
690 * deleted buffer.
691 */
692 if (buf == curbuf && !is_curbuf)
693 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200694
Bram Moolenaar4033c552017-09-16 20:54:51 +0200695 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100696 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200697
Bram Moolenaarc667da52019-11-30 20:52:27 +0100698 // Autocommands may have opened or closed windows for this buffer.
699 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200700 if (buf->b_nwindows > 0)
701 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 /*
704 * Remove the buffer from the list.
705 */
706 if (wipe_buf)
707 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200708 if (action == DOBUF_WIPE_REUSE)
709 {
710 // we can re-use this buffer number, store it
711 if (buf_reuse.ga_itemsize == 0)
712 ga_init2(&buf_reuse, sizeof(int), 50);
713 if (ga_grow(&buf_reuse, 1) == OK)
714 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
715 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200716 if (buf->b_sfname != buf->b_ffname)
717 VIM_CLEAR(buf->b_sfname);
718 else
719 buf->b_sfname = NULL;
720 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (buf->b_prev == NULL)
722 firstbuf = buf->b_next;
723 else
724 buf->b_prev->b_next = buf->b_next;
725 if (buf->b_next == NULL)
726 lastbuf = buf->b_prev;
727 else
728 buf->b_next->b_prev = buf->b_prev;
729 free_buffer(buf);
730 }
731 else
732 {
733 if (del_buf)
734 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100735 // Free all internal variables and reset option values, to make
736 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 free_buffer_stuff(buf, TRUE);
738
Bram Moolenaarc667da52019-11-30 20:52:27 +0100739 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
741
Bram Moolenaarc667da52019-11-30 20:52:27 +0100742 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 buf->b_p_initialized = FALSE;
744 }
745 buf_clear_file(buf);
746 if (del_buf)
747 buf->b_p_bl = FALSE;
748 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100749 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
752/*
753 * Make buffer not contain a file.
754 */
755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100756buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757{
758 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200759 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 buf->b_p_eol = TRUE;
762 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000764 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100766 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#ifdef FEAT_NETBEANS_INTG
768 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#endif
770}
771
772/*
773 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200774 * the file. Careful: get here with "curwin" NULL when exiting.
775 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100776 * BFA_DEL buffer is going to be deleted
777 * BFA_WIPE buffer is going to be wiped out
778 * BFA_KEEP_UNDO do not free undo information
779 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100782buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200785 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200786 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200787 win_T *the_curwin = curwin;
788 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaarc667da52019-11-30 20:52:27 +0100790 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200791 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200792 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200793 if (buf->b_ml.ml_mfp != NULL)
794 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200795 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
796 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200797 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100798 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200799 return;
800 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200801 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200803 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
804 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200805 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100806 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 return;
808 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200809 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200811 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
812 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200813 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100814 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return;
816 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200817 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200818
Bram Moolenaarc667da52019-11-30 20:52:27 +0100819 // If the buffer was in curwin and the window has changed, go back to that
820 // window, if it still exists. This avoids that ":edit x" triggering a
821 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200822 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
823 {
824 block_autocmds();
825 goto_tabpage_win(the_curtab, the_curwin);
826 unblock_autocmds();
827 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200828
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100829#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100830 // autocmds may abort script processing
831 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 /*
836 * It's possible that autocommands change curbuf to the one being deleted.
837 * This might cause curbuf to be deleted unexpectedly. But in some cases
838 * it's OK to delete the curbuf, because a new one is obtained anyway.
839 * Therefore only return if curbuf changed to the deleted buffer.
840 */
841 if (buf == curbuf && !is_curbuf)
842 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100844 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200846#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100847 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200848 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100849 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200850#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000851
852#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100853 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000854 {
855 win_T *win;
856 tabpage_T *tp;
857
858 FOR_ALL_TAB_WINDOWS(tp, win)
859 if (win->w_buffer == buf)
860 clearFolding(win);
861 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000862#endif
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_TCL
865 tcl_buffer_free(buf);
866#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100867 ml_close(buf, TRUE); // close and delete the memline/memfile
868 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200869 if ((flags & BFA_KEEP_UNDO) == 0)
870 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100871 u_blockfree(buf); // free the memory allocated for undo
872 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100875 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100877#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100878 clear_buf_prop_types(buf);
879#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100880 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881}
882
883/*
884 * Free a buffer structure and the things it contains related to the buffer
885 * itself (not the file, that must have been done already).
886 */
887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200890 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200892#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100893 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200894 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200895 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200896 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200897#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200898#ifdef FEAT_LUA
899 lua_buffer_free(buf);
900#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000901#ifdef FEAT_MZSCHEME
902 mzscheme_buffer_free(buf);
903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#ifdef FEAT_PERL
905 perl_buf_free(buf);
906#endif
907#ifdef FEAT_PYTHON
908 python_buffer_free(buf);
909#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200910#ifdef FEAT_PYTHON3
911 python3_buffer_free(buf);
912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#ifdef FEAT_RUBY
914 ruby_buffer_free(buf);
915#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200916#ifdef FEAT_JOB_CHANNEL
917 channel_buffer_free(buf);
918#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200919#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200920 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200921#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200922#ifdef FEAT_JOB_CHANNEL
923 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200924 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200925 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200926#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200927
928 buf_hashtab_remove(buf);
929
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200930 aubuflocal_remove(buf);
931
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200932 if (autocmd_busy)
933 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100934 // Do not free the buffer structure while autocommands are executing,
935 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200936 buf->b_next = au_pending_free_buf;
937 au_pending_free_buf = buf;
938 }
939 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100940 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200941 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100942 if (curbuf == buf)
943 curbuf = NULL; // make clear it's not to be used
944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945}
946
947/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100948 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100949 */
950 static void
951init_changedtick(buf_T *buf)
952{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100953 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100954
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100955 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
956 di->di_tv.v_type = VAR_NUMBER;
957 di->di_tv.v_lock = VAR_FIXED;
958 di->di_tv.vval.v_number = 0;
959
Bram Moolenaar92769c32017-02-25 15:41:37 +0100960#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100961 STRCPY(buf->b_ct_di.di_key, "changedtick");
962 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100963#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100964}
965
966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
968 */
969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100970free_buffer_stuff(
971 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100972 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 if (free_options)
975 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100976 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200978#ifdef FEAT_SPELL
979 ga_clear(&buf->b_s.b_langp);
980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100983 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100984 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100985
Bram Moolenaarc667da52019-11-30 20:52:27 +0100986 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100987 hash_init(&buf->b_vars->dv_hashtab);
988 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100989 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +0100990 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200993 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200995 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000997#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200998 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000999#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001000 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1001 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001002 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
1004
1005/*
1006 * Free the b_wininfo list for buffer "buf".
1007 */
1008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001009clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
1011 wininfo_T *wip;
1012
1013 while (buf->b_wininfo != NULL)
1014 {
1015 wip = buf->b_wininfo;
1016 buf->b_wininfo = wip->wi_next;
1017 if (wip->wi_optset)
1018 {
1019 clear_winopt(&wip->wi_opt);
1020#ifdef FEAT_FOLDING
1021 deleteFoldRecurse(&wip->wi_folds);
1022#endif
1023 }
1024 vim_free(wip);
1025 }
1026}
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028/*
1029 * Go to another buffer. Handles the result of the ATTENTION dialog.
1030 */
1031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001032goto_buffer(
1033 exarg_T *eap,
1034 int start,
1035 int dir,
1036 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001038 bufref_T old_curbuf;
1039
1040 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1044 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1046 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001047#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001048 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001049
Bram Moolenaarc667da52019-11-30 20:52:27 +01001050 // Reset the error/interrupt/exception state here so that
1051 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001052 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001053#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001054
Bram Moolenaarc667da52019-11-30 20:52:27 +01001055 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 win_close(curwin, TRUE);
1057 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001058 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001059
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001060#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001061 // Restore the error/interrupt/exception state if not discarded by a
1062 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001063 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001067 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070/*
1071 * Handle the situation of swap_exists_action being set.
1072 * It is allowed for "old_curbuf" to be NULL or invalid.
1073 */
1074 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001075handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001077#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001078 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001079#endif
1080#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001081 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001082#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001083 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (swap_exists_action == SEA_QUIT)
1086 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001087#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001088 // Reset the error/interrupt/exception state here so that
1089 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001090 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001091#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001092
Bram Moolenaarc667da52019-11-30 20:52:27 +01001093 // User selected Quit at ATTENTION prompt. Go back to previous
1094 // buffer. If that buffer is gone or the same as the current one,
1095 // open a new, empty buffer.
1096 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001097 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001098 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001099 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1100 || old_curbuf->br_buf == curbuf)
1101 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1102 else
1103 buf = old_curbuf->br_buf;
1104 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001105 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001106 int old_msg_silent = msg_silent;
1107
1108 if (shortmess(SHM_FILEINFO))
1109 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001110 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001111 // restore msg_silent, so that the command line will be shown
1112 msg_silent = old_msg_silent;
1113
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001114#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001115 if (old_tw != curbuf->b_p_tw)
1116 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001118 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001119 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001120
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001121#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001122 // Restore the error/interrupt/exception state if not discarded by a
1123 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001124 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 else if (swap_exists_action == SEA_RECOVER)
1128 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001129#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001130 // Reset the error/interrupt/exception state here so that
1131 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001132 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001133#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001134
Bram Moolenaarc667da52019-11-30 20:52:27 +01001135 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001137 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001138 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001140 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001141
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001142#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001143 // Restore the error/interrupt/exception state if not discarded by a
1144 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001145 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
1148 swap_exists_action = SEA_NONE;
1149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151/*
1152 * do_bufdel() - delete or unload buffer(s)
1153 *
1154 * addr_count == 0: ":bdel" - delete current buffer
1155 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1156 * buffer "end_bnr", then any other arguments.
1157 * addr_count == 2: ":N,N bdel" - delete buffers in range
1158 *
1159 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1160 * DOBUF_DEL (":bdel")
1161 *
1162 * Returns error message or NULL
1163 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001164 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001165do_bufdel(
1166 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001167 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001168 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001169 int start_bnr, // first buffer number in a range
1170 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001173 int do_current = 0; // delete current buffer?
1174 int deleted = 0; // number of buffers deleted
1175 char *errormsg = NULL; // return value
1176 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 char_u *p;
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (addr_count == 0)
1180 {
1181 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1182 }
1183 else
1184 {
1185 if (addr_count == 2)
1186 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001187 if (*arg) // both range and argument is not allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001188 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 bnr = start_bnr;
1190 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001191 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 bnr = end_bnr;
1193
1194 for ( ;!got_int; ui_breakcheck())
1195 {
1196 /*
1197 * delete the current buffer last, otherwise when the
1198 * current buffer is deleted, the next buffer becomes
1199 * the current one and will be loaded, which may then
1200 * also be deleted, etc.
1201 */
1202 if (bnr == curbuf->b_fnum)
1203 do_current = bnr;
1204 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1205 forceit) == OK)
1206 ++deleted;
1207
1208 /*
1209 * find next buffer number to delete/unload
1210 */
1211 if (addr_count == 2)
1212 {
1213 if (++bnr > end_bnr)
1214 break;
1215 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001216 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {
1218 arg = skipwhite(arg);
1219 if (*arg == NUL)
1220 break;
1221 if (!VIM_ISDIGIT(*arg))
1222 {
1223 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001224 bnr = buflist_findpat(arg, p,
1225 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001226 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001227 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 break;
1229 arg = p;
1230 }
1231 else
1232 bnr = getdigits(&arg);
1233 }
1234 }
1235 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1236 FORWARD, do_current, forceit) == OK)
1237 ++deleted;
1238
1239 if (deleted == 0)
1240 {
1241 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001246 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001247 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249 else if (deleted >= p_report)
1250 {
1251 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001253 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001255 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001256 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001259 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 }
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 return errormsg;
1265}
1266
Bram Moolenaara02471e2014-01-10 16:43:14 +01001267/*
1268 * Make the current buffer empty.
1269 * Used when it is wiped out and it's the last buffer.
1270 */
1271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001272empty_curbuf(
1273 int close_others,
1274 int forceit,
1275 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001276{
1277 int retval;
1278 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001279 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001280
1281 if (action == DOBUF_UNLOAD)
1282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001283 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001284 return FAIL;
1285 }
1286
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001287 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001288 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001289 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001290 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001291
1292 setpcmark();
1293 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1294 forceit ? ECMD_FORCEIT : 0, curwin);
1295
1296 /*
1297 * do_ecmd() may create a new buffer, then we have to delete
1298 * the old one. But do_ecmd() may have done that already, check
1299 * if the buffer still exists.
1300 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001301 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001302 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001303 if (!close_others)
1304 need_fileinfo = FALSE;
1305 return retval;
1306}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308/*
1309 * Implementation of the commands for the buffer list.
1310 *
1311 * action == DOBUF_GOTO go to specified buffer
1312 * action == DOBUF_SPLIT split window and go to specified buffer
1313 * action == DOBUF_UNLOAD unload specified buffer(s)
1314 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1315 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001316 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 *
1318 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1319 * start == DOBUF_FIRST go to "count" buffer from first buffer
1320 * start == DOBUF_LAST go to "count" buffer from last buffer
1321 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1322 *
1323 * Return FAIL or OK.
1324 */
1325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001326do_buffer(
1327 int action,
1328 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001329 int dir, // FORWARD or BACKWARD
1330 int count, // buffer number or number of buffers
1331 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 buf_T *buf;
1334 buf_T *bp;
1335 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001336 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338 switch (start)
1339 {
1340 case DOBUF_FIRST: buf = firstbuf; break;
1341 case DOBUF_LAST: buf = lastbuf; break;
1342 default: buf = curbuf; break;
1343 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001344 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
1346 while (count-- > 0)
1347 {
1348 do
1349 {
1350 buf = buf->b_next;
1351 if (buf == NULL)
1352 buf = firstbuf;
1353 }
1354 while (buf != curbuf && !bufIsChanged(buf));
1355 }
1356 if (!bufIsChanged(buf))
1357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 return FAIL;
1360 }
1361 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001362 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
1364 while (buf != NULL && buf->b_fnum != count)
1365 buf = buf->b_next;
1366 }
1367 else
1368 {
1369 bp = NULL;
1370 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1371 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001372 // remember the buffer where we start, we come back there when all
1373 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (bp == NULL)
1375 bp = buf;
1376 if (dir == FORWARD)
1377 {
1378 buf = buf->b_next;
1379 if (buf == NULL)
1380 buf = firstbuf;
1381 }
1382 else
1383 {
1384 buf = buf->b_prev;
1385 if (buf == NULL)
1386 buf = lastbuf;
1387 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001388 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (unload || buf->b_p_bl)
1390 {
1391 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001392 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 if (bp == buf)
1395 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001396 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001397 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 return FAIL;
1399 }
1400 }
1401 }
1402
Bram Moolenaarc667da52019-11-30 20:52:27 +01001403 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 if (start == DOBUF_FIRST)
1406 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001407 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001409 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001412 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 return FAIL;
1416 }
1417
1418#ifdef FEAT_GUI
1419 need_mouse_correct = TRUE;
1420#endif
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 /*
1423 * delete buffer buf from memory and/or the list
1424 */
1425 if (unload)
1426 {
1427 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001428 bufref_T bufref;
1429
Bram Moolenaar94f01952018-09-01 15:30:03 +02001430 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001431 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001432
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001433 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaarc667da52019-11-30 20:52:27 +01001435 // When unloading or deleting a buffer that's already unloaded and
1436 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001437 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1438 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 return FAIL;
1440
1441 if (!forceit && bufIsChanged(buf))
1442 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001443#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if ((p_confirm || cmdmod.confirm) && p_write)
1445 {
1446 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001447 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001448 // Autocommand deleted buffer, oops! It's not changed
1449 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001451 // If it's still changed fail silently, the dialog already
1452 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001453 if (bufIsChanged(buf))
1454 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001456 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001459 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 buf->b_fnum);
1461 return FAIL;
1462 }
1463 }
1464
Bram Moolenaarc667da52019-11-30 20:52:27 +01001465 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001466 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001467 end_visual_mode();
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 /*
1470 * If deleting the last (listed) buffer, make it empty.
1471 * The last (listed) buffer cannot be unloaded.
1472 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001473 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (bp->b_p_bl && bp != buf)
1475 break;
1476 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001477 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 /*
1480 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001481 * (unless it's the only window). Repeat this so long as we end up in
1482 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001484 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001485 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001486 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001487 {
1488 if (win_close(curwin, FALSE) == FAIL)
1489 break;
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 /*
1493 * If the buffer to be deleted is not the current one, delete it here.
1494 */
1495 if (buf != curbuf)
1496 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001497 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001498 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001499 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 return OK;
1501 }
1502
1503 /*
1504 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001505 * There should be another, otherwise it would have been handled
1506 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001507 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * Then prefer the buffer we most recently visited.
1509 * Else try to find one that is loaded, after the current buffer,
1510 * then before the current buffer.
1511 * Finally use any buffer.
1512 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001513 buf = NULL; // selected buffer
1514 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001515 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1516 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001518 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 {
1520 int jumpidx;
1521
1522 jumpidx = curwin->w_jumplistidx - 1;
1523 if (jumpidx < 0)
1524 jumpidx = curwin->w_jumplistlen - 1;
1525
1526 forward = jumpidx;
1527 while (jumpidx != curwin->w_jumplistidx)
1528 {
1529 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1530 if (buf != NULL)
1531 {
1532 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 else if (buf->b_ml.ml_mfp == NULL)
1535 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001536 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (bp == NULL)
1538 bp = buf;
1539 buf = NULL;
1540 }
1541 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001542 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001544 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1546 break;
1547 if (--jumpidx < 0)
1548 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001549 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 break;
1551 }
1552 }
1553#endif
1554
Bram Moolenaarc667da52019-11-30 20:52:27 +01001555 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 forward = TRUE;
1558 buf = curbuf->b_next;
1559 for (;;)
1560 {
1561 if (buf == NULL)
1562 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001563 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 break;
1565 buf = curbuf->b_prev;
1566 forward = FALSE;
1567 continue;
1568 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001569 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001572 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001574 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 bp = buf;
1576 }
1577 if (forward)
1578 buf = buf->b_next;
1579 else
1580 buf = buf->b_prev;
1581 }
1582 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001583 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001585 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001587 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (buf->b_p_bl && buf != curbuf)
1589 break;
1590 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001591 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 if (curbuf->b_next != NULL)
1594 buf = curbuf->b_next;
1595 else
1596 buf = curbuf->b_prev;
1597 }
1598 }
1599
Bram Moolenaara02471e2014-01-10 16:43:14 +01001600 if (buf == NULL)
1601 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001602 // Autocommands must have wiped out all other buffers. Only option
1603 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001604 return empty_curbuf(FALSE, forceit, action);
1605 }
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /*
1608 * make buf current buffer
1609 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001610 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001612 // If 'switchbuf' contains "useopen": jump to first window containing
1613 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001614 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001615 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001616 // If 'switchbuf' contains "usetab": jump to first window in any tab
1617 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001618 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 return OK;
1620 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 return FAIL;
1622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaarc667da52019-11-30 20:52:27 +01001624 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (buf == curbuf)
1626 return OK;
1627
1628 /*
1629 * Check if the current buffer may be abandoned.
1630 */
1631 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1632 {
1633#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1634 if ((p_confirm || cmdmod.confirm) && p_write)
1635 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001636 bufref_T bufref;
1637
1638 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001640 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001641 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 if (bufIsChanged(curbuf))
1645#endif
1646 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001647 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 return FAIL;
1649 }
1650 }
1651
Bram Moolenaarc667da52019-11-30 20:52:27 +01001652 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 set_curbuf(buf, action);
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001656 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001658#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001659 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 return FAIL;
1661#endif
1662
1663 return OK;
1664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666/*
1667 * Set current buffer to "buf". Executes autocommands and closes current
1668 * buffer. "action" tells how to close the current buffer:
1669 * DOBUF_GOTO free or hide it
1670 * DOBUF_SPLIT nothing
1671 * DOBUF_UNLOAD unload it
1672 * DOBUF_DEL delete it
1673 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001674 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 */
1676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001677set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 buf_T *prevbuf;
1680 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001681 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001682#ifdef FEAT_SYN_HL
1683 long old_tw = curbuf->b_p_tw;
1684#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001685 bufref_T newbufref;
1686 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
1688 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001689 if (!cmdmod.keepalt)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001690 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1691 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaarc667da52019-11-30 20:52:27 +01001693 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaarc667da52019-11-30 20:52:27 +01001696 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001698 set_bufref(&prevbufref, prevbuf);
1699 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001701 // Autocommands may delete the current buffer and/or the buffer we want to go
Bram Moolenaarc667da52019-11-30 20:52:27 +01001702 // to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001703 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001704 || (bufref_valid(&prevbufref)
1705 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001706#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001707 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001711#ifdef FEAT_SYN_HL
1712 if (prevbuf == curwin->w_buffer)
1713 reset_synblock(curwin);
1714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001716 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001717#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001718 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001720 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001722 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001723 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001724
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001725 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001726 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1728 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001729 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001730 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1731 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001732 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001733 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001734 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001737 // An autocommand may have deleted "buf", already entered it (e.g., when
1738 // it did ":bunload") or aborted the script processing.
1739 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001740 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001741#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001742 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001744 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001747#ifdef FEAT_SYN_HL
1748 if (old_tw != curbuf->b_p_tw)
1749 check_colorcolumn(curwin);
1750#endif
1751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
1754/*
1755 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001756 * Old curbuf must have been abandoned already! This also means "curbuf" may
1757 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001760enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001762 // Get the buffer in the current window.
1763 curwin->w_buffer = buf;
1764 curbuf = buf;
1765 ++curbuf->b_nwindows;
1766
1767 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1769 if (!buf->b_help)
1770 get_winopts(buf);
1771#ifdef FEAT_FOLDING
1772 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001773 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001775 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001779 if (curwin->w_p_diff)
1780 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#endif
1782
Bram Moolenaara971b822011-09-14 14:43:25 +02001783#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001784 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001785#endif
1786
Bram Moolenaarc667da52019-11-30 20:52:27 +01001787 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 curwin->w_cursor.lnum = 1;
1789 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001792 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaarc667da52019-11-30 20:52:27 +01001794 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001795 curwin->w_valid = 0;
1796
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001797 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1798 curbuf->b_last_cursor.col, TRUE);
1799
Bram Moolenaarc667da52019-11-30 20:52:27 +01001800 // Make sure the buffer is loaded.
1801 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001802 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001803 // If there is no filetype, allow for detecting one. Esp. useful for
1804 // ":ball" used in a autocommand. If there already is a filetype we
1805 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001806 if (*curbuf->b_p_ft == NUL)
1807 did_filetype = FALSE;
1808
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001809 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 else
1812 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001813 if (!msg_silent && !shortmess(SHM_FILEINFO))
1814 need_fileinfo = TRUE; // display file info after redraw
1815
1816 // check if file changed
1817 (void)buf_check_timestamp(curbuf, FALSE);
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001820#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1824 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826
Bram Moolenaarc667da52019-11-30 20:52:27 +01001827 // If autocommands did not change the cursor position, restore cursor lnum
1828 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (curwin->w_cursor.lnum == 1 && inindent(0))
1830 buflist_getfpos();
1831
Bram Moolenaarc667da52019-11-30 20:52:27 +01001832 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifdef FEAT_TITLE
1834 maketitle();
1835#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001836 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001837 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001838 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar009b2592004-10-24 19:18:58 +00001840#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001841 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001842 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001843#endif
1844
Bram Moolenaarc667da52019-11-30 20:52:27 +01001845 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001846 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848#ifdef FEAT_KEYMAP
1849 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001850 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001852#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001853 // May need to set the spell language. Can only do this after the buffer
1854 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001855 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1856 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001857#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001858#ifdef FEAT_VIMINFO
1859 curbuf->b_last_used = vim_time();
1860#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 redraw_later(NOT_VALID);
1863}
1864
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001865#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1866/*
1867 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001868 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001869 */
1870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001871do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001872{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001873 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001874 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001875 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001876 shorten_fnames(TRUE);
1877}
1878#endif
1879
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001880 void
1881no_write_message(void)
1882{
1883#ifdef FEAT_TERMINAL
1884 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001885 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001886 else
1887#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001889}
1890
1891 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001892no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001893{
1894#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001895 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001896 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001897 else
1898#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001900}
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902/*
1903 * functions for dealing with the buffer list
1904 */
1905
1906/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001907 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1908 * only one window. That means it can be re-used.
1909 */
1910 int
1911curbuf_reusable(void)
1912{
1913 return (curbuf != NULL
1914 && curbuf->b_ffname == NULL
1915 && curbuf->b_nwindows <= 1
1916 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001917#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001918 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001919#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001920 && !curbufIsChanged());
1921}
1922
1923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 * Add a file name to the buffer list. Return a pointer to the buffer.
1925 * If the same file name already exists return a pointer to that buffer.
1926 * If it does not exist, or if fname == NULL, a new entry is created.
1927 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1928 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1929 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001930 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001931 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1932 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001933 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 * This is the ONLY way to create a new buffer.
1935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001937buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001938 char_u *ffname_arg, // full path of fname or relative
1939 char_u *sfname_arg, // short fname or NULL
1940 linenr_T lnum, // preferred cursor line
1941 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001943 char_u *ffname = ffname_arg;
1944 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 buf_T *buf;
1946#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001947 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#endif
1949
Bram Moolenaar480778b2016-07-14 22:09:39 +02001950 if (top_file_num == 1)
1951 hash_init(&buf_hashtab);
1952
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001953 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 /*
1956 * If file name already exists in the list, update the entry.
1957 */
1958#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001959 // On Unix we can use inode numbers when the file exists. Works better
1960 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1962 st.st_dev = (dev_T)-1;
1963#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001964 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#ifdef UNIX
1966 buflist_findname_stat(ffname, &st)
1967#else
1968 buflist_findname(ffname)
1969#endif
1970 ) != NULL)
1971 {
1972 vim_free(ffname);
1973 if (lnum != 0)
1974 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001975
1976 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001977 // copy the options now, if 'cpo' doesn't have 's' and not done
1978 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02001979 buf_copy_options(buf, 0);
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1982 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001983 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001986 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001988 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001989 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001990 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001991 return NULL;
1992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 return buf;
1995 }
1996
1997 /*
1998 * If the current buffer has no name and no contents, use the current
1999 * buffer. Otherwise: Need to allocate a new buffer structure.
2000 *
2001 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002002 * (A spell file buffer is allocated in spell.c, but that's not a normal
2003 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002006 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002009 // It's like this buffer is deleted. Watch out for autocommands that
2010 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (curbuf->b_p_bl)
2012 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2013 if (buf == curbuf)
2014 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002015#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002016 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002017 {
2018 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002024 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 clear_string_option(&buf->b_p_bh);
2026 clear_string_option(&buf->b_p_bt);
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 if (buf != curbuf || curbuf == NULL)
2030 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002031 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (buf == NULL)
2033 {
2034 vim_free(ffname);
2035 return NULL;
2036 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002037#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002038 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002039 buf->b_vars = dict_alloc();
2040 if (buf->b_vars == NULL)
2041 {
2042 vim_free(ffname);
2043 vim_free(buf);
2044 return NULL;
2045 }
2046 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2047#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002048 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050
2051 if (ffname != NULL)
2052 {
2053 buf->b_ffname = ffname;
2054 buf->b_sfname = vim_strsave(sfname);
2055 }
2056
2057 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002058 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2061 || buf->b_wininfo == NULL)
2062 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002063 if (buf->b_sfname != buf->b_ffname)
2064 VIM_CLEAR(buf->b_sfname);
2065 else
2066 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002067 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (buf != curbuf)
2069 free_buffer(buf);
2070 return NULL;
2071 }
2072
2073 if (buf == curbuf)
2074 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002075 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002076 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002077 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002079#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002080 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return NULL;
2082#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002083 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002084
Bram Moolenaarc667da52019-11-30 20:52:27 +01002085 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002086 buf->b_p_initialized = FALSE;
2087 buf_copy_options(buf, BCO_ENTER);
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002090 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 curbuf->b_kmap_state |= KEYMAP_INIT;
2092#endif
2093 }
2094 else
2095 {
2096 /*
2097 * put new buffer at the end of the buffer list
2098 */
2099 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002100 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 buf->b_prev = NULL;
2103 firstbuf = buf;
2104 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002105 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 lastbuf->b_next = buf;
2108 buf->b_prev = lastbuf;
2109 }
2110 lastbuf = buf;
2111
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002112 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2113 {
2114 // Recycle a previously used buffer number. Used for buffers which
2115 // are normally hidden, e.g. in a popup window. Avoids that the
2116 // buffer number grows rapidly.
2117 --buf_reuse.ga_len;
2118 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002119
2120 // Move buffer to the right place in the buffer list.
2121 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2122 {
2123 buf_T *prev = buf->b_prev;
2124
2125 prev->b_next = buf->b_next;
2126 if (prev->b_next != NULL)
2127 prev->b_next->b_prev = prev;
2128 buf->b_next = prev;
2129 buf->b_prev = prev->b_prev;
2130 if (buf->b_prev != NULL)
2131 buf->b_prev->b_next = buf;
2132 prev->b_prev = buf;
2133 if (lastbuf == buf)
2134 lastbuf = prev;
2135 if (firstbuf == prev)
2136 firstbuf = buf;
2137 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002138 }
2139 else
2140 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002141 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002143 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (emsg_silent == 0)
2145 {
2146 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002147 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149 top_file_num = 1;
2150 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002151 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 /*
2154 * Always copy the options from the current buffer.
2155 */
2156 buf_copy_options(buf, BCO_ALWAYS);
2157 }
2158
2159 buf->b_wininfo->wi_fpos.lnum = lnum;
2160 buf->b_wininfo->wi_win = curwin;
2161
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002162#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002163 hash_init(&buf->b_s.b_keywtab);
2164 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165#endif
2166
2167 buf->b_fname = buf->b_sfname;
2168#ifdef UNIX
2169 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002170 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 else
2172 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002173 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 buf->b_dev = st.st_dev;
2175 buf->b_ino = st.st_ino;
2176 }
2177#endif
2178 buf->b_u_synced = TRUE;
2179 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002180 if (flags & BLN_DUMMY)
2181 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002183 clrallmarks(buf); // clear marks
2184 fmarks_check_names(buf); // check file marks for this file
2185 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (!(flags & BLN_DUMMY))
2187 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002188 bufref_T bufref;
2189
Bram Moolenaarc667da52019-11-30 20:52:27 +01002190 // Tricky: these autocommands may change the buffer list. They could
2191 // also split the window with re-using the one empty buffer. This may
2192 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002193 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002194 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002195 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002196 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002198 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002199 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002200 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002201 return NULL;
2202 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002203#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002204 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 return buf;
2210}
2211
2212/*
2213 * Free the memory for the options of a buffer.
2214 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2215 * 'fileencoding'.
2216 */
2217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218free_buf_options(
2219 buf_T *buf,
2220 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 if (free_p_ff)
2223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 clear_string_option(&buf->b_p_bh);
2227 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229#ifdef FEAT_FIND_ID
2230 clear_string_option(&buf->b_p_def);
2231 clear_string_option(&buf->b_p_inc);
2232# ifdef FEAT_EVAL
2233 clear_string_option(&buf->b_p_inex);
2234# endif
2235#endif
2236#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2237 clear_string_option(&buf->b_p_inde);
2238 clear_string_option(&buf->b_p_indk);
2239#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002240#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2241 clear_string_option(&buf->b_p_bexpr);
2242#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002243#if defined(FEAT_CRYPT)
2244 clear_string_option(&buf->b_p_cm);
2245#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002246 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002247#if defined(FEAT_EVAL)
2248 clear_string_option(&buf->b_p_fex);
2249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#ifdef FEAT_CRYPT
2251 clear_string_option(&buf->b_p_key);
2252#endif
2253 clear_string_option(&buf->b_p_kp);
2254 clear_string_option(&buf->b_p_mps);
2255 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002256 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002258#ifdef FEAT_VARTABS
2259 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002260 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002261 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002262 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002263 buf->b_p_vsts_array = NULL;
2264 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002265 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#ifdef FEAT_KEYMAP
2268 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002269 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 ga_clear(&buf->b_kmap_ga);
2271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#ifdef FEAT_FOLDING
2274 clear_string_option(&buf->b_p_cms);
2275#endif
2276 clear_string_option(&buf->b_p_nf);
2277#ifdef FEAT_SYN_HL
2278 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002279 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002280#endif
2281#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002282 clear_string_option(&buf->b_s.b_p_spc);
2283 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002284 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002285 buf->b_s.b_cap_prog = NULL;
2286 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287#endif
2288#ifdef FEAT_SEARCHPATH
2289 clear_string_option(&buf->b_p_sua);
2290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292#ifdef FEAT_CINDENT
2293 clear_string_option(&buf->b_p_cink);
2294 clear_string_option(&buf->b_p_cino);
2295#endif
2296#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2297 clear_string_option(&buf->b_p_cinw);
2298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002300#ifdef FEAT_COMPL_FUNC
2301 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002302 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#ifdef FEAT_QUICKFIX
2305 clear_string_option(&buf->b_p_gp);
2306 clear_string_option(&buf->b_p_mp);
2307 clear_string_option(&buf->b_p_efm);
2308#endif
2309 clear_string_option(&buf->b_p_ep);
2310 clear_string_option(&buf->b_p_path);
2311 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002312 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002313#ifdef FEAT_EVAL
2314 clear_string_option(&buf->b_p_tfu);
2315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 clear_string_option(&buf->b_p_dict);
2317 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002318#ifdef FEAT_TEXTOBJ
2319 clear_string_option(&buf->b_p_qe);
2320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002322 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002323#ifdef FEAT_LISP
2324 clear_string_option(&buf->b_p_lw);
2325#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002326 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002327 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328}
2329
2330/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002331 * Get alternate file "n".
2332 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2333 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * if (options & GETF_SETMARK) call setpcmark()
2335 * if (options & GETF_ALT) we are jumping to an alternate file.
2336 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2337 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002338 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 */
2340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002341buflist_getfile(
2342 int n,
2343 linenr_T lnum,
2344 int options,
2345 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
2347 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 pos_T *fpos;
2350 colnr_T col;
2351
2352 buf = buflist_findnr(n);
2353 if (buf == NULL)
2354 {
2355 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002356 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002358 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return FAIL;
2360 }
2361
Bram Moolenaarc667da52019-11-30 20:52:27 +01002362 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (buf == curbuf)
2364 return OK;
2365
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002366 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002367 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002368 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002370 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002371 if (curbuf_locked())
2372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaarc667da52019-11-30 20:52:27 +01002374 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (lnum == 0)
2376 {
2377 fpos = buflist_findfpos(buf);
2378 lnum = fpos->lnum;
2379 col = fpos->col;
2380 }
2381 else
2382 col = 0;
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (options & GETF_SWITCH)
2385 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002386 // If 'switchbuf' contains "useopen": jump to first window containing
2387 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002388 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002390
Bram Moolenaarc667da52019-11-30 20:52:27 +01002391 // If 'switchbuf' contains "usetab": jump to first window in any tab
2392 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002393 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002394 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002395
Bram Moolenaarc667da52019-11-30 20:52:27 +01002396 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2397 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002398 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002399 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002401 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002402 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002403 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2404 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002406 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002411 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2412 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 --RedrawingDisabled;
2415
Bram Moolenaarc667da52019-11-30 20:52:27 +01002416 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (!p_sol && col != 0)
2418 {
2419 curwin->w_cursor.col = col;
2420 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 curwin->w_set_curswant = TRUE;
2423 }
2424 return OK;
2425 }
2426 --RedrawingDisabled;
2427 return FAIL;
2428}
2429
2430/*
2431 * go to the last know line number for the current buffer
2432 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002434buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 pos_T *fpos;
2437
2438 fpos = buflist_findfpos(curbuf);
2439
2440 curwin->w_cursor.lnum = fpos->lnum;
2441 check_cursor_lnum();
2442
2443 if (p_sol)
2444 curwin->w_cursor.col = 0;
2445 else
2446 {
2447 curwin->w_cursor.col = fpos->col;
2448 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 curwin->w_set_curswant = TRUE;
2451 }
2452}
2453
Bram Moolenaar81695252004-12-29 20:58:21 +00002454#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2455/*
2456 * Find file in buffer list by name (it has to be for the current window).
2457 * Returns NULL if not found.
2458 */
2459 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002460buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002461{
2462 char_u *ffname;
2463 buf_T *buf = NULL;
2464
Bram Moolenaarc667da52019-11-30 20:52:27 +01002465 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002466 ffname = FullName_save(fname,
2467#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002468 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002469#else
2470 FALSE
2471#endif
2472 );
2473 if (ffname != NULL)
2474 {
2475 buf = buflist_findname(ffname);
2476 vim_free(ffname);
2477 }
2478 return buf;
2479}
2480#endif
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482/*
2483 * Find file in buffer list by name (it has to be for the current window).
2484 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002485 * Skips dummy buffers.
2486 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
2488 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002489buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002492 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 if (mch_stat((char *)ffname, &st) < 0)
2495 st.st_dev = (dev_T)-1;
2496 return buflist_findname_stat(ffname, &st);
2497}
2498
2499/*
2500 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2501 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002502 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
2504 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002505buflist_findname_stat(
2506 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002507 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509#endif
2510 buf_T *buf;
2511
Bram Moolenaarc667da52019-11-30 20:52:27 +01002512 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002513 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002514 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515#ifdef UNIX
2516 , stp
2517#endif
2518 ))
2519 return buf;
2520 return NULL;
2521}
2522
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523/*
2524 * Find file in buffer list by a regexp pattern.
2525 * Return fnum of the found buffer.
2526 * Return < 0 for error.
2527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002529buflist_findpat(
2530 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002531 char_u *pattern_end, // pointer to first char after pattern
2532 int unlisted, // find unlisted buffers
2533 int diffmode UNUSED, // find diff-mode buffers only
2534 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 int match = -1;
2538 int find_listed;
2539 char_u *pat;
2540 char_u *patend;
2541 int attempt;
2542 char_u *p;
2543 int toggledollar;
2544
2545 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2546 {
2547 if (*pattern == '%')
2548 match = curbuf->b_fnum;
2549 else
2550 match = curwin->w_alt_fnum;
2551#ifdef FEAT_DIFF
2552 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2553 match = -1;
2554#endif
2555 }
2556
2557 /*
2558 * Try four ways of matching a listed buffer:
2559 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002560 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 * attempt == 2: with '$' at end (only match at end)
2562 * attempt == 3: with '^' at start and '$' at end (only full match)
2563 * Repeat this for finding an unlisted buffer if there was no matching
2564 * listed buffer.
2565 */
2566 else
2567 {
2568 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2569 if (pat == NULL)
2570 return -1;
2571 patend = pat + STRLEN(pat) - 1;
2572 toggledollar = (patend > pat && *patend == '$');
2573
Bram Moolenaarc667da52019-11-30 20:52:27 +01002574 // First try finding a listed buffer. If not found and "unlisted"
2575 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 find_listed = TRUE;
2577 for (;;)
2578 {
2579 for (attempt = 0; attempt <= 3; ++attempt)
2580 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002581 regmatch_T regmatch;
2582
Bram Moolenaarc667da52019-11-30 20:52:27 +01002583 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002585 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002587 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002589 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2590 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 vim_free(pat);
2593 return -1;
2594 }
2595
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002596 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (buf->b_p_bl == find_listed
2598#ifdef FEAT_DIFF
2599 && (!diffmode || diff_mode_buf(buf))
2600#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002601 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002603 if (curtab_only)
2604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002605 // Ignore the match if the buffer is not open in
2606 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002607 win_T *wp;
2608
Bram Moolenaar29323592016-07-24 22:04:11 +02002609 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002610 if (wp->w_buffer == buf)
2611 break;
2612 if (wp == NULL)
2613 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002614 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002615 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 match = -2;
2618 break;
2619 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002620 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002623 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002624 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 break;
2626 }
2627
Bram Moolenaarc667da52019-11-30 20:52:27 +01002628 // Only search for unlisted buffers if there was no match with
2629 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (!unlisted || !find_listed || match != -1)
2631 break;
2632 find_listed = FALSE;
2633 }
2634
2635 vim_free(pat);
2636 }
2637
2638 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002639 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002641 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return match;
2643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
Bram Moolenaar52410572019-10-27 05:12:45 +01002645#ifdef FEAT_VIMINFO
2646typedef struct {
2647 buf_T *buf;
2648 char_u *match;
2649} bufmatch_T;
2650#endif
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652/*
2653 * Find all buffer names that match.
2654 * For command line expansion of ":buf" and ":sbuf".
2655 * Return OK if matches found, FAIL otherwise.
2656 */
2657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002658ExpandBufnames(
2659 char_u *pat,
2660 int *num_file,
2661 char_u ***file,
2662 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 int count = 0;
2665 buf_T *buf;
2666 int round;
2667 char_u *p;
2668 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002669 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002670#ifdef FEAT_VIMINFO
2671 bufmatch_T *matches = NULL;
2672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaarc667da52019-11-30 20:52:27 +01002674 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *file = NULL;
2676
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002677#ifdef FEAT_DIFF
2678 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2679 return FAIL;
2680#endif
2681
Bram Moolenaarc667da52019-11-30 20:52:27 +01002682 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002683 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002685 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002686 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002688 STRCPY(patc, "\\(^\\|[\\/]\\)");
2689 STRCPY(patc + 11, pat + 1);
2690 }
2691 else
2692 patc = pat;
2693
2694 /*
2695 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002696 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002697 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002698 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002699 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002700 regmatch_T regmatch;
2701
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002702 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002703 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002704 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2705 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002706 {
2707 if (patc != pat)
2708 vim_free(patc);
2709 return FAIL;
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 /*
2713 * round == 1: Count the matches.
2714 * round == 2: Build the array to keep the matches.
2715 */
2716 for (round = 1; round <= 2; ++round)
2717 {
2718 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002719 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002721 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002723#ifdef FEAT_DIFF
2724 if (options & BUF_DIFF_FILTER)
2725 // Skip buffers not suitable for
2726 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002727 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002728 continue;
2729#endif
2730
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002731 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (p != NULL)
2733 {
2734 if (round == 1)
2735 ++count;
2736 else
2737 {
2738 if (options & WILD_HOME_REPLACE)
2739 p = home_replace_save(buf, p);
2740 else
2741 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002742#ifdef FEAT_VIMINFO
2743 if (matches != NULL)
2744 {
2745 matches[count].buf = buf;
2746 matches[count].match = p;
2747 count++;
2748 }
2749 else
2750#endif
2751 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
2753 }
2754 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002755 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 break;
2757 if (round == 1)
2758 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002759 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (*file == NULL)
2761 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002762 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002763 if (patc != pat)
2764 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 return FAIL;
2766 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002767#ifdef FEAT_VIMINFO
2768 if (options & WILD_BUFLASTUSED)
2769 matches = ALLOC_MULT(bufmatch_T, count);
2770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002773 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002774 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 break;
2776 }
2777
Bram Moolenaar05159a02005-02-26 23:04:13 +00002778 if (patc != pat)
2779 vim_free(patc);
2780
Bram Moolenaar52410572019-10-27 05:12:45 +01002781#ifdef FEAT_VIMINFO
2782 if (matches != NULL)
2783 {
2784 int i;
2785 if (count > 1)
2786 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2787 // if the current buffer is first in the list, place it at the end
2788 if (matches[0].buf == curbuf)
2789 {
2790 for (i = 1; i < count; i++)
2791 (*file)[i-1] = matches[i].match;
2792 (*file)[count-1] = matches[0].match;
2793 }
2794 else
2795 {
2796 for (i = 0; i < count; i++)
2797 (*file)[i] = matches[i].match;
2798 }
2799 vim_free(matches);
2800 }
2801#endif
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 *num_file = count;
2804 return (count == 0 ? FAIL : OK);
2805}
2806
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807/*
2808 * Check for a match on the file name for buffer "buf" with regprog "prog".
2809 */
2810 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002811buflist_match(
2812 regmatch_T *rmp,
2813 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002814 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 char_u *match;
2817
Bram Moolenaarc667da52019-11-30 20:52:27 +01002818 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002819 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002821 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 return match;
2824}
2825
2826/*
2827 * Try matching the regexp in "prog" with file name "name".
2828 * Return "name" when there is a match, NULL when not.
2829 */
2830 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002831fname_match(
2832 regmatch_T *rmp,
2833 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002834 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 char_u *match = NULL;
2837 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 if (name != NULL)
2840 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002841 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002842 rmp->rm_ic = p_fic || ignore_case;
2843 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 match = name;
2845 else
2846 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002847 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002849 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 match = name;
2851 vim_free(p);
2852 }
2853 }
2854
2855 return match;
2856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002859 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 */
2861 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002862buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002864 char_u key[VIM_SIZEOF_INT * 2 + 1];
2865 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 if (nr == 0)
2868 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002869 sprintf((char *)key, "%x", nr);
2870 hi = hash_find(&buf_hashtab, key);
2871
2872 if (!HASHITEM_EMPTY(hi))
2873 return (buf_T *)(hi->hi_key
2874 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 return NULL;
2876}
2877
2878/*
2879 * Get name of file 'n' in the buffer list.
2880 * When the file has no name an empty string is returned.
2881 * home_replace() is used to shorten the file name (used for marks).
2882 * Returns a pointer to allocated memory, of NULL when failed.
2883 */
2884 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002885buflist_nr2name(
2886 int n,
2887 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002888 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 buf_T *buf;
2891
2892 buf = buflist_findnr(n);
2893 if (buf == NULL)
2894 return NULL;
2895 return home_replace_save(helptail ? buf : NULL,
2896 fullname ? buf->b_ffname : buf->b_fname);
2897}
2898
2899/*
2900 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2901 * When "copy_options" is TRUE save the local window option values.
2902 * When "lnum" is 0 only do the options.
2903 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905buflist_setfpos(
2906 buf_T *buf,
2907 win_T *win,
2908 linenr_T lnum,
2909 colnr_T col,
2910 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
2912 wininfo_T *wip;
2913
2914 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2915 if (wip->wi_win == win)
2916 break;
2917 if (wip == NULL)
2918 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002919 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002920 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if (wip == NULL)
2922 return;
2923 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002924 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 lnum = 1;
2926 }
2927 else
2928 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002929 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (wip->wi_prev)
2931 wip->wi_prev->wi_next = wip->wi_next;
2932 else
2933 buf->b_wininfo = wip->wi_next;
2934 if (wip->wi_next)
2935 wip->wi_next->wi_prev = wip->wi_prev;
2936 if (copy_options && wip->wi_optset)
2937 {
2938 clear_winopt(&wip->wi_opt);
2939#ifdef FEAT_FOLDING
2940 deleteFoldRecurse(&wip->wi_folds);
2941#endif
2942 }
2943 }
2944 if (lnum != 0)
2945 {
2946 wip->wi_fpos.lnum = lnum;
2947 wip->wi_fpos.col = col;
2948 }
2949 if (copy_options)
2950 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002951 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2953#ifdef FEAT_FOLDING
2954 wip->wi_fold_manual = win->w_fold_manual;
2955 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2956#endif
2957 wip->wi_optset = TRUE;
2958 }
2959
Bram Moolenaarc667da52019-11-30 20:52:27 +01002960 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 wip->wi_next = buf->b_wininfo;
2962 buf->b_wininfo = wip;
2963 wip->wi_prev = NULL;
2964 if (wip->wi_next)
2965 wip->wi_next->wi_prev = wip;
2966
2967 return;
2968}
2969
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002970#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002971/*
2972 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2973 * page. That's because a diff is local to a tab page.
2974 */
2975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002976wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002977{
2978 win_T *wp;
2979
2980 if (wip->wi_opt.wo_diff)
2981 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002982 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002983 // return FALSE when it's a window in the current tab page, thus
2984 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002985 if (wip->wi_win == wp)
2986 return FALSE;
2987 return TRUE;
2988 }
2989 return FALSE;
2990}
2991#endif
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993/*
2994 * Find info for the current window in buffer "buf".
2995 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002996 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2997 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 * Returns NULL when there isn't any info.
2999 */
3000 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001find_wininfo(
3002 buf_T *buf,
3003 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 wininfo_T *wip;
3006
3007 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003008 if (wip->wi_win == curwin
3009#ifdef FEAT_DIFF
3010 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3011#endif
3012 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003014
Bram Moolenaarc667da52019-11-30 20:52:27 +01003015 // If no wininfo for curwin, use the first in the list (that doesn't have
3016 // 'diff' set and is in another tab page).
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003017 if (wip == NULL)
3018 {
3019#ifdef FEAT_DIFF
3020 if (skip_diff_buffer)
3021 {
3022 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
3023 if (!wininfo_other_tab_diff(wip))
3024 break;
3025 }
3026 else
3027#endif
3028 wip = buf->b_wininfo;
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 return wip;
3031}
3032
3033/*
3034 * Reset the local window options to the values last used in this window.
3035 * If the buffer wasn't used in this window before, use the values from
3036 * the most recently used window. If the values were never set, use the
3037 * global values for the window.
3038 */
3039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003040get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 wininfo_T *wip;
3043
3044 clear_winopt(&curwin->w_onebuf_opt);
3045#ifdef FEAT_FOLDING
3046 clearFolding(curwin);
3047#endif
3048
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003049 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003050 if (wip != NULL && wip->wi_win != NULL
3051 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003053 // The buffer is currently displayed in the window: use the actual
3054 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003055 win_T *wp = wip->wi_win;
3056
3057 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3058#ifdef FEAT_FOLDING
3059 curwin->w_fold_manual = wp->w_fold_manual;
3060 curwin->w_foldinvalid = TRUE;
3061 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3062#endif
3063 }
3064 else if (wip != NULL && wip->wi_optset)
3065 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003066 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3068#ifdef FEAT_FOLDING
3069 curwin->w_fold_manual = wip->wi_fold_manual;
3070 curwin->w_foldinvalid = TRUE;
3071 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3072#endif
3073 }
3074 else
3075 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3076
3077#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003078 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (p_fdls >= 0)
3080 curwin->w_p_fdl = p_fdls;
3081#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003082 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083}
3084
3085/*
3086 * Find the position (lnum and col) for the buffer 'buf' for the current
3087 * window.
3088 * Returns a pointer to no_position if no position is found.
3089 */
3090 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003091buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003094 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003096 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (wip != NULL)
3098 return &(wip->wi_fpos);
3099 else
3100 return &no_position;
3101}
3102
3103/*
3104 * Find the lnum for the buffer 'buf' for the current window.
3105 */
3106 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 return buflist_findfpos(buf)->lnum;
3110}
3111
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003113 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaar52410572019-10-27 05:12:45 +01003118 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 int len;
3120 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003121 int ro_char;
3122 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003123#ifdef FEAT_TERMINAL
3124 int job_running;
3125 int job_none_open;
3126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaar52410572019-10-27 05:12:45 +01003128#ifdef FEAT_VIMINFO
3129 garray_T buflist;
3130 buf_T **buflist_data = NULL, **p;
3131
3132 if (vim_strchr(eap->arg, 't'))
3133 {
3134 ga_init2(&buflist, sizeof(buf_T *), 50);
3135 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3136 {
3137 if (ga_grow(&buflist, 1) == OK)
3138 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3139 }
3140
3141 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3142 sizeof(buf_T *), buf_compare);
3143
Bram Moolenaar3b991522019-11-06 23:26:20 +01003144 buflist_data = (buf_T **)buflist.ga_data;
3145 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003146 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003147 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003148
Bram Moolenaar3b991522019-11-06 23:26:20 +01003149 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003150 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3151 : buf->b_next)
3152#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003156#ifdef FEAT_TERMINAL
3157 job_running = term_job_running(buf->b_term);
3158 job_none_open = job_running && term_none_open(buf->b_term);
3159#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003160 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003161 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3162 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3163 || (vim_strchr(eap->arg, '+')
3164 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3165 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003166 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003167 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003168 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3169#ifdef FEAT_TERMINAL
3170 || (vim_strchr(eap->arg, 'R')
3171 && (!job_running || (job_running && job_none_open)))
3172 || (vim_strchr(eap->arg, '?')
3173 && (!job_running || (job_running && !job_none_open)))
3174 || (vim_strchr(eap->arg, 'F')
3175 && (job_running || buf->b_term == NULL))
3176#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003177 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3178 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3179 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3180 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3181 || (vim_strchr(eap->arg, '#')
3182 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003185 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 else
3187 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003188 if (message_filtered(NameBuff))
3189 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003191 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3192 : (bufIsChanged(buf) ? '+' : ' ');
3193#ifdef FEAT_TERMINAL
3194 if (term_job_running(buf->b_term))
3195 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003196 if (term_none_open(buf->b_term))
3197 ro_char = '?';
3198 else
3199 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003200 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3201 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003202 }
3203 else if (buf->b_term != NULL)
3204 ro_char = 'F';
3205 else
3206#endif
3207 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3208
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003209 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003210 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 buf->b_fnum,
3212 buf->b_p_bl ? ' ' : 'u',
3213 buf == curbuf ? '%' :
3214 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3215 buf->b_ml.ml_mfp == NULL ? ' ' :
3216 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003217 ro_char,
3218 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003219 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003220 if (len > IOSIZE - 20)
3221 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaarc667da52019-11-30 20:52:27 +01003223 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 i = 40 - vim_strsize(IObuff);
3225 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003227 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003228#ifdef FEAT_VIMINFO
3229 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3230 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3231 else
3232#endif
3233 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3234 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003235 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003237 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 ui_breakcheck();
3239 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003240
3241#ifdef FEAT_VIMINFO
3242 if (buflist_data)
3243 ga_clear(&buflist);
3244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246
3247/*
3248 * Get file name and line number for file 'fnum'.
3249 * Used by DoOneCmd() for translating '%' and '#'.
3250 * Used by insert_reg() and cmdline_paste() for '#' register.
3251 * Return FAIL if not found, OK for success.
3252 */
3253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003254buflist_name_nr(
3255 int fnum,
3256 char_u **fname,
3257 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 buf_T *buf;
3260
3261 buf = buflist_findnr(fnum);
3262 if (buf == NULL || buf->b_fname == NULL)
3263 return FAIL;
3264
3265 *fname = buf->b_fname;
3266 *lnum = buflist_findlnum(buf);
3267
3268 return OK;
3269}
3270
3271/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003272 * Set the file name for "buf"' to "ffname_arg", short file name to
3273 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * The file name with the full path is also remembered, for when :cd is used.
3275 * Returns FAIL for failure (file name already in use by other buffer)
3276 * OK otherwise.
3277 */
3278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279setfname(
3280 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003281 char_u *ffname_arg,
3282 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003283 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003285 char_u *ffname = ffname_arg;
3286 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003287 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003289 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#endif
3291
3292 if (ffname == NULL || *ffname == NUL)
3293 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003294 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003295 if (buf->b_sfname != buf->b_ffname)
3296 VIM_CLEAR(buf->b_sfname);
3297 else
3298 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003299 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef UNIX
3301 st.st_dev = (dev_T)-1;
3302#endif
3303 }
3304 else
3305 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003306 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3307 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return FAIL;
3309
3310 /*
3311 * if the file name is already used in another buffer:
3312 * - if the buffer is loaded, fail
3313 * - if the buffer is not loaded, delete it from the list
3314 */
3315#ifdef UNIX
3316 if (mch_stat((char *)ffname, &st) < 0)
3317 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003318#endif
3319 if (!(buf->b_flags & BF_DUMMY))
3320#ifdef UNIX
3321 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003323 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
3325 if (obuf != NULL && obuf != buf)
3326 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003327 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003330 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 vim_free(ffname);
3332 return FAIL;
3333 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003334 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003335 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 sfname = vim_strsave(sfname);
3338 if (ffname == NULL || sfname == NULL)
3339 {
3340 vim_free(sfname);
3341 vim_free(ffname);
3342 return FAIL;
3343 }
3344#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003345 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003347 if (buf->b_sfname != buf->b_ffname)
3348 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 buf->b_ffname = ffname;
3351 buf->b_sfname = sfname;
3352 }
3353 buf->b_fname = buf->b_sfname;
3354#ifdef UNIX
3355 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003356 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
3358 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003359 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 buf->b_dev = st.st_dev;
3361 buf->b_ino = st.st_ino;
3362 }
3363#endif
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 buf_name_changed(buf);
3368 return OK;
3369}
3370
3371/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003372 * Crude way of changing the name of a buffer. Use with care!
3373 * The name should be relative to the current directory.
3374 */
3375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003376buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003377{
3378 buf_T *buf;
3379
3380 buf = buflist_findnr(fnum);
3381 if (buf != NULL)
3382 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003383 if (buf->b_sfname != buf->b_ffname)
3384 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003385 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003386 buf->b_ffname = vim_strsave(name);
3387 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003388 // Allocate ffname and expand into full path. Also resolves .lnk
3389 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003390 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003391 buf->b_fname = buf->b_sfname;
3392 }
3393}
3394
3395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * Take care of what needs to be done when the name of buffer "buf" has
3397 * changed.
3398 */
3399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003400buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 /*
3403 * If the file name changed, also change the name of the swapfile
3404 */
3405 if (buf->b_ml.ml_mfp != NULL)
3406 ml_setname(buf);
3407
3408 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003409 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003411 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003413 status_redraw_all(); // status lines need to be redrawn
3414 fmarks_check_names(buf); // check named file marks
3415 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416}
3417
3418/*
3419 * set alternate file name for current window
3420 *
3421 * Used by do_one_cmd(), do_write() and do_ecmd().
3422 * Return the buffer.
3423 */
3424 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003425setaltfname(
3426 char_u *ffname,
3427 char_u *sfname,
3428 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 buf_T *buf;
3431
Bram Moolenaarc667da52019-11-30 20:52:27 +01003432 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003434 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 curwin->w_alt_fnum = buf->b_fnum;
3436 return buf;
3437}
3438
3439/*
3440 * Get alternate file name for current window.
3441 * Return NULL if there isn't any, and give error message if requested.
3442 */
3443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003444getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003445 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 char_u *fname;
3448 linenr_T dummy;
3449
3450 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3451 {
3452 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003453 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 return NULL;
3455 }
3456 return fname;
3457}
3458
3459/*
3460 * Add a file name to the buflist and return its number.
3461 * Uses same flags as buflist_new(), except BLN_DUMMY.
3462 *
3463 * used by qf_init(), main() and doarglist()
3464 */
3465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003466buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 buf_T *buf;
3469
3470 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3471 if (buf != NULL)
3472 return buf->b_fnum;
3473 return 0;
3474}
3475
3476#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3477/*
3478 * Adjust slashes in file names. Called after 'shellslash' was set.
3479 */
3480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003481buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
3483 buf_T *bp;
3484
Bram Moolenaar29323592016-07-24 22:04:11 +02003485 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
3487 if (bp->b_ffname != NULL)
3488 slash_adjust(bp->b_ffname);
3489 if (bp->b_sfname != NULL)
3490 slash_adjust(bp->b_sfname);
3491 }
3492}
3493#endif
3494
3495/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003496 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * Also save the local window option values.
3498 */
3499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003502 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503}
3504
3505/*
3506 * Return TRUE if 'ffname' is not the same file as current file.
3507 * Fname must have a full path (expanded by mch_FullName()).
3508 */
3509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003510otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 return otherfile_buf(curbuf, ffname
3513#ifdef UNIX
3514 , NULL
3515#endif
3516 );
3517}
3518
3519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520otherfile_buf(
3521 buf_T *buf,
3522 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003524 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003526 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003528 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3530 return TRUE;
3531 if (fnamecmp(ffname, buf->b_ffname) == 0)
3532 return FALSE;
3533#ifdef UNIX
3534 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003535 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
Bram Moolenaarc667da52019-11-30 20:52:27 +01003537 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (stp == NULL)
3539 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003540 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 st.st_dev = (dev_T)-1;
3542 stp = &st;
3543 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003544 // Use dev/ino to check if the files are the same, even when the names
3545 // are different (possible with links). Still need to compare the
3546 // name above, for when the file doesn't exist yet.
3547 // Problem: The dev/ino changes when a file is deleted (and created
3548 // again) and remains the same when renamed/moved. We don't want to
3549 // mch_stat() each buffer each time, that would be too slow. Get the
3550 // dev/ino again when they appear to match, but not when they appear
3551 // to be different: Could skip a buffer when it's actually the same
3552 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (buf_same_ino(buf, stp))
3554 {
3555 buf_setino(buf);
3556 if (buf_same_ino(buf, stp))
3557 return FALSE;
3558 }
3559 }
3560#endif
3561 return TRUE;
3562}
3563
3564#if defined(UNIX) || defined(PROTO)
3565/*
3566 * Set inode and device number for a buffer.
3567 * Must always be called when b_fname is changed!.
3568 */
3569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003572 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
3574 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3575 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003576 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 buf->b_dev = st.st_dev;
3578 buf->b_ino = st.st_ino;
3579 }
3580 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003581 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582}
3583
3584/*
3585 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3586 */
3587 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588buf_same_ino(
3589 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003590 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003592 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 && stp->st_dev == buf->b_dev
3594 && stp->st_ino == buf->b_ino);
3595}
3596#endif
3597
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003598/*
3599 * Print info about the current buffer.
3600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003602fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003603 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003604 int shorthelp,
3605 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
3607 char_u *name;
3608 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003609 char *p;
3610 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003611 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003613 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (buffer == NULL)
3615 return;
3616
Bram Moolenaarc667da52019-11-30 20:52:27 +01003617 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003619 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 p = buffer + STRLEN(buffer);
3621 }
3622 else
3623 p = buffer;
3624
3625 *p++ = '"';
3626 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003627 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 else
3629 {
3630 if (!fullname && curbuf->b_fname != NULL)
3631 name = curbuf->b_fname;
3632 else
3633 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003634 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 (int)(IOSIZE - (p - buffer)), TRUE);
3636 }
3637
Bram Moolenaar32526b32019-01-19 17:43:09 +01003638 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 curbufIsChanged() ? (shortmess(SHM_MOD)
3640 ? " [+]" : _(" [Modified]")) : " ",
3641 (curbuf->b_flags & BF_NOTEDITED)
3642#ifdef FEAT_QUICKFIX
3643 && !bt_dontwrite(curbuf)
3644#endif
3645 ? _("[Not edited]") : "",
3646 (curbuf->b_flags & BF_NEW)
3647#ifdef FEAT_QUICKFIX
3648 && !bt_dontwrite(curbuf)
3649#endif
3650 ? _("[New file]") : "",
3651 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003652 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 : _("[readonly]")) : "",
3654 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3655 || curbuf->b_p_ro) ?
3656 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003657 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3658 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (curwin->w_cursor.lnum > 1000000L)
3660 n = (int)(((long)curwin->w_cursor.lnum) /
3661 ((long)curbuf->b_ml.ml_line_count / 100L));
3662 else
3663 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3664 (long)curbuf->b_ml.ml_line_count);
3665 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003666 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667#ifdef FEAT_CMDL_INFO
3668 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003669 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003670 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003671 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3672 curbuf->b_ml.ml_line_count),
3673 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674#endif
3675 else
3676 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 _("line %ld of %ld --%d%%-- col "),
3679 (long)curwin->w_cursor.lnum,
3680 (long)curbuf->b_ml.ml_line_count,
3681 n);
3682 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003683 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003684 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3686 }
3687
Bram Moolenaar32526b32019-01-19 17:43:09 +01003688 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3689 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 if (dont_truncate)
3692 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003693 // Temporarily set msg_scroll to avoid the message being truncated.
3694 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 msg_start();
3696 n = msg_scroll;
3697 msg_scroll = TRUE;
3698 msg(buffer);
3699 msg_scroll = n;
3700 }
3701 else
3702 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003703 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003705 // Need to repeat the message after redrawing when:
3706 // - When restart_edit is set (otherwise there will be a delay
3707 // before redrawing).
3708 // - When the screen was scrolled but there is no wait-return
3709 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003710 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712
3713 vim_free(buffer);
3714}
3715
3716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717col_print(
3718 char_u *buf,
3719 size_t buflen,
3720 int col,
3721 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003724 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003726 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727}
3728
3729#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730static char_u *lasttitle = NULL;
3731static char_u *lasticon = NULL;
3732
Bram Moolenaar84a93082018-06-16 22:58:15 +02003733/*
3734 * Put the file name in the title bar and icon of the window.
3735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003740 char_u *title_str = NULL;
3741 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 int maxlen = 0;
3743 int len;
3744 int mustset;
3745 char_u buf[IOSIZE];
3746 int off;
3747
3748 if (!redrawing())
3749 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003750 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 need_maketitle = TRUE;
3752 return;
3753 }
3754
3755 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003756 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003757 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
3759 if (p_title)
3760 {
3761 if (p_titlelen > 0)
3762 {
3763 maxlen = p_titlelen * Columns / 100;
3764 if (maxlen < 10)
3765 maxlen = 10;
3766 }
3767
Bram Moolenaar84a93082018-06-16 22:58:15 +02003768 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (*p_titlestring != NUL)
3770 {
3771#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003772 if (stl_syntax & STL_IN_TITLE)
3773 {
3774 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003775 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003776
3777# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003778 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003779# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003780 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003781 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003782 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003783 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003784 set_string_option_direct((char_u *)"titlestring", -1,
3785 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 else
3788#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003789 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 else
3792 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003793 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
Bram Moolenaar2c666692012-09-05 13:30:40 +02003795#define SPACE_FOR_FNAME (IOSIZE - 100)
3796#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003797#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003799 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003800#ifdef FEAT_TERMINAL
3801 else if (curbuf->b_term != NULL)
3802 {
3803 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3804 SPACE_FOR_FNAME);
3805 }
3806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 else
3808 {
3809 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003810 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813
Bram Moolenaar21554412017-07-24 21:44:43 +02003814#ifdef FEAT_TERMINAL
3815 if (curbuf->b_term == NULL)
3816#endif
3817 switch (bufIsChanged(curbuf)
3818 + (curbuf->b_p_ro * 2)
3819 + (!curbuf->b_p_ma * 4))
3820 {
3821 case 1: STRCAT(buf, " +"); break;
3822 case 2: STRCAT(buf, " ="); break;
3823 case 3: STRCAT(buf, " =+"); break;
3824 case 4:
3825 case 6: STRCAT(buf, " -"); break;
3826 case 5:
3827 case 7: STRCAT(buf, " -+"); break;
3828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
Bram Moolenaar21554412017-07-24 21:44:43 +02003830 if (curbuf->b_fname != NULL
3831#ifdef FEAT_TERMINAL
3832 && curbuf->b_term == NULL
3833#endif
3834 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003836 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 off = (int)STRLEN(buf);
3838 buf[off++] = ' ';
3839 buf[off++] = '(';
3840 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003841 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003843 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (isalpha(buf[off]) && buf[off + 1] == ':')
3845 off += 2;
3846#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003847 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003848 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003850 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003851 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003852 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003853 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003857
Bram Moolenaarc667da52019-11-30 20:52:27 +01003858 // Translate unprintable chars and concatenate. Keep some
3859 // room for the server name. When there is no room (very long
3860 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003861 if (off < SPACE_FOR_DIR)
3862 {
3863 p = transstr(buf + off);
3864 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3865 vim_free(p);
3866 }
3867 else
3868 {
3869 vim_strncpy(buf + off, (char_u *)"...",
3870 (size_t)(SPACE_FOR_ARGNR - off));
3871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 STRCAT(buf, ")");
3873 }
3874
Bram Moolenaar2c666692012-09-05 13:30:40 +02003875 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877#if defined(FEAT_CLIENTSERVER)
3878 if (serverName != NULL)
3879 {
3880 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003881 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 else
3884#endif
3885 STRCAT(buf, " - VIM");
3886
3887 if (maxlen > 0)
3888 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003889 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003890 if (vim_strsize(buf) > maxlen)
3891 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003895 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
3897 if (p_icon)
3898 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003899 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (*p_iconstring != NUL)
3901 {
3902#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003903 if (stl_syntax & STL_IN_ICON)
3904 {
3905 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003906 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003907
3908# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003909 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003910# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003911 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003912 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003913 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003914 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003915 set_string_option_direct((char_u *)"iconstring", -1,
3916 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 else
3919#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003920 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else
3923 {
3924 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003925 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003926 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003927 p = gettail(curbuf->b_ffname);
3928 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003929 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003930 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (len > 100)
3932 {
3933 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003935 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003936 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003938 STRCPY(icon_str, p);
3939 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941 }
3942
Bram Moolenaar84a93082018-06-16 22:58:15 +02003943 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 if (mustset)
3946 resettitle();
3947}
3948
3949/*
3950 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3951 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003952 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 */
3954 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003955value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
3957 if ((str == NULL) != (*last == NULL)
3958 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3959 {
3960 vim_free(*last);
3961 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003964 mch_restore_title(
3965 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003970 return TRUE;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 return FALSE;
3974}
3975
3976/*
3977 * Put current window title back (used after calling a shell)
3978 */
3979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003980resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 mch_settitle(lasttitle, lasticon);
3983}
Bram Moolenaarea408852005-06-25 22:49:46 +00003984
3985# if defined(EXITFREE) || defined(PROTO)
3986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003987free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003988{
3989 vim_free(lasttitle);
3990 vim_free(lasticon);
3991}
3992# endif
3993
Bram Moolenaarc667da52019-11-30 20:52:27 +01003994#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003996#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003998 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Return length of string in screen cells.
4000 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004001 * Normally works for window "wp", except when working for 'tabline' then it
4002 * is "curwin".
4003 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * Items are drawn interspersed with the text that surrounds it
4005 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4006 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4007 *
4008 * If maxwidth is not zero, the string will be filled at any middle marker
4009 * or truncated if too long, fillchar is used for all whitespace.
4010 */
4011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004012build_stl_str_hl(
4013 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004014 char_u *out, // buffer to write into != NameBuff
4015 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004016 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004017 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004018 int fillchar,
4019 int maxwidth,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004020 struct stl_hlrec *hltab, // return: HL attributes (can be NULL)
4021 struct stl_hlrec *tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
Bram Moolenaar10772302019-01-20 18:25:54 +01004023 linenr_T lnum;
4024 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 char_u *p;
4026 char_u *s;
4027 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004028 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004030 win_T *save_curwin;
4031 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004032 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#endif
4034 int empty_line;
4035 colnr_T virtcol;
4036 long l;
4037 long n;
4038 int prevchar_isflag;
4039 int prevchar_isitem;
4040 int itemisflag;
4041 int fillable;
4042 char_u *str;
4043 long num;
4044 int width;
4045 int itemcnt;
4046 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004047 int group_end_userhl;
4048 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int groupitem[STL_MAX_ITEM];
4050 int groupdepth;
4051 struct stl_item
4052 {
4053 char_u *start;
4054 int minwid;
4055 int maxwid;
4056 enum
4057 {
4058 Normal,
4059 Empty,
4060 Group,
4061 Middle,
4062 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004063 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 Trunc
4065 } type;
4066 } item[STL_MAX_ITEM];
4067 int minwid;
4068 int maxwid;
4069 int zeropad;
4070 char_u base;
4071 char_u opt;
4072#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004073 char_u buf_tmp[TMPLEN];
4074 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004075 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004076 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004077 int save_must_redraw = must_redraw;
4078 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004079
4080#ifdef FEAT_EVAL
4081 /*
4082 * When the format starts with "%!" then evaluate it as an expression and
4083 * use the result as the actual format string.
4084 */
4085 if (fmt[0] == '%' && fmt[1] == '!')
4086 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004087 typval_T tv;
4088
4089 tv.v_type = VAR_NUMBER;
4090 tv.vval.v_number = wp->w_id;
4091 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4092
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004093 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
4094 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004095 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004096
4097 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004098 }
4099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 if (fillchar == 0)
4102 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004103 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004104 else if (mb_char2len(fillchar) > 1)
4105 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004106
Bram Moolenaar10772302019-01-20 18:25:54 +01004107 // The cursor in windows other than the current one isn't always
4108 // up-to-date, esp. because of autocommands and timers.
4109 lnum = wp->w_cursor.lnum;
4110 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4111 {
4112 lnum = wp->w_buffer->b_ml.ml_line_count;
4113 wp->w_cursor.lnum = lnum;
4114 }
4115
4116 // Get line & check if empty (cursorpos will show "0-1"). Note that
4117 // p will become invalid when getting another buffer line.
4118 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004119 empty_line = (*p == NUL);
4120
Bram Moolenaar10772302019-01-20 18:25:54 +01004121 // Get the byte value now, in case we need it below. This is more efficient
4122 // than making a copy of the line.
4123 len = STRLEN(p);
4124 if (wp->w_cursor.col > (colnr_T)len)
4125 {
4126 // Line may have changed since checking the cursor column, or the lnum
4127 // was adjusted above.
4128 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004129 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004130 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004131 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004132 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004133 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
4135 groupdepth = 0;
4136 p = out;
4137 curitem = 0;
4138 prevchar_isflag = TRUE;
4139 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004140 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004142 if (curitem == STL_MAX_ITEM)
4143 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004144 // There are too many items. Add the error code to the statusline
4145 // to give the user a hint about what went wrong.
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004146 if (p + 6 < out + outlen)
4147 {
4148 mch_memmove(p, " E541", (size_t)5);
4149 p += 5;
4150 }
4151 break;
4152 }
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (*s != NUL && *s != '%')
4155 prevchar_isflag = prevchar_isitem = FALSE;
4156
4157 /*
4158 * Handle up to the next '%' or the end.
4159 */
4160 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4161 *p++ = *s++;
4162 if (*s == NUL || p + 1 >= out + outlen)
4163 break;
4164
4165 /*
4166 * Handle one '%' item.
4167 */
4168 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004169 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (*s == '%')
4172 {
4173 if (p + 1 >= out + outlen)
4174 break;
4175 *p++ = *s++;
4176 prevchar_isflag = prevchar_isitem = FALSE;
4177 continue;
4178 }
4179 if (*s == STL_MIDDLEMARK)
4180 {
4181 s++;
4182 if (groupdepth > 0)
4183 continue;
4184 item[curitem].type = Middle;
4185 item[curitem++].start = p;
4186 continue;
4187 }
4188 if (*s == STL_TRUNCMARK)
4189 {
4190 s++;
4191 item[curitem].type = Trunc;
4192 item[curitem++].start = p;
4193 continue;
4194 }
4195 if (*s == ')')
4196 {
4197 s++;
4198 if (groupdepth < 1)
4199 continue;
4200 groupdepth--;
4201
4202 t = item[groupitem[groupdepth]].start;
4203 *p = NUL;
4204 l = vim_strsize(t);
4205 if (curitem > groupitem[groupdepth] + 1
4206 && item[groupitem[groupdepth]].minwid == 0)
4207 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004208 // remove group if all items are empty and highlight group
4209 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004210 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004211 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4212 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004213 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004214 {
4215 group_start_userhl = group_end_userhl = item[n].minwid;
4216 break;
4217 }
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004220 {
4221 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004223 if (item[n].type == Highlight)
4224 group_end_userhl = item[n].minwid;
4225 }
4226 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
4228 p = t;
4229 l = 0;
Bram Moolenaardbe5d362020-02-08 18:35:31 +01004230 // do not use the highlighting from the removed group
4231 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4232 if (item[n].type == Highlight)
4233 item[n].type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 }
4236 if (l > item[groupitem[groupdepth]].maxwid)
4237 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004238 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (has_mbyte)
4240 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004241 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 n = 0;
4243 while (l >= item[groupitem[groupdepth]].maxwid)
4244 {
4245 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004246 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 }
4249 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004250 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004253 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004255
4256 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 while (++l < item[groupitem[groupdepth]].minwid)
4258 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaarc667da52019-11-30 20:52:27 +01004260 // correct the start of the items for the truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4262 {
4263 item[l].start -= n;
4264 if (item[l].start < t)
4265 item[l].start = t;
4266 }
4267 }
4268 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4269 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004270 // fill
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 n = item[groupitem[groupdepth]].minwid;
4272 if (n < 0)
4273 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004274 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 n = 0 - n;
4276 while (l++ < n && p + 1 < out + outlen)
4277 *p++ = fillchar;
4278 }
4279 else
4280 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004281 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004282 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 l = n - l;
4284 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004285 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 p += l;
4287 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4288 item[n].start += l;
4289 for ( ; l > 0; l--)
4290 *t++ = fillchar;
4291 }
4292 }
4293 continue;
4294 }
4295 minwid = 0;
4296 maxwid = 9999;
4297 zeropad = FALSE;
4298 l = 1;
4299 if (*s == '0')
4300 {
4301 s++;
4302 zeropad = TRUE;
4303 }
4304 if (*s == '-')
4305 {
4306 s++;
4307 l = -1;
4308 }
4309 if (VIM_ISDIGIT(*s))
4310 {
4311 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004312 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 minwid = 0;
4314 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004315 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
4317 item[curitem].type = Highlight;
4318 item[curitem].start = p;
4319 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4320 s++;
4321 curitem++;
4322 continue;
4323 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004324 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4325 {
4326 if (*s == STL_TABCLOSENR)
4327 {
4328 if (minwid == 0)
4329 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004330 // %X ends the close label, go back to the previously
4331 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004332 for (n = curitem - 1; n >= 0; --n)
4333 if (item[n].type == TabPage && item[n].minwid >= 0)
4334 {
4335 minwid = item[n].minwid;
4336 break;
4337 }
4338 }
4339 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004340 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004341 minwid = - minwid;
4342 }
4343 item[curitem].type = TabPage;
4344 item[curitem].start = p;
4345 item[curitem].minwid = minwid;
4346 s++;
4347 curitem++;
4348 continue;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 if (*s == '.')
4351 {
4352 s++;
4353 if (VIM_ISDIGIT(*s))
4354 {
4355 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004356 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 maxwid = 50;
4358 }
4359 }
4360 minwid = (minwid > 50 ? 50 : minwid) * l;
4361 if (*s == '(')
4362 {
4363 groupitem[groupdepth++] = curitem;
4364 item[curitem].type = Group;
4365 item[curitem].start = p;
4366 item[curitem].minwid = minwid;
4367 item[curitem].maxwid = maxwid;
4368 s++;
4369 curitem++;
4370 continue;
4371 }
4372 if (vim_strchr(STL_ALL, *s) == NULL)
4373 {
4374 s++;
4375 continue;
4376 }
4377 opt = *s++;
4378
Bram Moolenaarc667da52019-11-30 20:52:27 +01004379 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 base = 'D';
4381 itemisflag = FALSE;
4382 fillable = TRUE;
4383 num = -1;
4384 str = NULL;
4385 switch (opt)
4386 {
4387 case STL_FILEPATH:
4388 case STL_FULLPATH:
4389 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004390 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004392 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 else
4394 {
4395 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004396 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4398 }
4399 trans_characters(NameBuff, MAXPATHL);
4400 if (opt != STL_FILENAME)
4401 str = NameBuff;
4402 else
4403 str = gettail(NameBuff);
4404 break;
4405
Bram Moolenaarc667da52019-11-30 20:52:27 +01004406 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 itemisflag = TRUE;
4408 t = p;
4409 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4410 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004411 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 break;
4413 s++;
4414 *p = 0;
4415 p = t;
4416
4417#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004418 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4419 "%d", curbuf->b_fnum);
4420 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4421 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4422 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004424 save_curbuf = curbuf;
4425 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004426 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 curwin = wp;
4428 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004429 // Visual mode is only valid in the current window.
4430 if (curwin != save_curwin)
4431 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004433 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004435 curwin = save_curwin;
4436 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004437 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004438 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004439 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 if (str != NULL && *str != 0)
4442 {
4443 if (*skipdigits(str) == NUL)
4444 {
4445 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004446 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 itemisflag = FALSE;
4448 }
4449 }
4450#endif
4451 break;
4452
4453 case STL_LINE:
4454 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4455 ? 0L : (long)(wp->w_cursor.lnum);
4456 break;
4457
4458 case STL_NUMLINES:
4459 num = wp->w_buffer->b_ml.ml_line_count;
4460 break;
4461
4462 case STL_COLUMN:
4463 num = !(State & INSERT) && empty_line
4464 ? 0 : (int)wp->w_cursor.col + 1;
4465 break;
4466
4467 case STL_VIRTCOL:
4468 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004469 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 virtcol = wp->w_virtcol;
4471 if (wp->w_p_list && lcs_tab1 == NUL)
4472 {
4473 wp->w_p_list = FALSE;
4474 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4475 wp->w_p_list = TRUE;
4476 }
4477 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004478 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (opt == STL_VIRTCOL_ALT
4480 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4481 ? 0 : (int)wp->w_cursor.col + 1)))
4482 break;
4483 num = (long)virtcol;
4484 break;
4485
4486 case STL_PERCENTAGE:
4487 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4488 (long)wp->w_buffer->b_ml.ml_line_count);
4489 break;
4490
4491 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004492 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004493 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
4495
4496 case STL_ARGLISTSTAT:
4497 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004498 buf_tmp[0] = 0;
4499 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4500 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 break;
4502
4503 case STL_KEYMAP:
4504 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004505 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4506 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 break;
4508 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004509#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004510 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#else
4512 num = 0;
4513#endif
4514 break;
4515
4516 case STL_BUFNO:
4517 num = wp->w_buffer->b_fnum;
4518 break;
4519
4520 case STL_OFFSET_X:
4521 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004522 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 case STL_OFFSET:
4524#ifdef FEAT_BYTEOFF
4525 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4526 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4527 0L : l + 1 + (!(State & INSERT) && empty_line ?
4528 0 : (int)wp->w_cursor.col);
4529#endif
4530 break;
4531
4532 case STL_BYTEVAL_X:
4533 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004534 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004536 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (num == NL)
4538 num = 0;
4539 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4540 num = NL;
4541 break;
4542
4543 case STL_ROFLAG:
4544 case STL_ROFLAG_ALT:
4545 itemisflag = TRUE;
4546 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004547 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 break;
4549
4550 case STL_HELPFLAG:
4551 case STL_HELPFLAG_ALT:
4552 itemisflag = TRUE;
4553 if (wp->w_buffer->b_help)
4554 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004555 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 break;
4557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 case STL_FILETYPE:
4559 if (*wp->w_buffer->b_p_ft != NUL
4560 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4561 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004562 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004563 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004564 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 break;
4567
4568 case STL_FILETYPE_ALT:
4569 itemisflag = TRUE;
4570 if (*wp->w_buffer->b_p_ft != NUL
4571 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4572 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004573 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004574 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004575 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004577 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaar4033c552017-09-16 20:54:51 +02004581#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 case STL_PREVIEWFLAG:
4583 case STL_PREVIEWFLAG_ALT:
4584 itemisflag = TRUE;
4585 if (wp->w_p_pvw)
4586 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4587 : _("[Preview]"));
4588 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004589
4590 case STL_QUICKFIX:
4591 if (bt_quickfix(wp->w_buffer))
4592 str = (char_u *)(wp->w_llist_ref
4593 ? _(msg_loclist)
4594 : _(msg_qflist));
4595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#endif
4597
4598 case STL_MODIFIED:
4599 case STL_MODIFIED_ALT:
4600 itemisflag = TRUE;
4601 switch ((opt == STL_MODIFIED_ALT)
4602 + bufIsChanged(wp->w_buffer) * 2
4603 + (!wp->w_buffer->b_p_ma) * 4)
4604 {
4605 case 2: str = (char_u *)"[+]"; break;
4606 case 3: str = (char_u *)",+"; break;
4607 case 4: str = (char_u *)"[-]"; break;
4608 case 5: str = (char_u *)",-"; break;
4609 case 6: str = (char_u *)"[+-]"; break;
4610 case 7: str = (char_u *)",+-"; break;
4611 }
4612 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004613
4614 case STL_HIGHLIGHT:
4615 t = s;
4616 while (*s != '#' && *s != NUL)
4617 ++s;
4618 if (*s == '#')
4619 {
4620 item[curitem].type = Highlight;
4621 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004622 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004623 curitem++;
4624 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004625 if (*s != NUL)
4626 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004627 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629
4630 item[curitem].start = p;
4631 item[curitem].type = Normal;
4632 if (str != NULL && *str)
4633 {
4634 t = str;
4635 if (itemisflag)
4636 {
4637 if ((t[0] && t[1])
4638 && ((!prevchar_isitem && *t == ',')
4639 || (prevchar_isflag && *t == ' ')))
4640 t++;
4641 prevchar_isflag = TRUE;
4642 }
4643 l = vim_strsize(t);
4644 if (l > 0)
4645 prevchar_isitem = TRUE;
4646 if (l > maxwid)
4647 {
4648 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (has_mbyte)
4650 {
4651 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004652 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 l -= byte2cells(*t++);
4656 if (p + 1 >= out + outlen)
4657 break;
4658 *p++ = '<';
4659 }
4660 if (minwid > 0)
4661 {
4662 for (; l < minwid && p + 1 < out + outlen; l++)
4663 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004664 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4666 *p++ = ' ';
4667 else
4668 *p++ = fillchar;
4669 }
4670 minwid = 0;
4671 }
4672 else
4673 minwid *= -1;
4674 while (*t && p + 1 < out + outlen)
4675 {
4676 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004677 // Change a space by fillchar, unless fillchar is '-' and a
4678 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (fillable && p[-1] == ' '
4680 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4681 p[-1] = fillchar;
4682 }
4683 for (; l < minwid && p + 1 < out + outlen; l++)
4684 *p++ = fillchar;
4685 }
4686 else if (num >= 0)
4687 {
4688 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4689 char_u nstr[20];
4690
4691 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004692 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 prevchar_isitem = TRUE;
4694 t = nstr;
4695 if (opt == STL_VIRTCOL_ALT)
4696 {
4697 *t++ = '-';
4698 minwid--;
4699 }
4700 *t++ = '%';
4701 if (zeropad)
4702 *t++ = '0';
4703 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004704 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 *t = 0;
4706
4707 for (n = num, l = 1; n >= nbase; n /= nbase)
4708 l++;
4709 if (opt == STL_VIRTCOL_ALT)
4710 l++;
4711 if (l > maxwid)
4712 {
4713 l += 2;
4714 n = l - maxwid;
4715 while (l-- > maxwid)
4716 num /= nbase;
4717 *t++ = '>';
4718 *t++ = '%';
4719 *t = t[-3];
4720 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004721 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4722 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004725 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4726 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 p += STRLEN(p);
4728 }
4729 else
4730 item[curitem].type = Empty;
4731
4732 if (opt == STL_VIM_EXPR)
4733 vim_free(str);
4734
4735 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004736 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 curitem++;
4738 }
4739 *p = NUL;
4740 itemcnt = curitem;
4741
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004742#ifdef FEAT_EVAL
4743 if (usefmt != fmt)
4744 vim_free(usefmt);
4745#endif
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 width = vim_strsize(out);
4748 if (maxwidth > 0 && width > maxwidth)
4749 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004750 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 l = 0;
4752 if (itemcnt == 0)
4753 s = out;
4754 else
4755 {
4756 for ( ; l < itemcnt; l++)
4757 if (item[l].type == Trunc)
4758 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004759 // Truncate at %< item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 s = item[l].start;
4761 break;
4762 }
4763 if (l == itemcnt)
4764 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004765 // No %< item, truncate first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 s = item[0].start;
4767 l = 0;
4768 }
4769 }
4770
4771 if (width - vim_strsize(s) >= maxwidth)
4772 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004773 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (has_mbyte)
4775 {
4776 s = out;
4777 width = 0;
4778 for (;;)
4779 {
4780 width += ptr2cells(s);
4781 if (width >= maxwidth)
4782 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004783 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004785 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 while (++width < maxwidth)
4787 *s++ = fillchar;
4788 }
4789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 s = out + maxwidth - 1;
4791 for (l = 0; l < itemcnt; l++)
4792 if (item[l].start > s)
4793 break;
4794 itemcnt = l;
4795 *s++ = '>';
4796 *s = 0;
4797 }
4798 else
4799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 if (has_mbyte)
4801 {
4802 n = 0;
4803 while (width >= maxwidth)
4804 {
4805 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004806 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 }
4809 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 n = width - maxwidth + 1;
4811 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004812 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 *s = '<';
4814
Bram Moolenaarc667da52019-11-30 20:52:27 +01004815 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 while (++width < maxwidth)
4817 {
4818 s = s + STRLEN(s);
4819 *s++ = fillchar;
4820 *s = NUL;
4821 }
4822
Bram Moolenaarc667da52019-11-30 20:52:27 +01004823 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 for (; l < itemcnt; l++)
4825 {
4826 if (item[l].start - n >= s)
4827 item[l].start -= n;
4828 else
4829 item[l].start = s;
4830 }
4831 }
4832 width = maxwidth;
4833 }
4834 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4835 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004836 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 for (l = 0; l < itemcnt; l++)
4838 if (item[l].type == Middle)
4839 break;
4840 if (l < itemcnt)
4841 {
4842 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004843 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 for (s = item[l].start; s < p; s++)
4845 *s = fillchar;
4846 for (l++; l < itemcnt; l++)
4847 item[l].start += maxwidth - width;
4848 width = maxwidth;
4849 }
4850 }
4851
Bram Moolenaarc667da52019-11-30 20:52:27 +01004852 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004853 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004855 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 for (l = 0; l < itemcnt; l++)
4857 {
4858 if (item[l].type == Highlight)
4859 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004860 sp->start = item[l].start;
4861 sp->userhl = item[l].minwid;
4862 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004865 sp->start = NULL;
4866 sp->userhl = 0;
4867 }
4868
Bram Moolenaarc667da52019-11-30 20:52:27 +01004869 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004870 if (tabtab != NULL)
4871 {
4872 sp = tabtab;
4873 for (l = 0; l < itemcnt; l++)
4874 {
4875 if (item[l].type == TabPage)
4876 {
4877 sp->start = item[l].start;
4878 sp->userhl = item[l].minwid;
4879 sp++;
4880 }
4881 }
4882 sp->start = NULL;
4883 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885
Bram Moolenaarc667da52019-11-30 20:52:27 +01004886 // When inside update_screen we do not want redrawing a stausline, ruler,
4887 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004888 if (updating_screen)
4889 {
4890 must_redraw = save_must_redraw;
4891 curwin->w_redr_type = save_redr_type;
4892 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004893
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 return width;
4895}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004896#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004898#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4899 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004901 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4902 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 */
4904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004905get_rel_pos(
4906 win_T *wp,
4907 char_u *buf,
4908 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004910 long above; // number of lines above window
4911 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
Bram Moolenaarc667da52019-11-30 20:52:27 +01004913 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004914 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 above = wp->w_topline - 1;
4916#ifdef FEAT_DIFF
4917 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004918 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004919 above = 0; // All buffer lines are displayed and there is an
4920 // indication of filler lines, that can be considered
4921 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922#endif
4923 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4924 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004925 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4926 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004928 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004930 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 ? (int)(above / ((above + below) / 100L))
4932 : (int)(above * 100L / (above + below)));
4933}
4934#endif
4935
4936/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004937 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 * Return TRUE if it was appended.
4939 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004940 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004941append_arg_number(
4942 win_T *wp,
4943 char_u *buf,
4944 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004945 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 char_u *p;
4948
Bram Moolenaarc667da52019-11-30 20:52:27 +01004949 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 return FALSE;
4951
Bram Moolenaarc667da52019-11-30 20:52:27 +01004952 p = buf + STRLEN(buf); // go to the end of the buffer
4953 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return FALSE;
4955 *p++ = ' ';
4956 *p++ = '(';
4957 if (add_file)
4958 {
4959 STRCPY(p, "file ");
4960 p += 5;
4961 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004962 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4963 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4965 return TRUE;
4966}
4967
4968/*
4969 * If fname is not a full path, make it a full path.
4970 * Returns pointer to allocated memory (NULL for failure).
4971 */
4972 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004973fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 /*
4976 * Force expanding the path always for Unix, because symbolic links may
4977 * mess up the full path name, even though it starts with a '/'.
4978 * Also expand when there is ".." in the file name, try to remove it,
4979 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004980 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 * For MS-Windows also expand names like "longna~1" to "longname".
4982 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004983#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 return FullName_save(fname, TRUE);
4985#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004986 if (!vim_isAbsName(fname)
4987 || strstr((char *)fname, "..") != NULL
4988 || strstr((char *)fname, "//") != NULL
4989# ifdef BACKSLASH_IN_FILENAME
4990 || strstr((char *)fname, "\\\\") != NULL
4991# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004992# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 )
4996 return FullName_save(fname, FALSE);
4997
4998 fname = vim_strsave(fname);
4999
Bram Moolenaar9b942202007-10-03 12:31:33 +00005000# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005001 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005002 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 return fname;
5006#endif
5007}
5008
5009/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005010 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5011 * "*ffname" becomes a pointer to allocated memory (or NULL).
5012 * When resolving a link both "*sfname" and "*ffname" will point to the same
5013 * allocated memory.
5014 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005015 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005018fname_expand(
5019 buf_T *buf UNUSED,
5020 char_u **ffname,
5021 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005023 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005025 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005027 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029#ifdef FEAT_SHORTCUT
5030 if (!buf->b_p_bin)
5031 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005032 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005034 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005035 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005036 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
5038 vim_free(*ffname);
5039 *ffname = rfname;
5040 *sfname = rfname;
5041 }
5042 }
5043#endif
5044}
5045
5046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 * Open a window for a number of buffers.
5048 */
5049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005050ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 buf_T *buf;
5053 win_T *wp, *wpnext;
5054 int split_ret = OK;
5055 int p_ea_save;
5056 int open_wins = 0;
5057 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005058 int count; // Maximum number of windows to open.
5059 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005060 int had_tab = cmdmod.tab;
5061 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
Bram Moolenaarc667da52019-11-30 20:52:27 +01005063 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 count = 9999;
5065 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005066 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5068 all = FALSE;
5069 else
5070 all = TRUE;
5071
5072 setpcmark();
5073
5074#ifdef FEAT_GUI
5075 need_mouse_correct = TRUE;
5076#endif
5077
5078 /*
5079 * Close superfluous windows (two windows for the same buffer).
5080 * Also close windows that are not full-width.
5081 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005082 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005083 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005084 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005086 tpnext = curtab->tp_next;
5087 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005089 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005090 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005091 || ((cmdmod.split & WSP_VERT)
5092 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005093 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005094 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005095 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005096 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005097 {
5098 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005099 wpnext = firstwin; // just in case an autocommand does
5100 // something strange with windows
5101 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005102 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005103 }
5104 else
5105 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005107
Bram Moolenaarc667da52019-11-30 20:52:27 +01005108 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005109 if (had_tab == 0 || tpnext == NULL)
5110 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005111 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113
5114 /*
5115 * Go through the buffer list. When a buffer doesn't have a window yet,
5116 * open one. Otherwise move the window to the right position.
5117 * Watch out for autocommands that delete buffers or windows!
5118 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005119 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5124 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005125 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5127 continue;
5128
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005129 if (had_tab != 0)
5130 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005131 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005132 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005133 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005134 else
5135 wp = NULL;
5136 }
5137 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005138 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005139 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005140 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005141 if (wp->w_buffer == buf)
5142 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005143 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005144 if (wp != NULL)
5145 win_move_after(wp, curwin);
5146 }
5147
5148 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005150 bufref_T bufref;
5151
5152 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005153
Bram Moolenaarc667da52019-11-30 20:52:27 +01005154 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005156 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5158 ++open_wins;
5159 p_ea = p_ea_save;
5160 if (split_ret == FAIL)
5161 continue;
5162
Bram Moolenaarc667da52019-11-30 20:52:27 +01005163 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005166 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005168 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (swap_exists_action == SEA_QUIT)
5173 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005174#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005175 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005176
Bram Moolenaarc667da52019-11-30 20:52:27 +01005177 // Reset the error/interrupt/exception state here so that
5178 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005179 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005180#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005181
Bram Moolenaarc667da52019-11-30 20:52:27 +01005182 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 win_close(curwin, TRUE);
5184 --open_wins;
5185 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005186 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005187
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005188#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005189 // Restore the error/interrupt/exception state if not
5190 // discarded by a new aborting error, interrupt, or uncaught
5191 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005192 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 else
5196 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198
5199 ui_breakcheck();
5200 if (got_int)
5201 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005202 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005205#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005206 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005207 if (aborting())
5208 break;
5209#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005210 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005211 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5212 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005215 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 /*
5219 * Close superfluous windows.
5220 */
5221 for (wp = lastwin; open_wins > count; )
5222 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005223 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (!win_valid(wp))
5226 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005227 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 wp = lastwin;
5229 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005230 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005232 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 --open_wins;
5234 wp = lastwin;
5235 }
5236 else
5237 {
5238 wp = wp->w_prev;
5239 if (wp == NULL)
5240 break;
5241 }
5242 }
5243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005246static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248/*
5249 * do_modelines() - process mode lines for the current file
5250 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005251 * "flags" can be:
5252 * OPT_WINONLY only set options local to window
5253 * OPT_NOWIN don't set options local to window
5254 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 * Returns immediately if the "ml" option isn't set.
5256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005258do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005260 linenr_T lnum;
5261 int nmlines;
5262 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5265 return;
5266
Bram Moolenaarc667da52019-11-30 20:52:27 +01005267 // Disallow recursive entry here. Can happen when executing a modeline
5268 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 if (entered)
5270 return;
5271
5272 ++entered;
5273 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5274 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005275 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 nmlines = 0;
5277
5278 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5279 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005280 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 nmlines = 0;
5282 --entered;
5283}
5284
Bram Moolenaarc667da52019-11-30 20:52:27 +01005285#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
5287/*
5288 * chk_modeline() - check a single line for a mode string
5289 * Return FAIL if an error encountered.
5290 */
5291 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005292chk_modeline(
5293 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005294 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 char_u *s;
5297 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005298 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 int prev;
5300 int vers;
5301 int end;
5302 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005304 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01005306 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
5308 prev = -1;
5309 for (s = ml_get(lnum); *s != NUL; ++s)
5310 {
5311 if (prev == -1 || vim_isspace(prev))
5312 {
5313 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5314 || STRNCMP(s, "vi:", (size_t)3) == 0)
5315 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005316 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005317 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5320 e = s + 4;
5321 else
5322 e = s + 3;
5323 vers = getdigits(&e);
5324 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005325 && (s[0] != 'V'
5326 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 && (s[3] == ':'
5328 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5329 || (VIM_VERSION_100 < vers && s[3] == '<')
5330 || (VIM_VERSION_100 > vers && s[3] == '>')
5331 || (VIM_VERSION_100 == vers && s[3] == '=')))
5332 break;
5333 }
5334 }
5335 prev = *s;
5336 }
5337
5338 if (*s)
5339 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005340 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 ++s;
5342 while (s[-1] != ':');
5343
Bram Moolenaarc667da52019-11-30 20:52:27 +01005344 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (linecopy == NULL)
5346 return FAIL;
5347
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005348 // prepare for emsg()
5349 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005350 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 end = FALSE;
5353 while (end == FALSE)
5354 {
5355 s = skipwhite(s);
5356 if (*s == NUL)
5357 break;
5358
5359 /*
5360 * Find end of set command: ':' or end of line.
5361 * Skip over "\:", replacing it with ":".
5362 */
5363 for (e = s; *e != ':' && *e != NUL; ++e)
5364 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005365 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (*e == NUL)
5367 end = TRUE;
5368
5369 /*
5370 * If there is a "set" command, require a terminating ':' and
5371 * ignore the stuff after the ':'.
5372 * "vi:set opt opt opt: foo" -- foo not interpreted
5373 * "vi:opt opt opt: foo" -- foo interpreted
5374 * Accept "se" for compatibility with Elvis.
5375 */
5376 if (STRNCMP(s, "set ", (size_t)4) == 0
5377 || STRNCMP(s, "se ", (size_t)3) == 0)
5378 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005379 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 break;
5381 end = TRUE;
5382 s = vim_strchr(s, ' ') + 1;
5383 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005384 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
Bram Moolenaarc667da52019-11-30 20:52:27 +01005386 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005388 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005390 save_current_sctx = current_sctx;
5391 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005392 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005393 current_sctx.sc_lnum = lnum;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005394 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005396 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005397 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005398
Bram Moolenaara3227e22006-03-08 21:32:40 +00005399 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005400
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005401 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005403 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005405 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 break;
5407 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005408 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410
Bram Moolenaare31ee862020-01-07 20:59:34 +01005411 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005412 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 vim_free(linecopy);
5414 }
5415 return retval;
5416}
5417
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005418/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005419 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5420 */
5421 int
5422bt_normal(buf_T *buf)
5423{
5424 return buf != NULL && buf->b_p_bt[0] == NUL;
5425}
5426
Bram Moolenaar113e1072019-01-20 15:30:40 +01005427#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005428/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005429 * Return TRUE if "buf" is the quickfix buffer.
5430 */
5431 int
5432bt_quickfix(buf_T *buf)
5433{
5434 return buf != NULL && buf->b_p_bt[0] == 'q';
5435}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005436#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005437
Bram Moolenaar113e1072019-01-20 15:30:40 +01005438#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005439/*
5440 * Return TRUE if "buf" is a terminal buffer.
5441 */
5442 int
5443bt_terminal(buf_T *buf)
5444{
5445 return buf != NULL && buf->b_p_bt[0] == 't';
5446}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005447#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005448
5449/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005450 * Return TRUE if "buf" is a help buffer.
5451 */
5452 int
5453bt_help(buf_T *buf)
5454{
5455 return buf != NULL && buf->b_help;
5456}
5457
5458/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005459 * Return TRUE if "buf" is a prompt buffer.
5460 */
5461 int
5462bt_prompt(buf_T *buf)
5463{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005464 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5465}
5466
5467/*
5468 * Return TRUE if "buf" is a buffer for a popup window.
5469 */
5470 int
5471bt_popup(buf_T *buf)
5472{
5473 return buf != NULL && buf->b_p_bt != NULL
5474 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005475}
5476
5477/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005478 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5479 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005480 */
5481 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005482bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005483{
5484 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5485 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005486 || buf->b_p_bt[0] == 't'
5487 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005488}
5489
5490/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005491 * Return TRUE if "buf" has 'buftype' set to "nofile".
5492 */
5493 int
5494bt_nofile(buf_T *buf)
5495{
5496 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5497}
5498
5499/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005500 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5501 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005502 */
5503 int
5504bt_dontwrite(buf_T *buf)
5505{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005506 return buf != NULL && (buf->b_p_bt[0] == 'n'
5507 || buf->b_p_bt[0] == 't'
5508 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005509}
5510
Bram Moolenaar113e1072019-01-20 15:30:40 +01005511#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005512 int
5513bt_dontwrite_msg(buf_T *buf)
5514{
5515 if (bt_dontwrite(buf))
5516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005517 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005518 return TRUE;
5519 }
5520 return FALSE;
5521}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005522#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005523
5524/*
5525 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5526 * and 'bufhidden'.
5527 */
5528 int
5529buf_hide(buf_T *buf)
5530{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005531 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005532 switch (buf->b_p_bh[0])
5533 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005534 case 'u': // "unload"
5535 case 'w': // "wipe"
5536 case 'd': return FALSE; // "delete"
5537 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005538 }
5539 return (p_hid || cmdmod.hide);
5540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
5542/*
5543 * Return special buffer name.
5544 * Returns NULL when the buffer has a normal file name.
5545 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005547buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005549#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005551 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005552 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005553 * Differentiate between the quickfix and location list buffers using
5554 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005555 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005556 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005557 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005558 else
5559 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005562
Bram Moolenaarc667da52019-11-30 20:52:27 +01005563 // There is no _file_ when 'buftype' is "nofile", b_sfname
5564 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005565 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005567#ifdef FEAT_TERMINAL
5568 if (buf->b_term != NULL)
5569 return term_get_status_text(buf->b_term);
5570#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005571 if (buf->b_fname != NULL)
5572 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005573#ifdef FEAT_JOB_CHANNEL
5574 if (bt_prompt(buf))
5575 return (char_u *)_("[Prompt]");
5576#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005577#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005578 if (bt_popup(buf))
5579 return (char_u *)_("[Popup]");
5580#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005581 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005585 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 return NULL;
5587}
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589/*
5590 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5591 */
5592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005593set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 if (on != curbuf->b_p_bl)
5596 {
5597 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (on)
5599 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5600 else
5601 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603}
5604
5605/*
5606 * Read the file for "buf" again and check if the contents changed.
5607 * Return TRUE if it changed or this could not be checked.
5608 */
5609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005610buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 buf_T *newbuf;
5613 int differ = TRUE;
5614 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 exarg_T ea;
5617
Bram Moolenaarc667da52019-11-30 20:52:27 +01005618 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5620 if (newbuf == NULL)
5621 return TRUE;
5622
Bram Moolenaarc667da52019-11-30 20:52:27 +01005623 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 if (prep_exarg(&ea, buf) == FAIL)
5625 {
5626 wipe_buffer(newbuf, FALSE);
5627 return TRUE;
5628 }
5629
Bram Moolenaarc667da52019-11-30 20:52:27 +01005630 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaar4770d092006-01-12 23:22:24 +00005633 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 && readfile(buf->b_ffname, buf->b_fname,
5635 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5636 &ea, READ_NEW | READ_DUMMY) == OK)
5637 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005638 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5640 {
5641 differ = FALSE;
5642 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5643 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5644 {
5645 differ = TRUE;
5646 break;
5647 }
5648 }
5649 }
5650 vim_free(ea.cmd);
5651
Bram Moolenaarc667da52019-11-30 20:52:27 +01005652 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaarc667da52019-11-30 20:52:27 +01005655 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 wipe_buffer(newbuf, FALSE);
5657
5658 return differ;
5659}
5660
5661/*
5662 * Wipe out a buffer and decrement the last buffer number if it was used for
5663 * this buffer. Call this to wipe out a temp buffer that does not contain any
5664 * marks.
5665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005667wipe_buffer(
5668 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005669 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 if (buf->b_fnum == top_file_num - 1)
5672 --top_file_num;
5673
Bram Moolenaarc667da52019-11-30 20:52:27 +01005674 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005675 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005676
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005677 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005680 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681}