blob: b4992dda0747f4a50fa3e98ec372c304761bee3a [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 Moolenaar42ec6562012-02-22 14:58:37 +0100187 close_buffer(NULL, curbuf, 0, 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 Moolenaar071d4272004-06-13 20:20:40 +0000490 */
491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100492close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100493 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100494 buf_T *buf,
495 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200496 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100499 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200500 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100501 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200502 win_T *the_curwin = curwin;
503 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200505 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
506 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200508 /*
509 * Force unloading or deleting when 'bufhidden' says so.
510 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
511 * "hide" (otherwise we could never free or delete a buffer).
512 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100513 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200514 {
515 del_buf = TRUE;
516 unload_buf = TRUE;
517 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100518 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200519 {
520 del_buf = TRUE;
521 unload_buf = TRUE;
522 wipe_buf = TRUE;
523 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100524 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200525 unload_buf = TRUE;
526
Bram Moolenaar94053a52017-08-01 21:44:33 +0200527#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200528 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200529 {
530 if (term_job_running(buf->b_term))
531 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200532 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200533 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200534 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200535 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200536
Bram Moolenaarc667da52019-11-30 20:52:27 +0100537 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200538 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200539 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200540 else
541 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100542 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 del_buf = FALSE;
544 unload_buf = FALSE;
545 }
546 }
547 else
548 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100549 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200550 del_buf = TRUE;
551 unload_buf = TRUE;
552 wipe_buf = TRUE;
553 }
554 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaarc667da52019-11-30 20:52:27 +0100557 // Disallow deleting the buffer when it is locked (already being closed or
558 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200559 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200560 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200561
Bram Moolenaarc667da52019-11-30 20:52:27 +0100562 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200563 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100565 // Set b_last_cursor when closing the last window for the buffer.
566 // Remember the last cursor position and window options of the buffer.
567 // This used to be only for the current window, but then options like
568 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (buf->b_nwindows == 1)
570 set_last_cursor(win);
571 buflist_setfpos(buf, win,
572 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
573 win->w_cursor.col, TRUE);
574 }
575
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200576 set_bufref(&bufref, buf);
577
Bram Moolenaarc667da52019-11-30 20:52:27 +0100578 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (buf->b_nwindows == 1)
580 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200581 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200582 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
583 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200584 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100585 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100586 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200587aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100588 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100590 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200591 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200592 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100593 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200594 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
Bram Moolenaarc667da52019-11-30 20:52:27 +0100596 // When the buffer becomes hidden, but is not unloaded, trigger
597 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (!unload_buf)
599 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200600 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200601 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
602 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200603 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100604 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200605 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200606 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200607 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100608 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200609 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100611#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100612 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200616
Bram Moolenaarc667da52019-11-30 20:52:27 +0100617 // If the buffer was in curwin and the window has changed, go back to that
618 // window, if it still exists. This avoids that ":edit x" triggering a
619 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200620 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
621 {
622 block_autocmds();
623 goto_tabpage_win(the_curtab, the_curwin);
624 unblock_autocmds();
625 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
Bram Moolenaarc667da52019-11-30 20:52:27 +0100629 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (buf->b_nwindows > 0)
631 --buf->b_nwindows;
632
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100633#ifdef FEAT_DIFF
634 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100635 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100636#endif
637
Bram Moolenaarc667da52019-11-30 20:52:27 +0100638 // Return when a window is displaying the buffer or when it's not
639 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaarc667da52019-11-30 20:52:27 +0100643 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (buf->b_ffname == NULL)
645 del_buf = TRUE;
646
Bram Moolenaarc667da52019-11-30 20:52:27 +0100647 // When closing the current buffer stop Visual mode before freeing
648 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200649 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200650#if defined(EXITFREE)
651 && !entered_free_all_mem
652#endif
653 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200654 end_visual_mode();
655
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 /*
657 * Free all things allocated for this buffer.
658 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
659 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100660 // Remember if we are closing the current buffer. Restore the number of
661 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 is_curbuf = (buf == curbuf);
663 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200665 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200668 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100670#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100671 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 /*
676 * It's possible that autocommands change curbuf to the one being deleted.
677 * This might cause the previous curbuf to be deleted unexpectedly. But
678 * in some cases it's OK to delete the curbuf, because a new one is
679 * obtained anyway. Therefore only return if curbuf changed to the
680 * deleted buffer.
681 */
682 if (buf == curbuf && !is_curbuf)
683 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200684
Bram Moolenaar4033c552017-09-16 20:54:51 +0200685 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100686 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200687
Bram Moolenaarc667da52019-11-30 20:52:27 +0100688 // Autocommands may have opened or closed windows for this buffer.
689 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200690 if (buf->b_nwindows > 0)
691 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 /*
694 * Remove the buffer from the list.
695 */
696 if (wipe_buf)
697 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200698 if (action == DOBUF_WIPE_REUSE)
699 {
700 // we can re-use this buffer number, store it
701 if (buf_reuse.ga_itemsize == 0)
702 ga_init2(&buf_reuse, sizeof(int), 50);
703 if (ga_grow(&buf_reuse, 1) == OK)
704 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
705 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200706 if (buf->b_sfname != buf->b_ffname)
707 VIM_CLEAR(buf->b_sfname);
708 else
709 buf->b_sfname = NULL;
710 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (buf->b_prev == NULL)
712 firstbuf = buf->b_next;
713 else
714 buf->b_prev->b_next = buf->b_next;
715 if (buf->b_next == NULL)
716 lastbuf = buf->b_prev;
717 else
718 buf->b_next->b_prev = buf->b_prev;
719 free_buffer(buf);
720 }
721 else
722 {
723 if (del_buf)
724 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100725 // Free all internal variables and reset option values, to make
726 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 free_buffer_stuff(buf, TRUE);
728
Bram Moolenaarc667da52019-11-30 20:52:27 +0100729 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
731
Bram Moolenaarc667da52019-11-30 20:52:27 +0100732 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 buf->b_p_initialized = FALSE;
734 }
735 buf_clear_file(buf);
736 if (del_buf)
737 buf->b_p_bl = FALSE;
738 }
739}
740
741/*
742 * Make buffer not contain a file.
743 */
744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100745buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200748 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 buf->b_p_eol = TRUE;
751 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000753 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100755 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#ifdef FEAT_NETBEANS_INTG
757 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758#endif
759}
760
761/*
762 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200763 * the file. Careful: get here with "curwin" NULL when exiting.
764 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200765 * BFA_DEL buffer is going to be deleted
766 * BFA_WIPE buffer is going to be wiped out
767 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100770buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200773 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200774 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200775 win_T *the_curwin = curwin;
776 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaarc667da52019-11-30 20:52:27 +0100778 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200779 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200780 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200781 if (buf->b_ml.ml_mfp != NULL)
782 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200783 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
784 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200785 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100786 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200787 return;
788 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200789 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200791 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
792 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200793 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100794 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 return;
796 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200797 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200799 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
800 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200801 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100802 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 return;
804 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200805 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200806
Bram Moolenaarc667da52019-11-30 20:52:27 +0100807 // If the buffer was in curwin and the window has changed, go back to that
808 // window, if it still exists. This avoids that ":edit x" triggering a
809 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200810 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
811 {
812 block_autocmds();
813 goto_tabpage_win(the_curtab, the_curwin);
814 unblock_autocmds();
815 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200816
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100817#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100818 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822 /*
823 * It's possible that autocommands change curbuf to the one being deleted.
824 * This might cause curbuf to be deleted unexpectedly. But in some cases
825 * it's OK to delete the curbuf, because a new one is obtained anyway.
826 * Therefore only return if curbuf changed to the deleted buffer.
827 */
828 if (buf == curbuf && !is_curbuf)
829 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100831 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200833#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100834 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200835 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100836 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200837#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000838
839#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100840 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000841 {
842 win_T *win;
843 tabpage_T *tp;
844
845 FOR_ALL_TAB_WINDOWS(tp, win)
846 if (win->w_buffer == buf)
847 clearFolding(win);
848 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000849#endif
850
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#ifdef FEAT_TCL
852 tcl_buffer_free(buf);
853#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100854 ml_close(buf, TRUE); // close and delete the memline/memfile
855 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200856 if ((flags & BFA_KEEP_UNDO) == 0)
857 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100858 u_blockfree(buf); // free the memory allocated for undo
859 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100862 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100864#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100865 clear_buf_prop_types(buf);
866#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100867 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868}
869
870/*
871 * Free a buffer structure and the things it contains related to the buffer
872 * itself (not the file, that must have been done already).
873 */
874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100875free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200877 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200879#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100880 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200881 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200882 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200883 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200884#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200885#ifdef FEAT_LUA
886 lua_buffer_free(buf);
887#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000888#ifdef FEAT_MZSCHEME
889 mzscheme_buffer_free(buf);
890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_PERL
892 perl_buf_free(buf);
893#endif
894#ifdef FEAT_PYTHON
895 python_buffer_free(buf);
896#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200897#ifdef FEAT_PYTHON3
898 python3_buffer_free(buf);
899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#ifdef FEAT_RUBY
901 ruby_buffer_free(buf);
902#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200903#ifdef FEAT_JOB_CHANNEL
904 channel_buffer_free(buf);
905#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200906#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200907 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200908#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200909#ifdef FEAT_JOB_CHANNEL
910 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200911 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200912 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200913#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200914
915 buf_hashtab_remove(buf);
916
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200917 aubuflocal_remove(buf);
918
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200919 if (autocmd_busy)
920 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100921 // Do not free the buffer structure while autocommands are executing,
922 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200923 buf->b_next = au_pending_free_buf;
924 au_pending_free_buf = buf;
925 }
926 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200927 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929
930/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100931 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100932 */
933 static void
934init_changedtick(buf_T *buf)
935{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100936 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100937
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100938 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
939 di->di_tv.v_type = VAR_NUMBER;
940 di->di_tv.v_lock = VAR_FIXED;
941 di->di_tv.vval.v_number = 0;
942
Bram Moolenaar92769c32017-02-25 15:41:37 +0100943#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100944 STRCPY(buf->b_ct_di.di_key, "changedtick");
945 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100946#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100947}
948
949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
951 */
952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100953free_buffer_stuff(
954 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100955 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 if (free_options)
958 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100959 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200961#ifdef FEAT_SPELL
962 ga_clear(&buf->b_s.b_langp);
963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100966 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100967 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100968
Bram Moolenaarc667da52019-11-30 20:52:27 +0100969 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100970 hash_init(&buf->b_vars->dv_hashtab);
971 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100972 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200975 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200977 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000979#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200980 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000981#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100982 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
983 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +0100984 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986
987/*
988 * Free the b_wininfo list for buffer "buf".
989 */
990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100991clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 wininfo_T *wip;
994
995 while (buf->b_wininfo != NULL)
996 {
997 wip = buf->b_wininfo;
998 buf->b_wininfo = wip->wi_next;
999 if (wip->wi_optset)
1000 {
1001 clear_winopt(&wip->wi_opt);
1002#ifdef FEAT_FOLDING
1003 deleteFoldRecurse(&wip->wi_folds);
1004#endif
1005 }
1006 vim_free(wip);
1007 }
1008}
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/*
1011 * Go to another buffer. Handles the result of the ATTENTION dialog.
1012 */
1013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001014goto_buffer(
1015 exarg_T *eap,
1016 int start,
1017 int dir,
1018 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001020 bufref_T old_curbuf;
1021
1022 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1026 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1028 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001029#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001030 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001031
Bram Moolenaarc667da52019-11-30 20:52:27 +01001032 // Reset the error/interrupt/exception state here so that
1033 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001034 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001035#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001036
Bram Moolenaarc667da52019-11-30 20:52:27 +01001037 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 win_close(curwin, TRUE);
1039 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001040 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001041
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001042#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001043 // Restore the error/interrupt/exception state if not discarded by a
1044 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001045 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001049 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052/*
1053 * Handle the situation of swap_exists_action being set.
1054 * It is allowed for "old_curbuf" to be NULL or invalid.
1055 */
1056 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001057handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001059#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001060 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001061#endif
1062#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001063 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001064#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001065 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (swap_exists_action == SEA_QUIT)
1068 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001069#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001070 // Reset the error/interrupt/exception state here so that
1071 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001072 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001073#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074
Bram Moolenaarc667da52019-11-30 20:52:27 +01001075 // User selected Quit at ATTENTION prompt. Go back to previous
1076 // buffer. If that buffer is gone or the same as the current one,
1077 // open a new, empty buffer.
1078 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001079 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001080 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1082 || old_curbuf->br_buf == curbuf)
1083 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1084 else
1085 buf = old_curbuf->br_buf;
1086 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001087 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001088 int old_msg_silent = msg_silent;
1089
1090 if (shortmess(SHM_FILEINFO))
1091 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001092 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001093 // restore msg_silent, so that the command line will be shown
1094 msg_silent = old_msg_silent;
1095
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001096#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001097 if (old_tw != curbuf->b_p_tw)
1098 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001099#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001100 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001101 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001102
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001103#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001104 // Restore the error/interrupt/exception state if not discarded by a
1105 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001106 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109 else if (swap_exists_action == SEA_RECOVER)
1110 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001111#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001112 // Reset the error/interrupt/exception state here so that
1113 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001114 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001115#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001116
Bram Moolenaarc667da52019-11-30 20:52:27 +01001117 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001119 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001120 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001122 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001123
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001124#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001125 // Restore the error/interrupt/exception state if not discarded by a
1126 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001127 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 swap_exists_action = SEA_NONE;
1131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133/*
1134 * do_bufdel() - delete or unload buffer(s)
1135 *
1136 * addr_count == 0: ":bdel" - delete current buffer
1137 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1138 * buffer "end_bnr", then any other arguments.
1139 * addr_count == 2: ":N,N bdel" - delete buffers in range
1140 *
1141 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1142 * DOBUF_DEL (":bdel")
1143 *
1144 * Returns error message or NULL
1145 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001146 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001147do_bufdel(
1148 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001149 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001150 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001151 int start_bnr, // first buffer number in a range
1152 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001153 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001155 int do_current = 0; // delete current buffer?
1156 int deleted = 0; // number of buffers deleted
1157 char *errormsg = NULL; // return value
1158 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 char_u *p;
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (addr_count == 0)
1162 {
1163 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1164 }
1165 else
1166 {
1167 if (addr_count == 2)
1168 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001169 if (*arg) // both range and argument is not allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001170 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 bnr = start_bnr;
1172 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001173 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 bnr = end_bnr;
1175
1176 for ( ;!got_int; ui_breakcheck())
1177 {
1178 /*
1179 * delete the current buffer last, otherwise when the
1180 * current buffer is deleted, the next buffer becomes
1181 * the current one and will be loaded, which may then
1182 * also be deleted, etc.
1183 */
1184 if (bnr == curbuf->b_fnum)
1185 do_current = bnr;
1186 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1187 forceit) == OK)
1188 ++deleted;
1189
1190 /*
1191 * find next buffer number to delete/unload
1192 */
1193 if (addr_count == 2)
1194 {
1195 if (++bnr > end_bnr)
1196 break;
1197 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001198 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
1200 arg = skipwhite(arg);
1201 if (*arg == NUL)
1202 break;
1203 if (!VIM_ISDIGIT(*arg))
1204 {
1205 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001206 bnr = buflist_findpat(arg, p,
1207 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001208 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001209 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 break;
1211 arg = p;
1212 }
1213 else
1214 bnr = getdigits(&arg);
1215 }
1216 }
1217 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1218 FORWARD, do_current, forceit) == OK)
1219 ++deleted;
1220
1221 if (deleted == 0)
1222 {
1223 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001228 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231 else if (deleted >= p_report)
1232 {
1233 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001234 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001235 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001237 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001238 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001240 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001241 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 }
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 return errormsg;
1247}
1248
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249/*
1250 * Make the current buffer empty.
1251 * Used when it is wiped out and it's the last buffer.
1252 */
1253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001254empty_curbuf(
1255 int close_others,
1256 int forceit,
1257 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001258{
1259 int retval;
1260 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001261 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001262
1263 if (action == DOBUF_UNLOAD)
1264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001266 return FAIL;
1267 }
1268
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001269 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001270 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001271 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001272 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001273
1274 setpcmark();
1275 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1276 forceit ? ECMD_FORCEIT : 0, curwin);
1277
1278 /*
1279 * do_ecmd() may create a new buffer, then we have to delete
1280 * the old one. But do_ecmd() may have done that already, check
1281 * if the buffer still exists.
1282 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001283 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001284 close_buffer(NULL, buf, action, FALSE);
1285 if (!close_others)
1286 need_fileinfo = FALSE;
1287 return retval;
1288}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290/*
1291 * Implementation of the commands for the buffer list.
1292 *
1293 * action == DOBUF_GOTO go to specified buffer
1294 * action == DOBUF_SPLIT split window and go to specified buffer
1295 * action == DOBUF_UNLOAD unload specified buffer(s)
1296 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1297 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001298 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 *
1300 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1301 * start == DOBUF_FIRST go to "count" buffer from first buffer
1302 * start == DOBUF_LAST go to "count" buffer from last buffer
1303 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1304 *
1305 * Return FAIL or OK.
1306 */
1307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308do_buffer(
1309 int action,
1310 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001311 int dir, // FORWARD or BACKWARD
1312 int count, // buffer number or number of buffers
1313 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 buf_T *buf;
1316 buf_T *bp;
1317 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001318 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320 switch (start)
1321 {
1322 case DOBUF_FIRST: buf = firstbuf; break;
1323 case DOBUF_LAST: buf = lastbuf; break;
1324 default: buf = curbuf; break;
1325 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001326 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
1328 while (count-- > 0)
1329 {
1330 do
1331 {
1332 buf = buf->b_next;
1333 if (buf == NULL)
1334 buf = firstbuf;
1335 }
1336 while (buf != curbuf && !bufIsChanged(buf));
1337 }
1338 if (!bufIsChanged(buf))
1339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001340 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 return FAIL;
1342 }
1343 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001344 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
1346 while (buf != NULL && buf->b_fnum != count)
1347 buf = buf->b_next;
1348 }
1349 else
1350 {
1351 bp = NULL;
1352 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1353 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 // remember the buffer where we start, we come back there when all
1355 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (bp == NULL)
1357 bp = buf;
1358 if (dir == FORWARD)
1359 {
1360 buf = buf->b_next;
1361 if (buf == NULL)
1362 buf = firstbuf;
1363 }
1364 else
1365 {
1366 buf = buf->b_prev;
1367 if (buf == NULL)
1368 buf = lastbuf;
1369 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001370 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (unload || buf->b_p_bl)
1372 {
1373 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001374 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 if (bp == buf)
1377 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001378 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001379 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 return FAIL;
1381 }
1382 }
1383 }
1384
Bram Moolenaarc667da52019-11-30 20:52:27 +01001385 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
1387 if (start == DOBUF_FIRST)
1388 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001389 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001391 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001394 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001396 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 return FAIL;
1398 }
1399
1400#ifdef FEAT_GUI
1401 need_mouse_correct = TRUE;
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /*
1405 * delete buffer buf from memory and/or the list
1406 */
1407 if (unload)
1408 {
1409 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001410 bufref_T bufref;
1411
Bram Moolenaar94f01952018-09-01 15:30:03 +02001412 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001413 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001414
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001415 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaarc667da52019-11-30 20:52:27 +01001417 // When unloading or deleting a buffer that's already unloaded and
1418 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001419 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1420 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 return FAIL;
1422
1423 if (!forceit && bufIsChanged(buf))
1424 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001425#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if ((p_confirm || cmdmod.confirm) && p_write)
1427 {
1428 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001429 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001430 // Autocommand deleted buffer, oops! It's not changed
1431 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001433 // If it's still changed fail silently, the dialog already
1434 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001435 if (bufIsChanged(buf))
1436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001438 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001441 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 buf->b_fnum);
1443 return FAIL;
1444 }
1445 }
1446
Bram Moolenaarc667da52019-11-30 20:52:27 +01001447 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001448 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001449 end_visual_mode();
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 /*
1452 * If deleting the last (listed) buffer, make it empty.
1453 * The last (listed) buffer cannot be unloaded.
1454 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001455 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (bp->b_p_bl && bp != buf)
1457 break;
1458 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001459 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 /*
1462 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001463 * (unless it's the only window). Repeat this so long as we end up in
1464 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001466 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001467 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001468 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001469 {
1470 if (win_close(curwin, FALSE) == FAIL)
1471 break;
1472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
1474 /*
1475 * If the buffer to be deleted is not the current one, delete it here.
1476 */
1477 if (buf != curbuf)
1478 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001479 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001480 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001481 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 return OK;
1483 }
1484
1485 /*
1486 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001487 * There should be another, otherwise it would have been handled
1488 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001489 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 * Then prefer the buffer we most recently visited.
1491 * Else try to find one that is loaded, after the current buffer,
1492 * then before the current buffer.
1493 * Finally use any buffer.
1494 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001495 buf = NULL; // selected buffer
1496 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001497 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1498 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001500 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 int jumpidx;
1503
1504 jumpidx = curwin->w_jumplistidx - 1;
1505 if (jumpidx < 0)
1506 jumpidx = curwin->w_jumplistlen - 1;
1507
1508 forward = jumpidx;
1509 while (jumpidx != curwin->w_jumplistidx)
1510 {
1511 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1512 if (buf != NULL)
1513 {
1514 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001515 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 else if (buf->b_ml.ml_mfp == NULL)
1517 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001518 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (bp == NULL)
1520 bp = buf;
1521 buf = NULL;
1522 }
1523 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001524 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001526 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1528 break;
1529 if (--jumpidx < 0)
1530 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001531 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 break;
1533 }
1534 }
1535#endif
1536
Bram Moolenaarc667da52019-11-30 20:52:27 +01001537 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 forward = TRUE;
1540 buf = curbuf->b_next;
1541 for (;;)
1542 {
1543 if (buf == NULL)
1544 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001545 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 break;
1547 buf = curbuf->b_prev;
1548 forward = FALSE;
1549 continue;
1550 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001551 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001554 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001556 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 bp = buf;
1558 }
1559 if (forward)
1560 buf = buf->b_next;
1561 else
1562 buf = buf->b_prev;
1563 }
1564 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001565 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001567 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001569 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (buf->b_p_bl && buf != curbuf)
1571 break;
1572 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001573 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 {
1575 if (curbuf->b_next != NULL)
1576 buf = curbuf->b_next;
1577 else
1578 buf = curbuf->b_prev;
1579 }
1580 }
1581
Bram Moolenaara02471e2014-01-10 16:43:14 +01001582 if (buf == NULL)
1583 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001584 // Autocommands must have wiped out all other buffers. Only option
1585 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001586 return empty_curbuf(FALSE, forceit, action);
1587 }
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 /*
1590 * make buf current buffer
1591 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001592 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001594 // If 'switchbuf' contains "useopen": jump to first window containing
1595 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001596 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001597 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001598 // If 'switchbuf' contains "usetab": jump to first window in any tab
1599 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001600 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 return OK;
1602 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 return FAIL;
1604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaarc667da52019-11-30 20:52:27 +01001606 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (buf == curbuf)
1608 return OK;
1609
1610 /*
1611 * Check if the current buffer may be abandoned.
1612 */
1613 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1614 {
1615#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1616 if ((p_confirm || cmdmod.confirm) && p_write)
1617 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001618 bufref_T bufref;
1619
1620 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001622 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001623 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 if (bufIsChanged(curbuf))
1627#endif
1628 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001629 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 return FAIL;
1631 }
1632 }
1633
Bram Moolenaarc667da52019-11-30 20:52:27 +01001634 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 set_curbuf(buf, action);
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001638 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001640#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001641 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return FAIL;
1643#endif
1644
1645 return OK;
1646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648/*
1649 * Set current buffer to "buf". Executes autocommands and closes current
1650 * buffer. "action" tells how to close the current buffer:
1651 * DOBUF_GOTO free or hide it
1652 * DOBUF_SPLIT nothing
1653 * DOBUF_UNLOAD unload it
1654 * DOBUF_DEL delete it
1655 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001656 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
1658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660{
1661 buf_T *prevbuf;
1662 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001663 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001664#ifdef FEAT_SYN_HL
1665 long old_tw = curbuf->b_p_tw;
1666#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001667 bufref_T newbufref;
1668 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001671 if (!cmdmod.keepalt)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001672 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1673 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaarc667da52019-11-30 20:52:27 +01001675 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaarc667da52019-11-30 20:52:27 +01001678 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001680 set_bufref(&prevbufref, prevbuf);
1681 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaarc667da52019-11-30 20:52:27 +01001683 // Autocommands may delete the curren buffer and/or the buffer we wan to go
1684 // to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001685 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001686 || (bufref_valid(&prevbufref)
1687 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001688#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001689 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001691 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001693#ifdef FEAT_SYN_HL
1694 if (prevbuf == curwin->w_buffer)
1695 reset_synblock(curwin);
1696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001698 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001699#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001700 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001702 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001704 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001705 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001706 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001707 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1709 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001710 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001711 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001712 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001713 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001714 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001717 // An autocommand may have deleted "buf", already entered it (e.g., when
1718 // it did ":bunload") or aborted the script processing.
1719 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001720 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001721#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001722 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001724 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001727#ifdef FEAT_SYN_HL
1728 if (old_tw != curbuf->b_p_tw)
1729 check_colorcolumn(curwin);
1730#endif
1731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
1734/*
1735 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001736 * Old curbuf must have been abandoned already! This also means "curbuf" may
1737 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001740enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001742 // Get the buffer in the current window.
1743 curwin->w_buffer = buf;
1744 curbuf = buf;
1745 ++curbuf->b_nwindows;
1746
1747 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1749 if (!buf->b_help)
1750 get_winopts(buf);
1751#ifdef FEAT_FOLDING
1752 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001753 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001755 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001759 if (curwin->w_p_diff)
1760 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#endif
1762
Bram Moolenaara971b822011-09-14 14:43:25 +02001763#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001764 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001765#endif
1766
Bram Moolenaarc667da52019-11-30 20:52:27 +01001767 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 curwin->w_cursor.lnum = 1;
1769 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001772 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaarc667da52019-11-30 20:52:27 +01001774 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001775 curwin->w_valid = 0;
1776
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001777 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1778 curbuf->b_last_cursor.col, TRUE);
1779
Bram Moolenaarc667da52019-11-30 20:52:27 +01001780 // Make sure the buffer is loaded.
1781 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001782 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001783 // If there is no filetype, allow for detecting one. Esp. useful for
1784 // ":ball" used in a autocommand. If there already is a filetype we
1785 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001786 if (*curbuf->b_p_ft == NUL)
1787 did_filetype = FALSE;
1788
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001789 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 else
1792 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001793 if (!msg_silent && !shortmess(SHM_FILEINFO))
1794 need_fileinfo = TRUE; // display file info after redraw
1795
1796 // check if file changed
1797 (void)buf_check_timestamp(curbuf, FALSE);
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001800#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1804 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806
Bram Moolenaarc667da52019-11-30 20:52:27 +01001807 // If autocommands did not change the cursor position, restore cursor lnum
1808 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (curwin->w_cursor.lnum == 1 && inindent(0))
1810 buflist_getfpos();
1811
Bram Moolenaarc667da52019-11-30 20:52:27 +01001812 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#ifdef FEAT_TITLE
1814 maketitle();
1815#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001816 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001817 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001818 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar009b2592004-10-24 19:18:58 +00001820#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001821 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001822 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001823#endif
1824
Bram Moolenaarc667da52019-11-30 20:52:27 +01001825 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001826 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
1828#ifdef FEAT_KEYMAP
1829 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001830 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001832#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001833 // May need to set the spell language. Can only do this after the buffer
1834 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001835 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1836 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001837#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001838#ifdef FEAT_VIMINFO
1839 curbuf->b_last_used = vim_time();
1840#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 redraw_later(NOT_VALID);
1843}
1844
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001845#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1846/*
1847 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001848 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001849 */
1850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001852{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001853 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001854 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001855 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001856 shorten_fnames(TRUE);
1857}
1858#endif
1859
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001860 void
1861no_write_message(void)
1862{
1863#ifdef FEAT_TERMINAL
1864 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001865 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001866 else
1867#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001868 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001869}
1870
1871 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001872no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001873{
1874#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001875 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001876 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001877 else
1878#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001879 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001880}
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
1883 * functions for dealing with the buffer list
1884 */
1885
1886/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001887 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1888 * only one window. That means it can be re-used.
1889 */
1890 int
1891curbuf_reusable(void)
1892{
1893 return (curbuf != NULL
1894 && curbuf->b_ffname == NULL
1895 && curbuf->b_nwindows <= 1
1896 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001897#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001898 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001899#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001900 && !curbufIsChanged());
1901}
1902
1903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 * Add a file name to the buffer list. Return a pointer to the buffer.
1905 * If the same file name already exists return a pointer to that buffer.
1906 * If it does not exist, or if fname == NULL, a new entry is created.
1907 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1908 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1909 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001910 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001911 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1912 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001913 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 * This is the ONLY way to create a new buffer.
1915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001917buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001918 char_u *ffname_arg, // full path of fname or relative
1919 char_u *sfname_arg, // short fname or NULL
1920 linenr_T lnum, // preferred cursor line
1921 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001923 char_u *ffname = ffname_arg;
1924 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 buf_T *buf;
1926#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001927 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#endif
1929
Bram Moolenaar480778b2016-07-14 22:09:39 +02001930 if (top_file_num == 1)
1931 hash_init(&buf_hashtab);
1932
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001933 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
1935 /*
1936 * If file name already exists in the list, update the entry.
1937 */
1938#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001939 // On Unix we can use inode numbers when the file exists. Works better
1940 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1942 st.st_dev = (dev_T)-1;
1943#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001944 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945#ifdef UNIX
1946 buflist_findname_stat(ffname, &st)
1947#else
1948 buflist_findname(ffname)
1949#endif
1950 ) != NULL)
1951 {
1952 vim_free(ffname);
1953 if (lnum != 0)
1954 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001955
1956 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001957 // copy the options now, if 'cpo' doesn't have 's' and not done
1958 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02001959 buf_copy_options(buf, 0);
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1962 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001963 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001966 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001968 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001969 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001970 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001971 return NULL;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974 return buf;
1975 }
1976
1977 /*
1978 * If the current buffer has no name and no contents, use the current
1979 * buffer. Otherwise: Need to allocate a new buffer structure.
1980 *
1981 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001982 * (A spell file buffer is allocated in spell.c, but that's not a normal
1983 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 */
1985 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001986 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
1988 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001989 // It's like this buffer is deleted. Watch out for autocommands that
1990 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (curbuf->b_p_bl)
1992 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1993 if (buf == curbuf)
1994 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001995#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001996 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002001 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 clear_string_option(&buf->b_p_bh);
2003 clear_string_option(&buf->b_p_bt);
2004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 if (buf != curbuf || curbuf == NULL)
2007 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002008 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (buf == NULL)
2010 {
2011 vim_free(ffname);
2012 return NULL;
2013 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002014#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002015 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002016 buf->b_vars = dict_alloc();
2017 if (buf->b_vars == NULL)
2018 {
2019 vim_free(ffname);
2020 vim_free(buf);
2021 return NULL;
2022 }
2023 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2024#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002025 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027
2028 if (ffname != NULL)
2029 {
2030 buf->b_ffname = ffname;
2031 buf->b_sfname = vim_strsave(sfname);
2032 }
2033
2034 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002035 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2038 || buf->b_wininfo == NULL)
2039 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002040 if (buf->b_sfname != buf->b_ffname)
2041 VIM_CLEAR(buf->b_sfname);
2042 else
2043 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002044 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (buf != curbuf)
2046 free_buffer(buf);
2047 return NULL;
2048 }
2049
2050 if (buf == curbuf)
2051 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002052 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002053 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002054 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002056#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002057 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 return NULL;
2059#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002060 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002061
Bram Moolenaarc667da52019-11-30 20:52:27 +01002062 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002063 buf->b_p_initialized = FALSE;
2064 buf_copy_options(buf, BCO_ENTER);
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002067 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 curbuf->b_kmap_state |= KEYMAP_INIT;
2069#endif
2070 }
2071 else
2072 {
2073 /*
2074 * put new buffer at the end of the buffer list
2075 */
2076 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002077 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079 buf->b_prev = NULL;
2080 firstbuf = buf;
2081 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002082 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 lastbuf->b_next = buf;
2085 buf->b_prev = lastbuf;
2086 }
2087 lastbuf = buf;
2088
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002089 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2090 {
2091 // Recycle a previously used buffer number. Used for buffers which
2092 // are normally hidden, e.g. in a popup window. Avoids that the
2093 // buffer number grows rapidly.
2094 --buf_reuse.ga_len;
2095 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002096
2097 // Move buffer to the right place in the buffer list.
2098 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2099 {
2100 buf_T *prev = buf->b_prev;
2101
2102 prev->b_next = buf->b_next;
2103 if (prev->b_next != NULL)
2104 prev->b_next->b_prev = prev;
2105 buf->b_next = prev;
2106 buf->b_prev = prev->b_prev;
2107 if (buf->b_prev != NULL)
2108 buf->b_prev->b_next = buf;
2109 prev->b_prev = buf;
2110 if (lastbuf == buf)
2111 lastbuf = prev;
2112 if (firstbuf == prev)
2113 firstbuf = buf;
2114 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002115 }
2116 else
2117 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002118 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (emsg_silent == 0)
2122 {
2123 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002124 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 top_file_num = 1;
2127 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002128 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130 /*
2131 * Always copy the options from the current buffer.
2132 */
2133 buf_copy_options(buf, BCO_ALWAYS);
2134 }
2135
2136 buf->b_wininfo->wi_fpos.lnum = lnum;
2137 buf->b_wininfo->wi_win = curwin;
2138
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002139#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002140 hash_init(&buf->b_s.b_keywtab);
2141 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
2143
2144 buf->b_fname = buf->b_sfname;
2145#ifdef UNIX
2146 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002147 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 else
2149 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002150 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 buf->b_dev = st.st_dev;
2152 buf->b_ino = st.st_ino;
2153 }
2154#endif
2155 buf->b_u_synced = TRUE;
2156 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002157 if (flags & BLN_DUMMY)
2158 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002160 clrallmarks(buf); // clear marks
2161 fmarks_check_names(buf); // check file marks for this file
2162 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (!(flags & BLN_DUMMY))
2164 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002165 bufref_T bufref;
2166
Bram Moolenaarc667da52019-11-30 20:52:27 +01002167 // Tricky: these autocommands may change the buffer list. They could
2168 // also split the window with re-using the one empty buffer. This may
2169 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002170 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002171 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002172 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002173 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002175 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002176 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002177 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002178 return NULL;
2179 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002180#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002181 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
2186 return buf;
2187}
2188
2189/*
2190 * Free the memory for the options of a buffer.
2191 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2192 * 'fileencoding'.
2193 */
2194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195free_buf_options(
2196 buf_T *buf,
2197 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 if (free_p_ff)
2200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 clear_string_option(&buf->b_p_bh);
2204 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206#ifdef FEAT_FIND_ID
2207 clear_string_option(&buf->b_p_def);
2208 clear_string_option(&buf->b_p_inc);
2209# ifdef FEAT_EVAL
2210 clear_string_option(&buf->b_p_inex);
2211# endif
2212#endif
2213#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2214 clear_string_option(&buf->b_p_inde);
2215 clear_string_option(&buf->b_p_indk);
2216#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002217#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2218 clear_string_option(&buf->b_p_bexpr);
2219#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002220#if defined(FEAT_CRYPT)
2221 clear_string_option(&buf->b_p_cm);
2222#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002223 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002224#if defined(FEAT_EVAL)
2225 clear_string_option(&buf->b_p_fex);
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#ifdef FEAT_CRYPT
2228 clear_string_option(&buf->b_p_key);
2229#endif
2230 clear_string_option(&buf->b_p_kp);
2231 clear_string_option(&buf->b_p_mps);
2232 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002233 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002235#ifdef FEAT_VARTABS
2236 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002237 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002238 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002239 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002240 buf->b_p_vsts_array = NULL;
2241 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002242 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244#ifdef FEAT_KEYMAP
2245 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002246 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ga_clear(&buf->b_kmap_ga);
2248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#ifdef FEAT_FOLDING
2251 clear_string_option(&buf->b_p_cms);
2252#endif
2253 clear_string_option(&buf->b_p_nf);
2254#ifdef FEAT_SYN_HL
2255 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002256 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002257#endif
2258#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002259 clear_string_option(&buf->b_s.b_p_spc);
2260 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002261 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002262 buf->b_s.b_cap_prog = NULL;
2263 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#endif
2265#ifdef FEAT_SEARCHPATH
2266 clear_string_option(&buf->b_p_sua);
2267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#ifdef FEAT_CINDENT
2270 clear_string_option(&buf->b_p_cink);
2271 clear_string_option(&buf->b_p_cino);
2272#endif
2273#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2274 clear_string_option(&buf->b_p_cinw);
2275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002277#ifdef FEAT_COMPL_FUNC
2278 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002279 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#ifdef FEAT_QUICKFIX
2282 clear_string_option(&buf->b_p_gp);
2283 clear_string_option(&buf->b_p_mp);
2284 clear_string_option(&buf->b_p_efm);
2285#endif
2286 clear_string_option(&buf->b_p_ep);
2287 clear_string_option(&buf->b_p_path);
2288 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002289 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002290#ifdef FEAT_EVAL
2291 clear_string_option(&buf->b_p_tfu);
2292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 clear_string_option(&buf->b_p_dict);
2294 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002295#ifdef FEAT_TEXTOBJ
2296 clear_string_option(&buf->b_p_qe);
2297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002299 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002300#ifdef FEAT_LISP
2301 clear_string_option(&buf->b_p_lw);
2302#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002303 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002304 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305}
2306
2307/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002308 * Get alternate file "n".
2309 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2310 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 * if (options & GETF_SETMARK) call setpcmark()
2312 * if (options & GETF_ALT) we are jumping to an alternate file.
2313 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2314 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002315 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
2317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002318buflist_getfile(
2319 int n,
2320 linenr_T lnum,
2321 int options,
2322 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
2324 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 pos_T *fpos;
2327 colnr_T col;
2328
2329 buf = buflist_findnr(n);
2330 if (buf == NULL)
2331 {
2332 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002335 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 return FAIL;
2337 }
2338
Bram Moolenaarc667da52019-11-30 20:52:27 +01002339 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (buf == curbuf)
2341 return OK;
2342
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002343 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002344 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002345 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002347 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002348 if (curbuf_locked())
2349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaarc667da52019-11-30 20:52:27 +01002351 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (lnum == 0)
2353 {
2354 fpos = buflist_findfpos(buf);
2355 lnum = fpos->lnum;
2356 col = fpos->col;
2357 }
2358 else
2359 col = 0;
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (options & GETF_SWITCH)
2362 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002363 // If 'switchbuf' contains "useopen": jump to first window containing
2364 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002365 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002367
Bram Moolenaarc667da52019-11-30 20:52:27 +01002368 // If 'switchbuf' contains "usetab": jump to first window in any tab
2369 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002370 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002371 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002372
Bram Moolenaarc667da52019-11-30 20:52:27 +01002373 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2374 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002375 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002376 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002378 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002379 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002380 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2381 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002383 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
2385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002388 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2389 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 --RedrawingDisabled;
2392
Bram Moolenaarc667da52019-11-30 20:52:27 +01002393 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (!p_sol && col != 0)
2395 {
2396 curwin->w_cursor.col = col;
2397 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 curwin->w_set_curswant = TRUE;
2400 }
2401 return OK;
2402 }
2403 --RedrawingDisabled;
2404 return FAIL;
2405}
2406
2407/*
2408 * go to the last know line number for the current buffer
2409 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002411buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 pos_T *fpos;
2414
2415 fpos = buflist_findfpos(curbuf);
2416
2417 curwin->w_cursor.lnum = fpos->lnum;
2418 check_cursor_lnum();
2419
2420 if (p_sol)
2421 curwin->w_cursor.col = 0;
2422 else
2423 {
2424 curwin->w_cursor.col = fpos->col;
2425 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 curwin->w_set_curswant = TRUE;
2428 }
2429}
2430
Bram Moolenaar81695252004-12-29 20:58:21 +00002431#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2432/*
2433 * Find file in buffer list by name (it has to be for the current window).
2434 * Returns NULL if not found.
2435 */
2436 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002437buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002438{
2439 char_u *ffname;
2440 buf_T *buf = NULL;
2441
Bram Moolenaarc667da52019-11-30 20:52:27 +01002442 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002443 ffname = FullName_save(fname,
2444#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002445 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002446#else
2447 FALSE
2448#endif
2449 );
2450 if (ffname != NULL)
2451 {
2452 buf = buflist_findname(ffname);
2453 vim_free(ffname);
2454 }
2455 return buf;
2456}
2457#endif
2458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459/*
2460 * Find file in buffer list by name (it has to be for the current window).
2461 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002462 * Skips dummy buffers.
2463 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 */
2465 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002466buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002469 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 if (mch_stat((char *)ffname, &st) < 0)
2472 st.st_dev = (dev_T)-1;
2473 return buflist_findname_stat(ffname, &st);
2474}
2475
2476/*
2477 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2478 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002479 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
2481 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482buflist_findname_stat(
2483 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002484 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486#endif
2487 buf_T *buf;
2488
Bram Moolenaarc667da52019-11-30 20:52:27 +01002489 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002490 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002491 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492#ifdef UNIX
2493 , stp
2494#endif
2495 ))
2496 return buf;
2497 return NULL;
2498}
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500/*
2501 * Find file in buffer list by a regexp pattern.
2502 * Return fnum of the found buffer.
2503 * Return < 0 for error.
2504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002506buflist_findpat(
2507 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002508 char_u *pattern_end, // pointer to first char after pattern
2509 int unlisted, // find unlisted buffers
2510 int diffmode UNUSED, // find diff-mode buffers only
2511 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
2513 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 int match = -1;
2515 int find_listed;
2516 char_u *pat;
2517 char_u *patend;
2518 int attempt;
2519 char_u *p;
2520 int toggledollar;
2521
2522 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2523 {
2524 if (*pattern == '%')
2525 match = curbuf->b_fnum;
2526 else
2527 match = curwin->w_alt_fnum;
2528#ifdef FEAT_DIFF
2529 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2530 match = -1;
2531#endif
2532 }
2533
2534 /*
2535 * Try four ways of matching a listed buffer:
2536 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002537 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 * attempt == 2: with '$' at end (only match at end)
2539 * attempt == 3: with '^' at start and '$' at end (only full match)
2540 * Repeat this for finding an unlisted buffer if there was no matching
2541 * listed buffer.
2542 */
2543 else
2544 {
2545 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2546 if (pat == NULL)
2547 return -1;
2548 patend = pat + STRLEN(pat) - 1;
2549 toggledollar = (patend > pat && *patend == '$');
2550
Bram Moolenaarc667da52019-11-30 20:52:27 +01002551 // First try finding a listed buffer. If not found and "unlisted"
2552 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 find_listed = TRUE;
2554 for (;;)
2555 {
2556 for (attempt = 0; attempt <= 3; ++attempt)
2557 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002558 regmatch_T regmatch;
2559
Bram Moolenaarc667da52019-11-30 20:52:27 +01002560 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002562 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002564 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002566 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2567 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
2569 vim_free(pat);
2570 return -1;
2571 }
2572
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002573 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (buf->b_p_bl == find_listed
2575#ifdef FEAT_DIFF
2576 && (!diffmode || diff_mode_buf(buf))
2577#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002578 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002580 if (curtab_only)
2581 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002582 // Ignore the match if the buffer is not open in
2583 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002584 win_T *wp;
2585
Bram Moolenaar29323592016-07-24 22:04:11 +02002586 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002587 if (wp->w_buffer == buf)
2588 break;
2589 if (wp == NULL)
2590 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002591 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002592 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 match = -2;
2595 break;
2596 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002597 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002600 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002601 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 break;
2603 }
2604
Bram Moolenaarc667da52019-11-30 20:52:27 +01002605 // Only search for unlisted buffers if there was no match with
2606 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 if (!unlisted || !find_listed || match != -1)
2608 break;
2609 find_listed = FALSE;
2610 }
2611
2612 vim_free(pat);
2613 }
2614
2615 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002618 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 return match;
2620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
Bram Moolenaar52410572019-10-27 05:12:45 +01002622#ifdef FEAT_VIMINFO
2623typedef struct {
2624 buf_T *buf;
2625 char_u *match;
2626} bufmatch_T;
2627#endif
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
2630 * Find all buffer names that match.
2631 * For command line expansion of ":buf" and ":sbuf".
2632 * Return OK if matches found, FAIL otherwise.
2633 */
2634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002635ExpandBufnames(
2636 char_u *pat,
2637 int *num_file,
2638 char_u ***file,
2639 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 int count = 0;
2642 buf_T *buf;
2643 int round;
2644 char_u *p;
2645 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002646 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002647#ifdef FEAT_VIMINFO
2648 bufmatch_T *matches = NULL;
2649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaarc667da52019-11-30 20:52:27 +01002651 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 *file = NULL;
2653
Bram Moolenaarc667da52019-11-30 20:52:27 +01002654 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002655 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002657 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002658 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002660 STRCPY(patc, "\\(^\\|[\\/]\\)");
2661 STRCPY(patc + 11, pat + 1);
2662 }
2663 else
2664 patc = pat;
2665
2666 /*
2667 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002668 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002669 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002670 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002671 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002672 regmatch_T regmatch;
2673
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002674 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002675 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002676 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2677 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002678 {
2679 if (patc != pat)
2680 vim_free(patc);
2681 return FAIL;
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 /*
2685 * round == 1: Count the matches.
2686 * round == 2: Build the array to keep the matches.
2687 */
2688 for (round = 1; round <= 2; ++round)
2689 {
2690 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002691 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002693 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002695 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (p != NULL)
2697 {
2698 if (round == 1)
2699 ++count;
2700 else
2701 {
2702 if (options & WILD_HOME_REPLACE)
2703 p = home_replace_save(buf, p);
2704 else
2705 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002706#ifdef FEAT_VIMINFO
2707 if (matches != NULL)
2708 {
2709 matches[count].buf = buf;
2710 matches[count].match = p;
2711 count++;
2712 }
2713 else
2714#endif
2715 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 }
2718 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002719 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 break;
2721 if (round == 1)
2722 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002723 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (*file == NULL)
2725 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002726 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002727 if (patc != pat)
2728 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 return FAIL;
2730 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002731#ifdef FEAT_VIMINFO
2732 if (options & WILD_BUFLASTUSED)
2733 matches = ALLOC_MULT(bufmatch_T, count);
2734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002737 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002738 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 break;
2740 }
2741
Bram Moolenaar05159a02005-02-26 23:04:13 +00002742 if (patc != pat)
2743 vim_free(patc);
2744
Bram Moolenaar52410572019-10-27 05:12:45 +01002745#ifdef FEAT_VIMINFO
2746 if (matches != NULL)
2747 {
2748 int i;
2749 if (count > 1)
2750 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2751 // if the current buffer is first in the list, place it at the end
2752 if (matches[0].buf == curbuf)
2753 {
2754 for (i = 1; i < count; i++)
2755 (*file)[i-1] = matches[i].match;
2756 (*file)[count-1] = matches[0].match;
2757 }
2758 else
2759 {
2760 for (i = 0; i < count; i++)
2761 (*file)[i] = matches[i].match;
2762 }
2763 vim_free(matches);
2764 }
2765#endif
2766
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 *num_file = count;
2768 return (count == 0 ? FAIL : OK);
2769}
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771/*
2772 * Check for a match on the file name for buffer "buf" with regprog "prog".
2773 */
2774 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002775buflist_match(
2776 regmatch_T *rmp,
2777 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002778 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 char_u *match;
2781
Bram Moolenaarc667da52019-11-30 20:52:27 +01002782 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002783 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002785 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 return match;
2788}
2789
2790/*
2791 * Try matching the regexp in "prog" with file name "name".
2792 * Return "name" when there is a match, NULL when not.
2793 */
2794 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002795fname_match(
2796 regmatch_T *rmp,
2797 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002798 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 char_u *match = NULL;
2801 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 if (name != NULL)
2804 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002805 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002806 rmp->rm_ic = p_fic || ignore_case;
2807 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 match = name;
2809 else
2810 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002811 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002813 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 match = name;
2815 vim_free(p);
2816 }
2817 }
2818
2819 return match;
2820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
2822/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002823 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 */
2825 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002826buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002828 char_u key[VIM_SIZEOF_INT * 2 + 1];
2829 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 if (nr == 0)
2832 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002833 sprintf((char *)key, "%x", nr);
2834 hi = hash_find(&buf_hashtab, key);
2835
2836 if (!HASHITEM_EMPTY(hi))
2837 return (buf_T *)(hi->hi_key
2838 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 return NULL;
2840}
2841
2842/*
2843 * Get name of file 'n' in the buffer list.
2844 * When the file has no name an empty string is returned.
2845 * home_replace() is used to shorten the file name (used for marks).
2846 * Returns a pointer to allocated memory, of NULL when failed.
2847 */
2848 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002849buflist_nr2name(
2850 int n,
2851 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002852 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 buf_T *buf;
2855
2856 buf = buflist_findnr(n);
2857 if (buf == NULL)
2858 return NULL;
2859 return home_replace_save(helptail ? buf : NULL,
2860 fullname ? buf->b_ffname : buf->b_fname);
2861}
2862
2863/*
2864 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2865 * When "copy_options" is TRUE save the local window option values.
2866 * When "lnum" is 0 only do the options.
2867 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869buflist_setfpos(
2870 buf_T *buf,
2871 win_T *win,
2872 linenr_T lnum,
2873 colnr_T col,
2874 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 wininfo_T *wip;
2877
2878 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2879 if (wip->wi_win == win)
2880 break;
2881 if (wip == NULL)
2882 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002883 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002884 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (wip == NULL)
2886 return;
2887 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002888 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 lnum = 1;
2890 }
2891 else
2892 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002893 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (wip->wi_prev)
2895 wip->wi_prev->wi_next = wip->wi_next;
2896 else
2897 buf->b_wininfo = wip->wi_next;
2898 if (wip->wi_next)
2899 wip->wi_next->wi_prev = wip->wi_prev;
2900 if (copy_options && wip->wi_optset)
2901 {
2902 clear_winopt(&wip->wi_opt);
2903#ifdef FEAT_FOLDING
2904 deleteFoldRecurse(&wip->wi_folds);
2905#endif
2906 }
2907 }
2908 if (lnum != 0)
2909 {
2910 wip->wi_fpos.lnum = lnum;
2911 wip->wi_fpos.col = col;
2912 }
2913 if (copy_options)
2914 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002915 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2917#ifdef FEAT_FOLDING
2918 wip->wi_fold_manual = win->w_fold_manual;
2919 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2920#endif
2921 wip->wi_optset = TRUE;
2922 }
2923
Bram Moolenaarc667da52019-11-30 20:52:27 +01002924 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 wip->wi_next = buf->b_wininfo;
2926 buf->b_wininfo = wip;
2927 wip->wi_prev = NULL;
2928 if (wip->wi_next)
2929 wip->wi_next->wi_prev = wip;
2930
2931 return;
2932}
2933
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002934#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002935/*
2936 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2937 * page. That's because a diff is local to a tab page.
2938 */
2939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002940wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002941{
2942 win_T *wp;
2943
2944 if (wip->wi_opt.wo_diff)
2945 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002946 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002947 // return FALSE when it's a window in the current tab page, thus
2948 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002949 if (wip->wi_win == wp)
2950 return FALSE;
2951 return TRUE;
2952 }
2953 return FALSE;
2954}
2955#endif
2956
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957/*
2958 * Find info for the current window in buffer "buf".
2959 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002960 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2961 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 * Returns NULL when there isn't any info.
2963 */
2964 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965find_wininfo(
2966 buf_T *buf,
2967 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 wininfo_T *wip;
2970
2971 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002972 if (wip->wi_win == curwin
2973#ifdef FEAT_DIFF
2974 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2975#endif
2976 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002978
Bram Moolenaarc667da52019-11-30 20:52:27 +01002979 // If no wininfo for curwin, use the first in the list (that doesn't have
2980 // 'diff' set and is in another tab page).
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002981 if (wip == NULL)
2982 {
2983#ifdef FEAT_DIFF
2984 if (skip_diff_buffer)
2985 {
2986 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2987 if (!wininfo_other_tab_diff(wip))
2988 break;
2989 }
2990 else
2991#endif
2992 wip = buf->b_wininfo;
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 return wip;
2995}
2996
2997/*
2998 * Reset the local window options to the values last used in this window.
2999 * If the buffer wasn't used in this window before, use the values from
3000 * the most recently used window. If the values were never set, use the
3001 * global values for the window.
3002 */
3003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003004get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 wininfo_T *wip;
3007
3008 clear_winopt(&curwin->w_onebuf_opt);
3009#ifdef FEAT_FOLDING
3010 clearFolding(curwin);
3011#endif
3012
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003013 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003014 if (wip != NULL && wip->wi_win != NULL
3015 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003017 // The buffer is currently displayed in the window: use the actual
3018 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003019 win_T *wp = wip->wi_win;
3020
3021 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3022#ifdef FEAT_FOLDING
3023 curwin->w_fold_manual = wp->w_fold_manual;
3024 curwin->w_foldinvalid = TRUE;
3025 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3026#endif
3027 }
3028 else if (wip != NULL && wip->wi_optset)
3029 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003030 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3032#ifdef FEAT_FOLDING
3033 curwin->w_fold_manual = wip->wi_fold_manual;
3034 curwin->w_foldinvalid = TRUE;
3035 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3036#endif
3037 }
3038 else
3039 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3040
3041#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003042 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (p_fdls >= 0)
3044 curwin->w_p_fdl = p_fdls;
3045#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003046 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048
3049/*
3050 * Find the position (lnum and col) for the buffer 'buf' for the current
3051 * window.
3052 * Returns a pointer to no_position if no position is found.
3053 */
3054 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003055buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003058 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003060 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 if (wip != NULL)
3062 return &(wip->wi_fpos);
3063 else
3064 return &no_position;
3065}
3066
3067/*
3068 * Find the lnum for the buffer 'buf' for the current window.
3069 */
3070 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003071buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 return buflist_findfpos(buf)->lnum;
3074}
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003077 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar52410572019-10-27 05:12:45 +01003082 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int len;
3084 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003085 int ro_char;
3086 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003087#ifdef FEAT_TERMINAL
3088 int job_running;
3089 int job_none_open;
3090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaar52410572019-10-27 05:12:45 +01003092#ifdef FEAT_VIMINFO
3093 garray_T buflist;
3094 buf_T **buflist_data = NULL, **p;
3095
3096 if (vim_strchr(eap->arg, 't'))
3097 {
3098 ga_init2(&buflist, sizeof(buf_T *), 50);
3099 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3100 {
3101 if (ga_grow(&buflist, 1) == OK)
3102 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3103 }
3104
3105 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3106 sizeof(buf_T *), buf_compare);
3107
Bram Moolenaar3b991522019-11-06 23:26:20 +01003108 buflist_data = (buf_T **)buflist.ga_data;
3109 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003110 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003111 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003112
Bram Moolenaar3b991522019-11-06 23:26:20 +01003113 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003114 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3115 : buf->b_next)
3116#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003120#ifdef FEAT_TERMINAL
3121 job_running = term_job_running(buf->b_term);
3122 job_none_open = job_running && term_none_open(buf->b_term);
3123#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003124 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003125 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3126 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3127 || (vim_strchr(eap->arg, '+')
3128 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3129 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003130 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003131 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003132 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3133#ifdef FEAT_TERMINAL
3134 || (vim_strchr(eap->arg, 'R')
3135 && (!job_running || (job_running && job_none_open)))
3136 || (vim_strchr(eap->arg, '?')
3137 && (!job_running || (job_running && !job_none_open)))
3138 || (vim_strchr(eap->arg, 'F')
3139 && (job_running || buf->b_term == NULL))
3140#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003141 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3142 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3143 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3144 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3145 || (vim_strchr(eap->arg, '#')
3146 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003149 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 else
3151 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003152 if (message_filtered(NameBuff))
3153 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003155 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3156 : (bufIsChanged(buf) ? '+' : ' ');
3157#ifdef FEAT_TERMINAL
3158 if (term_job_running(buf->b_term))
3159 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003160 if (term_none_open(buf->b_term))
3161 ro_char = '?';
3162 else
3163 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003164 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3165 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003166 }
3167 else if (buf->b_term != NULL)
3168 ro_char = 'F';
3169 else
3170#endif
3171 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3172
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003173 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003174 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 buf->b_fnum,
3176 buf->b_p_bl ? ' ' : 'u',
3177 buf == curbuf ? '%' :
3178 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3179 buf->b_ml.ml_mfp == NULL ? ' ' :
3180 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003181 ro_char,
3182 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003183 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003184 if (len > IOSIZE - 20)
3185 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaarc667da52019-11-30 20:52:27 +01003187 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 i = 40 - vim_strsize(IObuff);
3189 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003191 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003192#ifdef FEAT_VIMINFO
3193 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3194 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3195 else
3196#endif
3197 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3198 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003199 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003201 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 ui_breakcheck();
3203 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003204
3205#ifdef FEAT_VIMINFO
3206 if (buflist_data)
3207 ga_clear(&buflist);
3208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211/*
3212 * Get file name and line number for file 'fnum'.
3213 * Used by DoOneCmd() for translating '%' and '#'.
3214 * Used by insert_reg() and cmdline_paste() for '#' register.
3215 * Return FAIL if not found, OK for success.
3216 */
3217 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218buflist_name_nr(
3219 int fnum,
3220 char_u **fname,
3221 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 buf_T *buf;
3224
3225 buf = buflist_findnr(fnum);
3226 if (buf == NULL || buf->b_fname == NULL)
3227 return FAIL;
3228
3229 *fname = buf->b_fname;
3230 *lnum = buflist_findlnum(buf);
3231
3232 return OK;
3233}
3234
3235/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003236 * Set the file name for "buf"' to "ffname_arg", short file name to
3237 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 * The file name with the full path is also remembered, for when :cd is used.
3239 * Returns FAIL for failure (file name already in use by other buffer)
3240 * OK otherwise.
3241 */
3242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243setfname(
3244 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003245 char_u *ffname_arg,
3246 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003247 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003249 char_u *ffname = ffname_arg;
3250 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003251 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003253 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#endif
3255
3256 if (ffname == NULL || *ffname == NUL)
3257 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003258 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003259 if (buf->b_sfname != buf->b_ffname)
3260 VIM_CLEAR(buf->b_sfname);
3261 else
3262 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003263 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#ifdef UNIX
3265 st.st_dev = (dev_T)-1;
3266#endif
3267 }
3268 else
3269 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003270 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3271 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return FAIL;
3273
3274 /*
3275 * if the file name is already used in another buffer:
3276 * - if the buffer is loaded, fail
3277 * - if the buffer is not loaded, delete it from the list
3278 */
3279#ifdef UNIX
3280 if (mch_stat((char *)ffname, &st) < 0)
3281 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003282#endif
3283 if (!(buf->b_flags & BF_DUMMY))
3284#ifdef UNIX
3285 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003287 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#endif
3289 if (obuf != NULL && obuf != buf)
3290 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003291 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003294 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 vim_free(ffname);
3296 return FAIL;
3297 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003298 // delete from the list
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003299 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 sfname = vim_strsave(sfname);
3302 if (ffname == NULL || sfname == NULL)
3303 {
3304 vim_free(sfname);
3305 vim_free(ffname);
3306 return FAIL;
3307 }
3308#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003309 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003311 if (buf->b_sfname != buf->b_ffname)
3312 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 buf->b_ffname = ffname;
3315 buf->b_sfname = sfname;
3316 }
3317 buf->b_fname = buf->b_sfname;
3318#ifdef UNIX
3319 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003320 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 else
3322 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003323 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 buf->b_dev = st.st_dev;
3325 buf->b_ino = st.st_ino;
3326 }
3327#endif
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331 buf_name_changed(buf);
3332 return OK;
3333}
3334
3335/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003336 * Crude way of changing the name of a buffer. Use with care!
3337 * The name should be relative to the current directory.
3338 */
3339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003340buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003341{
3342 buf_T *buf;
3343
3344 buf = buflist_findnr(fnum);
3345 if (buf != NULL)
3346 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003347 if (buf->b_sfname != buf->b_ffname)
3348 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003349 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003350 buf->b_ffname = vim_strsave(name);
3351 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003352 // Allocate ffname and expand into full path. Also resolves .lnk
3353 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003354 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003355 buf->b_fname = buf->b_sfname;
3356 }
3357}
3358
3359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 * Take care of what needs to be done when the name of buffer "buf" has
3361 * changed.
3362 */
3363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 /*
3367 * If the file name changed, also change the name of the swapfile
3368 */
3369 if (buf->b_ml.ml_mfp != NULL)
3370 ml_setname(buf);
3371
3372 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003373 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003375 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003377 status_redraw_all(); // status lines need to be redrawn
3378 fmarks_check_names(buf); // check named file marks
3379 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381
3382/*
3383 * set alternate file name for current window
3384 *
3385 * Used by do_one_cmd(), do_write() and do_ecmd().
3386 * Return the buffer.
3387 */
3388 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389setaltfname(
3390 char_u *ffname,
3391 char_u *sfname,
3392 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 buf_T *buf;
3395
Bram Moolenaarc667da52019-11-30 20:52:27 +01003396 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003398 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 curwin->w_alt_fnum = buf->b_fnum;
3400 return buf;
3401}
3402
3403/*
3404 * Get alternate file name for current window.
3405 * Return NULL if there isn't any, and give error message if requested.
3406 */
3407 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003408getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003409 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
3411 char_u *fname;
3412 linenr_T dummy;
3413
3414 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3415 {
3416 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003417 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 return NULL;
3419 }
3420 return fname;
3421}
3422
3423/*
3424 * Add a file name to the buflist and return its number.
3425 * Uses same flags as buflist_new(), except BLN_DUMMY.
3426 *
3427 * used by qf_init(), main() and doarglist()
3428 */
3429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003430buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
3432 buf_T *buf;
3433
3434 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3435 if (buf != NULL)
3436 return buf->b_fnum;
3437 return 0;
3438}
3439
3440#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3441/*
3442 * Adjust slashes in file names. Called after 'shellslash' was set.
3443 */
3444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 buf_T *bp;
3448
Bram Moolenaar29323592016-07-24 22:04:11 +02003449 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 if (bp->b_ffname != NULL)
3452 slash_adjust(bp->b_ffname);
3453 if (bp->b_sfname != NULL)
3454 slash_adjust(bp->b_sfname);
3455 }
3456}
3457#endif
3458
3459/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003460 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 * Also save the local window option values.
3462 */
3463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003464buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003466 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467}
3468
3469/*
3470 * Return TRUE if 'ffname' is not the same file as current file.
3471 * Fname must have a full path (expanded by mch_FullName()).
3472 */
3473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
3476 return otherfile_buf(curbuf, ffname
3477#ifdef UNIX
3478 , NULL
3479#endif
3480 );
3481}
3482
3483 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484otherfile_buf(
3485 buf_T *buf,
3486 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003488 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003490 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003492 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3494 return TRUE;
3495 if (fnamecmp(ffname, buf->b_ffname) == 0)
3496 return FALSE;
3497#ifdef UNIX
3498 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003499 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
Bram Moolenaarc667da52019-11-30 20:52:27 +01003501 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (stp == NULL)
3503 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003504 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 st.st_dev = (dev_T)-1;
3506 stp = &st;
3507 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003508 // Use dev/ino to check if the files are the same, even when the names
3509 // are different (possible with links). Still need to compare the
3510 // name above, for when the file doesn't exist yet.
3511 // Problem: The dev/ino changes when a file is deleted (and created
3512 // again) and remains the same when renamed/moved. We don't want to
3513 // mch_stat() each buffer each time, that would be too slow. Get the
3514 // dev/ino again when they appear to match, but not when they appear
3515 // to be different: Could skip a buffer when it's actually the same
3516 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (buf_same_ino(buf, stp))
3518 {
3519 buf_setino(buf);
3520 if (buf_same_ino(buf, stp))
3521 return FALSE;
3522 }
3523 }
3524#endif
3525 return TRUE;
3526}
3527
3528#if defined(UNIX) || defined(PROTO)
3529/*
3530 * Set inode and device number for a buffer.
3531 * Must always be called when b_fname is changed!.
3532 */
3533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003536 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3539 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003540 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 buf->b_dev = st.st_dev;
3542 buf->b_ino = st.st_ino;
3543 }
3544 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003545 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546}
3547
3548/*
3549 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3550 */
3551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003552buf_same_ino(
3553 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003554 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003556 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 && stp->st_dev == buf->b_dev
3558 && stp->st_ino == buf->b_ino);
3559}
3560#endif
3561
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562/*
3563 * Print info about the current buffer.
3564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003567 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568 int shorthelp,
3569 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 char_u *name;
3572 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003573 char *p;
3574 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003575 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003577 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (buffer == NULL)
3579 return;
3580
Bram Moolenaarc667da52019-11-30 20:52:27 +01003581 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003583 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 p = buffer + STRLEN(buffer);
3585 }
3586 else
3587 p = buffer;
3588
3589 *p++ = '"';
3590 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003591 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 else
3593 {
3594 if (!fullname && curbuf->b_fname != NULL)
3595 name = curbuf->b_fname;
3596 else
3597 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003598 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 (int)(IOSIZE - (p - buffer)), TRUE);
3600 }
3601
Bram Moolenaar32526b32019-01-19 17:43:09 +01003602 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 curbufIsChanged() ? (shortmess(SHM_MOD)
3604 ? " [+]" : _(" [Modified]")) : " ",
3605 (curbuf->b_flags & BF_NOTEDITED)
3606#ifdef FEAT_QUICKFIX
3607 && !bt_dontwrite(curbuf)
3608#endif
3609 ? _("[Not edited]") : "",
3610 (curbuf->b_flags & BF_NEW)
3611#ifdef FEAT_QUICKFIX
3612 && !bt_dontwrite(curbuf)
3613#endif
3614 ? _("[New file]") : "",
3615 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003616 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 : _("[readonly]")) : "",
3618 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3619 || curbuf->b_p_ro) ?
3620 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003621 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3622 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (curwin->w_cursor.lnum > 1000000L)
3624 n = (int)(((long)curwin->w_cursor.lnum) /
3625 ((long)curbuf->b_ml.ml_line_count / 100L));
3626 else
3627 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3628 (long)curbuf->b_ml.ml_line_count);
3629 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003630 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631#ifdef FEAT_CMDL_INFO
3632 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003633 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003634 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003635 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3636 curbuf->b_ml.ml_line_count),
3637 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638#endif
3639 else
3640 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003641 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 _("line %ld of %ld --%d%%-- col "),
3643 (long)curwin->w_cursor.lnum,
3644 (long)curbuf->b_ml.ml_line_count,
3645 n);
3646 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003647 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003648 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3650 }
3651
Bram Moolenaar32526b32019-01-19 17:43:09 +01003652 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3653 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 if (dont_truncate)
3656 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003657 // Temporarily set msg_scroll to avoid the message being truncated.
3658 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 msg_start();
3660 n = msg_scroll;
3661 msg_scroll = TRUE;
3662 msg(buffer);
3663 msg_scroll = n;
3664 }
3665 else
3666 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003667 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003669 // Need to repeat the message after redrawing when:
3670 // - When restart_edit is set (otherwise there will be a delay
3671 // before redrawing).
3672 // - When the screen was scrolled but there is no wait-return
3673 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003674 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676
3677 vim_free(buffer);
3678}
3679
3680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003681col_print(
3682 char_u *buf,
3683 size_t buflen,
3684 int col,
3685 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003688 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003690 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691}
3692
3693#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694static char_u *lasttitle = NULL;
3695static char_u *lasticon = NULL;
3696
Bram Moolenaar84a93082018-06-16 22:58:15 +02003697/*
3698 * Put the file name in the title bar and icon of the window.
3699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003704 char_u *title_str = NULL;
3705 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 int maxlen = 0;
3707 int len;
3708 int mustset;
3709 char_u buf[IOSIZE];
3710 int off;
3711
3712 if (!redrawing())
3713 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003714 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 need_maketitle = TRUE;
3716 return;
3717 }
3718
3719 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003720 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003721 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 if (p_title)
3724 {
3725 if (p_titlelen > 0)
3726 {
3727 maxlen = p_titlelen * Columns / 100;
3728 if (maxlen < 10)
3729 maxlen = 10;
3730 }
3731
Bram Moolenaar84a93082018-06-16 22:58:15 +02003732 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (*p_titlestring != NUL)
3734 {
3735#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003736 if (stl_syntax & STL_IN_TITLE)
3737 {
3738 int use_sandbox = FALSE;
3739 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003740
3741# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003742 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003743# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003744 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003745 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003746 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003747 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003748 if (called_emsg)
3749 set_string_option_direct((char_u *)"titlestring", -1,
3750 (char_u *)"", OPT_FREE, SID_ERROR);
3751 called_emsg |= save_called_emsg;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
3754#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003755 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003759 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
Bram Moolenaar2c666692012-09-05 13:30:40 +02003761#define SPACE_FOR_FNAME (IOSIZE - 100)
3762#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003763#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003765 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003766#ifdef FEAT_TERMINAL
3767 else if (curbuf->b_term != NULL)
3768 {
3769 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3770 SPACE_FOR_FNAME);
3771 }
3772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 else
3774 {
3775 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003776 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779
Bram Moolenaar21554412017-07-24 21:44:43 +02003780#ifdef FEAT_TERMINAL
3781 if (curbuf->b_term == NULL)
3782#endif
3783 switch (bufIsChanged(curbuf)
3784 + (curbuf->b_p_ro * 2)
3785 + (!curbuf->b_p_ma * 4))
3786 {
3787 case 1: STRCAT(buf, " +"); break;
3788 case 2: STRCAT(buf, " ="); break;
3789 case 3: STRCAT(buf, " =+"); break;
3790 case 4:
3791 case 6: STRCAT(buf, " -"); break;
3792 case 5:
3793 case 7: STRCAT(buf, " -+"); break;
3794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaar21554412017-07-24 21:44:43 +02003796 if (curbuf->b_fname != NULL
3797#ifdef FEAT_TERMINAL
3798 && curbuf->b_term == NULL
3799#endif
3800 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003802 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 off = (int)STRLEN(buf);
3804 buf[off++] = ' ';
3805 buf[off++] = '(';
3806 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003807 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003809 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (isalpha(buf[off]) && buf[off + 1] == ':')
3811 off += 2;
3812#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003813 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003814 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003816 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003817 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003818 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003819 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003823
Bram Moolenaarc667da52019-11-30 20:52:27 +01003824 // Translate unprintable chars and concatenate. Keep some
3825 // room for the server name. When there is no room (very long
3826 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003827 if (off < SPACE_FOR_DIR)
3828 {
3829 p = transstr(buf + off);
3830 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3831 vim_free(p);
3832 }
3833 else
3834 {
3835 vim_strncpy(buf + off, (char_u *)"...",
3836 (size_t)(SPACE_FOR_ARGNR - off));
3837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 STRCAT(buf, ")");
3839 }
3840
Bram Moolenaar2c666692012-09-05 13:30:40 +02003841 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843#if defined(FEAT_CLIENTSERVER)
3844 if (serverName != NULL)
3845 {
3846 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003847 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 else
3850#endif
3851 STRCAT(buf, " - VIM");
3852
3853 if (maxlen > 0)
3854 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003855 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003856 if (vim_strsize(buf) > maxlen)
3857 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 }
3860 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003861 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 if (p_icon)
3864 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003865 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (*p_iconstring != NUL)
3867 {
3868#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003869 if (stl_syntax & STL_IN_ICON)
3870 {
3871 int use_sandbox = FALSE;
3872 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003873
3874# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003875 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003876# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003877 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003878 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003879 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003880 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003881 if (called_emsg)
3882 set_string_option_direct((char_u *)"iconstring", -1,
3883 (char_u *)"", OPT_FREE, SID_ERROR);
3884 called_emsg |= save_called_emsg;
3885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 else
3887#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003888 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 else
3891 {
3892 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003893 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003894 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003895 p = gettail(curbuf->b_ffname);
3896 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003897 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003898 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (len > 100)
3900 {
3901 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003903 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003904 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003906 STRCPY(icon_str, p);
3907 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
3910
Bram Moolenaar84a93082018-06-16 22:58:15 +02003911 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
3913 if (mustset)
3914 resettitle();
3915}
3916
3917/*
3918 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3919 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003920 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 */
3922 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003923value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 if ((str == NULL) != (*last == NULL)
3926 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3927 {
3928 vim_free(*last);
3929 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003932 mch_restore_title(
3933 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003938 return TRUE;
3939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941 return FALSE;
3942}
3943
3944/*
3945 * Put current window title back (used after calling a shell)
3946 */
3947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
3950 mch_settitle(lasttitle, lasticon);
3951}
Bram Moolenaarea408852005-06-25 22:49:46 +00003952
3953# if defined(EXITFREE) || defined(PROTO)
3954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003955free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003956{
3957 vim_free(lasttitle);
3958 vim_free(lasticon);
3959}
3960# endif
3961
Bram Moolenaarc667da52019-11-30 20:52:27 +01003962#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003964#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003966 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * Return length of string in screen cells.
3968 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003969 * Normally works for window "wp", except when working for 'tabline' then it
3970 * is "curwin".
3971 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 * Items are drawn interspersed with the text that surrounds it
3973 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3974 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3975 *
3976 * If maxwidth is not zero, the string will be filled at any middle marker
3977 * or truncated if too long, fillchar is used for all whitespace.
3978 */
3979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003980build_stl_str_hl(
3981 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003982 char_u *out, // buffer to write into != NameBuff
3983 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01003984 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003985 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01003986 int fillchar,
3987 int maxwidth,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003988 struct stl_hlrec *hltab, // return: HL attributes (can be NULL)
3989 struct stl_hlrec *tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar10772302019-01-20 18:25:54 +01003991 linenr_T lnum;
3992 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 char_u *p;
3994 char_u *s;
3995 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003996 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003998 win_T *save_curwin;
3999 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004000 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001#endif
4002 int empty_line;
4003 colnr_T virtcol;
4004 long l;
4005 long n;
4006 int prevchar_isflag;
4007 int prevchar_isitem;
4008 int itemisflag;
4009 int fillable;
4010 char_u *str;
4011 long num;
4012 int width;
4013 int itemcnt;
4014 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004015 int group_end_userhl;
4016 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 int groupitem[STL_MAX_ITEM];
4018 int groupdepth;
4019 struct stl_item
4020 {
4021 char_u *start;
4022 int minwid;
4023 int maxwid;
4024 enum
4025 {
4026 Normal,
4027 Empty,
4028 Group,
4029 Middle,
4030 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004031 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 Trunc
4033 } type;
4034 } item[STL_MAX_ITEM];
4035 int minwid;
4036 int maxwid;
4037 int zeropad;
4038 char_u base;
4039 char_u opt;
4040#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004041 char_u buf_tmp[TMPLEN];
4042 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004043 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004044 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004045 int save_must_redraw = must_redraw;
4046 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004047
4048#ifdef FEAT_EVAL
4049 /*
4050 * When the format starts with "%!" then evaluate it as an expression and
4051 * use the result as the actual format string.
4052 */
4053 if (fmt[0] == '%' && fmt[1] == '!')
4054 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004055 typval_T tv;
4056
4057 tv.v_type = VAR_NUMBER;
4058 tv.vval.v_number = wp->w_id;
4059 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4060
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004061 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
4062 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004063 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004064
4065 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004066 }
4067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 if (fillchar == 0)
4070 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004071 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 else if (mb_char2len(fillchar) > 1)
4073 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074
Bram Moolenaar10772302019-01-20 18:25:54 +01004075 // The cursor in windows other than the current one isn't always
4076 // up-to-date, esp. because of autocommands and timers.
4077 lnum = wp->w_cursor.lnum;
4078 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4079 {
4080 lnum = wp->w_buffer->b_ml.ml_line_count;
4081 wp->w_cursor.lnum = lnum;
4082 }
4083
4084 // Get line & check if empty (cursorpos will show "0-1"). Note that
4085 // p will become invalid when getting another buffer line.
4086 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004087 empty_line = (*p == NUL);
4088
Bram Moolenaar10772302019-01-20 18:25:54 +01004089 // Get the byte value now, in case we need it below. This is more efficient
4090 // than making a copy of the line.
4091 len = STRLEN(p);
4092 if (wp->w_cursor.col > (colnr_T)len)
4093 {
4094 // Line may have changed since checking the cursor column, or the lnum
4095 // was adjusted above.
4096 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004097 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004098 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004099 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004100 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004101 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 groupdepth = 0;
4104 p = out;
4105 curitem = 0;
4106 prevchar_isflag = TRUE;
4107 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004108 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004110 if (curitem == STL_MAX_ITEM)
4111 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004112 // There are too many items. Add the error code to the statusline
4113 // to give the user a hint about what went wrong.
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004114 if (p + 6 < out + outlen)
4115 {
4116 mch_memmove(p, " E541", (size_t)5);
4117 p += 5;
4118 }
4119 break;
4120 }
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (*s != NUL && *s != '%')
4123 prevchar_isflag = prevchar_isitem = FALSE;
4124
4125 /*
4126 * Handle up to the next '%' or the end.
4127 */
4128 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4129 *p++ = *s++;
4130 if (*s == NUL || p + 1 >= out + outlen)
4131 break;
4132
4133 /*
4134 * Handle one '%' item.
4135 */
4136 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004137 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004138 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (*s == '%')
4140 {
4141 if (p + 1 >= out + outlen)
4142 break;
4143 *p++ = *s++;
4144 prevchar_isflag = prevchar_isitem = FALSE;
4145 continue;
4146 }
4147 if (*s == STL_MIDDLEMARK)
4148 {
4149 s++;
4150 if (groupdepth > 0)
4151 continue;
4152 item[curitem].type = Middle;
4153 item[curitem++].start = p;
4154 continue;
4155 }
4156 if (*s == STL_TRUNCMARK)
4157 {
4158 s++;
4159 item[curitem].type = Trunc;
4160 item[curitem++].start = p;
4161 continue;
4162 }
4163 if (*s == ')')
4164 {
4165 s++;
4166 if (groupdepth < 1)
4167 continue;
4168 groupdepth--;
4169
4170 t = item[groupitem[groupdepth]].start;
4171 *p = NUL;
4172 l = vim_strsize(t);
4173 if (curitem > groupitem[groupdepth] + 1
4174 && item[groupitem[groupdepth]].minwid == 0)
4175 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004176 // remove group if all items are empty and highlight group
4177 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004178 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004179 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4180 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004181 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004182 {
4183 group_start_userhl = group_end_userhl = item[n].minwid;
4184 break;
4185 }
4186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004188 {
4189 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004191 if (item[n].type == Highlight)
4192 group_end_userhl = item[n].minwid;
4193 }
4194 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 p = t;
4197 l = 0;
4198 }
4199 }
4200 if (l > item[groupitem[groupdepth]].maxwid)
4201 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004202 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (has_mbyte)
4204 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004205 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 n = 0;
4207 while (l >= item[groupitem[groupdepth]].maxwid)
4208 {
4209 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004210 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212 }
4213 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004214 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004217 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004219
4220 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 while (++l < item[groupitem[groupdepth]].minwid)
4222 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
Bram Moolenaarc667da52019-11-30 20:52:27 +01004224 // correct the start of the items for the truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4226 {
4227 item[l].start -= n;
4228 if (item[l].start < t)
4229 item[l].start = t;
4230 }
4231 }
4232 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4233 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004234 // fill
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 n = item[groupitem[groupdepth]].minwid;
4236 if (n < 0)
4237 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004238 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 n = 0 - n;
4240 while (l++ < n && p + 1 < out + outlen)
4241 *p++ = fillchar;
4242 }
4243 else
4244 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004245 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004246 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 l = n - l;
4248 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004249 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 p += l;
4251 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4252 item[n].start += l;
4253 for ( ; l > 0; l--)
4254 *t++ = fillchar;
4255 }
4256 }
4257 continue;
4258 }
4259 minwid = 0;
4260 maxwid = 9999;
4261 zeropad = FALSE;
4262 l = 1;
4263 if (*s == '0')
4264 {
4265 s++;
4266 zeropad = TRUE;
4267 }
4268 if (*s == '-')
4269 {
4270 s++;
4271 l = -1;
4272 }
4273 if (VIM_ISDIGIT(*s))
4274 {
4275 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004276 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 minwid = 0;
4278 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004279 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 item[curitem].type = Highlight;
4282 item[curitem].start = p;
4283 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4284 s++;
4285 curitem++;
4286 continue;
4287 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004288 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4289 {
4290 if (*s == STL_TABCLOSENR)
4291 {
4292 if (minwid == 0)
4293 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004294 // %X ends the close label, go back to the previously
4295 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004296 for (n = curitem - 1; n >= 0; --n)
4297 if (item[n].type == TabPage && item[n].minwid >= 0)
4298 {
4299 minwid = item[n].minwid;
4300 break;
4301 }
4302 }
4303 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004304 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004305 minwid = - minwid;
4306 }
4307 item[curitem].type = TabPage;
4308 item[curitem].start = p;
4309 item[curitem].minwid = minwid;
4310 s++;
4311 curitem++;
4312 continue;
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (*s == '.')
4315 {
4316 s++;
4317 if (VIM_ISDIGIT(*s))
4318 {
4319 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004320 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 maxwid = 50;
4322 }
4323 }
4324 minwid = (minwid > 50 ? 50 : minwid) * l;
4325 if (*s == '(')
4326 {
4327 groupitem[groupdepth++] = curitem;
4328 item[curitem].type = Group;
4329 item[curitem].start = p;
4330 item[curitem].minwid = minwid;
4331 item[curitem].maxwid = maxwid;
4332 s++;
4333 curitem++;
4334 continue;
4335 }
4336 if (vim_strchr(STL_ALL, *s) == NULL)
4337 {
4338 s++;
4339 continue;
4340 }
4341 opt = *s++;
4342
Bram Moolenaarc667da52019-11-30 20:52:27 +01004343 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 base = 'D';
4345 itemisflag = FALSE;
4346 fillable = TRUE;
4347 num = -1;
4348 str = NULL;
4349 switch (opt)
4350 {
4351 case STL_FILEPATH:
4352 case STL_FULLPATH:
4353 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004354 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004356 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 else
4358 {
4359 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004360 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4362 }
4363 trans_characters(NameBuff, MAXPATHL);
4364 if (opt != STL_FILENAME)
4365 str = NameBuff;
4366 else
4367 str = gettail(NameBuff);
4368 break;
4369
Bram Moolenaarc667da52019-11-30 20:52:27 +01004370 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 itemisflag = TRUE;
4372 t = p;
4373 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4374 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004375 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 break;
4377 s++;
4378 *p = 0;
4379 p = t;
4380
4381#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004382 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4383 "%d", curbuf->b_fnum);
4384 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4385 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4386 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004388 save_curbuf = curbuf;
4389 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004390 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 curwin = wp;
4392 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004393 // Visual mode is only valid in the current window.
4394 if (curwin != save_curwin)
4395 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004397 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004399 curwin = save_curwin;
4400 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004401 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004402 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004403 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 if (str != NULL && *str != 0)
4406 {
4407 if (*skipdigits(str) == NUL)
4408 {
4409 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004410 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 itemisflag = FALSE;
4412 }
4413 }
4414#endif
4415 break;
4416
4417 case STL_LINE:
4418 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4419 ? 0L : (long)(wp->w_cursor.lnum);
4420 break;
4421
4422 case STL_NUMLINES:
4423 num = wp->w_buffer->b_ml.ml_line_count;
4424 break;
4425
4426 case STL_COLUMN:
4427 num = !(State & INSERT) && empty_line
4428 ? 0 : (int)wp->w_cursor.col + 1;
4429 break;
4430
4431 case STL_VIRTCOL:
4432 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004433 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 virtcol = wp->w_virtcol;
4435 if (wp->w_p_list && lcs_tab1 == NUL)
4436 {
4437 wp->w_p_list = FALSE;
4438 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4439 wp->w_p_list = TRUE;
4440 }
4441 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004442 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (opt == STL_VIRTCOL_ALT
4444 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4445 ? 0 : (int)wp->w_cursor.col + 1)))
4446 break;
4447 num = (long)virtcol;
4448 break;
4449
4450 case STL_PERCENTAGE:
4451 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4452 (long)wp->w_buffer->b_ml.ml_line_count);
4453 break;
4454
4455 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004456 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004457 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
4459
4460 case STL_ARGLISTSTAT:
4461 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004462 buf_tmp[0] = 0;
4463 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4464 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 break;
4466
4467 case STL_KEYMAP:
4468 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004469 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4470 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 break;
4472 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004473#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004474 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475#else
4476 num = 0;
4477#endif
4478 break;
4479
4480 case STL_BUFNO:
4481 num = wp->w_buffer->b_fnum;
4482 break;
4483
4484 case STL_OFFSET_X:
4485 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004486 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 case STL_OFFSET:
4488#ifdef FEAT_BYTEOFF
4489 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4490 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4491 0L : l + 1 + (!(State & INSERT) && empty_line ?
4492 0 : (int)wp->w_cursor.col);
4493#endif
4494 break;
4495
4496 case STL_BYTEVAL_X:
4497 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004498 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004500 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (num == NL)
4502 num = 0;
4503 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4504 num = NL;
4505 break;
4506
4507 case STL_ROFLAG:
4508 case STL_ROFLAG_ALT:
4509 itemisflag = TRUE;
4510 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004511 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513
4514 case STL_HELPFLAG:
4515 case STL_HELPFLAG_ALT:
4516 itemisflag = TRUE;
4517 if (wp->w_buffer->b_help)
4518 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004519 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 break;
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 case STL_FILETYPE:
4523 if (*wp->w_buffer->b_p_ft != NUL
4524 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4525 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004526 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004528 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 break;
4531
4532 case STL_FILETYPE_ALT:
4533 itemisflag = TRUE;
4534 if (*wp->w_buffer->b_p_ft != NUL
4535 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4536 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004537 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004538 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004539 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004541 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
Bram Moolenaar4033c552017-09-16 20:54:51 +02004545#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 case STL_PREVIEWFLAG:
4547 case STL_PREVIEWFLAG_ALT:
4548 itemisflag = TRUE;
4549 if (wp->w_p_pvw)
4550 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4551 : _("[Preview]"));
4552 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004553
4554 case STL_QUICKFIX:
4555 if (bt_quickfix(wp->w_buffer))
4556 str = (char_u *)(wp->w_llist_ref
4557 ? _(msg_loclist)
4558 : _(msg_qflist));
4559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#endif
4561
4562 case STL_MODIFIED:
4563 case STL_MODIFIED_ALT:
4564 itemisflag = TRUE;
4565 switch ((opt == STL_MODIFIED_ALT)
4566 + bufIsChanged(wp->w_buffer) * 2
4567 + (!wp->w_buffer->b_p_ma) * 4)
4568 {
4569 case 2: str = (char_u *)"[+]"; break;
4570 case 3: str = (char_u *)",+"; break;
4571 case 4: str = (char_u *)"[-]"; break;
4572 case 5: str = (char_u *)",-"; break;
4573 case 6: str = (char_u *)"[+-]"; break;
4574 case 7: str = (char_u *)",+-"; break;
4575 }
4576 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004577
4578 case STL_HIGHLIGHT:
4579 t = s;
4580 while (*s != '#' && *s != NUL)
4581 ++s;
4582 if (*s == '#')
4583 {
4584 item[curitem].type = Highlight;
4585 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004586 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004587 curitem++;
4588 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004589 if (*s != NUL)
4590 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004591 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
4594 item[curitem].start = p;
4595 item[curitem].type = Normal;
4596 if (str != NULL && *str)
4597 {
4598 t = str;
4599 if (itemisflag)
4600 {
4601 if ((t[0] && t[1])
4602 && ((!prevchar_isitem && *t == ',')
4603 || (prevchar_isflag && *t == ' ')))
4604 t++;
4605 prevchar_isflag = TRUE;
4606 }
4607 l = vim_strsize(t);
4608 if (l > 0)
4609 prevchar_isitem = TRUE;
4610 if (l > maxwid)
4611 {
4612 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (has_mbyte)
4614 {
4615 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004616 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 l -= byte2cells(*t++);
4620 if (p + 1 >= out + outlen)
4621 break;
4622 *p++ = '<';
4623 }
4624 if (minwid > 0)
4625 {
4626 for (; l < minwid && p + 1 < out + outlen; l++)
4627 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004628 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4630 *p++ = ' ';
4631 else
4632 *p++ = fillchar;
4633 }
4634 minwid = 0;
4635 }
4636 else
4637 minwid *= -1;
4638 while (*t && p + 1 < out + outlen)
4639 {
4640 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004641 // Change a space by fillchar, unless fillchar is '-' and a
4642 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (fillable && p[-1] == ' '
4644 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4645 p[-1] = fillchar;
4646 }
4647 for (; l < minwid && p + 1 < out + outlen; l++)
4648 *p++ = fillchar;
4649 }
4650 else if (num >= 0)
4651 {
4652 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4653 char_u nstr[20];
4654
4655 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004656 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 prevchar_isitem = TRUE;
4658 t = nstr;
4659 if (opt == STL_VIRTCOL_ALT)
4660 {
4661 *t++ = '-';
4662 minwid--;
4663 }
4664 *t++ = '%';
4665 if (zeropad)
4666 *t++ = '0';
4667 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004668 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 *t = 0;
4670
4671 for (n = num, l = 1; n >= nbase; n /= nbase)
4672 l++;
4673 if (opt == STL_VIRTCOL_ALT)
4674 l++;
4675 if (l > maxwid)
4676 {
4677 l += 2;
4678 n = l - maxwid;
4679 while (l-- > maxwid)
4680 num /= nbase;
4681 *t++ = '>';
4682 *t++ = '%';
4683 *t = t[-3];
4684 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004685 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4686 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004689 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4690 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 p += STRLEN(p);
4692 }
4693 else
4694 item[curitem].type = Empty;
4695
4696 if (opt == STL_VIM_EXPR)
4697 vim_free(str);
4698
4699 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004700 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 curitem++;
4702 }
4703 *p = NUL;
4704 itemcnt = curitem;
4705
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004706#ifdef FEAT_EVAL
4707 if (usefmt != fmt)
4708 vim_free(usefmt);
4709#endif
4710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 width = vim_strsize(out);
4712 if (maxwidth > 0 && width > maxwidth)
4713 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004714 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 l = 0;
4716 if (itemcnt == 0)
4717 s = out;
4718 else
4719 {
4720 for ( ; l < itemcnt; l++)
4721 if (item[l].type == Trunc)
4722 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004723 // Truncate at %< item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 s = item[l].start;
4725 break;
4726 }
4727 if (l == itemcnt)
4728 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004729 // No %< item, truncate first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 s = item[0].start;
4731 l = 0;
4732 }
4733 }
4734
4735 if (width - vim_strsize(s) >= maxwidth)
4736 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004737 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (has_mbyte)
4739 {
4740 s = out;
4741 width = 0;
4742 for (;;)
4743 {
4744 width += ptr2cells(s);
4745 if (width >= maxwidth)
4746 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004747 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004749 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 while (++width < maxwidth)
4751 *s++ = fillchar;
4752 }
4753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 s = out + maxwidth - 1;
4755 for (l = 0; l < itemcnt; l++)
4756 if (item[l].start > s)
4757 break;
4758 itemcnt = l;
4759 *s++ = '>';
4760 *s = 0;
4761 }
4762 else
4763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (has_mbyte)
4765 {
4766 n = 0;
4767 while (width >= maxwidth)
4768 {
4769 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004770 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 }
4773 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 n = width - maxwidth + 1;
4775 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004776 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 *s = '<';
4778
Bram Moolenaarc667da52019-11-30 20:52:27 +01004779 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 while (++width < maxwidth)
4781 {
4782 s = s + STRLEN(s);
4783 *s++ = fillchar;
4784 *s = NUL;
4785 }
4786
Bram Moolenaarc667da52019-11-30 20:52:27 +01004787 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 for (; l < itemcnt; l++)
4789 {
4790 if (item[l].start - n >= s)
4791 item[l].start -= n;
4792 else
4793 item[l].start = s;
4794 }
4795 }
4796 width = maxwidth;
4797 }
4798 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4799 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004800 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 for (l = 0; l < itemcnt; l++)
4802 if (item[l].type == Middle)
4803 break;
4804 if (l < itemcnt)
4805 {
4806 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004807 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 for (s = item[l].start; s < p; s++)
4809 *s = fillchar;
4810 for (l++; l < itemcnt; l++)
4811 item[l].start += maxwidth - width;
4812 width = maxwidth;
4813 }
4814 }
4815
Bram Moolenaarc667da52019-11-30 20:52:27 +01004816 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004817 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004819 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 for (l = 0; l < itemcnt; l++)
4821 {
4822 if (item[l].type == Highlight)
4823 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004824 sp->start = item[l].start;
4825 sp->userhl = item[l].minwid;
4826 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004829 sp->start = NULL;
4830 sp->userhl = 0;
4831 }
4832
Bram Moolenaarc667da52019-11-30 20:52:27 +01004833 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004834 if (tabtab != NULL)
4835 {
4836 sp = tabtab;
4837 for (l = 0; l < itemcnt; l++)
4838 {
4839 if (item[l].type == TabPage)
4840 {
4841 sp->start = item[l].start;
4842 sp->userhl = item[l].minwid;
4843 sp++;
4844 }
4845 }
4846 sp->start = NULL;
4847 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849
Bram Moolenaarc667da52019-11-30 20:52:27 +01004850 // When inside update_screen we do not want redrawing a stausline, ruler,
4851 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004852 if (updating_screen)
4853 {
4854 must_redraw = save_must_redraw;
4855 curwin->w_redr_type = save_redr_type;
4856 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return width;
4859}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004860#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004862#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4863 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004865 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4866 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 */
4868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869get_rel_pos(
4870 win_T *wp,
4871 char_u *buf,
4872 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004874 long above; // number of lines above window
4875 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaarc667da52019-11-30 20:52:27 +01004877 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004878 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 above = wp->w_topline - 1;
4880#ifdef FEAT_DIFF
4881 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004882 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004883 above = 0; // All buffer lines are displayed and there is an
4884 // indication of filler lines, that can be considered
4885 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#endif
4887 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4888 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004889 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4890 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004892 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004894 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 ? (int)(above / ((above + below) / 100L))
4896 : (int)(above * 100L / (above + below)));
4897}
4898#endif
4899
4900/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004901 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 * Return TRUE if it was appended.
4903 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004905append_arg_number(
4906 win_T *wp,
4907 char_u *buf,
4908 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004909 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
4911 char_u *p;
4912
Bram Moolenaarc667da52019-11-30 20:52:27 +01004913 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return FALSE;
4915
Bram Moolenaarc667da52019-11-30 20:52:27 +01004916 p = buf + STRLEN(buf); // go to the end of the buffer
4917 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 return FALSE;
4919 *p++ = ' ';
4920 *p++ = '(';
4921 if (add_file)
4922 {
4923 STRCPY(p, "file ");
4924 p += 5;
4925 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004926 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4927 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4929 return TRUE;
4930}
4931
4932/*
4933 * If fname is not a full path, make it a full path.
4934 * Returns pointer to allocated memory (NULL for failure).
4935 */
4936 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004937fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 /*
4940 * Force expanding the path always for Unix, because symbolic links may
4941 * mess up the full path name, even though it starts with a '/'.
4942 * Also expand when there is ".." in the file name, try to remove it,
4943 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004944 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * For MS-Windows also expand names like "longna~1" to "longname".
4946 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004947#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return FullName_save(fname, TRUE);
4949#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004950 if (!vim_isAbsName(fname)
4951 || strstr((char *)fname, "..") != NULL
4952 || strstr((char *)fname, "//") != NULL
4953# ifdef BACKSLASH_IN_FILENAME
4954 || strstr((char *)fname, "\\\\") != NULL
4955# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004956# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 )
4960 return FullName_save(fname, FALSE);
4961
4962 fname = vim_strsave(fname);
4963
Bram Moolenaar9b942202007-10-03 12:31:33 +00004964# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004965 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004966 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00004967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 return fname;
4970#endif
4971}
4972
4973/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004974 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4975 * "*ffname" becomes a pointer to allocated memory (or NULL).
4976 * When resolving a link both "*sfname" and "*ffname" will point to the same
4977 * allocated memory.
4978 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004979 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004982fname_expand(
4983 buf_T *buf UNUSED,
4984 char_u **ffname,
4985 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004987 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004989 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004991 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993#ifdef FEAT_SHORTCUT
4994 if (!buf->b_p_bin)
4995 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004996 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004998 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004999 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005000 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 vim_free(*ffname);
5003 *ffname = rfname;
5004 *sfname = rfname;
5005 }
5006 }
5007#endif
5008}
5009
5010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 * Open a window for a number of buffers.
5012 */
5013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005014ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 buf_T *buf;
5017 win_T *wp, *wpnext;
5018 int split_ret = OK;
5019 int p_ea_save;
5020 int open_wins = 0;
5021 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005022 int count; // Maximum number of windows to open.
5023 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005024 int had_tab = cmdmod.tab;
5025 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaarc667da52019-11-30 20:52:27 +01005027 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 count = 9999;
5029 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005030 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5032 all = FALSE;
5033 else
5034 all = TRUE;
5035
5036 setpcmark();
5037
5038#ifdef FEAT_GUI
5039 need_mouse_correct = TRUE;
5040#endif
5041
5042 /*
5043 * Close superfluous windows (two windows for the same buffer).
5044 * Also close windows that are not full-width.
5045 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005046 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005047 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005048 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005050 tpnext = curtab->tp_next;
5051 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005053 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005054 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005055 || ((cmdmod.split & WSP_VERT)
5056 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005057 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005058 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005059 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005060 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005061 {
5062 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005063 wpnext = firstwin; // just in case an autocommand does
5064 // something strange with windows
5065 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005066 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005067 }
5068 else
5069 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005071
Bram Moolenaarc667da52019-11-30 20:52:27 +01005072 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005073 if (had_tab == 0 || tpnext == NULL)
5074 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005075 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077
5078 /*
5079 * Go through the buffer list. When a buffer doesn't have a window yet,
5080 * open one. Otherwise move the window to the right position.
5081 * Watch out for autocommands that delete buffers or windows!
5082 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005083 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5088 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005089 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5091 continue;
5092
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005093 if (had_tab != 0)
5094 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005095 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005096 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005097 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005098 else
5099 wp = NULL;
5100 }
5101 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005102 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005103 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005104 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005105 if (wp->w_buffer == buf)
5106 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005107 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005108 if (wp != NULL)
5109 win_move_after(wp, curwin);
5110 }
5111
5112 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005114 bufref_T bufref;
5115
5116 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005117
Bram Moolenaarc667da52019-11-30 20:52:27 +01005118 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005120 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5122 ++open_wins;
5123 p_ea = p_ea_save;
5124 if (split_ret == FAIL)
5125 continue;
5126
Bram Moolenaarc667da52019-11-30 20:52:27 +01005127 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005130 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005132 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 break;
5135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (swap_exists_action == SEA_QUIT)
5137 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005138#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005139 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005140
Bram Moolenaarc667da52019-11-30 20:52:27 +01005141 // Reset the error/interrupt/exception state here so that
5142 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005143 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005144#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005145
Bram Moolenaarc667da52019-11-30 20:52:27 +01005146 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 win_close(curwin, TRUE);
5148 --open_wins;
5149 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005150 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005151
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005152#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005153 // Restore the error/interrupt/exception state if not
5154 // discarded by a new aborting error, interrupt, or uncaught
5155 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005156 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 else
5160 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162
5163 ui_breakcheck();
5164 if (got_int)
5165 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005166 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005169#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005170 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005171 if (aborting())
5172 break;
5173#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005174 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5176 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005179 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
5182 /*
5183 * Close superfluous windows.
5184 */
5185 for (wp = lastwin; open_wins > count; )
5186 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005187 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (!win_valid(wp))
5190 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005191 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 wp = lastwin;
5193 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005194 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005196 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 --open_wins;
5198 wp = lastwin;
5199 }
5200 else
5201 {
5202 wp = wp->w_prev;
5203 if (wp == NULL)
5204 break;
5205 }
5206 }
5207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005210static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212/*
5213 * do_modelines() - process mode lines for the current file
5214 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005215 * "flags" can be:
5216 * OPT_WINONLY only set options local to window
5217 * OPT_NOWIN don't set options local to window
5218 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 * Returns immediately if the "ml" option isn't set.
5220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005222do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005224 linenr_T lnum;
5225 int nmlines;
5226 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
5228 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5229 return;
5230
Bram Moolenaarc667da52019-11-30 20:52:27 +01005231 // Disallow recursive entry here. Can happen when executing a modeline
5232 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (entered)
5234 return;
5235
5236 ++entered;
5237 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5238 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005239 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 nmlines = 0;
5241
5242 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5243 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005244 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 nmlines = 0;
5246 --entered;
5247}
5248
Bram Moolenaarc667da52019-11-30 20:52:27 +01005249#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251/*
5252 * chk_modeline() - check a single line for a mode string
5253 * Return FAIL if an error encountered.
5254 */
5255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005256chk_modeline(
5257 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005258 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 char_u *s;
5261 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005262 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 int prev;
5264 int vers;
5265 int end;
5266 int retval = OK;
5267 char_u *save_sourcing_name;
5268 linenr_T save_sourcing_lnum;
5269#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005270 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#endif
5272
5273 prev = -1;
5274 for (s = ml_get(lnum); *s != NUL; ++s)
5275 {
5276 if (prev == -1 || vim_isspace(prev))
5277 {
5278 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5279 || STRNCMP(s, "vi:", (size_t)3) == 0)
5280 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005281 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005282 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
5284 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5285 e = s + 4;
5286 else
5287 e = s + 3;
5288 vers = getdigits(&e);
5289 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005290 && (s[0] != 'V'
5291 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 && (s[3] == ':'
5293 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5294 || (VIM_VERSION_100 < vers && s[3] == '<')
5295 || (VIM_VERSION_100 > vers && s[3] == '>')
5296 || (VIM_VERSION_100 == vers && s[3] == '=')))
5297 break;
5298 }
5299 }
5300 prev = *s;
5301 }
5302
5303 if (*s)
5304 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005305 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ++s;
5307 while (s[-1] != ':');
5308
Bram Moolenaarc667da52019-11-30 20:52:27 +01005309 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (linecopy == NULL)
5311 return FAIL;
5312
5313 save_sourcing_lnum = sourcing_lnum;
5314 save_sourcing_name = sourcing_name;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005315 sourcing_lnum = lnum; // prepare for emsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 sourcing_name = (char_u *)"modelines";
5317
5318 end = FALSE;
5319 while (end == FALSE)
5320 {
5321 s = skipwhite(s);
5322 if (*s == NUL)
5323 break;
5324
5325 /*
5326 * Find end of set command: ':' or end of line.
5327 * Skip over "\:", replacing it with ":".
5328 */
5329 for (e = s; *e != ':' && *e != NUL; ++e)
5330 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005331 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (*e == NUL)
5333 end = TRUE;
5334
5335 /*
5336 * If there is a "set" command, require a terminating ':' and
5337 * ignore the stuff after the ':'.
5338 * "vi:set opt opt opt: foo" -- foo not interpreted
5339 * "vi:opt opt opt: foo" -- foo interpreted
5340 * Accept "se" for compatibility with Elvis.
5341 */
5342 if (STRNCMP(s, "set ", (size_t)4) == 0
5343 || STRNCMP(s, "se ", (size_t)3) == 0)
5344 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005345 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 break;
5347 end = TRUE;
5348 s = vim_strchr(s, ' ') + 1;
5349 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005350 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaarc667da52019-11-30 20:52:27 +01005352 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005354 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005356 save_current_sctx = current_sctx;
5357 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005358 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005359 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005360 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005362 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005363 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005364
Bram Moolenaara3227e22006-03-08 21:32:40 +00005365 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005366
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005367 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005369 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005371 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 break;
5373 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005374 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376
5377 sourcing_lnum = save_sourcing_lnum;
5378 sourcing_name = save_sourcing_name;
5379
5380 vim_free(linecopy);
5381 }
5382 return retval;
5383}
5384
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005385/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005386 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5387 */
5388 int
5389bt_normal(buf_T *buf)
5390{
5391 return buf != NULL && buf->b_p_bt[0] == NUL;
5392}
5393
Bram Moolenaar113e1072019-01-20 15:30:40 +01005394#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005395/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005396 * Return TRUE if "buf" is the quickfix buffer.
5397 */
5398 int
5399bt_quickfix(buf_T *buf)
5400{
5401 return buf != NULL && buf->b_p_bt[0] == 'q';
5402}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005403#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005404
Bram Moolenaar113e1072019-01-20 15:30:40 +01005405#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005406/*
5407 * Return TRUE if "buf" is a terminal buffer.
5408 */
5409 int
5410bt_terminal(buf_T *buf)
5411{
5412 return buf != NULL && buf->b_p_bt[0] == 't';
5413}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005414#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005415
5416/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005417 * Return TRUE if "buf" is a help buffer.
5418 */
5419 int
5420bt_help(buf_T *buf)
5421{
5422 return buf != NULL && buf->b_help;
5423}
5424
5425/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005426 * Return TRUE if "buf" is a prompt buffer.
5427 */
5428 int
5429bt_prompt(buf_T *buf)
5430{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005431 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5432}
5433
5434/*
5435 * Return TRUE if "buf" is a buffer for a popup window.
5436 */
5437 int
5438bt_popup(buf_T *buf)
5439{
5440 return buf != NULL && buf->b_p_bt != NULL
5441 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005442}
5443
5444/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005445 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5446 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005447 */
5448 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005449bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005450{
5451 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5452 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005453 || buf->b_p_bt[0] == 't'
5454 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005455}
5456
5457/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005458 * Return TRUE if "buf" has 'buftype' set to "nofile".
5459 */
5460 int
5461bt_nofile(buf_T *buf)
5462{
5463 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5464}
5465
5466/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005467 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5468 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005469 */
5470 int
5471bt_dontwrite(buf_T *buf)
5472{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005473 return buf != NULL && (buf->b_p_bt[0] == 'n'
5474 || buf->b_p_bt[0] == 't'
5475 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005476}
5477
Bram Moolenaar113e1072019-01-20 15:30:40 +01005478#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005479 int
5480bt_dontwrite_msg(buf_T *buf)
5481{
5482 if (bt_dontwrite(buf))
5483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005484 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005485 return TRUE;
5486 }
5487 return FALSE;
5488}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005489#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005490
5491/*
5492 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5493 * and 'bufhidden'.
5494 */
5495 int
5496buf_hide(buf_T *buf)
5497{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005498 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005499 switch (buf->b_p_bh[0])
5500 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005501 case 'u': // "unload"
5502 case 'w': // "wipe"
5503 case 'd': return FALSE; // "delete"
5504 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005505 }
5506 return (p_hid || cmdmod.hide);
5507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509/*
5510 * Return special buffer name.
5511 * Returns NULL when the buffer has a normal file name.
5512 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005513 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005514buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005516#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005518 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005519 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005520 * Differentiate between the quickfix and location list buffers using
5521 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005522 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005523 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005524 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005525 else
5526 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005529
Bram Moolenaarc667da52019-11-30 20:52:27 +01005530 // There is no _file_ when 'buftype' is "nofile", b_sfname
5531 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005532 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005534#ifdef FEAT_TERMINAL
5535 if (buf->b_term != NULL)
5536 return term_get_status_text(buf->b_term);
5537#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005538 if (buf->b_fname != NULL)
5539 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005540#ifdef FEAT_JOB_CHANNEL
5541 if (bt_prompt(buf))
5542 return (char_u *)_("[Prompt]");
5543#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005544#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005545 if (bt_popup(buf))
5546 return (char_u *)_("[Popup]");
5547#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005548 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005552 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 return NULL;
5554}
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556/*
5557 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5558 */
5559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005560set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 if (on != curbuf->b_p_bl)
5563 {
5564 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (on)
5566 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5567 else
5568 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570}
5571
5572/*
5573 * Read the file for "buf" again and check if the contents changed.
5574 * Return TRUE if it changed or this could not be checked.
5575 */
5576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 buf_T *newbuf;
5580 int differ = TRUE;
5581 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 exarg_T ea;
5584
Bram Moolenaarc667da52019-11-30 20:52:27 +01005585 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5587 if (newbuf == NULL)
5588 return TRUE;
5589
Bram Moolenaarc667da52019-11-30 20:52:27 +01005590 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (prep_exarg(&ea, buf) == FAIL)
5592 {
5593 wipe_buffer(newbuf, FALSE);
5594 return TRUE;
5595 }
5596
Bram Moolenaarc667da52019-11-30 20:52:27 +01005597 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar4770d092006-01-12 23:22:24 +00005600 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 && readfile(buf->b_ffname, buf->b_fname,
5602 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5603 &ea, READ_NEW | READ_DUMMY) == OK)
5604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005605 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5607 {
5608 differ = FALSE;
5609 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5610 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5611 {
5612 differ = TRUE;
5613 break;
5614 }
5615 }
5616 }
5617 vim_free(ea.cmd);
5618
Bram Moolenaarc667da52019-11-30 20:52:27 +01005619 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaarc667da52019-11-30 20:52:27 +01005622 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 wipe_buffer(newbuf, FALSE);
5624
5625 return differ;
5626}
5627
5628/*
5629 * Wipe out a buffer and decrement the last buffer number if it was used for
5630 * this buffer. Call this to wipe out a temp buffer that does not contain any
5631 * marks.
5632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005634wipe_buffer(
5635 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005636 int aucmd UNUSED) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 if (buf->b_fnum == top_file_num - 1)
5639 --top_file_num;
5640
Bram Moolenaarc667da52019-11-30 20:52:27 +01005641 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005642 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005643
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005644 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005647 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648}