blob: dd3593fb52430c5bbbc4d93efaee5139cf6e11da [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void enter_buffer(buf_T *buf);
31static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020035static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
36static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
37static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
41#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020042static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
45static void free_buffer(buf_T *);
46static void free_buffer_stuff(buf_T *buf, int free_options);
47static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49#ifdef UNIX
50# define dev_T dev_t
51#else
52# define dev_T unsigned
53#endif
54
Bram Moolenaar4033c552017-09-16 20:54:51 +020055#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020056static char *msg_loclist = N_("[Location List]");
57static char *msg_qflist = N_("[Quickfix List]");
58#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010059static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020061// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020062static int buf_free_count = 0;
63
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020064static int top_file_num = 1; // highest file number
65static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
66
67/*
68 * Return the highest possible buffer number.
69 */
70 int
71get_highest_fnum(void)
72{
73 return top_file_num - 1;
74}
75
Bram Moolenaarc024b462019-06-08 18:07:21 +020076/*
77 * Read data from buffer for retrying.
78 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020079 static int
80read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010081 int read_stdin, // read file from stdin, otherwise fifo
82 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
83 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020084{
85 int retval = OK;
86 linenr_T line_count;
87
88 /*
89 * Read from the buffer which the text is already filled in and append at
90 * the end. This makes it possible to retry when 'fileformat' or
91 * 'fileencoding' was guessed wrong.
92 */
93 line_count = curbuf->b_ml.ml_line_count;
94 retval = readfile(
95 read_stdin ? NULL : curbuf->b_ffname,
96 read_stdin ? NULL : curbuf->b_fname,
97 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
98 flags | READ_BUFFER);
99 if (retval == OK)
100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100101 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200102 while (--line_count >= 0)
103 ml_delete((linenr_T)1, FALSE);
104 }
105 else
106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100107 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200108 while (curbuf->b_ml.ml_line_count > line_count)
109 ml_delete(line_count, FALSE);
110 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100111 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200112 curwin->w_cursor.lnum = 1;
113 curwin->w_cursor.col = 0;
114
115 if (read_stdin)
116 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100117 // Set or reset 'modified' before executing autocommands, so that
118 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100119 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200120 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100121 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200122 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200123
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100124 if (retval == OK)
125 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100126#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100127 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100128 curbuf, &retval);
129#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100130 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200131#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200133 }
134 return retval;
135}
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200138 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
139 */
140 void
141buffer_ensure_loaded(buf_T *buf)
142{
143 if (buf->b_ml.ml_mfp == NULL)
144 {
145 aco_save_T aco;
146
147 aucmd_prepbuf(&aco, buf);
148 swap_exists_action = SEA_NONE;
149 open_buffer(FALSE, NULL, 0);
150 aucmd_restbuf(&aco);
151 }
152}
153
154/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200155 * Open current buffer, that is: open the memfile and read the file into
156 * memory.
157 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 */
159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100160open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100161 int read_stdin, // read file from stdin
162 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
163 int flags) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200166 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100167#ifdef FEAT_SYN_HL
168 long old_tw = curbuf->b_p_tw;
169#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200170 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172 /*
173 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
174 * When re-entering the same buffer, it should not change, because the
175 * user may have reset the flag by hand.
176 */
177 if (readonlymode && curbuf->b_ffname != NULL
178 && (curbuf->b_flags & BF_NEVERLOADED))
179 curbuf->b_p_ro = TRUE;
180
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 {
183 /*
184 * There MUST be a memfile, otherwise we can't do anything
185 * If we can't create one for the current buffer, take another buffer
186 */
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100187 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200188 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (curbuf->b_ml.ml_mfp != NULL)
190 break;
191 /*
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200192 * If there is no memfile at all, exit.
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000193 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 */
195 if (curbuf == NULL)
196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100197 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200198
199 // Don't try to do any saving, with "curbuf" NULL almost nothing
200 // will work.
201 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 getout(2);
203 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200204
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100205 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100207#ifdef FEAT_SYN_HL
208 if (old_tw != curbuf->b_p_tw)
209 check_colorcolumn(curwin);
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 return FAIL;
212 }
213
Bram Moolenaarc667da52019-11-30 20:52:27 +0100214 // The autocommands in readfile() may change the buffer, but only AFTER
215 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200216 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaarc667da52019-11-30 20:52:27 +0100219 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100220 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
222 if (curbuf->b_ffname != NULL
223#ifdef FEAT_NETBEANS_INTG
224 && netbeansReadFile
225#endif
226 )
227 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100228 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200229#ifdef UNIX
230 int save_bin = curbuf->b_p_bin;
231 int perm;
232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_NETBEANS_INTG
234 int oldFire = netbeansFireChanges;
235
236 netbeansFireChanges = 0;
237#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238#ifdef UNIX
239 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200240 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200241 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200242# ifdef OPEN_CHR_FILES
243 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
244# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200245 ))
246 read_fifo = TRUE;
247 if (read_fifo)
248 curbuf->b_p_bin = TRUE;
249#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100250 if (shortmess(SHM_FILEINFO))
251 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200253 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
255#ifdef UNIX
256 if (read_fifo)
257 {
258 curbuf->b_p_bin = save_bin;
259 if (retval == OK)
260 retval = read_buffer(FALSE, eap, flags);
261 }
262#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100263 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_NETBEANS_INTG
265 netbeansFireChanges = oldFire;
266#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100267 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200268 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 fix_help_buffer();
270 }
271 else if (read_stdin)
272 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200273 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 /*
276 * First read the text in binary mode into the buffer.
277 * Then read from that same buffer and append at the end. This makes
278 * it possible to retry when 'fileformat' or 'fileencoding' was
279 * guessed wrong.
280 */
281 curbuf->b_p_bin = TRUE;
282 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200283 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
284 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 curbuf->b_p_bin = save_bin;
286 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200287 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289
Bram Moolenaarc667da52019-11-30 20:52:27 +0100290 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100294#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100295 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100296#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
299 /*
300 * Set/reset the Changed flag first, autocmds may change the buffer.
301 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200302 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100304 // When reading stdin, the buffer contents always needs writing, so set
305 // the changed flag. Unless in readonly mode: "ls | gview -".
306 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000307 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100308 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100309#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000312 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100314 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200315 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100316 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
Bram Moolenaarc667da52019-11-30 20:52:27 +0100318 // Set last_changedtick to avoid triggering a TextChanged autocommand right
319 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100320 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100321 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100322
Bram Moolenaarc667da52019-11-30 20:52:27 +0100323 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_EVAL
325 if (aborting())
326#else
327 if (got_int)
328#endif
329 curbuf->b_flags |= BF_READERR;
330
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000331#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100332 // Need to update automatic folding. Do this before the autocommands,
333 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000334 foldUpdateAll(curwin);
335#endif
336
Bram Moolenaarc667da52019-11-30 20:52:27 +0100337 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 if (!(curwin->w_valid & VALID_TOPLINE))
339 {
340 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100341#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100347#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#endif
350
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100351 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * The autocommands may have changed the current buffer. Apply the
355 * modelines to the correct buffer, if it still exists and is loaded.
356 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200357 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
359 aco_save_T aco;
360
Bram Moolenaarc667da52019-11-30 20:52:27 +0100361 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200362 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000363 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
365
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100366#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100368 &retval);
369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaarc667da52019-11-30 20:52:27 +0100373 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 aucmd_restbuf(&aco);
375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 }
377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 return retval;
379}
380
381/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200382 * Store "buf" in "bufref" and set the free count.
383 */
384 void
385set_bufref(bufref_T *bufref, buf_T *buf)
386{
387 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200388 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200389 bufref->br_buf_free_count = buf_free_count;
390}
391
392/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200393 * Return TRUE if "bufref->br_buf" points to the same buffer as when
394 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200395 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200396 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
397 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200398 */
399 int
400bufref_valid(bufref_T *bufref)
401{
402 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200403 ? TRUE : buf_valid(bufref->br_buf)
404 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200405}
406
407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200409 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 */
411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100412buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
414 buf_T *bp;
415
Bram Moolenaarc667da52019-11-30 20:52:27 +0100416 // Assume that we more often have a recent buffer, start with the last
417 // one.
Bram Moolenaar82404332016-07-10 17:00:38 +0200418 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 if (bp == buf)
420 return TRUE;
421 return FALSE;
422}
423
424/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200425 * A hash table used to quickly lookup a buffer by its number.
426 */
427static hashtab_T buf_hashtab;
428
429 static void
430buf_hashtab_add(buf_T *buf)
431{
432 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
433 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100434 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200435}
436
437 static void
438buf_hashtab_remove(buf_T *buf)
439{
440 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
441
442 if (!HASHITEM_EMPTY(hi))
443 hash_remove(&buf_hashtab, hi);
444}
445
Bram Moolenaar94f01952018-09-01 15:30:03 +0200446/*
447 * Return TRUE when buffer "buf" can be unloaded.
448 * Give an error message and return FALSE when the buffer is locked or the
449 * screen is being redrawn and the buffer is in a window.
450 */
451 static int
452can_unload_buffer(buf_T *buf)
453{
454 int can_unload = !buf->b_locked;
455
456 if (can_unload && updating_screen)
457 {
458 win_T *wp;
459
460 FOR_ALL_WINDOWS(wp)
461 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200462 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200463 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200464 break;
465 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200466 }
467 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200468 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
469 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200470 return can_unload;
471}
Bram Moolenaara997b452018-04-17 23:24:06 +0200472
Bram Moolenaar480778b2016-07-14 22:09:39 +0200473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Close the link to a buffer.
475 * "action" is used when there is no longer a window for the buffer.
476 * It can be:
477 * 0 buffer becomes hidden
478 * DOBUF_UNLOAD buffer is unloaded
479 * DOBUF_DELETE buffer is unloaded and removed from buffer list
480 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200481 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * When doing all but the first one on the current buffer, the caller should
483 * get a new buffer very soon!
484 *
485 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100486 *
487 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
488 * cause there to be only one window with this buffer. e.g. when ":quit" is
489 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100490 *
491 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 */
493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100494close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100495 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100496 buf_T *buf,
497 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100498 int abort_if_last,
499 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100502 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200503 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100504 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200505 win_T *the_curwin = curwin;
506 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200508 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
509 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200511 /*
512 * Force unloading or deleting when 'bufhidden' says so.
513 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
514 * "hide" (otherwise we could never free or delete a buffer).
515 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100516 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200517 {
518 del_buf = TRUE;
519 unload_buf = TRUE;
520 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100521 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200522 {
523 del_buf = TRUE;
524 unload_buf = TRUE;
525 wipe_buf = TRUE;
526 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100527 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200528 unload_buf = TRUE;
529
Bram Moolenaar94053a52017-08-01 21:44:33 +0200530#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200531 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200532 {
533 if (term_job_running(buf->b_term))
534 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200535 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200536 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200537 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200538 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200539
Bram Moolenaarc667da52019-11-30 20:52:27 +0100540 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200541 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200542 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 else
544 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100545 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200546 del_buf = FALSE;
547 unload_buf = FALSE;
548 }
549 }
550 else
551 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100552 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200553 del_buf = TRUE;
554 unload_buf = TRUE;
555 wipe_buf = TRUE;
556 }
557 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaarc667da52019-11-30 20:52:27 +0100560 // Disallow deleting the buffer when it is locked (already being closed or
561 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200562 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200563 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200564
Bram Moolenaarc667da52019-11-30 20:52:27 +0100565 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200566 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100568 // Set b_last_cursor when closing the last window for the buffer.
569 // Remember the last cursor position and window options of the buffer.
570 // This used to be only for the current window, but then options like
571 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (buf->b_nwindows == 1)
573 set_last_cursor(win);
574 buflist_setfpos(buf, win,
575 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
576 win->w_cursor.col, TRUE);
577 }
578
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200579 set_bufref(&bufref, buf);
580
Bram Moolenaarc667da52019-11-30 20:52:27 +0100581 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 if (buf->b_nwindows == 1)
583 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200584 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200585 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
586 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200587 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100588 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100589 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200590aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100591 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100593 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200594 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200595 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100596 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200597 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaarc667da52019-11-30 20:52:27 +0100599 // When the buffer becomes hidden, but is not unloaded, trigger
600 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (!unload_buf)
602 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200603 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200604 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
605 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200606 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200608 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200609 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200610 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100611 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200612 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100614#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100615 // autocmds may abort script processing
616 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200620
Bram Moolenaarc667da52019-11-30 20:52:27 +0100621 // If the buffer was in curwin and the window has changed, go back to that
622 // window, if it still exists. This avoids that ":edit x" triggering a
623 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200624 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
625 {
626 block_autocmds();
627 goto_tabpage_win(the_curtab, the_curwin);
628 unblock_autocmds();
629 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaarc667da52019-11-30 20:52:27 +0100633 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (buf->b_nwindows > 0)
635 --buf->b_nwindows;
636
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100637#ifdef FEAT_DIFF
638 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100639 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100640#endif
641
Bram Moolenaarc667da52019-11-30 20:52:27 +0100642 // Return when a window is displaying the buffer or when it's not
643 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaarc667da52019-11-30 20:52:27 +0100647 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (buf->b_ffname == NULL)
649 del_buf = TRUE;
650
Bram Moolenaarc667da52019-11-30 20:52:27 +0100651 // When closing the current buffer stop Visual mode before freeing
652 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200653 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200654#if defined(EXITFREE)
655 && !entered_free_all_mem
656#endif
657 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200658 end_visual_mode();
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 /*
661 * Free all things allocated for this buffer.
662 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
663 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100664 // Remember if we are closing the current buffer. Restore the number of
665 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 is_curbuf = (buf == curbuf);
667 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100669 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
670 + (wipe_buf ? BFA_WIPE : 0)
671 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
Bram Moolenaarc667da52019-11-30 20:52:27 +0100673 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200674 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100676#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100677 // autocmds may abort script processing
678 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 /*
683 * It's possible that autocommands change curbuf to the one being deleted.
684 * This might cause the previous curbuf to be deleted unexpectedly. But
685 * in some cases it's OK to delete the curbuf, because a new one is
686 * obtained anyway. Therefore only return if curbuf changed to the
687 * deleted buffer.
688 */
689 if (buf == curbuf && !is_curbuf)
690 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200691
Bram Moolenaar4033c552017-09-16 20:54:51 +0200692 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200694
Bram Moolenaarc667da52019-11-30 20:52:27 +0100695 // Autocommands may have opened or closed windows for this buffer.
696 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200697 if (buf->b_nwindows > 0)
698 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 /*
701 * Remove the buffer from the list.
702 */
703 if (wipe_buf)
704 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200705 if (action == DOBUF_WIPE_REUSE)
706 {
707 // we can re-use this buffer number, store it
708 if (buf_reuse.ga_itemsize == 0)
709 ga_init2(&buf_reuse, sizeof(int), 50);
710 if (ga_grow(&buf_reuse, 1) == OK)
711 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
712 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200713 if (buf->b_sfname != buf->b_ffname)
714 VIM_CLEAR(buf->b_sfname);
715 else
716 buf->b_sfname = NULL;
717 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (buf->b_prev == NULL)
719 firstbuf = buf->b_next;
720 else
721 buf->b_prev->b_next = buf->b_next;
722 if (buf->b_next == NULL)
723 lastbuf = buf->b_prev;
724 else
725 buf->b_next->b_prev = buf->b_prev;
726 free_buffer(buf);
727 }
728 else
729 {
730 if (del_buf)
731 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100732 // Free all internal variables and reset option values, to make
733 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 free_buffer_stuff(buf, TRUE);
735
Bram Moolenaarc667da52019-11-30 20:52:27 +0100736 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
738
Bram Moolenaarc667da52019-11-30 20:52:27 +0100739 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 buf->b_p_initialized = FALSE;
741 }
742 buf_clear_file(buf);
743 if (del_buf)
744 buf->b_p_bl = FALSE;
745 }
746}
747
748/*
749 * Make buffer not contain a file.
750 */
751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100752buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200755 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 buf->b_p_eol = TRUE;
758 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000760 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100762 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#ifdef FEAT_NETBEANS_INTG
764 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#endif
766}
767
768/*
769 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200770 * the file. Careful: get here with "curwin" NULL when exiting.
771 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100772 * BFA_DEL buffer is going to be deleted
773 * BFA_WIPE buffer is going to be wiped out
774 * BFA_KEEP_UNDO do not free undo information
775 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100778buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200781 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200782 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200783 win_T *the_curwin = curwin;
784 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaarc667da52019-11-30 20:52:27 +0100786 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200787 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200788 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200789 if (buf->b_ml.ml_mfp != NULL)
790 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200791 if (apply_autocmds(EVENT_BUFUNLOAD, 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 Moolenaarc67e8922016-05-24 16:07:40 +0200795 return;
796 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200797 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200799 if (apply_autocmds(EVENT_BUFDELETE, 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 Moolenaar59f931e2010-07-24 20:27:03 +0200805 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200807 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
808 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200809 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100810 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 return;
812 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200813 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200814
Bram Moolenaarc667da52019-11-30 20:52:27 +0100815 // If the buffer was in curwin and the window has changed, go back to that
816 // window, if it still exists. This avoids that ":edit x" triggering a
817 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200818 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
819 {
820 block_autocmds();
821 goto_tabpage_win(the_curtab, the_curwin);
822 unblock_autocmds();
823 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200824
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100825#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100826 // autocmds may abort script processing
827 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /*
832 * It's possible that autocommands change curbuf to the one being deleted.
833 * This might cause curbuf to be deleted unexpectedly. But in some cases
834 * it's OK to delete the curbuf, because a new one is obtained anyway.
835 * Therefore only return if curbuf changed to the deleted buffer.
836 */
837 if (buf == curbuf && !is_curbuf)
838 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100840 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200842#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100843 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200844 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100845 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200846#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000847
848#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100849 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000850 {
851 win_T *win;
852 tabpage_T *tp;
853
854 FOR_ALL_TAB_WINDOWS(tp, win)
855 if (win->w_buffer == buf)
856 clearFolding(win);
857 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000858#endif
859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#ifdef FEAT_TCL
861 tcl_buffer_free(buf);
862#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100863 ml_close(buf, TRUE); // close and delete the memline/memfile
864 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200865 if ((flags & BFA_KEEP_UNDO) == 0)
866 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100867 u_blockfree(buf); // free the memory allocated for undo
868 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100871 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100873#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100874 clear_buf_prop_types(buf);
875#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100876 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877}
878
879/*
880 * Free a buffer structure and the things it contains related to the buffer
881 * itself (not the file, that must have been done already).
882 */
883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100884free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200886 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200888#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100889 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200890 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200891 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200892 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200893#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200894#ifdef FEAT_LUA
895 lua_buffer_free(buf);
896#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000897#ifdef FEAT_MZSCHEME
898 mzscheme_buffer_free(buf);
899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#ifdef FEAT_PERL
901 perl_buf_free(buf);
902#endif
903#ifdef FEAT_PYTHON
904 python_buffer_free(buf);
905#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200906#ifdef FEAT_PYTHON3
907 python3_buffer_free(buf);
908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#ifdef FEAT_RUBY
910 ruby_buffer_free(buf);
911#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200912#ifdef FEAT_JOB_CHANNEL
913 channel_buffer_free(buf);
914#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200915#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200916 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200917#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200918#ifdef FEAT_JOB_CHANNEL
919 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200920 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200921 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200922#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200923
924 buf_hashtab_remove(buf);
925
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200926 aubuflocal_remove(buf);
927
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200928 if (autocmd_busy)
929 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100930 // Do not free the buffer structure while autocommands are executing,
931 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200932 buf->b_next = au_pending_free_buf;
933 au_pending_free_buf = buf;
934 }
935 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200936 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100940 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100941 */
942 static void
943init_changedtick(buf_T *buf)
944{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100945 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100946
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100947 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
948 di->di_tv.v_type = VAR_NUMBER;
949 di->di_tv.v_lock = VAR_FIXED;
950 di->di_tv.vval.v_number = 0;
951
Bram Moolenaar92769c32017-02-25 15:41:37 +0100952#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100953 STRCPY(buf->b_ct_di.di_key, "changedtick");
954 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100955#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100956}
957
958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
960 */
961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100962free_buffer_stuff(
963 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100964 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 if (free_options)
967 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100968 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200970#ifdef FEAT_SPELL
971 ga_clear(&buf->b_s.b_langp);
972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100975 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100976 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100977
Bram Moolenaarc667da52019-11-30 20:52:27 +0100978 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100979 hash_init(&buf->b_vars->dv_hashtab);
980 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100981 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200984 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200986 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000988#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200989 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000990#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100991 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
992 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +0100993 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
995
996/*
997 * Free the b_wininfo list for buffer "buf".
998 */
999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001000clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
1002 wininfo_T *wip;
1003
1004 while (buf->b_wininfo != NULL)
1005 {
1006 wip = buf->b_wininfo;
1007 buf->b_wininfo = wip->wi_next;
1008 if (wip->wi_optset)
1009 {
1010 clear_winopt(&wip->wi_opt);
1011#ifdef FEAT_FOLDING
1012 deleteFoldRecurse(&wip->wi_folds);
1013#endif
1014 }
1015 vim_free(wip);
1016 }
1017}
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/*
1020 * Go to another buffer. Handles the result of the ATTENTION dialog.
1021 */
1022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001023goto_buffer(
1024 exarg_T *eap,
1025 int start,
1026 int dir,
1027 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029 bufref_T old_curbuf;
1030
1031 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1035 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1037 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001038#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001039 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001040
Bram Moolenaarc667da52019-11-30 20:52:27 +01001041 // Reset the error/interrupt/exception state here so that
1042 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001043 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001044#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001045
Bram Moolenaarc667da52019-11-30 20:52:27 +01001046 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 win_close(curwin, TRUE);
1048 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001049 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001050
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001051#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001052 // Restore the error/interrupt/exception state if not discarded by a
1053 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001054 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001058 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061/*
1062 * Handle the situation of swap_exists_action being set.
1063 * It is allowed for "old_curbuf" to be NULL or invalid.
1064 */
1065 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001066handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001068#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001070#endif
1071#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001072 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001073#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (swap_exists_action == SEA_QUIT)
1077 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001078#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001079 // Reset the error/interrupt/exception state here so that
1080 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001081 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001082#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001083
Bram Moolenaarc667da52019-11-30 20:52:27 +01001084 // User selected Quit at ATTENTION prompt. Go back to previous
1085 // buffer. If that buffer is gone or the same as the current one,
1086 // open a new, empty buffer.
1087 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001088 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001089 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001090 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1091 || old_curbuf->br_buf == curbuf)
1092 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1093 else
1094 buf = old_curbuf->br_buf;
1095 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001096 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001097 int old_msg_silent = msg_silent;
1098
1099 if (shortmess(SHM_FILEINFO))
1100 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001102 // restore msg_silent, so that the command line will be shown
1103 msg_silent = old_msg_silent;
1104
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001105#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001106 if (old_tw != curbuf->b_p_tw)
1107 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001108#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001109 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001110 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001111
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001112#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001113 // Restore the error/interrupt/exception state if not discarded by a
1114 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001115 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 else if (swap_exists_action == SEA_RECOVER)
1119 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001120#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001121 // Reset the error/interrupt/exception state here so that
1122 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001123 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001124#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001125
Bram Moolenaarc667da52019-11-30 20:52:27 +01001126 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001128 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001129 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001131 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001132
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001133#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001134 // Restore the error/interrupt/exception state if not discarded by a
1135 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001136 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 swap_exists_action = SEA_NONE;
1140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142/*
1143 * do_bufdel() - delete or unload buffer(s)
1144 *
1145 * addr_count == 0: ":bdel" - delete current buffer
1146 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1147 * buffer "end_bnr", then any other arguments.
1148 * addr_count == 2: ":N,N bdel" - delete buffers in range
1149 *
1150 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1151 * DOBUF_DEL (":bdel")
1152 *
1153 * Returns error message or NULL
1154 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001155 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001156do_bufdel(
1157 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001158 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001159 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001160 int start_bnr, // first buffer number in a range
1161 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001162 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001164 int do_current = 0; // delete current buffer?
1165 int deleted = 0; // number of buffers deleted
1166 char *errormsg = NULL; // return value
1167 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 char_u *p;
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (addr_count == 0)
1171 {
1172 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1173 }
1174 else
1175 {
1176 if (addr_count == 2)
1177 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001178 if (*arg) // both range and argument is not allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001179 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 bnr = start_bnr;
1181 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001182 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 bnr = end_bnr;
1184
1185 for ( ;!got_int; ui_breakcheck())
1186 {
1187 /*
1188 * delete the current buffer last, otherwise when the
1189 * current buffer is deleted, the next buffer becomes
1190 * the current one and will be loaded, which may then
1191 * also be deleted, etc.
1192 */
1193 if (bnr == curbuf->b_fnum)
1194 do_current = bnr;
1195 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1196 forceit) == OK)
1197 ++deleted;
1198
1199 /*
1200 * find next buffer number to delete/unload
1201 */
1202 if (addr_count == 2)
1203 {
1204 if (++bnr > end_bnr)
1205 break;
1206 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001207 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
1209 arg = skipwhite(arg);
1210 if (*arg == NUL)
1211 break;
1212 if (!VIM_ISDIGIT(*arg))
1213 {
1214 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001215 bnr = buflist_findpat(arg, p,
1216 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001217 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001218 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 break;
1220 arg = p;
1221 }
1222 else
1223 bnr = getdigits(&arg);
1224 }
1225 }
1226 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1227 FORWARD, do_current, forceit) == OK)
1228 ++deleted;
1229
1230 if (deleted == 0)
1231 {
1232 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001238 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 else if (deleted >= p_report)
1241 {
1242 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001243 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001244 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001247 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001249 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001250 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252 }
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
1255 return errormsg;
1256}
1257
Bram Moolenaara02471e2014-01-10 16:43:14 +01001258/*
1259 * Make the current buffer empty.
1260 * Used when it is wiped out and it's the last buffer.
1261 */
1262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263empty_curbuf(
1264 int close_others,
1265 int forceit,
1266 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001267{
1268 int retval;
1269 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001270 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001271
1272 if (action == DOBUF_UNLOAD)
1273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001274 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275 return FAIL;
1276 }
1277
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001278 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001279 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001280 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001281 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001282
1283 setpcmark();
1284 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1285 forceit ? ECMD_FORCEIT : 0, curwin);
1286
1287 /*
1288 * do_ecmd() may create a new buffer, then we have to delete
1289 * the old one. But do_ecmd() may have done that already, check
1290 * if the buffer still exists.
1291 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001292 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001293 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001294 if (!close_others)
1295 need_fileinfo = FALSE;
1296 return retval;
1297}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299/*
1300 * Implementation of the commands for the buffer list.
1301 *
1302 * action == DOBUF_GOTO go to specified buffer
1303 * action == DOBUF_SPLIT split window and go to specified buffer
1304 * action == DOBUF_UNLOAD unload specified buffer(s)
1305 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1306 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001307 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 *
1309 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1310 * start == DOBUF_FIRST go to "count" buffer from first buffer
1311 * start == DOBUF_LAST go to "count" buffer from last buffer
1312 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1313 *
1314 * Return FAIL or OK.
1315 */
1316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001317do_buffer(
1318 int action,
1319 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001320 int dir, // FORWARD or BACKWARD
1321 int count, // buffer number or number of buffers
1322 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 buf_T *buf;
1325 buf_T *bp;
1326 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001327 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
1329 switch (start)
1330 {
1331 case DOBUF_FIRST: buf = firstbuf; break;
1332 case DOBUF_LAST: buf = lastbuf; break;
1333 default: buf = curbuf; break;
1334 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001335 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 while (count-- > 0)
1338 {
1339 do
1340 {
1341 buf = buf->b_next;
1342 if (buf == NULL)
1343 buf = firstbuf;
1344 }
1345 while (buf != curbuf && !bufIsChanged(buf));
1346 }
1347 if (!bufIsChanged(buf))
1348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001349 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 return FAIL;
1351 }
1352 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001353 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 {
1355 while (buf != NULL && buf->b_fnum != count)
1356 buf = buf->b_next;
1357 }
1358 else
1359 {
1360 bp = NULL;
1361 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1362 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001363 // remember the buffer where we start, we come back there when all
1364 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (bp == NULL)
1366 bp = buf;
1367 if (dir == FORWARD)
1368 {
1369 buf = buf->b_next;
1370 if (buf == NULL)
1371 buf = firstbuf;
1372 }
1373 else
1374 {
1375 buf = buf->b_prev;
1376 if (buf == NULL)
1377 buf = lastbuf;
1378 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001379 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (unload || buf->b_p_bl)
1381 {
1382 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001383 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385 if (bp == buf)
1386 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001387 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001388 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return FAIL;
1390 }
1391 }
1392 }
1393
Bram Moolenaarc667da52019-11-30 20:52:27 +01001394 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 if (start == DOBUF_FIRST)
1397 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001398 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001400 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001403 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001405 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 return FAIL;
1407 }
1408
1409#ifdef FEAT_GUI
1410 need_mouse_correct = TRUE;
1411#endif
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * delete buffer buf from memory and/or the list
1415 */
1416 if (unload)
1417 {
1418 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001419 bufref_T bufref;
1420
Bram Moolenaar94f01952018-09-01 15:30:03 +02001421 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001422 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001423
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001424 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaarc667da52019-11-30 20:52:27 +01001426 // When unloading or deleting a buffer that's already unloaded and
1427 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001428 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1429 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 return FAIL;
1431
1432 if (!forceit && bufIsChanged(buf))
1433 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001434#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if ((p_confirm || cmdmod.confirm) && p_write)
1436 {
1437 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001438 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001439 // Autocommand deleted buffer, oops! It's not changed
1440 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001442 // If it's still changed fail silently, the dialog already
1443 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001444 if (bufIsChanged(buf))
1445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001447 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001450 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 buf->b_fnum);
1452 return FAIL;
1453 }
1454 }
1455
Bram Moolenaarc667da52019-11-30 20:52:27 +01001456 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001457 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001458 end_visual_mode();
1459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 /*
1461 * If deleting the last (listed) buffer, make it empty.
1462 * The last (listed) buffer cannot be unloaded.
1463 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001464 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (bp->b_p_bl && bp != buf)
1466 break;
1467 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001468 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 /*
1471 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001472 * (unless it's the only window). Repeat this so long as we end up in
1473 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001475 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001476 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001477 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001478 {
1479 if (win_close(curwin, FALSE) == FAIL)
1480 break;
1481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 /*
1484 * If the buffer to be deleted is not the current one, delete it here.
1485 */
1486 if (buf != curbuf)
1487 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001488 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001489 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001490 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 return OK;
1492 }
1493
1494 /*
1495 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001496 * There should be another, otherwise it would have been handled
1497 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001498 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 * Then prefer the buffer we most recently visited.
1500 * Else try to find one that is loaded, after the current buffer,
1501 * then before the current buffer.
1502 * Finally use any buffer.
1503 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001504 buf = NULL; // selected buffer
1505 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001506 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1507 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001509 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511 int jumpidx;
1512
1513 jumpidx = curwin->w_jumplistidx - 1;
1514 if (jumpidx < 0)
1515 jumpidx = curwin->w_jumplistlen - 1;
1516
1517 forward = jumpidx;
1518 while (jumpidx != curwin->w_jumplistidx)
1519 {
1520 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1521 if (buf != NULL)
1522 {
1523 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001524 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 else if (buf->b_ml.ml_mfp == NULL)
1526 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001527 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (bp == NULL)
1529 bp = buf;
1530 buf = NULL;
1531 }
1532 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001535 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1537 break;
1538 if (--jumpidx < 0)
1539 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001540 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 break;
1542 }
1543 }
1544#endif
1545
Bram Moolenaarc667da52019-11-30 20:52:27 +01001546 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
1548 forward = TRUE;
1549 buf = curbuf->b_next;
1550 for (;;)
1551 {
1552 if (buf == NULL)
1553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001554 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 break;
1556 buf = curbuf->b_prev;
1557 forward = FALSE;
1558 continue;
1559 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001560 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1562 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001563 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001565 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 bp = buf;
1567 }
1568 if (forward)
1569 buf = buf->b_next;
1570 else
1571 buf = buf->b_prev;
1572 }
1573 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001574 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001576 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001578 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (buf->b_p_bl && buf != curbuf)
1580 break;
1581 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001582 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 if (curbuf->b_next != NULL)
1585 buf = curbuf->b_next;
1586 else
1587 buf = curbuf->b_prev;
1588 }
1589 }
1590
Bram Moolenaara02471e2014-01-10 16:43:14 +01001591 if (buf == NULL)
1592 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001593 // Autocommands must have wiped out all other buffers. Only option
1594 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001595 return empty_curbuf(FALSE, forceit, action);
1596 }
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /*
1599 * make buf current buffer
1600 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001601 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001603 // If 'switchbuf' contains "useopen": jump to first window containing
1604 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001605 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001606 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001607 // If 'switchbuf' contains "usetab": jump to first window in any tab
1608 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001609 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return OK;
1611 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 return FAIL;
1613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaarc667da52019-11-30 20:52:27 +01001615 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (buf == curbuf)
1617 return OK;
1618
1619 /*
1620 * Check if the current buffer may be abandoned.
1621 */
1622 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1623 {
1624#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1625 if ((p_confirm || cmdmod.confirm) && p_write)
1626 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001627 bufref_T bufref;
1628
1629 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001631 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001632 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 if (bufIsChanged(curbuf))
1636#endif
1637 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001638 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 return FAIL;
1640 }
1641 }
1642
Bram Moolenaarc667da52019-11-30 20:52:27 +01001643 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 set_curbuf(buf, action);
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001647 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001650 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 return FAIL;
1652#endif
1653
1654 return OK;
1655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
1657/*
1658 * Set current buffer to "buf". Executes autocommands and closes current
1659 * buffer. "action" tells how to close the current buffer:
1660 * DOBUF_GOTO free or hide it
1661 * DOBUF_SPLIT nothing
1662 * DOBUF_UNLOAD unload it
1663 * DOBUF_DEL delete it
1664 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001665 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 */
1667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001668set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 buf_T *prevbuf;
1671 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001672 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001673#ifdef FEAT_SYN_HL
1674 long old_tw = curbuf->b_p_tw;
1675#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001676 bufref_T newbufref;
1677 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001680 if (!cmdmod.keepalt)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001681 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1682 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaarc667da52019-11-30 20:52:27 +01001684 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaarc667da52019-11-30 20:52:27 +01001687 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001689 set_bufref(&prevbufref, prevbuf);
1690 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaarc667da52019-11-30 20:52:27 +01001692 // Autocommands may delete the curren buffer and/or the buffer we wan to go
1693 // to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001694 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001695 || (bufref_valid(&prevbufref)
1696 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001697#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001698 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001700 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001702#ifdef FEAT_SYN_HL
1703 if (prevbuf == curwin->w_buffer)
1704 reset_synblock(curwin);
1705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001707 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001709 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001711 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001713 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001714 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001715 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001716 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1718 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001719 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001720 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1721 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001722 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001723 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001724 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001727 // An autocommand may have deleted "buf", already entered it (e.g., when
1728 // it did ":bunload") or aborted the script processing.
1729 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001730 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001732 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001734 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001737#ifdef FEAT_SYN_HL
1738 if (old_tw != curbuf->b_p_tw)
1739 check_colorcolumn(curwin);
1740#endif
1741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742}
1743
1744/*
1745 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001746 * Old curbuf must have been abandoned already! This also means "curbuf" may
1747 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001752 // Get the buffer in the current window.
1753 curwin->w_buffer = buf;
1754 curbuf = buf;
1755 ++curbuf->b_nwindows;
1756
1757 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1759 if (!buf->b_help)
1760 get_winopts(buf);
1761#ifdef FEAT_FOLDING
1762 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001763 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001765 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#endif
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001769 if (curwin->w_p_diff)
1770 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#endif
1772
Bram Moolenaara971b822011-09-14 14:43:25 +02001773#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001774 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001775#endif
1776
Bram Moolenaarc667da52019-11-30 20:52:27 +01001777 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 curwin->w_cursor.lnum = 1;
1779 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001782 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaarc667da52019-11-30 20:52:27 +01001784 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001785 curwin->w_valid = 0;
1786
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001787 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1788 curbuf->b_last_cursor.col, TRUE);
1789
Bram Moolenaarc667da52019-11-30 20:52:27 +01001790 // Make sure the buffer is loaded.
1791 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001792 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001793 // If there is no filetype, allow for detecting one. Esp. useful for
1794 // ":ball" used in a autocommand. If there already is a filetype we
1795 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001796 if (*curbuf->b_p_ft == NUL)
1797 did_filetype = FALSE;
1798
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001799 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 else
1802 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001803 if (!msg_silent && !shortmess(SHM_FILEINFO))
1804 need_fileinfo = TRUE; // display file info after redraw
1805
1806 // check if file changed
1807 (void)buf_check_timestamp(curbuf, FALSE);
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001810#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1814 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816
Bram Moolenaarc667da52019-11-30 20:52:27 +01001817 // If autocommands did not change the cursor position, restore cursor lnum
1818 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (curwin->w_cursor.lnum == 1 && inindent(0))
1820 buflist_getfpos();
1821
Bram Moolenaarc667da52019-11-30 20:52:27 +01001822 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#ifdef FEAT_TITLE
1824 maketitle();
1825#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001826 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001827 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001828 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar009b2592004-10-24 19:18:58 +00001830#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001831 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001832 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001833#endif
1834
Bram Moolenaarc667da52019-11-30 20:52:27 +01001835 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001836 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838#ifdef FEAT_KEYMAP
1839 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001840 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001842#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // May need to set the spell language. Can only do this after the buffer
1844 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001845 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1846 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001847#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001848#ifdef FEAT_VIMINFO
1849 curbuf->b_last_used = vim_time();
1850#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 redraw_later(NOT_VALID);
1853}
1854
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001855#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1856/*
1857 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001858 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001859 */
1860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001861do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001862{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001863 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001864 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001865 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001866 shorten_fnames(TRUE);
1867}
1868#endif
1869
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001870 void
1871no_write_message(void)
1872{
1873#ifdef FEAT_TERMINAL
1874 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001875 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001876 else
1877#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001878 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001879}
1880
1881 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001882no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001883{
1884#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001885 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001886 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001887 else
1888#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001890}
1891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892/*
1893 * functions for dealing with the buffer list
1894 */
1895
1896/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001897 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1898 * only one window. That means it can be re-used.
1899 */
1900 int
1901curbuf_reusable(void)
1902{
1903 return (curbuf != NULL
1904 && curbuf->b_ffname == NULL
1905 && curbuf->b_nwindows <= 1
1906 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001907#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001908 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001909#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001910 && !curbufIsChanged());
1911}
1912
1913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 * Add a file name to the buffer list. Return a pointer to the buffer.
1915 * If the same file name already exists return a pointer to that buffer.
1916 * If it does not exist, or if fname == NULL, a new entry is created.
1917 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1918 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1919 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001920 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001921 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1922 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001923 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 * This is the ONLY way to create a new buffer.
1925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001928 char_u *ffname_arg, // full path of fname or relative
1929 char_u *sfname_arg, // short fname or NULL
1930 linenr_T lnum, // preferred cursor line
1931 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001933 char_u *ffname = ffname_arg;
1934 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 buf_T *buf;
1936#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001937 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#endif
1939
Bram Moolenaar480778b2016-07-14 22:09:39 +02001940 if (top_file_num == 1)
1941 hash_init(&buf_hashtab);
1942
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001943 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 /*
1946 * If file name already exists in the list, update the entry.
1947 */
1948#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001949 // On Unix we can use inode numbers when the file exists. Works better
1950 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1952 st.st_dev = (dev_T)-1;
1953#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001954 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955#ifdef UNIX
1956 buflist_findname_stat(ffname, &st)
1957#else
1958 buflist_findname(ffname)
1959#endif
1960 ) != NULL)
1961 {
1962 vim_free(ffname);
1963 if (lnum != 0)
1964 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001965
1966 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001967 // copy the options now, if 'cpo' doesn't have 's' and not done
1968 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02001969 buf_copy_options(buf, 0);
1970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1972 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001973 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001976 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001978 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001979 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001980 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001981 return NULL;
1982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 }
1984 return buf;
1985 }
1986
1987 /*
1988 * If the current buffer has no name and no contents, use the current
1989 * buffer. Otherwise: Need to allocate a new buffer structure.
1990 *
1991 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001992 * (A spell file buffer is allocated in spell.c, but that's not a normal
1993 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
1995 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001996 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001999 // It's like this buffer is deleted. Watch out for autocommands that
2000 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (curbuf->b_p_bl)
2002 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2003 if (buf == curbuf)
2004 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002005#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002006 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002011 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 clear_string_option(&buf->b_p_bh);
2013 clear_string_option(&buf->b_p_bt);
2014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 if (buf != curbuf || curbuf == NULL)
2017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002018 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (buf == NULL)
2020 {
2021 vim_free(ffname);
2022 return NULL;
2023 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002024#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002025 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002026 buf->b_vars = dict_alloc();
2027 if (buf->b_vars == NULL)
2028 {
2029 vim_free(ffname);
2030 vim_free(buf);
2031 return NULL;
2032 }
2033 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2034#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002035 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037
2038 if (ffname != NULL)
2039 {
2040 buf->b_ffname = ffname;
2041 buf->b_sfname = vim_strsave(sfname);
2042 }
2043
2044 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002045 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2048 || buf->b_wininfo == NULL)
2049 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002050 if (buf->b_sfname != buf->b_ffname)
2051 VIM_CLEAR(buf->b_sfname);
2052 else
2053 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002054 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 if (buf != curbuf)
2056 free_buffer(buf);
2057 return NULL;
2058 }
2059
2060 if (buf == curbuf)
2061 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002062 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002063 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002064 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002066#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002067 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return NULL;
2069#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002070 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002071
Bram Moolenaarc667da52019-11-30 20:52:27 +01002072 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002073 buf->b_p_initialized = FALSE;
2074 buf_copy_options(buf, BCO_ENTER);
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002077 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 curbuf->b_kmap_state |= KEYMAP_INIT;
2079#endif
2080 }
2081 else
2082 {
2083 /*
2084 * put new buffer at the end of the buffer list
2085 */
2086 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002087 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
2089 buf->b_prev = NULL;
2090 firstbuf = buf;
2091 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002092 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 lastbuf->b_next = buf;
2095 buf->b_prev = lastbuf;
2096 }
2097 lastbuf = buf;
2098
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002099 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2100 {
2101 // Recycle a previously used buffer number. Used for buffers which
2102 // are normally hidden, e.g. in a popup window. Avoids that the
2103 // buffer number grows rapidly.
2104 --buf_reuse.ga_len;
2105 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002106
2107 // Move buffer to the right place in the buffer list.
2108 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2109 {
2110 buf_T *prev = buf->b_prev;
2111
2112 prev->b_next = buf->b_next;
2113 if (prev->b_next != NULL)
2114 prev->b_next->b_prev = prev;
2115 buf->b_next = prev;
2116 buf->b_prev = prev->b_prev;
2117 if (buf->b_prev != NULL)
2118 buf->b_prev->b_next = buf;
2119 prev->b_prev = buf;
2120 if (lastbuf == buf)
2121 lastbuf = prev;
2122 if (firstbuf == prev)
2123 firstbuf = buf;
2124 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002125 }
2126 else
2127 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002128 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (emsg_silent == 0)
2132 {
2133 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002134 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 top_file_num = 1;
2137 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002138 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140 /*
2141 * Always copy the options from the current buffer.
2142 */
2143 buf_copy_options(buf, BCO_ALWAYS);
2144 }
2145
2146 buf->b_wininfo->wi_fpos.lnum = lnum;
2147 buf->b_wininfo->wi_win = curwin;
2148
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002149#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002150 hash_init(&buf->b_s.b_keywtab);
2151 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152#endif
2153
2154 buf->b_fname = buf->b_sfname;
2155#ifdef UNIX
2156 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002157 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 else
2159 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002160 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 buf->b_dev = st.st_dev;
2162 buf->b_ino = st.st_ino;
2163 }
2164#endif
2165 buf->b_u_synced = TRUE;
2166 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002167 if (flags & BLN_DUMMY)
2168 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002170 clrallmarks(buf); // clear marks
2171 fmarks_check_names(buf); // check file marks for this file
2172 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (!(flags & BLN_DUMMY))
2174 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002175 bufref_T bufref;
2176
Bram Moolenaarc667da52019-11-30 20:52:27 +01002177 // Tricky: these autocommands may change the buffer list. They could
2178 // also split the window with re-using the one empty buffer. This may
2179 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002180 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002181 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002182 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002183 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002185 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002186 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002187 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002188 return NULL;
2189 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002190#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002191 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 return buf;
2197}
2198
2199/*
2200 * Free the memory for the options of a buffer.
2201 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2202 * 'fileencoding'.
2203 */
2204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205free_buf_options(
2206 buf_T *buf,
2207 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 if (free_p_ff)
2210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 clear_string_option(&buf->b_p_bh);
2214 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216#ifdef FEAT_FIND_ID
2217 clear_string_option(&buf->b_p_def);
2218 clear_string_option(&buf->b_p_inc);
2219# ifdef FEAT_EVAL
2220 clear_string_option(&buf->b_p_inex);
2221# endif
2222#endif
2223#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2224 clear_string_option(&buf->b_p_inde);
2225 clear_string_option(&buf->b_p_indk);
2226#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002227#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2228 clear_string_option(&buf->b_p_bexpr);
2229#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002230#if defined(FEAT_CRYPT)
2231 clear_string_option(&buf->b_p_cm);
2232#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002233 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002234#if defined(FEAT_EVAL)
2235 clear_string_option(&buf->b_p_fex);
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#ifdef FEAT_CRYPT
2238 clear_string_option(&buf->b_p_key);
2239#endif
2240 clear_string_option(&buf->b_p_kp);
2241 clear_string_option(&buf->b_p_mps);
2242 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002243 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002245#ifdef FEAT_VARTABS
2246 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002247 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002248 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002249 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002250 buf->b_p_vsts_array = NULL;
2251 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002252 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#ifdef FEAT_KEYMAP
2255 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002256 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 ga_clear(&buf->b_kmap_ga);
2258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#ifdef FEAT_FOLDING
2261 clear_string_option(&buf->b_p_cms);
2262#endif
2263 clear_string_option(&buf->b_p_nf);
2264#ifdef FEAT_SYN_HL
2265 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002266 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002267#endif
2268#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002269 clear_string_option(&buf->b_s.b_p_spc);
2270 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002271 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002272 buf->b_s.b_cap_prog = NULL;
2273 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#endif
2275#ifdef FEAT_SEARCHPATH
2276 clear_string_option(&buf->b_p_sua);
2277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279#ifdef FEAT_CINDENT
2280 clear_string_option(&buf->b_p_cink);
2281 clear_string_option(&buf->b_p_cino);
2282#endif
2283#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2284 clear_string_option(&buf->b_p_cinw);
2285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002287#ifdef FEAT_COMPL_FUNC
2288 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002289 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#ifdef FEAT_QUICKFIX
2292 clear_string_option(&buf->b_p_gp);
2293 clear_string_option(&buf->b_p_mp);
2294 clear_string_option(&buf->b_p_efm);
2295#endif
2296 clear_string_option(&buf->b_p_ep);
2297 clear_string_option(&buf->b_p_path);
2298 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002299 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002300#ifdef FEAT_EVAL
2301 clear_string_option(&buf->b_p_tfu);
2302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 clear_string_option(&buf->b_p_dict);
2304 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002305#ifdef FEAT_TEXTOBJ
2306 clear_string_option(&buf->b_p_qe);
2307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002309 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002310#ifdef FEAT_LISP
2311 clear_string_option(&buf->b_p_lw);
2312#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002313 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002314 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315}
2316
2317/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002318 * Get alternate file "n".
2319 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2320 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 * if (options & GETF_SETMARK) call setpcmark()
2322 * if (options & GETF_ALT) we are jumping to an alternate file.
2323 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2324 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002325 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 */
2327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328buflist_getfile(
2329 int n,
2330 linenr_T lnum,
2331 int options,
2332 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 pos_T *fpos;
2337 colnr_T col;
2338
2339 buf = buflist_findnr(n);
2340 if (buf == NULL)
2341 {
2342 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002343 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002345 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 return FAIL;
2347 }
2348
Bram Moolenaarc667da52019-11-30 20:52:27 +01002349 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (buf == curbuf)
2351 return OK;
2352
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002353 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002354 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002355 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002357 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002358 if (curbuf_locked())
2359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
Bram Moolenaarc667da52019-11-30 20:52:27 +01002361 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (lnum == 0)
2363 {
2364 fpos = buflist_findfpos(buf);
2365 lnum = fpos->lnum;
2366 col = fpos->col;
2367 }
2368 else
2369 col = 0;
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (options & GETF_SWITCH)
2372 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002373 // If 'switchbuf' contains "useopen": jump to first window containing
2374 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002375 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002377
Bram Moolenaarc667da52019-11-30 20:52:27 +01002378 // If 'switchbuf' contains "usetab": jump to first window in any tab
2379 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002380 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002381 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002382
Bram Moolenaarc667da52019-11-30 20:52:27 +01002383 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2384 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002385 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002386 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002388 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002389 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002390 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2391 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002393 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002398 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2399 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
2401 --RedrawingDisabled;
2402
Bram Moolenaarc667da52019-11-30 20:52:27 +01002403 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (!p_sol && col != 0)
2405 {
2406 curwin->w_cursor.col = col;
2407 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 curwin->w_set_curswant = TRUE;
2410 }
2411 return OK;
2412 }
2413 --RedrawingDisabled;
2414 return FAIL;
2415}
2416
2417/*
2418 * go to the last know line number for the current buffer
2419 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002421buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 pos_T *fpos;
2424
2425 fpos = buflist_findfpos(curbuf);
2426
2427 curwin->w_cursor.lnum = fpos->lnum;
2428 check_cursor_lnum();
2429
2430 if (p_sol)
2431 curwin->w_cursor.col = 0;
2432 else
2433 {
2434 curwin->w_cursor.col = fpos->col;
2435 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 curwin->w_set_curswant = TRUE;
2438 }
2439}
2440
Bram Moolenaar81695252004-12-29 20:58:21 +00002441#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2442/*
2443 * Find file in buffer list by name (it has to be for the current window).
2444 * Returns NULL if not found.
2445 */
2446 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002447buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002448{
2449 char_u *ffname;
2450 buf_T *buf = NULL;
2451
Bram Moolenaarc667da52019-11-30 20:52:27 +01002452 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002453 ffname = FullName_save(fname,
2454#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002455 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002456#else
2457 FALSE
2458#endif
2459 );
2460 if (ffname != NULL)
2461 {
2462 buf = buflist_findname(ffname);
2463 vim_free(ffname);
2464 }
2465 return buf;
2466}
2467#endif
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469/*
2470 * Find file in buffer list by name (it has to be for the current window).
2471 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002472 * Skips dummy buffers.
2473 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
2475 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002476buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002479 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 if (mch_stat((char *)ffname, &st) < 0)
2482 st.st_dev = (dev_T)-1;
2483 return buflist_findname_stat(ffname, &st);
2484}
2485
2486/*
2487 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2488 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002489 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 */
2491 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002492buflist_findname_stat(
2493 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002494 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496#endif
2497 buf_T *buf;
2498
Bram Moolenaarc667da52019-11-30 20:52:27 +01002499 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002500 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002501 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502#ifdef UNIX
2503 , stp
2504#endif
2505 ))
2506 return buf;
2507 return NULL;
2508}
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510/*
2511 * Find file in buffer list by a regexp pattern.
2512 * Return fnum of the found buffer.
2513 * Return < 0 for error.
2514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002516buflist_findpat(
2517 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002518 char_u *pattern_end, // pointer to first char after pattern
2519 int unlisted, // find unlisted buffers
2520 int diffmode UNUSED, // find diff-mode buffers only
2521 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int match = -1;
2525 int find_listed;
2526 char_u *pat;
2527 char_u *patend;
2528 int attempt;
2529 char_u *p;
2530 int toggledollar;
2531
2532 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2533 {
2534 if (*pattern == '%')
2535 match = curbuf->b_fnum;
2536 else
2537 match = curwin->w_alt_fnum;
2538#ifdef FEAT_DIFF
2539 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2540 match = -1;
2541#endif
2542 }
2543
2544 /*
2545 * Try four ways of matching a listed buffer:
2546 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002547 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 * attempt == 2: with '$' at end (only match at end)
2549 * attempt == 3: with '^' at start and '$' at end (only full match)
2550 * Repeat this for finding an unlisted buffer if there was no matching
2551 * listed buffer.
2552 */
2553 else
2554 {
2555 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2556 if (pat == NULL)
2557 return -1;
2558 patend = pat + STRLEN(pat) - 1;
2559 toggledollar = (patend > pat && *patend == '$');
2560
Bram Moolenaarc667da52019-11-30 20:52:27 +01002561 // First try finding a listed buffer. If not found and "unlisted"
2562 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 find_listed = TRUE;
2564 for (;;)
2565 {
2566 for (attempt = 0; attempt <= 3; ++attempt)
2567 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002568 regmatch_T regmatch;
2569
Bram Moolenaarc667da52019-11-30 20:52:27 +01002570 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002572 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002574 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002576 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2577 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 vim_free(pat);
2580 return -1;
2581 }
2582
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002583 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (buf->b_p_bl == find_listed
2585#ifdef FEAT_DIFF
2586 && (!diffmode || diff_mode_buf(buf))
2587#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002588 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002590 if (curtab_only)
2591 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002592 // Ignore the match if the buffer is not open in
2593 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002594 win_T *wp;
2595
Bram Moolenaar29323592016-07-24 22:04:11 +02002596 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002597 if (wp->w_buffer == buf)
2598 break;
2599 if (wp == NULL)
2600 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002601 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002602 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 {
2604 match = -2;
2605 break;
2606 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002607 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002610 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002611 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 break;
2613 }
2614
Bram Moolenaarc667da52019-11-30 20:52:27 +01002615 // Only search for unlisted buffers if there was no match with
2616 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 if (!unlisted || !find_listed || match != -1)
2618 break;
2619 find_listed = FALSE;
2620 }
2621
2622 vim_free(pat);
2623 }
2624
2625 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002626 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002628 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 return match;
2630}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar52410572019-10-27 05:12:45 +01002632#ifdef FEAT_VIMINFO
2633typedef struct {
2634 buf_T *buf;
2635 char_u *match;
2636} bufmatch_T;
2637#endif
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
2640 * Find all buffer names that match.
2641 * For command line expansion of ":buf" and ":sbuf".
2642 * Return OK if matches found, FAIL otherwise.
2643 */
2644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002645ExpandBufnames(
2646 char_u *pat,
2647 int *num_file,
2648 char_u ***file,
2649 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 int count = 0;
2652 buf_T *buf;
2653 int round;
2654 char_u *p;
2655 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002656 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002657#ifdef FEAT_VIMINFO
2658 bufmatch_T *matches = NULL;
2659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaarc667da52019-11-30 20:52:27 +01002661 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 *file = NULL;
2663
Bram Moolenaarc667da52019-11-30 20:52:27 +01002664 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002665 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002667 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002668 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002670 STRCPY(patc, "\\(^\\|[\\/]\\)");
2671 STRCPY(patc + 11, pat + 1);
2672 }
2673 else
2674 patc = pat;
2675
2676 /*
2677 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002678 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002679 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002680 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002681 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002682 regmatch_T regmatch;
2683
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002684 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002685 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002686 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2687 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002688 {
2689 if (patc != pat)
2690 vim_free(patc);
2691 return FAIL;
2692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 /*
2695 * round == 1: Count the matches.
2696 * round == 2: Build the array to keep the matches.
2697 */
2698 for (round = 1; round <= 2; ++round)
2699 {
2700 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002701 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002703 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002705#ifdef FEAT_DIFF
2706 if (options & BUF_DIFF_FILTER)
2707 // Skip buffers not suitable for
2708 // :diffget or :diffput completion.
2709 if (buf == curbuf
2710 || !diff_mode_buf(curbuf) || !diff_mode_buf(buf))
2711 continue;
2712#endif
2713
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002714 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (p != NULL)
2716 {
2717 if (round == 1)
2718 ++count;
2719 else
2720 {
2721 if (options & WILD_HOME_REPLACE)
2722 p = home_replace_save(buf, p);
2723 else
2724 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002725#ifdef FEAT_VIMINFO
2726 if (matches != NULL)
2727 {
2728 matches[count].buf = buf;
2729 matches[count].match = p;
2730 count++;
2731 }
2732 else
2733#endif
2734 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 }
2737 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002738 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 break;
2740 if (round == 1)
2741 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002742 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (*file == NULL)
2744 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002745 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002746 if (patc != pat)
2747 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 return FAIL;
2749 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002750#ifdef FEAT_VIMINFO
2751 if (options & WILD_BUFLASTUSED)
2752 matches = ALLOC_MULT(bufmatch_T, count);
2753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002756 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002757 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 break;
2759 }
2760
Bram Moolenaar05159a02005-02-26 23:04:13 +00002761 if (patc != pat)
2762 vim_free(patc);
2763
Bram Moolenaar52410572019-10-27 05:12:45 +01002764#ifdef FEAT_VIMINFO
2765 if (matches != NULL)
2766 {
2767 int i;
2768 if (count > 1)
2769 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2770 // if the current buffer is first in the list, place it at the end
2771 if (matches[0].buf == curbuf)
2772 {
2773 for (i = 1; i < count; i++)
2774 (*file)[i-1] = matches[i].match;
2775 (*file)[count-1] = matches[0].match;
2776 }
2777 else
2778 {
2779 for (i = 0; i < count; i++)
2780 (*file)[i] = matches[i].match;
2781 }
2782 vim_free(matches);
2783 }
2784#endif
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 *num_file = count;
2787 return (count == 0 ? FAIL : OK);
2788}
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790/*
2791 * Check for a match on the file name for buffer "buf" with regprog "prog".
2792 */
2793 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794buflist_match(
2795 regmatch_T *rmp,
2796 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002797 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 char_u *match;
2800
Bram Moolenaarc667da52019-11-30 20:52:27 +01002801 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002802 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002804 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 return match;
2807}
2808
2809/*
2810 * Try matching the regexp in "prog" with file name "name".
2811 * Return "name" when there is a match, NULL when not.
2812 */
2813 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002814fname_match(
2815 regmatch_T *rmp,
2816 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002817 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 char_u *match = NULL;
2820 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
2822 if (name != NULL)
2823 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002824 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002825 rmp->rm_ic = p_fic || ignore_case;
2826 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 match = name;
2828 else
2829 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002830 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002832 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 match = name;
2834 vim_free(p);
2835 }
2836 }
2837
2838 return match;
2839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002842 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 */
2844 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002847 char_u key[VIM_SIZEOF_INT * 2 + 1];
2848 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 if (nr == 0)
2851 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002852 sprintf((char *)key, "%x", nr);
2853 hi = hash_find(&buf_hashtab, key);
2854
2855 if (!HASHITEM_EMPTY(hi))
2856 return (buf_T *)(hi->hi_key
2857 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 return NULL;
2859}
2860
2861/*
2862 * Get name of file 'n' in the buffer list.
2863 * When the file has no name an empty string is returned.
2864 * home_replace() is used to shorten the file name (used for marks).
2865 * Returns a pointer to allocated memory, of NULL when failed.
2866 */
2867 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868buflist_nr2name(
2869 int n,
2870 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002871 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 buf_T *buf;
2874
2875 buf = buflist_findnr(n);
2876 if (buf == NULL)
2877 return NULL;
2878 return home_replace_save(helptail ? buf : NULL,
2879 fullname ? buf->b_ffname : buf->b_fname);
2880}
2881
2882/*
2883 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2884 * When "copy_options" is TRUE save the local window option values.
2885 * When "lnum" is 0 only do the options.
2886 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002888buflist_setfpos(
2889 buf_T *buf,
2890 win_T *win,
2891 linenr_T lnum,
2892 colnr_T col,
2893 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 wininfo_T *wip;
2896
2897 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2898 if (wip->wi_win == win)
2899 break;
2900 if (wip == NULL)
2901 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002902 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002903 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (wip == NULL)
2905 return;
2906 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002907 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 lnum = 1;
2909 }
2910 else
2911 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002912 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (wip->wi_prev)
2914 wip->wi_prev->wi_next = wip->wi_next;
2915 else
2916 buf->b_wininfo = wip->wi_next;
2917 if (wip->wi_next)
2918 wip->wi_next->wi_prev = wip->wi_prev;
2919 if (copy_options && wip->wi_optset)
2920 {
2921 clear_winopt(&wip->wi_opt);
2922#ifdef FEAT_FOLDING
2923 deleteFoldRecurse(&wip->wi_folds);
2924#endif
2925 }
2926 }
2927 if (lnum != 0)
2928 {
2929 wip->wi_fpos.lnum = lnum;
2930 wip->wi_fpos.col = col;
2931 }
2932 if (copy_options)
2933 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002934 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2936#ifdef FEAT_FOLDING
2937 wip->wi_fold_manual = win->w_fold_manual;
2938 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2939#endif
2940 wip->wi_optset = TRUE;
2941 }
2942
Bram Moolenaarc667da52019-11-30 20:52:27 +01002943 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 wip->wi_next = buf->b_wininfo;
2945 buf->b_wininfo = wip;
2946 wip->wi_prev = NULL;
2947 if (wip->wi_next)
2948 wip->wi_next->wi_prev = wip;
2949
2950 return;
2951}
2952
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002953#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002954/*
2955 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2956 * page. That's because a diff is local to a tab page.
2957 */
2958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002960{
2961 win_T *wp;
2962
2963 if (wip->wi_opt.wo_diff)
2964 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002965 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002966 // return FALSE when it's a window in the current tab page, thus
2967 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002968 if (wip->wi_win == wp)
2969 return FALSE;
2970 return TRUE;
2971 }
2972 return FALSE;
2973}
2974#endif
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Find info for the current window in buffer "buf".
2978 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002979 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2980 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 * Returns NULL when there isn't any info.
2982 */
2983 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002984find_wininfo(
2985 buf_T *buf,
2986 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 wininfo_T *wip;
2989
2990 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002991 if (wip->wi_win == curwin
2992#ifdef FEAT_DIFF
2993 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2994#endif
2995 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002997
Bram Moolenaarc667da52019-11-30 20:52:27 +01002998 // If no wininfo for curwin, use the first in the list (that doesn't have
2999 // 'diff' set and is in another tab page).
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003000 if (wip == NULL)
3001 {
3002#ifdef FEAT_DIFF
3003 if (skip_diff_buffer)
3004 {
3005 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
3006 if (!wininfo_other_tab_diff(wip))
3007 break;
3008 }
3009 else
3010#endif
3011 wip = buf->b_wininfo;
3012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 return wip;
3014}
3015
3016/*
3017 * Reset the local window options to the values last used in this window.
3018 * If the buffer wasn't used in this window before, use the values from
3019 * the most recently used window. If the values were never set, use the
3020 * global values for the window.
3021 */
3022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003023get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 wininfo_T *wip;
3026
3027 clear_winopt(&curwin->w_onebuf_opt);
3028#ifdef FEAT_FOLDING
3029 clearFolding(curwin);
3030#endif
3031
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003032 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003033 if (wip != NULL && wip->wi_win != NULL
3034 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003036 // The buffer is currently displayed in the window: use the actual
3037 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003038 win_T *wp = wip->wi_win;
3039
3040 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3041#ifdef FEAT_FOLDING
3042 curwin->w_fold_manual = wp->w_fold_manual;
3043 curwin->w_foldinvalid = TRUE;
3044 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3045#endif
3046 }
3047 else if (wip != NULL && wip->wi_optset)
3048 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003049 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3051#ifdef FEAT_FOLDING
3052 curwin->w_fold_manual = wip->wi_fold_manual;
3053 curwin->w_foldinvalid = TRUE;
3054 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3055#endif
3056 }
3057 else
3058 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3059
3060#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003061 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 if (p_fdls >= 0)
3063 curwin->w_p_fdl = p_fdls;
3064#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003065 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066}
3067
3068/*
3069 * Find the position (lnum and col) for the buffer 'buf' for the current
3070 * window.
3071 * Returns a pointer to no_position if no position is found.
3072 */
3073 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003077 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003079 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (wip != NULL)
3081 return &(wip->wi_fpos);
3082 else
3083 return &no_position;
3084}
3085
3086/*
3087 * Find the lnum for the buffer 'buf' for the current window.
3088 */
3089 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003090buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 return buflist_findfpos(buf)->lnum;
3093}
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003096 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003099buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
Bram Moolenaar52410572019-10-27 05:12:45 +01003101 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 int len;
3103 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003104 int ro_char;
3105 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003106#ifdef FEAT_TERMINAL
3107 int job_running;
3108 int job_none_open;
3109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
Bram Moolenaar52410572019-10-27 05:12:45 +01003111#ifdef FEAT_VIMINFO
3112 garray_T buflist;
3113 buf_T **buflist_data = NULL, **p;
3114
3115 if (vim_strchr(eap->arg, 't'))
3116 {
3117 ga_init2(&buflist, sizeof(buf_T *), 50);
3118 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3119 {
3120 if (ga_grow(&buflist, 1) == OK)
3121 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3122 }
3123
3124 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3125 sizeof(buf_T *), buf_compare);
3126
Bram Moolenaar3b991522019-11-06 23:26:20 +01003127 buflist_data = (buf_T **)buflist.ga_data;
3128 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003129 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003130 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003131
Bram Moolenaar3b991522019-11-06 23:26:20 +01003132 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003133 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3134 : buf->b_next)
3135#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003139#ifdef FEAT_TERMINAL
3140 job_running = term_job_running(buf->b_term);
3141 job_none_open = job_running && term_none_open(buf->b_term);
3142#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003143 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003144 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3145 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3146 || (vim_strchr(eap->arg, '+')
3147 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3148 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003149 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003150 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003151 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3152#ifdef FEAT_TERMINAL
3153 || (vim_strchr(eap->arg, 'R')
3154 && (!job_running || (job_running && job_none_open)))
3155 || (vim_strchr(eap->arg, '?')
3156 && (!job_running || (job_running && !job_none_open)))
3157 || (vim_strchr(eap->arg, 'F')
3158 && (job_running || buf->b_term == NULL))
3159#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003160 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3161 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3162 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3163 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3164 || (vim_strchr(eap->arg, '#')
3165 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003168 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 else
3170 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003171 if (message_filtered(NameBuff))
3172 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003174 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3175 : (bufIsChanged(buf) ? '+' : ' ');
3176#ifdef FEAT_TERMINAL
3177 if (term_job_running(buf->b_term))
3178 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003179 if (term_none_open(buf->b_term))
3180 ro_char = '?';
3181 else
3182 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003183 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3184 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003185 }
3186 else if (buf->b_term != NULL)
3187 ro_char = 'F';
3188 else
3189#endif
3190 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3191
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003192 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003193 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 buf->b_fnum,
3195 buf->b_p_bl ? ' ' : 'u',
3196 buf == curbuf ? '%' :
3197 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3198 buf->b_ml.ml_mfp == NULL ? ' ' :
3199 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003200 ro_char,
3201 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003202 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003203 if (len > IOSIZE - 20)
3204 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
Bram Moolenaarc667da52019-11-30 20:52:27 +01003206 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 i = 40 - vim_strsize(IObuff);
3208 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003210 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003211#ifdef FEAT_VIMINFO
3212 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3213 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3214 else
3215#endif
3216 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3217 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003218 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003220 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 ui_breakcheck();
3222 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003223
3224#ifdef FEAT_VIMINFO
3225 if (buflist_data)
3226 ga_clear(&buflist);
3227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230/*
3231 * Get file name and line number for file 'fnum'.
3232 * Used by DoOneCmd() for translating '%' and '#'.
3233 * Used by insert_reg() and cmdline_paste() for '#' register.
3234 * Return FAIL if not found, OK for success.
3235 */
3236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003237buflist_name_nr(
3238 int fnum,
3239 char_u **fname,
3240 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 buf_T *buf;
3243
3244 buf = buflist_findnr(fnum);
3245 if (buf == NULL || buf->b_fname == NULL)
3246 return FAIL;
3247
3248 *fname = buf->b_fname;
3249 *lnum = buflist_findlnum(buf);
3250
3251 return OK;
3252}
3253
3254/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003255 * Set the file name for "buf"' to "ffname_arg", short file name to
3256 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 * The file name with the full path is also remembered, for when :cd is used.
3258 * Returns FAIL for failure (file name already in use by other buffer)
3259 * OK otherwise.
3260 */
3261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003262setfname(
3263 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003264 char_u *ffname_arg,
3265 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003266 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003268 char_u *ffname = ffname_arg;
3269 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003270 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003272 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#endif
3274
3275 if (ffname == NULL || *ffname == NUL)
3276 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003277 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003278 if (buf->b_sfname != buf->b_ffname)
3279 VIM_CLEAR(buf->b_sfname);
3280 else
3281 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003282 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#ifdef UNIX
3284 st.st_dev = (dev_T)-1;
3285#endif
3286 }
3287 else
3288 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003289 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3290 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return FAIL;
3292
3293 /*
3294 * if the file name is already used in another buffer:
3295 * - if the buffer is loaded, fail
3296 * - if the buffer is not loaded, delete it from the list
3297 */
3298#ifdef UNIX
3299 if (mch_stat((char *)ffname, &st) < 0)
3300 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003301#endif
3302 if (!(buf->b_flags & BF_DUMMY))
3303#ifdef UNIX
3304 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003306 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#endif
3308 if (obuf != NULL && obuf != buf)
3309 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003310 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 vim_free(ffname);
3315 return FAIL;
3316 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003317 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003318 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 sfname = vim_strsave(sfname);
3321 if (ffname == NULL || sfname == NULL)
3322 {
3323 vim_free(sfname);
3324 vim_free(ffname);
3325 return FAIL;
3326 }
3327#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003328 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003330 if (buf->b_sfname != buf->b_ffname)
3331 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 buf->b_ffname = ffname;
3334 buf->b_sfname = sfname;
3335 }
3336 buf->b_fname = buf->b_sfname;
3337#ifdef UNIX
3338 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003339 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
3341 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003342 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 buf->b_dev = st.st_dev;
3344 buf->b_ino = st.st_ino;
3345 }
3346#endif
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 buf_name_changed(buf);
3351 return OK;
3352}
3353
3354/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003355 * Crude way of changing the name of a buffer. Use with care!
3356 * The name should be relative to the current directory.
3357 */
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003360{
3361 buf_T *buf;
3362
3363 buf = buflist_findnr(fnum);
3364 if (buf != NULL)
3365 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003366 if (buf->b_sfname != buf->b_ffname)
3367 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003368 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003369 buf->b_ffname = vim_strsave(name);
3370 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003371 // Allocate ffname and expand into full path. Also resolves .lnk
3372 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003373 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003374 buf->b_fname = buf->b_sfname;
3375 }
3376}
3377
3378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 * Take care of what needs to be done when the name of buffer "buf" has
3380 * changed.
3381 */
3382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003383buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
3385 /*
3386 * If the file name changed, also change the name of the swapfile
3387 */
3388 if (buf->b_ml.ml_mfp != NULL)
3389 ml_setname(buf);
3390
3391 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003392 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003394 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003396 status_redraw_all(); // status lines need to be redrawn
3397 fmarks_check_names(buf); // check named file marks
3398 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399}
3400
3401/*
3402 * set alternate file name for current window
3403 *
3404 * Used by do_one_cmd(), do_write() and do_ecmd().
3405 * Return the buffer.
3406 */
3407 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003408setaltfname(
3409 char_u *ffname,
3410 char_u *sfname,
3411 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 buf_T *buf;
3414
Bram Moolenaarc667da52019-11-30 20:52:27 +01003415 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003417 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 curwin->w_alt_fnum = buf->b_fnum;
3419 return buf;
3420}
3421
3422/*
3423 * Get alternate file name for current window.
3424 * Return NULL if there isn't any, and give error message if requested.
3425 */
3426 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003427getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 char_u *fname;
3431 linenr_T dummy;
3432
3433 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3434 {
3435 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003436 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return NULL;
3438 }
3439 return fname;
3440}
3441
3442/*
3443 * Add a file name to the buflist and return its number.
3444 * Uses same flags as buflist_new(), except BLN_DUMMY.
3445 *
3446 * used by qf_init(), main() and doarglist()
3447 */
3448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 buf_T *buf;
3452
3453 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3454 if (buf != NULL)
3455 return buf->b_fnum;
3456 return 0;
3457}
3458
3459#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3460/*
3461 * Adjust slashes in file names. Called after 'shellslash' was set.
3462 */
3463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003464buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
3466 buf_T *bp;
3467
Bram Moolenaar29323592016-07-24 22:04:11 +02003468 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 if (bp->b_ffname != NULL)
3471 slash_adjust(bp->b_ffname);
3472 if (bp->b_sfname != NULL)
3473 slash_adjust(bp->b_sfname);
3474 }
3475}
3476#endif
3477
3478/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003479 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 * Also save the local window option values.
3481 */
3482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003483buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003485 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486}
3487
3488/*
3489 * Return TRUE if 'ffname' is not the same file as current file.
3490 * Fname must have a full path (expanded by mch_FullName()).
3491 */
3492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 return otherfile_buf(curbuf, ffname
3496#ifdef UNIX
3497 , NULL
3498#endif
3499 );
3500}
3501
3502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003503otherfile_buf(
3504 buf_T *buf,
3505 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003507 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003509 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003511 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3513 return TRUE;
3514 if (fnamecmp(ffname, buf->b_ffname) == 0)
3515 return FALSE;
3516#ifdef UNIX
3517 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003518 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaarc667da52019-11-30 20:52:27 +01003520 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (stp == NULL)
3522 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003523 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 st.st_dev = (dev_T)-1;
3525 stp = &st;
3526 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003527 // Use dev/ino to check if the files are the same, even when the names
3528 // are different (possible with links). Still need to compare the
3529 // name above, for when the file doesn't exist yet.
3530 // Problem: The dev/ino changes when a file is deleted (and created
3531 // again) and remains the same when renamed/moved. We don't want to
3532 // mch_stat() each buffer each time, that would be too slow. Get the
3533 // dev/ino again when they appear to match, but not when they appear
3534 // to be different: Could skip a buffer when it's actually the same
3535 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (buf_same_ino(buf, stp))
3537 {
3538 buf_setino(buf);
3539 if (buf_same_ino(buf, stp))
3540 return FALSE;
3541 }
3542 }
3543#endif
3544 return TRUE;
3545}
3546
3547#if defined(UNIX) || defined(PROTO)
3548/*
3549 * Set inode and device number for a buffer.
3550 * Must always be called when b_fname is changed!.
3551 */
3552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003555 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3558 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003559 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 buf->b_dev = st.st_dev;
3561 buf->b_ino = st.st_ino;
3562 }
3563 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003564 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567/*
3568 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3569 */
3570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571buf_same_ino(
3572 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003573 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003575 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 && stp->st_dev == buf->b_dev
3577 && stp->st_ino == buf->b_ino);
3578}
3579#endif
3580
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003581/*
3582 * Print info about the current buffer.
3583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003586 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587 int shorthelp,
3588 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
3590 char_u *name;
3591 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003592 char *p;
3593 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003594 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003596 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (buffer == NULL)
3598 return;
3599
Bram Moolenaarc667da52019-11-30 20:52:27 +01003600 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003602 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 p = buffer + STRLEN(buffer);
3604 }
3605 else
3606 p = buffer;
3607
3608 *p++ = '"';
3609 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003610 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 else
3612 {
3613 if (!fullname && curbuf->b_fname != NULL)
3614 name = curbuf->b_fname;
3615 else
3616 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003617 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 (int)(IOSIZE - (p - buffer)), TRUE);
3619 }
3620
Bram Moolenaar32526b32019-01-19 17:43:09 +01003621 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 curbufIsChanged() ? (shortmess(SHM_MOD)
3623 ? " [+]" : _(" [Modified]")) : " ",
3624 (curbuf->b_flags & BF_NOTEDITED)
3625#ifdef FEAT_QUICKFIX
3626 && !bt_dontwrite(curbuf)
3627#endif
3628 ? _("[Not edited]") : "",
3629 (curbuf->b_flags & BF_NEW)
3630#ifdef FEAT_QUICKFIX
3631 && !bt_dontwrite(curbuf)
3632#endif
3633 ? _("[New file]") : "",
3634 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003635 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 : _("[readonly]")) : "",
3637 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3638 || curbuf->b_p_ro) ?
3639 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003640 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3641 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (curwin->w_cursor.lnum > 1000000L)
3643 n = (int)(((long)curwin->w_cursor.lnum) /
3644 ((long)curbuf->b_ml.ml_line_count / 100L));
3645 else
3646 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3647 (long)curbuf->b_ml.ml_line_count);
3648 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003649 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#ifdef FEAT_CMDL_INFO
3651 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003652 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003653 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003654 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3655 curbuf->b_ml.ml_line_count),
3656 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657#endif
3658 else
3659 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003660 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 _("line %ld of %ld --%d%%-- col "),
3662 (long)curwin->w_cursor.lnum,
3663 (long)curbuf->b_ml.ml_line_count,
3664 n);
3665 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003666 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003667 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3669 }
3670
Bram Moolenaar32526b32019-01-19 17:43:09 +01003671 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3672 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 if (dont_truncate)
3675 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003676 // Temporarily set msg_scroll to avoid the message being truncated.
3677 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 msg_start();
3679 n = msg_scroll;
3680 msg_scroll = TRUE;
3681 msg(buffer);
3682 msg_scroll = n;
3683 }
3684 else
3685 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003686 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003688 // Need to repeat the message after redrawing when:
3689 // - When restart_edit is set (otherwise there will be a delay
3690 // before redrawing).
3691 // - When the screen was scrolled but there is no wait-return
3692 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003693 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695
3696 vim_free(buffer);
3697}
3698
3699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003700col_print(
3701 char_u *buf,
3702 size_t buflen,
3703 int col,
3704 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003707 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003709 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710}
3711
3712#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713static char_u *lasttitle = NULL;
3714static char_u *lasticon = NULL;
3715
Bram Moolenaar84a93082018-06-16 22:58:15 +02003716/*
3717 * Put the file name in the title bar and icon of the window.
3718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003723 char_u *title_str = NULL;
3724 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 int maxlen = 0;
3726 int len;
3727 int mustset;
3728 char_u buf[IOSIZE];
3729 int off;
3730
3731 if (!redrawing())
3732 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003733 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 need_maketitle = TRUE;
3735 return;
3736 }
3737
3738 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003739 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003740 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 if (p_title)
3743 {
3744 if (p_titlelen > 0)
3745 {
3746 maxlen = p_titlelen * Columns / 100;
3747 if (maxlen < 10)
3748 maxlen = 10;
3749 }
3750
Bram Moolenaar84a93082018-06-16 22:58:15 +02003751 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (*p_titlestring != NUL)
3753 {
3754#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003755 if (stl_syntax & STL_IN_TITLE)
3756 {
3757 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003758 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003759
3760# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003761 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003762# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003763 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003764 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003765 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003766 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003767 set_string_option_direct((char_u *)"titlestring", -1,
3768 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 else
3771#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003772 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 else
3775 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003776 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaar2c666692012-09-05 13:30:40 +02003778#define SPACE_FOR_FNAME (IOSIZE - 100)
3779#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003780#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003782 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003783#ifdef FEAT_TERMINAL
3784 else if (curbuf->b_term != NULL)
3785 {
3786 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3787 SPACE_FOR_FNAME);
3788 }
3789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 else
3791 {
3792 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003793 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796
Bram Moolenaar21554412017-07-24 21:44:43 +02003797#ifdef FEAT_TERMINAL
3798 if (curbuf->b_term == NULL)
3799#endif
3800 switch (bufIsChanged(curbuf)
3801 + (curbuf->b_p_ro * 2)
3802 + (!curbuf->b_p_ma * 4))
3803 {
3804 case 1: STRCAT(buf, " +"); break;
3805 case 2: STRCAT(buf, " ="); break;
3806 case 3: STRCAT(buf, " =+"); break;
3807 case 4:
3808 case 6: STRCAT(buf, " -"); break;
3809 case 5:
3810 case 7: STRCAT(buf, " -+"); break;
3811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
Bram Moolenaar21554412017-07-24 21:44:43 +02003813 if (curbuf->b_fname != NULL
3814#ifdef FEAT_TERMINAL
3815 && curbuf->b_term == NULL
3816#endif
3817 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003819 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 off = (int)STRLEN(buf);
3821 buf[off++] = ' ';
3822 buf[off++] = '(';
3823 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003824 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003826 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (isalpha(buf[off]) && buf[off + 1] == ':')
3828 off += 2;
3829#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003830 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003831 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003833 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003834 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003835 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003836 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003840
Bram Moolenaarc667da52019-11-30 20:52:27 +01003841 // Translate unprintable chars and concatenate. Keep some
3842 // room for the server name. When there is no room (very long
3843 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003844 if (off < SPACE_FOR_DIR)
3845 {
3846 p = transstr(buf + off);
3847 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3848 vim_free(p);
3849 }
3850 else
3851 {
3852 vim_strncpy(buf + off, (char_u *)"...",
3853 (size_t)(SPACE_FOR_ARGNR - off));
3854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 STRCAT(buf, ")");
3856 }
3857
Bram Moolenaar2c666692012-09-05 13:30:40 +02003858 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
3860#if defined(FEAT_CLIENTSERVER)
3861 if (serverName != NULL)
3862 {
3863 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003864 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 else
3867#endif
3868 STRCAT(buf, " - VIM");
3869
3870 if (maxlen > 0)
3871 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003872 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003873 if (vim_strsize(buf) > maxlen)
3874 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 }
3877 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003878 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 if (p_icon)
3881 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003882 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (*p_iconstring != NUL)
3884 {
3885#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003886 if (stl_syntax & STL_IN_ICON)
3887 {
3888 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003889 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003890
3891# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003892 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003893# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003894 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003895 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003896 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003897 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003898 set_string_option_direct((char_u *)"iconstring", -1,
3899 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
3902#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003903 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 else
3906 {
3907 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003908 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003909 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003910 p = gettail(curbuf->b_ffname);
3911 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003912 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003913 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (len > 100)
3915 {
3916 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003918 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003919 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003921 STRCPY(icon_str, p);
3922 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924 }
3925
Bram Moolenaar84a93082018-06-16 22:58:15 +02003926 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (mustset)
3929 resettitle();
3930}
3931
3932/*
3933 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3934 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003935 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
3937 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003938value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
3940 if ((str == NULL) != (*last == NULL)
3941 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3942 {
3943 vim_free(*last);
3944 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003947 mch_restore_title(
3948 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003953 return TRUE;
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 return FALSE;
3957}
3958
3959/*
3960 * Put current window title back (used after calling a shell)
3961 */
3962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003963resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 mch_settitle(lasttitle, lasticon);
3966}
Bram Moolenaarea408852005-06-25 22:49:46 +00003967
3968# if defined(EXITFREE) || defined(PROTO)
3969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003970free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003971{
3972 vim_free(lasttitle);
3973 vim_free(lasticon);
3974}
3975# endif
3976
Bram Moolenaarc667da52019-11-30 20:52:27 +01003977#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003979#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003981 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 * Return length of string in screen cells.
3983 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003984 * Normally works for window "wp", except when working for 'tabline' then it
3985 * is "curwin".
3986 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 * Items are drawn interspersed with the text that surrounds it
3988 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3989 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3990 *
3991 * If maxwidth is not zero, the string will be filled at any middle marker
3992 * or truncated if too long, fillchar is used for all whitespace.
3993 */
3994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003995build_stl_str_hl(
3996 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003997 char_u *out, // buffer to write into != NameBuff
3998 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004000 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001 int fillchar,
4002 int maxwidth,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004003 struct stl_hlrec *hltab, // return: HL attributes (can be NULL)
4004 struct stl_hlrec *tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar10772302019-01-20 18:25:54 +01004006 linenr_T lnum;
4007 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 char_u *p;
4009 char_u *s;
4010 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004011 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004013 win_T *save_curwin;
4014 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004015 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
4017 int empty_line;
4018 colnr_T virtcol;
4019 long l;
4020 long n;
4021 int prevchar_isflag;
4022 int prevchar_isitem;
4023 int itemisflag;
4024 int fillable;
4025 char_u *str;
4026 long num;
4027 int width;
4028 int itemcnt;
4029 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004030 int group_end_userhl;
4031 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int groupitem[STL_MAX_ITEM];
4033 int groupdepth;
4034 struct stl_item
4035 {
4036 char_u *start;
4037 int minwid;
4038 int maxwid;
4039 enum
4040 {
4041 Normal,
4042 Empty,
4043 Group,
4044 Middle,
4045 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004046 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 Trunc
4048 } type;
4049 } item[STL_MAX_ITEM];
4050 int minwid;
4051 int maxwid;
4052 int zeropad;
4053 char_u base;
4054 char_u opt;
4055#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004056 char_u buf_tmp[TMPLEN];
4057 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004058 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004059 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004060 int save_must_redraw = must_redraw;
4061 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004062
4063#ifdef FEAT_EVAL
4064 /*
4065 * When the format starts with "%!" then evaluate it as an expression and
4066 * use the result as the actual format string.
4067 */
4068 if (fmt[0] == '%' && fmt[1] == '!')
4069 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004070 typval_T tv;
4071
4072 tv.v_type = VAR_NUMBER;
4073 tv.vval.v_number = wp->w_id;
4074 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4075
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004076 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
4077 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004078 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004079
4080 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004081 }
4082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 if (fillchar == 0)
4085 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004086 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087 else if (mb_char2len(fillchar) > 1)
4088 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004089
Bram Moolenaar10772302019-01-20 18:25:54 +01004090 // The cursor in windows other than the current one isn't always
4091 // up-to-date, esp. because of autocommands and timers.
4092 lnum = wp->w_cursor.lnum;
4093 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4094 {
4095 lnum = wp->w_buffer->b_ml.ml_line_count;
4096 wp->w_cursor.lnum = lnum;
4097 }
4098
4099 // Get line & check if empty (cursorpos will show "0-1"). Note that
4100 // p will become invalid when getting another buffer line.
4101 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004102 empty_line = (*p == NUL);
4103
Bram Moolenaar10772302019-01-20 18:25:54 +01004104 // Get the byte value now, in case we need it below. This is more efficient
4105 // than making a copy of the line.
4106 len = STRLEN(p);
4107 if (wp->w_cursor.col > (colnr_T)len)
4108 {
4109 // Line may have changed since checking the cursor column, or the lnum
4110 // was adjusted above.
4111 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004112 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004113 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004114 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004115 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004116 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 groupdepth = 0;
4119 p = out;
4120 curitem = 0;
4121 prevchar_isflag = TRUE;
4122 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004123 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004125 if (curitem == STL_MAX_ITEM)
4126 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004127 // There are too many items. Add the error code to the statusline
4128 // to give the user a hint about what went wrong.
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004129 if (p + 6 < out + outlen)
4130 {
4131 mch_memmove(p, " E541", (size_t)5);
4132 p += 5;
4133 }
4134 break;
4135 }
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (*s != NUL && *s != '%')
4138 prevchar_isflag = prevchar_isitem = FALSE;
4139
4140 /*
4141 * Handle up to the next '%' or the end.
4142 */
4143 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4144 *p++ = *s++;
4145 if (*s == NUL || p + 1 >= out + outlen)
4146 break;
4147
4148 /*
4149 * Handle one '%' item.
4150 */
4151 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004152 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (*s == '%')
4155 {
4156 if (p + 1 >= out + outlen)
4157 break;
4158 *p++ = *s++;
4159 prevchar_isflag = prevchar_isitem = FALSE;
4160 continue;
4161 }
4162 if (*s == STL_MIDDLEMARK)
4163 {
4164 s++;
4165 if (groupdepth > 0)
4166 continue;
4167 item[curitem].type = Middle;
4168 item[curitem++].start = p;
4169 continue;
4170 }
4171 if (*s == STL_TRUNCMARK)
4172 {
4173 s++;
4174 item[curitem].type = Trunc;
4175 item[curitem++].start = p;
4176 continue;
4177 }
4178 if (*s == ')')
4179 {
4180 s++;
4181 if (groupdepth < 1)
4182 continue;
4183 groupdepth--;
4184
4185 t = item[groupitem[groupdepth]].start;
4186 *p = NUL;
4187 l = vim_strsize(t);
4188 if (curitem > groupitem[groupdepth] + 1
4189 && item[groupitem[groupdepth]].minwid == 0)
4190 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004191 // remove group if all items are empty and highlight group
4192 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004193 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004194 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4195 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004196 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004197 {
4198 group_start_userhl = group_end_userhl = item[n].minwid;
4199 break;
4200 }
4201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004203 {
4204 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004206 if (item[n].type == Highlight)
4207 group_end_userhl = item[n].minwid;
4208 }
4209 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 p = t;
4212 l = 0;
4213 }
4214 }
4215 if (l > item[groupitem[groupdepth]].maxwid)
4216 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004217 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (has_mbyte)
4219 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004220 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 n = 0;
4222 while (l >= item[groupitem[groupdepth]].maxwid)
4223 {
4224 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004225 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 }
4228 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004229 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004232 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004234
4235 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 while (++l < item[groupitem[groupdepth]].minwid)
4237 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaarc667da52019-11-30 20:52:27 +01004239 // correct the start of the items for the truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4241 {
4242 item[l].start -= n;
4243 if (item[l].start < t)
4244 item[l].start = t;
4245 }
4246 }
4247 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4248 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004249 // fill
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 n = item[groupitem[groupdepth]].minwid;
4251 if (n < 0)
4252 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004253 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 n = 0 - n;
4255 while (l++ < n && p + 1 < out + outlen)
4256 *p++ = fillchar;
4257 }
4258 else
4259 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004260 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004261 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 l = n - l;
4263 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004264 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 p += l;
4266 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4267 item[n].start += l;
4268 for ( ; l > 0; l--)
4269 *t++ = fillchar;
4270 }
4271 }
4272 continue;
4273 }
4274 minwid = 0;
4275 maxwid = 9999;
4276 zeropad = FALSE;
4277 l = 1;
4278 if (*s == '0')
4279 {
4280 s++;
4281 zeropad = TRUE;
4282 }
4283 if (*s == '-')
4284 {
4285 s++;
4286 l = -1;
4287 }
4288 if (VIM_ISDIGIT(*s))
4289 {
4290 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004291 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 minwid = 0;
4293 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004294 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 item[curitem].type = Highlight;
4297 item[curitem].start = p;
4298 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4299 s++;
4300 curitem++;
4301 continue;
4302 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004303 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4304 {
4305 if (*s == STL_TABCLOSENR)
4306 {
4307 if (minwid == 0)
4308 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004309 // %X ends the close label, go back to the previously
4310 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004311 for (n = curitem - 1; n >= 0; --n)
4312 if (item[n].type == TabPage && item[n].minwid >= 0)
4313 {
4314 minwid = item[n].minwid;
4315 break;
4316 }
4317 }
4318 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004319 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004320 minwid = - minwid;
4321 }
4322 item[curitem].type = TabPage;
4323 item[curitem].start = p;
4324 item[curitem].minwid = minwid;
4325 s++;
4326 curitem++;
4327 continue;
4328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (*s == '.')
4330 {
4331 s++;
4332 if (VIM_ISDIGIT(*s))
4333 {
4334 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004335 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 maxwid = 50;
4337 }
4338 }
4339 minwid = (minwid > 50 ? 50 : minwid) * l;
4340 if (*s == '(')
4341 {
4342 groupitem[groupdepth++] = curitem;
4343 item[curitem].type = Group;
4344 item[curitem].start = p;
4345 item[curitem].minwid = minwid;
4346 item[curitem].maxwid = maxwid;
4347 s++;
4348 curitem++;
4349 continue;
4350 }
4351 if (vim_strchr(STL_ALL, *s) == NULL)
4352 {
4353 s++;
4354 continue;
4355 }
4356 opt = *s++;
4357
Bram Moolenaarc667da52019-11-30 20:52:27 +01004358 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 base = 'D';
4360 itemisflag = FALSE;
4361 fillable = TRUE;
4362 num = -1;
4363 str = NULL;
4364 switch (opt)
4365 {
4366 case STL_FILEPATH:
4367 case STL_FULLPATH:
4368 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004369 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004371 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 else
4373 {
4374 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004375 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4377 }
4378 trans_characters(NameBuff, MAXPATHL);
4379 if (opt != STL_FILENAME)
4380 str = NameBuff;
4381 else
4382 str = gettail(NameBuff);
4383 break;
4384
Bram Moolenaarc667da52019-11-30 20:52:27 +01004385 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 itemisflag = TRUE;
4387 t = p;
4388 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4389 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004390 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 break;
4392 s++;
4393 *p = 0;
4394 p = t;
4395
4396#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004397 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4398 "%d", curbuf->b_fnum);
4399 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4400 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4401 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004403 save_curbuf = curbuf;
4404 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004405 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 curwin = wp;
4407 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004408 // Visual mode is only valid in the current window.
4409 if (curwin != save_curwin)
4410 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004412 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004414 curwin = save_curwin;
4415 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004416 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004417 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004418 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420 if (str != NULL && *str != 0)
4421 {
4422 if (*skipdigits(str) == NUL)
4423 {
4424 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004425 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 itemisflag = FALSE;
4427 }
4428 }
4429#endif
4430 break;
4431
4432 case STL_LINE:
4433 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4434 ? 0L : (long)(wp->w_cursor.lnum);
4435 break;
4436
4437 case STL_NUMLINES:
4438 num = wp->w_buffer->b_ml.ml_line_count;
4439 break;
4440
4441 case STL_COLUMN:
4442 num = !(State & INSERT) && empty_line
4443 ? 0 : (int)wp->w_cursor.col + 1;
4444 break;
4445
4446 case STL_VIRTCOL:
4447 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004448 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 virtcol = wp->w_virtcol;
4450 if (wp->w_p_list && lcs_tab1 == NUL)
4451 {
4452 wp->w_p_list = FALSE;
4453 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4454 wp->w_p_list = TRUE;
4455 }
4456 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004457 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if (opt == STL_VIRTCOL_ALT
4459 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4460 ? 0 : (int)wp->w_cursor.col + 1)))
4461 break;
4462 num = (long)virtcol;
4463 break;
4464
4465 case STL_PERCENTAGE:
4466 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4467 (long)wp->w_buffer->b_ml.ml_line_count);
4468 break;
4469
4470 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004471 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004472 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 break;
4474
4475 case STL_ARGLISTSTAT:
4476 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004477 buf_tmp[0] = 0;
4478 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4479 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 break;
4481
4482 case STL_KEYMAP:
4483 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004484 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4485 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004488#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004489 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490#else
4491 num = 0;
4492#endif
4493 break;
4494
4495 case STL_BUFNO:
4496 num = wp->w_buffer->b_fnum;
4497 break;
4498
4499 case STL_OFFSET_X:
4500 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004501 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 case STL_OFFSET:
4503#ifdef FEAT_BYTEOFF
4504 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4505 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4506 0L : l + 1 + (!(State & INSERT) && empty_line ?
4507 0 : (int)wp->w_cursor.col);
4508#endif
4509 break;
4510
4511 case STL_BYTEVAL_X:
4512 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004513 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004515 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 if (num == NL)
4517 num = 0;
4518 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4519 num = NL;
4520 break;
4521
4522 case STL_ROFLAG:
4523 case STL_ROFLAG_ALT:
4524 itemisflag = TRUE;
4525 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004526 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 break;
4528
4529 case STL_HELPFLAG:
4530 case STL_HELPFLAG_ALT:
4531 itemisflag = TRUE;
4532 if (wp->w_buffer->b_help)
4533 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004534 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 break;
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 case STL_FILETYPE:
4538 if (*wp->w_buffer->b_p_ft != NUL
4539 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4540 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004541 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004542 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004543 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 break;
4546
4547 case STL_FILETYPE_ALT:
4548 itemisflag = TRUE;
4549 if (*wp->w_buffer->b_p_ft != NUL
4550 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4551 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004552 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004553 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004554 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004556 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
Bram Moolenaar4033c552017-09-16 20:54:51 +02004560#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 case STL_PREVIEWFLAG:
4562 case STL_PREVIEWFLAG_ALT:
4563 itemisflag = TRUE;
4564 if (wp->w_p_pvw)
4565 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4566 : _("[Preview]"));
4567 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004568
4569 case STL_QUICKFIX:
4570 if (bt_quickfix(wp->w_buffer))
4571 str = (char_u *)(wp->w_llist_ref
4572 ? _(msg_loclist)
4573 : _(msg_qflist));
4574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#endif
4576
4577 case STL_MODIFIED:
4578 case STL_MODIFIED_ALT:
4579 itemisflag = TRUE;
4580 switch ((opt == STL_MODIFIED_ALT)
4581 + bufIsChanged(wp->w_buffer) * 2
4582 + (!wp->w_buffer->b_p_ma) * 4)
4583 {
4584 case 2: str = (char_u *)"[+]"; break;
4585 case 3: str = (char_u *)",+"; break;
4586 case 4: str = (char_u *)"[-]"; break;
4587 case 5: str = (char_u *)",-"; break;
4588 case 6: str = (char_u *)"[+-]"; break;
4589 case 7: str = (char_u *)",+-"; break;
4590 }
4591 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004592
4593 case STL_HIGHLIGHT:
4594 t = s;
4595 while (*s != '#' && *s != NUL)
4596 ++s;
4597 if (*s == '#')
4598 {
4599 item[curitem].type = Highlight;
4600 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004601 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004602 curitem++;
4603 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004604 if (*s != NUL)
4605 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004606 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
4609 item[curitem].start = p;
4610 item[curitem].type = Normal;
4611 if (str != NULL && *str)
4612 {
4613 t = str;
4614 if (itemisflag)
4615 {
4616 if ((t[0] && t[1])
4617 && ((!prevchar_isitem && *t == ',')
4618 || (prevchar_isflag && *t == ' ')))
4619 t++;
4620 prevchar_isflag = TRUE;
4621 }
4622 l = vim_strsize(t);
4623 if (l > 0)
4624 prevchar_isitem = TRUE;
4625 if (l > maxwid)
4626 {
4627 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (has_mbyte)
4629 {
4630 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004631 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 l -= byte2cells(*t++);
4635 if (p + 1 >= out + outlen)
4636 break;
4637 *p++ = '<';
4638 }
4639 if (minwid > 0)
4640 {
4641 for (; l < minwid && p + 1 < out + outlen; l++)
4642 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004643 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4645 *p++ = ' ';
4646 else
4647 *p++ = fillchar;
4648 }
4649 minwid = 0;
4650 }
4651 else
4652 minwid *= -1;
4653 while (*t && p + 1 < out + outlen)
4654 {
4655 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004656 // Change a space by fillchar, unless fillchar is '-' and a
4657 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (fillable && p[-1] == ' '
4659 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4660 p[-1] = fillchar;
4661 }
4662 for (; l < minwid && p + 1 < out + outlen; l++)
4663 *p++ = fillchar;
4664 }
4665 else if (num >= 0)
4666 {
4667 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4668 char_u nstr[20];
4669
4670 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004671 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 prevchar_isitem = TRUE;
4673 t = nstr;
4674 if (opt == STL_VIRTCOL_ALT)
4675 {
4676 *t++ = '-';
4677 minwid--;
4678 }
4679 *t++ = '%';
4680 if (zeropad)
4681 *t++ = '0';
4682 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004683 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 *t = 0;
4685
4686 for (n = num, l = 1; n >= nbase; n /= nbase)
4687 l++;
4688 if (opt == STL_VIRTCOL_ALT)
4689 l++;
4690 if (l > maxwid)
4691 {
4692 l += 2;
4693 n = l - maxwid;
4694 while (l-- > maxwid)
4695 num /= nbase;
4696 *t++ = '>';
4697 *t++ = '%';
4698 *t = t[-3];
4699 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004700 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4701 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004704 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4705 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 p += STRLEN(p);
4707 }
4708 else
4709 item[curitem].type = Empty;
4710
4711 if (opt == STL_VIM_EXPR)
4712 vim_free(str);
4713
4714 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004715 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 curitem++;
4717 }
4718 *p = NUL;
4719 itemcnt = curitem;
4720
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004721#ifdef FEAT_EVAL
4722 if (usefmt != fmt)
4723 vim_free(usefmt);
4724#endif
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 width = vim_strsize(out);
4727 if (maxwidth > 0 && width > maxwidth)
4728 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004729 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 l = 0;
4731 if (itemcnt == 0)
4732 s = out;
4733 else
4734 {
4735 for ( ; l < itemcnt; l++)
4736 if (item[l].type == Trunc)
4737 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004738 // Truncate at %< item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 s = item[l].start;
4740 break;
4741 }
4742 if (l == itemcnt)
4743 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004744 // No %< item, truncate first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 s = item[0].start;
4746 l = 0;
4747 }
4748 }
4749
4750 if (width - vim_strsize(s) >= maxwidth)
4751 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004752 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 if (has_mbyte)
4754 {
4755 s = out;
4756 width = 0;
4757 for (;;)
4758 {
4759 width += ptr2cells(s);
4760 if (width >= maxwidth)
4761 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004762 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004764 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 while (++width < maxwidth)
4766 *s++ = fillchar;
4767 }
4768 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 s = out + maxwidth - 1;
4770 for (l = 0; l < itemcnt; l++)
4771 if (item[l].start > s)
4772 break;
4773 itemcnt = l;
4774 *s++ = '>';
4775 *s = 0;
4776 }
4777 else
4778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (has_mbyte)
4780 {
4781 n = 0;
4782 while (width >= maxwidth)
4783 {
4784 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004785 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 }
4788 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 n = width - maxwidth + 1;
4790 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004791 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 *s = '<';
4793
Bram Moolenaarc667da52019-11-30 20:52:27 +01004794 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 while (++width < maxwidth)
4796 {
4797 s = s + STRLEN(s);
4798 *s++ = fillchar;
4799 *s = NUL;
4800 }
4801
Bram Moolenaarc667da52019-11-30 20:52:27 +01004802 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 for (; l < itemcnt; l++)
4804 {
4805 if (item[l].start - n >= s)
4806 item[l].start -= n;
4807 else
4808 item[l].start = s;
4809 }
4810 }
4811 width = maxwidth;
4812 }
4813 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4814 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004815 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 for (l = 0; l < itemcnt; l++)
4817 if (item[l].type == Middle)
4818 break;
4819 if (l < itemcnt)
4820 {
4821 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004822 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 for (s = item[l].start; s < p; s++)
4824 *s = fillchar;
4825 for (l++; l < itemcnt; l++)
4826 item[l].start += maxwidth - width;
4827 width = maxwidth;
4828 }
4829 }
4830
Bram Moolenaarc667da52019-11-30 20:52:27 +01004831 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004832 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004834 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 for (l = 0; l < itemcnt; l++)
4836 {
4837 if (item[l].type == Highlight)
4838 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004839 sp->start = item[l].start;
4840 sp->userhl = item[l].minwid;
4841 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004844 sp->start = NULL;
4845 sp->userhl = 0;
4846 }
4847
Bram Moolenaarc667da52019-11-30 20:52:27 +01004848 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004849 if (tabtab != NULL)
4850 {
4851 sp = tabtab;
4852 for (l = 0; l < itemcnt; l++)
4853 {
4854 if (item[l].type == TabPage)
4855 {
4856 sp->start = item[l].start;
4857 sp->userhl = item[l].minwid;
4858 sp++;
4859 }
4860 }
4861 sp->start = NULL;
4862 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864
Bram Moolenaarc667da52019-11-30 20:52:27 +01004865 // When inside update_screen we do not want redrawing a stausline, ruler,
4866 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004867 if (updating_screen)
4868 {
4869 must_redraw = save_must_redraw;
4870 curwin->w_redr_type = save_redr_type;
4871 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 return width;
4874}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004875#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004877#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4878 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004880 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4881 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 */
4883 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004884get_rel_pos(
4885 win_T *wp,
4886 char_u *buf,
4887 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004889 long above; // number of lines above window
4890 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
Bram Moolenaarc667da52019-11-30 20:52:27 +01004892 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004893 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 above = wp->w_topline - 1;
4895#ifdef FEAT_DIFF
4896 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004897 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004898 above = 0; // All buffer lines are displayed and there is an
4899 // indication of filler lines, that can be considered
4900 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
4902 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4903 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004904 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4905 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004907 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004909 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 ? (int)(above / ((above + below) / 100L))
4911 : (int)(above * 100L / (above + below)));
4912}
4913#endif
4914
4915/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004916 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 * Return TRUE if it was appended.
4918 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004920append_arg_number(
4921 win_T *wp,
4922 char_u *buf,
4923 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004924 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 char_u *p;
4927
Bram Moolenaarc667da52019-11-30 20:52:27 +01004928 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return FALSE;
4930
Bram Moolenaarc667da52019-11-30 20:52:27 +01004931 p = buf + STRLEN(buf); // go to the end of the buffer
4932 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 return FALSE;
4934 *p++ = ' ';
4935 *p++ = '(';
4936 if (add_file)
4937 {
4938 STRCPY(p, "file ");
4939 p += 5;
4940 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004941 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4942 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4944 return TRUE;
4945}
4946
4947/*
4948 * If fname is not a full path, make it a full path.
4949 * Returns pointer to allocated memory (NULL for failure).
4950 */
4951 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004952fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 /*
4955 * Force expanding the path always for Unix, because symbolic links may
4956 * mess up the full path name, even though it starts with a '/'.
4957 * Also expand when there is ".." in the file name, try to remove it,
4958 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004959 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 * For MS-Windows also expand names like "longna~1" to "longname".
4961 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004962#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return FullName_save(fname, TRUE);
4964#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004965 if (!vim_isAbsName(fname)
4966 || strstr((char *)fname, "..") != NULL
4967 || strstr((char *)fname, "//") != NULL
4968# ifdef BACKSLASH_IN_FILENAME
4969 || strstr((char *)fname, "\\\\") != NULL
4970# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004971# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 )
4975 return FullName_save(fname, FALSE);
4976
4977 fname = vim_strsave(fname);
4978
Bram Moolenaar9b942202007-10-03 12:31:33 +00004979# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004980 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004981 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00004982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
4984 return fname;
4985#endif
4986}
4987
4988/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004989 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4990 * "*ffname" becomes a pointer to allocated memory (or NULL).
4991 * When resolving a link both "*sfname" and "*ffname" will point to the same
4992 * allocated memory.
4993 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004994 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004997fname_expand(
4998 buf_T *buf UNUSED,
4999 char_u **ffname,
5000 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005002 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005004 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005006 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008#ifdef FEAT_SHORTCUT
5009 if (!buf->b_p_bin)
5010 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005011 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005013 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005014 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005015 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 vim_free(*ffname);
5018 *ffname = rfname;
5019 *sfname = rfname;
5020 }
5021 }
5022#endif
5023}
5024
5025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 * Open a window for a number of buffers.
5027 */
5028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005029ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 buf_T *buf;
5032 win_T *wp, *wpnext;
5033 int split_ret = OK;
5034 int p_ea_save;
5035 int open_wins = 0;
5036 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005037 int count; // Maximum number of windows to open.
5038 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005039 int had_tab = cmdmod.tab;
5040 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaarc667da52019-11-30 20:52:27 +01005042 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 count = 9999;
5044 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005045 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5047 all = FALSE;
5048 else
5049 all = TRUE;
5050
5051 setpcmark();
5052
5053#ifdef FEAT_GUI
5054 need_mouse_correct = TRUE;
5055#endif
5056
5057 /*
5058 * Close superfluous windows (two windows for the same buffer).
5059 * Also close windows that are not full-width.
5060 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005061 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005062 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005063 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005065 tpnext = curtab->tp_next;
5066 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005068 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005069 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005070 || ((cmdmod.split & WSP_VERT)
5071 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005072 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005073 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005074 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005075 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005076 {
5077 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005078 wpnext = firstwin; // just in case an autocommand does
5079 // something strange with windows
5080 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005081 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005082 }
5083 else
5084 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005086
Bram Moolenaarc667da52019-11-30 20:52:27 +01005087 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005088 if (had_tab == 0 || tpnext == NULL)
5089 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005090 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092
5093 /*
5094 * Go through the buffer list. When a buffer doesn't have a window yet,
5095 * open one. Otherwise move the window to the right position.
5096 * Watch out for autocommands that delete buffers or windows!
5097 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005098 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5103 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005104 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5106 continue;
5107
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005108 if (had_tab != 0)
5109 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005110 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005111 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005112 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005113 else
5114 wp = NULL;
5115 }
5116 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005117 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005118 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005119 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005120 if (wp->w_buffer == buf)
5121 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005122 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005123 if (wp != NULL)
5124 win_move_after(wp, curwin);
5125 }
5126
5127 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005129 bufref_T bufref;
5130
5131 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005132
Bram Moolenaarc667da52019-11-30 20:52:27 +01005133 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005135 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5137 ++open_wins;
5138 p_ea = p_ea_save;
5139 if (split_ret == FAIL)
5140 continue;
5141
Bram Moolenaarc667da52019-11-30 20:52:27 +01005142 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005145 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005147 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (swap_exists_action == SEA_QUIT)
5152 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005153#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005154 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005155
Bram Moolenaarc667da52019-11-30 20:52:27 +01005156 // Reset the error/interrupt/exception state here so that
5157 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005158 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005159#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005160
Bram Moolenaarc667da52019-11-30 20:52:27 +01005161 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 win_close(curwin, TRUE);
5163 --open_wins;
5164 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005165 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005166
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005167#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005168 // Restore the error/interrupt/exception state if not
5169 // discarded by a new aborting error, interrupt, or uncaught
5170 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005171 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174 else
5175 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177
5178 ui_breakcheck();
5179 if (got_int)
5180 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005181 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005184#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005185 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005186 if (aborting())
5187 break;
5188#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005189 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5191 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005194 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197 /*
5198 * Close superfluous windows.
5199 */
5200 for (wp = lastwin; open_wins > count; )
5201 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005202 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (!win_valid(wp))
5205 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005206 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 wp = lastwin;
5208 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005209 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005211 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 --open_wins;
5213 wp = lastwin;
5214 }
5215 else
5216 {
5217 wp = wp->w_prev;
5218 if (wp == NULL)
5219 break;
5220 }
5221 }
5222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005225static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227/*
5228 * do_modelines() - process mode lines for the current file
5229 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005230 * "flags" can be:
5231 * OPT_WINONLY only set options local to window
5232 * OPT_NOWIN don't set options local to window
5233 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 * Returns immediately if the "ml" option isn't set.
5235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005237do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005239 linenr_T lnum;
5240 int nmlines;
5241 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5244 return;
5245
Bram Moolenaarc667da52019-11-30 20:52:27 +01005246 // Disallow recursive entry here. Can happen when executing a modeline
5247 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 if (entered)
5249 return;
5250
5251 ++entered;
5252 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5253 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005254 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 nmlines = 0;
5256
5257 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5258 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005259 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 nmlines = 0;
5261 --entered;
5262}
5263
Bram Moolenaarc667da52019-11-30 20:52:27 +01005264#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266/*
5267 * chk_modeline() - check a single line for a mode string
5268 * Return FAIL if an error encountered.
5269 */
5270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005271chk_modeline(
5272 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005273 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *s;
5276 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005277 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 int prev;
5279 int vers;
5280 int end;
5281 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005283 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284#endif
5285
5286 prev = -1;
5287 for (s = ml_get(lnum); *s != NUL; ++s)
5288 {
5289 if (prev == -1 || vim_isspace(prev))
5290 {
5291 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5292 || STRNCMP(s, "vi:", (size_t)3) == 0)
5293 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005294 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005295 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
5297 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5298 e = s + 4;
5299 else
5300 e = s + 3;
5301 vers = getdigits(&e);
5302 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005303 && (s[0] != 'V'
5304 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 && (s[3] == ':'
5306 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5307 || (VIM_VERSION_100 < vers && s[3] == '<')
5308 || (VIM_VERSION_100 > vers && s[3] == '>')
5309 || (VIM_VERSION_100 == vers && s[3] == '=')))
5310 break;
5311 }
5312 }
5313 prev = *s;
5314 }
5315
5316 if (*s)
5317 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005318 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 ++s;
5320 while (s[-1] != ':');
5321
Bram Moolenaarc667da52019-11-30 20:52:27 +01005322 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (linecopy == NULL)
5324 return FAIL;
5325
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005326 // prepare for emsg()
5327 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 end = FALSE;
5330 while (end == FALSE)
5331 {
5332 s = skipwhite(s);
5333 if (*s == NUL)
5334 break;
5335
5336 /*
5337 * Find end of set command: ':' or end of line.
5338 * Skip over "\:", replacing it with ":".
5339 */
5340 for (e = s; *e != ':' && *e != NUL; ++e)
5341 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005342 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (*e == NUL)
5344 end = TRUE;
5345
5346 /*
5347 * If there is a "set" command, require a terminating ':' and
5348 * ignore the stuff after the ':'.
5349 * "vi:set opt opt opt: foo" -- foo not interpreted
5350 * "vi:opt opt opt: foo" -- foo interpreted
5351 * Accept "se" for compatibility with Elvis.
5352 */
5353 if (STRNCMP(s, "set ", (size_t)4) == 0
5354 || STRNCMP(s, "se ", (size_t)3) == 0)
5355 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005356 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
5358 end = TRUE;
5359 s = vim_strchr(s, ' ') + 1;
5360 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005361 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
Bram Moolenaarc667da52019-11-30 20:52:27 +01005363 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005365 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005367 save_current_sctx = current_sctx;
5368 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005369 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005370 current_sctx.sc_lnum = lnum;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005371 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005373 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005374 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005375
Bram Moolenaara3227e22006-03-08 21:32:40 +00005376 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005377
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005378 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005380 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005382 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 break;
5384 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005385 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005388 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 vim_free(linecopy);
5390 }
5391 return retval;
5392}
5393
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005394/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005395 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5396 */
5397 int
5398bt_normal(buf_T *buf)
5399{
5400 return buf != NULL && buf->b_p_bt[0] == NUL;
5401}
5402
Bram Moolenaar113e1072019-01-20 15:30:40 +01005403#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005404/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005405 * Return TRUE if "buf" is the quickfix buffer.
5406 */
5407 int
5408bt_quickfix(buf_T *buf)
5409{
5410 return buf != NULL && buf->b_p_bt[0] == 'q';
5411}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005412#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005413
Bram Moolenaar113e1072019-01-20 15:30:40 +01005414#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005415/*
5416 * Return TRUE if "buf" is a terminal buffer.
5417 */
5418 int
5419bt_terminal(buf_T *buf)
5420{
5421 return buf != NULL && buf->b_p_bt[0] == 't';
5422}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005423#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005424
5425/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005426 * Return TRUE if "buf" is a help buffer.
5427 */
5428 int
5429bt_help(buf_T *buf)
5430{
5431 return buf != NULL && buf->b_help;
5432}
5433
5434/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005435 * Return TRUE if "buf" is a prompt buffer.
5436 */
5437 int
5438bt_prompt(buf_T *buf)
5439{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005440 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5441}
5442
5443/*
5444 * Return TRUE if "buf" is a buffer for a popup window.
5445 */
5446 int
5447bt_popup(buf_T *buf)
5448{
5449 return buf != NULL && buf->b_p_bt != NULL
5450 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005451}
5452
5453/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005454 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5455 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005456 */
5457 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005458bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005459{
5460 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5461 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005462 || buf->b_p_bt[0] == 't'
5463 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005464}
5465
5466/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005467 * Return TRUE if "buf" has 'buftype' set to "nofile".
5468 */
5469 int
5470bt_nofile(buf_T *buf)
5471{
5472 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5473}
5474
5475/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005476 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5477 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005478 */
5479 int
5480bt_dontwrite(buf_T *buf)
5481{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005482 return buf != NULL && (buf->b_p_bt[0] == 'n'
5483 || buf->b_p_bt[0] == 't'
5484 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005485}
5486
Bram Moolenaar113e1072019-01-20 15:30:40 +01005487#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005488 int
5489bt_dontwrite_msg(buf_T *buf)
5490{
5491 if (bt_dontwrite(buf))
5492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005493 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005494 return TRUE;
5495 }
5496 return FALSE;
5497}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005498#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005499
5500/*
5501 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5502 * and 'bufhidden'.
5503 */
5504 int
5505buf_hide(buf_T *buf)
5506{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005507 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005508 switch (buf->b_p_bh[0])
5509 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005510 case 'u': // "unload"
5511 case 'w': // "wipe"
5512 case 'd': return FALSE; // "delete"
5513 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005514 }
5515 return (p_hid || cmdmod.hide);
5516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518/*
5519 * Return special buffer name.
5520 * Returns NULL when the buffer has a normal file name.
5521 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005523buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005525#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005527 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005528 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005529 * Differentiate between the quickfix and location list buffers using
5530 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005531 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005532 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005533 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005534 else
5535 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005538
Bram Moolenaarc667da52019-11-30 20:52:27 +01005539 // There is no _file_ when 'buftype' is "nofile", b_sfname
5540 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005541 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005543#ifdef FEAT_TERMINAL
5544 if (buf->b_term != NULL)
5545 return term_get_status_text(buf->b_term);
5546#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005547 if (buf->b_fname != NULL)
5548 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005549#ifdef FEAT_JOB_CHANNEL
5550 if (bt_prompt(buf))
5551 return (char_u *)_("[Prompt]");
5552#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005553#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005554 if (bt_popup(buf))
5555 return (char_u *)_("[Popup]");
5556#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005557 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005561 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return NULL;
5563}
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565/*
5566 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5567 */
5568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 if (on != curbuf->b_p_bl)
5572 {
5573 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (on)
5575 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5576 else
5577 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
5579}
5580
5581/*
5582 * Read the file for "buf" again and check if the contents changed.
5583 * Return TRUE if it changed or this could not be checked.
5584 */
5585 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005586buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 buf_T *newbuf;
5589 int differ = TRUE;
5590 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 exarg_T ea;
5593
Bram Moolenaarc667da52019-11-30 20:52:27 +01005594 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5596 if (newbuf == NULL)
5597 return TRUE;
5598
Bram Moolenaarc667da52019-11-30 20:52:27 +01005599 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (prep_exarg(&ea, buf) == FAIL)
5601 {
5602 wipe_buffer(newbuf, FALSE);
5603 return TRUE;
5604 }
5605
Bram Moolenaarc667da52019-11-30 20:52:27 +01005606 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Bram Moolenaar4770d092006-01-12 23:22:24 +00005609 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 && readfile(buf->b_ffname, buf->b_fname,
5611 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5612 &ea, READ_NEW | READ_DUMMY) == OK)
5613 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005614 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5616 {
5617 differ = FALSE;
5618 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5619 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5620 {
5621 differ = TRUE;
5622 break;
5623 }
5624 }
5625 }
5626 vim_free(ea.cmd);
5627
Bram Moolenaarc667da52019-11-30 20:52:27 +01005628 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaarc667da52019-11-30 20:52:27 +01005631 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 wipe_buffer(newbuf, FALSE);
5633
5634 return differ;
5635}
5636
5637/*
5638 * Wipe out a buffer and decrement the last buffer number if it was used for
5639 * this buffer. Call this to wipe out a temp buffer that does not contain any
5640 * marks.
5641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005643wipe_buffer(
5644 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005645 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 if (buf->b_fnum == top_file_num - 1)
5648 --top_file_num;
5649
Bram Moolenaarc667da52019-11-30 20:52:27 +01005650 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005651 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005652
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005653 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005656 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}