blob: 25620afb5dcb416d29eef3b7cd545773074c2b02 [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 Moolenaarf10997a2020-01-03 21:00:02 +0100982 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200985 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200987 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000989#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200990 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000991#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100992 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
993 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +0100994 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995}
996
997/*
998 * Free the b_wininfo list for buffer "buf".
999 */
1000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001001clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 wininfo_T *wip;
1004
1005 while (buf->b_wininfo != NULL)
1006 {
1007 wip = buf->b_wininfo;
1008 buf->b_wininfo = wip->wi_next;
1009 if (wip->wi_optset)
1010 {
1011 clear_winopt(&wip->wi_opt);
1012#ifdef FEAT_FOLDING
1013 deleteFoldRecurse(&wip->wi_folds);
1014#endif
1015 }
1016 vim_free(wip);
1017 }
1018}
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020/*
1021 * Go to another buffer. Handles the result of the ATTENTION dialog.
1022 */
1023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001024goto_buffer(
1025 exarg_T *eap,
1026 int start,
1027 int dir,
1028 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001030 bufref_T old_curbuf;
1031
1032 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1036 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1038 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001039#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001040 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001041
Bram Moolenaarc667da52019-11-30 20:52:27 +01001042 // Reset the error/interrupt/exception state here so that
1043 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001044 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001045#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001046
Bram Moolenaarc667da52019-11-30 20:52:27 +01001047 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 win_close(curwin, TRUE);
1049 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001050 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001051
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001052#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001053 // Restore the error/interrupt/exception state if not discarded by a
1054 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001055 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 }
1058 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001059 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062/*
1063 * Handle the situation of swap_exists_action being set.
1064 * It is allowed for "old_curbuf" to be NULL or invalid.
1065 */
1066 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001067handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001069#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001071#endif
1072#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001073 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001074#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001075 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (swap_exists_action == SEA_QUIT)
1078 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001079#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001080 // Reset the error/interrupt/exception state here so that
1081 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001082 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001083#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001084
Bram Moolenaarc667da52019-11-30 20:52:27 +01001085 // User selected Quit at ATTENTION prompt. Go back to previous
1086 // buffer. If that buffer is gone or the same as the current one,
1087 // open a new, empty buffer.
1088 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001089 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001090 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001091 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1092 || old_curbuf->br_buf == curbuf)
1093 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1094 else
1095 buf = old_curbuf->br_buf;
1096 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001097 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001098 int old_msg_silent = msg_silent;
1099
1100 if (shortmess(SHM_FILEINFO))
1101 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001102 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001103 // restore msg_silent, so that the command line will be shown
1104 msg_silent = old_msg_silent;
1105
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001106#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001107 if (old_tw != curbuf->b_p_tw)
1108 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001109#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001110 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001111 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001112
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001113#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001114 // Restore the error/interrupt/exception state if not discarded by a
1115 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001116 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
1119 else if (swap_exists_action == SEA_RECOVER)
1120 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001121#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001122 // Reset the error/interrupt/exception state here so that
1123 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001124 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001125#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001126
Bram Moolenaarc667da52019-11-30 20:52:27 +01001127 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001129 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001130 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001132 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001133
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001134#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001135 // Restore the error/interrupt/exception state if not discarded by a
1136 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001137 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 swap_exists_action = SEA_NONE;
1141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143/*
1144 * do_bufdel() - delete or unload buffer(s)
1145 *
1146 * addr_count == 0: ":bdel" - delete current buffer
1147 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1148 * buffer "end_bnr", then any other arguments.
1149 * addr_count == 2: ":N,N bdel" - delete buffers in range
1150 *
1151 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1152 * DOBUF_DEL (":bdel")
1153 *
1154 * Returns error message or NULL
1155 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001156 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001157do_bufdel(
1158 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001159 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001160 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001161 int start_bnr, // first buffer number in a range
1162 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001163 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001165 int do_current = 0; // delete current buffer?
1166 int deleted = 0; // number of buffers deleted
1167 char *errormsg = NULL; // return value
1168 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 char_u *p;
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (addr_count == 0)
1172 {
1173 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1174 }
1175 else
1176 {
1177 if (addr_count == 2)
1178 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001179 if (*arg) // both range and argument is not allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001180 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 bnr = start_bnr;
1182 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001183 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 bnr = end_bnr;
1185
1186 for ( ;!got_int; ui_breakcheck())
1187 {
1188 /*
1189 * delete the current buffer last, otherwise when the
1190 * current buffer is deleted, the next buffer becomes
1191 * the current one and will be loaded, which may then
1192 * also be deleted, etc.
1193 */
1194 if (bnr == curbuf->b_fnum)
1195 do_current = bnr;
1196 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1197 forceit) == OK)
1198 ++deleted;
1199
1200 /*
1201 * find next buffer number to delete/unload
1202 */
1203 if (addr_count == 2)
1204 {
1205 if (++bnr > end_bnr)
1206 break;
1207 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001208 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
1210 arg = skipwhite(arg);
1211 if (*arg == NUL)
1212 break;
1213 if (!VIM_ISDIGIT(*arg))
1214 {
1215 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001216 bnr = buflist_findpat(arg, p,
1217 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001218 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001219 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 break;
1221 arg = p;
1222 }
1223 else
1224 bnr = getdigits(&arg);
1225 }
1226 }
1227 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1228 FORWARD, do_current, forceit) == OK)
1229 ++deleted;
1230
1231 if (deleted == 0)
1232 {
1233 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001236 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001238 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001239 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 else if (deleted >= p_report)
1242 {
1243 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001244 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001245 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001247 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001248 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001251 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 }
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256 return errormsg;
1257}
1258
Bram Moolenaara02471e2014-01-10 16:43:14 +01001259/*
1260 * Make the current buffer empty.
1261 * Used when it is wiped out and it's the last buffer.
1262 */
1263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001264empty_curbuf(
1265 int close_others,
1266 int forceit,
1267 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001268{
1269 int retval;
1270 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001271 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001272
1273 if (action == DOBUF_UNLOAD)
1274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001275 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001276 return FAIL;
1277 }
1278
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001279 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001280 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001281 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001282 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001283
1284 setpcmark();
1285 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1286 forceit ? ECMD_FORCEIT : 0, curwin);
1287
1288 /*
1289 * do_ecmd() may create a new buffer, then we have to delete
1290 * the old one. But do_ecmd() may have done that already, check
1291 * if the buffer still exists.
1292 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001293 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001294 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001295 if (!close_others)
1296 need_fileinfo = FALSE;
1297 return retval;
1298}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300/*
1301 * Implementation of the commands for the buffer list.
1302 *
1303 * action == DOBUF_GOTO go to specified buffer
1304 * action == DOBUF_SPLIT split window and go to specified buffer
1305 * action == DOBUF_UNLOAD unload specified buffer(s)
1306 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1307 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001308 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 *
1310 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1311 * start == DOBUF_FIRST go to "count" buffer from first buffer
1312 * start == DOBUF_LAST go to "count" buffer from last buffer
1313 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1314 *
1315 * Return FAIL or OK.
1316 */
1317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318do_buffer(
1319 int action,
1320 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001321 int dir, // FORWARD or BACKWARD
1322 int count, // buffer number or number of buffers
1323 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
1325 buf_T *buf;
1326 buf_T *bp;
1327 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001328 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
1330 switch (start)
1331 {
1332 case DOBUF_FIRST: buf = firstbuf; break;
1333 case DOBUF_LAST: buf = lastbuf; break;
1334 default: buf = curbuf; break;
1335 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001336 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 while (count-- > 0)
1339 {
1340 do
1341 {
1342 buf = buf->b_next;
1343 if (buf == NULL)
1344 buf = firstbuf;
1345 }
1346 while (buf != curbuf && !bufIsChanged(buf));
1347 }
1348 if (!bufIsChanged(buf))
1349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001350 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 return FAIL;
1352 }
1353 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
1356 while (buf != NULL && buf->b_fnum != count)
1357 buf = buf->b_next;
1358 }
1359 else
1360 {
1361 bp = NULL;
1362 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1363 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001364 // remember the buffer where we start, we come back there when all
1365 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (bp == NULL)
1367 bp = buf;
1368 if (dir == FORWARD)
1369 {
1370 buf = buf->b_next;
1371 if (buf == NULL)
1372 buf = firstbuf;
1373 }
1374 else
1375 {
1376 buf = buf->b_prev;
1377 if (buf == NULL)
1378 buf = lastbuf;
1379 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001380 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (unload || buf->b_p_bl)
1382 {
1383 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001384 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 if (bp == buf)
1387 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001388 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001389 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return FAIL;
1391 }
1392 }
1393 }
1394
Bram Moolenaarc667da52019-11-30 20:52:27 +01001395 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 {
1397 if (start == DOBUF_FIRST)
1398 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001399 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001401 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001404 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001406 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 return FAIL;
1408 }
1409
1410#ifdef FEAT_GUI
1411 need_mouse_correct = TRUE;
1412#endif
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /*
1415 * delete buffer buf from memory and/or the list
1416 */
1417 if (unload)
1418 {
1419 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001420 bufref_T bufref;
1421
Bram Moolenaar94f01952018-09-01 15:30:03 +02001422 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001423 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001424
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001425 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaarc667da52019-11-30 20:52:27 +01001427 // When unloading or deleting a buffer that's already unloaded and
1428 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001429 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1430 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 return FAIL;
1432
1433 if (!forceit && bufIsChanged(buf))
1434 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001435#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if ((p_confirm || cmdmod.confirm) && p_write)
1437 {
1438 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001439 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001440 // Autocommand deleted buffer, oops! It's not changed
1441 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001443 // If it's still changed fail silently, the dialog already
1444 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001445 if (bufIsChanged(buf))
1446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001448 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001451 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 buf->b_fnum);
1453 return FAIL;
1454 }
1455 }
1456
Bram Moolenaarc667da52019-11-30 20:52:27 +01001457 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001458 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001459 end_visual_mode();
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 /*
1462 * If deleting the last (listed) buffer, make it empty.
1463 * The last (listed) buffer cannot be unloaded.
1464 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001465 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (bp->b_p_bl && bp != buf)
1467 break;
1468 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001469 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 /*
1472 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001473 * (unless it's the only window). Repeat this so long as we end up in
1474 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001476 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001477 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001478 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001479 {
1480 if (win_close(curwin, FALSE) == FAIL)
1481 break;
1482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 /*
1485 * If the buffer to be deleted is not the current one, delete it here.
1486 */
1487 if (buf != curbuf)
1488 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001489 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001490 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001491 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 return OK;
1493 }
1494
1495 /*
1496 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001497 * There should be another, otherwise it would have been handled
1498 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001499 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * Then prefer the buffer we most recently visited.
1501 * Else try to find one that is loaded, after the current buffer,
1502 * then before the current buffer.
1503 * Finally use any buffer.
1504 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001505 buf = NULL; // selected buffer
1506 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001507 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1508 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001510 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 int jumpidx;
1513
1514 jumpidx = curwin->w_jumplistidx - 1;
1515 if (jumpidx < 0)
1516 jumpidx = curwin->w_jumplistlen - 1;
1517
1518 forward = jumpidx;
1519 while (jumpidx != curwin->w_jumplistidx)
1520 {
1521 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1522 if (buf != NULL)
1523 {
1524 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001525 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 else if (buf->b_ml.ml_mfp == NULL)
1527 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001528 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (bp == NULL)
1530 bp = buf;
1531 buf = NULL;
1532 }
1533 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001534 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001536 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1538 break;
1539 if (--jumpidx < 0)
1540 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001541 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 break;
1543 }
1544 }
1545#endif
1546
Bram Moolenaarc667da52019-11-30 20:52:27 +01001547 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549 forward = TRUE;
1550 buf = curbuf->b_next;
1551 for (;;)
1552 {
1553 if (buf == NULL)
1554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001555 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 break;
1557 buf = curbuf->b_prev;
1558 forward = FALSE;
1559 continue;
1560 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001561 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1563 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001564 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001566 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 bp = buf;
1568 }
1569 if (forward)
1570 buf = buf->b_next;
1571 else
1572 buf = buf->b_prev;
1573 }
1574 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001577 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001579 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (buf->b_p_bl && buf != curbuf)
1581 break;
1582 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001583 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 if (curbuf->b_next != NULL)
1586 buf = curbuf->b_next;
1587 else
1588 buf = curbuf->b_prev;
1589 }
1590 }
1591
Bram Moolenaara02471e2014-01-10 16:43:14 +01001592 if (buf == NULL)
1593 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001594 // Autocommands must have wiped out all other buffers. Only option
1595 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001596 return empty_curbuf(FALSE, forceit, action);
1597 }
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 /*
1600 * make buf current buffer
1601 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001602 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001604 // If 'switchbuf' contains "useopen": jump to first window containing
1605 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001606 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001607 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001608 // If 'switchbuf' contains "usetab": jump to first window in any tab
1609 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001610 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 return OK;
1612 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 return FAIL;
1614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaarc667da52019-11-30 20:52:27 +01001616 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (buf == curbuf)
1618 return OK;
1619
1620 /*
1621 * Check if the current buffer may be abandoned.
1622 */
1623 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1624 {
1625#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1626 if ((p_confirm || cmdmod.confirm) && p_write)
1627 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001628 bufref_T bufref;
1629
1630 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001632 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001633 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 if (bufIsChanged(curbuf))
1637#endif
1638 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001639 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 return FAIL;
1641 }
1642 }
1643
Bram Moolenaarc667da52019-11-30 20:52:27 +01001644 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 set_curbuf(buf, action);
1646
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001648 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001650#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001651 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 return FAIL;
1653#endif
1654
1655 return OK;
1656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658/*
1659 * Set current buffer to "buf". Executes autocommands and closes current
1660 * buffer. "action" tells how to close the current buffer:
1661 * DOBUF_GOTO free or hide it
1662 * DOBUF_SPLIT nothing
1663 * DOBUF_UNLOAD unload it
1664 * DOBUF_DEL delete it
1665 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001666 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 */
1668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001669set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 buf_T *prevbuf;
1672 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001673 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001674#ifdef FEAT_SYN_HL
1675 long old_tw = curbuf->b_p_tw;
1676#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001677 bufref_T newbufref;
1678 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001681 if (!cmdmod.keepalt)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001682 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1683 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaarc667da52019-11-30 20:52:27 +01001685 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaarc667da52019-11-30 20:52:27 +01001688 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001690 set_bufref(&prevbufref, prevbuf);
1691 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001693 // Autocommands may delete the current buffer and/or the buffer we want to go
Bram Moolenaarc667da52019-11-30 20:52:27 +01001694 // to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001695 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001696 || (bufref_valid(&prevbufref)
1697 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001698#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001699 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001701 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001703#ifdef FEAT_SYN_HL
1704 if (prevbuf == curwin->w_buffer)
1705 reset_synblock(curwin);
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001708 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001710 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001712 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001714 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001715 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001716
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001717 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001718 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1720 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001721 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001722 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1723 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001724 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001725 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001726 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001729 // An autocommand may have deleted "buf", already entered it (e.g., when
1730 // it did ":bunload") or aborted the script processing.
1731 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001732 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001733#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001734 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001736 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001739#ifdef FEAT_SYN_HL
1740 if (old_tw != curbuf->b_p_tw)
1741 check_colorcolumn(curwin);
1742#endif
1743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
1746/*
1747 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001748 * Old curbuf must have been abandoned already! This also means "curbuf" may
1749 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001754 // Get the buffer in the current window.
1755 curwin->w_buffer = buf;
1756 curbuf = buf;
1757 ++curbuf->b_nwindows;
1758
1759 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1761 if (!buf->b_help)
1762 get_winopts(buf);
1763#ifdef FEAT_FOLDING
1764 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001765 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001767 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001771 if (curwin->w_p_diff)
1772 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
1774
Bram Moolenaara971b822011-09-14 14:43:25 +02001775#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001776 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001777#endif
1778
Bram Moolenaarc667da52019-11-30 20:52:27 +01001779 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 curwin->w_cursor.lnum = 1;
1781 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001784 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaarc667da52019-11-30 20:52:27 +01001786 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001787 curwin->w_valid = 0;
1788
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001789 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1790 curbuf->b_last_cursor.col, TRUE);
1791
Bram Moolenaarc667da52019-11-30 20:52:27 +01001792 // Make sure the buffer is loaded.
1793 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001794 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001795 // If there is no filetype, allow for detecting one. Esp. useful for
1796 // ":ball" used in a autocommand. If there already is a filetype we
1797 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001798 if (*curbuf->b_p_ft == NUL)
1799 did_filetype = FALSE;
1800
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001801 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 else
1804 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001805 if (!msg_silent && !shortmess(SHM_FILEINFO))
1806 need_fileinfo = TRUE; // display file info after redraw
1807
1808 // check if file changed
1809 (void)buf_check_timestamp(curbuf, FALSE);
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001812#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1816 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
1818
Bram Moolenaarc667da52019-11-30 20:52:27 +01001819 // If autocommands did not change the cursor position, restore cursor lnum
1820 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (curwin->w_cursor.lnum == 1 && inindent(0))
1822 buflist_getfpos();
1823
Bram Moolenaarc667da52019-11-30 20:52:27 +01001824 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825#ifdef FEAT_TITLE
1826 maketitle();
1827#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001828 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001829 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001830 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar009b2592004-10-24 19:18:58 +00001832#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001833 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001834 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001835#endif
1836
Bram Moolenaarc667da52019-11-30 20:52:27 +01001837 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001838 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840#ifdef FEAT_KEYMAP
1841 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001842 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001844#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001845 // May need to set the spell language. Can only do this after the buffer
1846 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001847 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1848 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001849#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001850#ifdef FEAT_VIMINFO
1851 curbuf->b_last_used = vim_time();
1852#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 redraw_later(NOT_VALID);
1855}
1856
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001857#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1858/*
1859 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001860 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001861 */
1862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001863do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001864{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001865 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001866 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001867 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001868 shorten_fnames(TRUE);
1869}
1870#endif
1871
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001872 void
1873no_write_message(void)
1874{
1875#ifdef FEAT_TERMINAL
1876 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001877 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001878 else
1879#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001880 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001881}
1882
1883 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001884no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001885{
1886#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001887 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001889 else
1890#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001891 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001892}
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894/*
1895 * functions for dealing with the buffer list
1896 */
1897
1898/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001899 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1900 * only one window. That means it can be re-used.
1901 */
1902 int
1903curbuf_reusable(void)
1904{
1905 return (curbuf != NULL
1906 && curbuf->b_ffname == NULL
1907 && curbuf->b_nwindows <= 1
1908 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001909#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001910 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001911#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001912 && !curbufIsChanged());
1913}
1914
1915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 * Add a file name to the buffer list. Return a pointer to the buffer.
1917 * If the same file name already exists return a pointer to that buffer.
1918 * If it does not exist, or if fname == NULL, a new entry is created.
1919 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1920 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1921 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001922 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001923 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1924 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001925 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 * This is the ONLY way to create a new buffer.
1927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001929buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001930 char_u *ffname_arg, // full path of fname or relative
1931 char_u *sfname_arg, // short fname or NULL
1932 linenr_T lnum, // preferred cursor line
1933 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001935 char_u *ffname = ffname_arg;
1936 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 buf_T *buf;
1938#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001939 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#endif
1941
Bram Moolenaar480778b2016-07-14 22:09:39 +02001942 if (top_file_num == 1)
1943 hash_init(&buf_hashtab);
1944
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001945 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 /*
1948 * If file name already exists in the list, update the entry.
1949 */
1950#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001951 // On Unix we can use inode numbers when the file exists. Works better
1952 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1954 st.st_dev = (dev_T)-1;
1955#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001956 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957#ifdef UNIX
1958 buflist_findname_stat(ffname, &st)
1959#else
1960 buflist_findname(ffname)
1961#endif
1962 ) != NULL)
1963 {
1964 vim_free(ffname);
1965 if (lnum != 0)
1966 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001967
1968 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001969 // copy the options now, if 'cpo' doesn't have 's' and not done
1970 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02001971 buf_copy_options(buf, 0);
1972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1974 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001975 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001978 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001980 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001981 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001982 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001983 return NULL;
1984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 return buf;
1987 }
1988
1989 /*
1990 * If the current buffer has no name and no contents, use the current
1991 * buffer. Otherwise: Need to allocate a new buffer structure.
1992 *
1993 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001994 * (A spell file buffer is allocated in spell.c, but that's not a normal
1995 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 */
1997 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001998 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002001 // It's like this buffer is deleted. Watch out for autocommands that
2002 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (curbuf->b_p_bl)
2004 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2005 if (buf == curbuf)
2006 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002007#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002008 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002013 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 clear_string_option(&buf->b_p_bh);
2015 clear_string_option(&buf->b_p_bt);
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 if (buf != curbuf || curbuf == NULL)
2019 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002020 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (buf == NULL)
2022 {
2023 vim_free(ffname);
2024 return NULL;
2025 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002026#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002027 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002028 buf->b_vars = dict_alloc();
2029 if (buf->b_vars == NULL)
2030 {
2031 vim_free(ffname);
2032 vim_free(buf);
2033 return NULL;
2034 }
2035 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2036#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002037 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039
2040 if (ffname != NULL)
2041 {
2042 buf->b_ffname = ffname;
2043 buf->b_sfname = vim_strsave(sfname);
2044 }
2045
2046 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002047 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2050 || buf->b_wininfo == NULL)
2051 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002052 if (buf->b_sfname != buf->b_ffname)
2053 VIM_CLEAR(buf->b_sfname);
2054 else
2055 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002056 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (buf != curbuf)
2058 free_buffer(buf);
2059 return NULL;
2060 }
2061
2062 if (buf == curbuf)
2063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002064 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002065 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002066 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002068#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002069 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 return NULL;
2071#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002072 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002073
Bram Moolenaarc667da52019-11-30 20:52:27 +01002074 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002075 buf->b_p_initialized = FALSE;
2076 buf_copy_options(buf, BCO_ENTER);
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002079 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 curbuf->b_kmap_state |= KEYMAP_INIT;
2081#endif
2082 }
2083 else
2084 {
2085 /*
2086 * put new buffer at the end of the buffer list
2087 */
2088 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002089 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 buf->b_prev = NULL;
2092 firstbuf = buf;
2093 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002094 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
2096 lastbuf->b_next = buf;
2097 buf->b_prev = lastbuf;
2098 }
2099 lastbuf = buf;
2100
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002101 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2102 {
2103 // Recycle a previously used buffer number. Used for buffers which
2104 // are normally hidden, e.g. in a popup window. Avoids that the
2105 // buffer number grows rapidly.
2106 --buf_reuse.ga_len;
2107 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002108
2109 // Move buffer to the right place in the buffer list.
2110 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2111 {
2112 buf_T *prev = buf->b_prev;
2113
2114 prev->b_next = buf->b_next;
2115 if (prev->b_next != NULL)
2116 prev->b_next->b_prev = prev;
2117 buf->b_next = prev;
2118 buf->b_prev = prev->b_prev;
2119 if (buf->b_prev != NULL)
2120 buf->b_prev->b_next = buf;
2121 prev->b_prev = buf;
2122 if (lastbuf == buf)
2123 lastbuf = prev;
2124 if (firstbuf == prev)
2125 firstbuf = buf;
2126 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002127 }
2128 else
2129 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002130 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002132 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (emsg_silent == 0)
2134 {
2135 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002136 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 top_file_num = 1;
2139 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002140 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /*
2143 * Always copy the options from the current buffer.
2144 */
2145 buf_copy_options(buf, BCO_ALWAYS);
2146 }
2147
2148 buf->b_wininfo->wi_fpos.lnum = lnum;
2149 buf->b_wininfo->wi_win = curwin;
2150
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002151#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002152 hash_init(&buf->b_s.b_keywtab);
2153 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154#endif
2155
2156 buf->b_fname = buf->b_sfname;
2157#ifdef UNIX
2158 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002159 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 else
2161 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002162 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 buf->b_dev = st.st_dev;
2164 buf->b_ino = st.st_ino;
2165 }
2166#endif
2167 buf->b_u_synced = TRUE;
2168 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002169 if (flags & BLN_DUMMY)
2170 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002172 clrallmarks(buf); // clear marks
2173 fmarks_check_names(buf); // check file marks for this file
2174 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (!(flags & BLN_DUMMY))
2176 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002177 bufref_T bufref;
2178
Bram Moolenaarc667da52019-11-30 20:52:27 +01002179 // Tricky: these autocommands may change the buffer list. They could
2180 // also split the window with re-using the one empty buffer. This may
2181 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002182 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002183 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002184 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002185 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002187 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002188 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002189 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002190 return NULL;
2191 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002192#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002193 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198 return buf;
2199}
2200
2201/*
2202 * Free the memory for the options of a buffer.
2203 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2204 * 'fileencoding'.
2205 */
2206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002207free_buf_options(
2208 buf_T *buf,
2209 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 if (free_p_ff)
2212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 clear_string_option(&buf->b_p_bh);
2216 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218#ifdef FEAT_FIND_ID
2219 clear_string_option(&buf->b_p_def);
2220 clear_string_option(&buf->b_p_inc);
2221# ifdef FEAT_EVAL
2222 clear_string_option(&buf->b_p_inex);
2223# endif
2224#endif
2225#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2226 clear_string_option(&buf->b_p_inde);
2227 clear_string_option(&buf->b_p_indk);
2228#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002229#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2230 clear_string_option(&buf->b_p_bexpr);
2231#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002232#if defined(FEAT_CRYPT)
2233 clear_string_option(&buf->b_p_cm);
2234#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002235 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002236#if defined(FEAT_EVAL)
2237 clear_string_option(&buf->b_p_fex);
2238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#ifdef FEAT_CRYPT
2240 clear_string_option(&buf->b_p_key);
2241#endif
2242 clear_string_option(&buf->b_p_kp);
2243 clear_string_option(&buf->b_p_mps);
2244 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002245 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002247#ifdef FEAT_VARTABS
2248 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002249 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002250 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002251 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002252 buf->b_p_vsts_array = NULL;
2253 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002254 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256#ifdef FEAT_KEYMAP
2257 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002258 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ga_clear(&buf->b_kmap_ga);
2260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#ifdef FEAT_FOLDING
2263 clear_string_option(&buf->b_p_cms);
2264#endif
2265 clear_string_option(&buf->b_p_nf);
2266#ifdef FEAT_SYN_HL
2267 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002268 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002269#endif
2270#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002271 clear_string_option(&buf->b_s.b_p_spc);
2272 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002273 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002274 buf->b_s.b_cap_prog = NULL;
2275 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#endif
2277#ifdef FEAT_SEARCHPATH
2278 clear_string_option(&buf->b_p_sua);
2279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#ifdef FEAT_CINDENT
2282 clear_string_option(&buf->b_p_cink);
2283 clear_string_option(&buf->b_p_cino);
2284#endif
2285#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2286 clear_string_option(&buf->b_p_cinw);
2287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002289#ifdef FEAT_COMPL_FUNC
2290 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002291 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#ifdef FEAT_QUICKFIX
2294 clear_string_option(&buf->b_p_gp);
2295 clear_string_option(&buf->b_p_mp);
2296 clear_string_option(&buf->b_p_efm);
2297#endif
2298 clear_string_option(&buf->b_p_ep);
2299 clear_string_option(&buf->b_p_path);
2300 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002301 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002302#ifdef FEAT_EVAL
2303 clear_string_option(&buf->b_p_tfu);
2304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 clear_string_option(&buf->b_p_dict);
2306 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002307#ifdef FEAT_TEXTOBJ
2308 clear_string_option(&buf->b_p_qe);
2309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002311 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002312#ifdef FEAT_LISP
2313 clear_string_option(&buf->b_p_lw);
2314#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002315 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002316 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317}
2318
2319/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002320 * Get alternate file "n".
2321 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2322 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * if (options & GETF_SETMARK) call setpcmark()
2324 * if (options & GETF_ALT) we are jumping to an alternate file.
2325 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2326 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002327 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 */
2329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002330buflist_getfile(
2331 int n,
2332 linenr_T lnum,
2333 int options,
2334 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 pos_T *fpos;
2339 colnr_T col;
2340
2341 buf = buflist_findnr(n);
2342 if (buf == NULL)
2343 {
2344 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002345 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002347 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return FAIL;
2349 }
2350
Bram Moolenaarc667da52019-11-30 20:52:27 +01002351 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (buf == curbuf)
2353 return OK;
2354
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002355 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002356 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002357 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002359 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002360 if (curbuf_locked())
2361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaarc667da52019-11-30 20:52:27 +01002363 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (lnum == 0)
2365 {
2366 fpos = buflist_findfpos(buf);
2367 lnum = fpos->lnum;
2368 col = fpos->col;
2369 }
2370 else
2371 col = 0;
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (options & GETF_SWITCH)
2374 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002375 // If 'switchbuf' contains "useopen": jump to first window containing
2376 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002377 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002379
Bram Moolenaarc667da52019-11-30 20:52:27 +01002380 // If 'switchbuf' contains "usetab": jump to first window in any tab
2381 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002382 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002383 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002384
Bram Moolenaarc667da52019-11-30 20:52:27 +01002385 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2386 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002387 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002388 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002390 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002391 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002392 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2393 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002395 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002400 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2401 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
2403 --RedrawingDisabled;
2404
Bram Moolenaarc667da52019-11-30 20:52:27 +01002405 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (!p_sol && col != 0)
2407 {
2408 curwin->w_cursor.col = col;
2409 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 curwin->w_set_curswant = TRUE;
2412 }
2413 return OK;
2414 }
2415 --RedrawingDisabled;
2416 return FAIL;
2417}
2418
2419/*
2420 * go to the last know line number for the current buffer
2421 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002423buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
2425 pos_T *fpos;
2426
2427 fpos = buflist_findfpos(curbuf);
2428
2429 curwin->w_cursor.lnum = fpos->lnum;
2430 check_cursor_lnum();
2431
2432 if (p_sol)
2433 curwin->w_cursor.col = 0;
2434 else
2435 {
2436 curwin->w_cursor.col = fpos->col;
2437 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 curwin->w_set_curswant = TRUE;
2440 }
2441}
2442
Bram Moolenaar81695252004-12-29 20:58:21 +00002443#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2444/*
2445 * Find file in buffer list by name (it has to be for the current window).
2446 * Returns NULL if not found.
2447 */
2448 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002449buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002450{
2451 char_u *ffname;
2452 buf_T *buf = NULL;
2453
Bram Moolenaarc667da52019-11-30 20:52:27 +01002454 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002455 ffname = FullName_save(fname,
2456#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002457 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002458#else
2459 FALSE
2460#endif
2461 );
2462 if (ffname != NULL)
2463 {
2464 buf = buflist_findname(ffname);
2465 vim_free(ffname);
2466 }
2467 return buf;
2468}
2469#endif
2470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471/*
2472 * Find file in buffer list by name (it has to be for the current window).
2473 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002474 * Skips dummy buffers.
2475 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 */
2477 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002478buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002481 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 if (mch_stat((char *)ffname, &st) < 0)
2484 st.st_dev = (dev_T)-1;
2485 return buflist_findname_stat(ffname, &st);
2486}
2487
2488/*
2489 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2490 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002491 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494buflist_findname_stat(
2495 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002496 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498#endif
2499 buf_T *buf;
2500
Bram Moolenaarc667da52019-11-30 20:52:27 +01002501 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002502 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002503 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504#ifdef UNIX
2505 , stp
2506#endif
2507 ))
2508 return buf;
2509 return NULL;
2510}
2511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512/*
2513 * Find file in buffer list by a regexp pattern.
2514 * Return fnum of the found buffer.
2515 * Return < 0 for error.
2516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002518buflist_findpat(
2519 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002520 char_u *pattern_end, // pointer to first char after pattern
2521 int unlisted, // find unlisted buffers
2522 int diffmode UNUSED, // find diff-mode buffers only
2523 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 int match = -1;
2527 int find_listed;
2528 char_u *pat;
2529 char_u *patend;
2530 int attempt;
2531 char_u *p;
2532 int toggledollar;
2533
2534 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2535 {
2536 if (*pattern == '%')
2537 match = curbuf->b_fnum;
2538 else
2539 match = curwin->w_alt_fnum;
2540#ifdef FEAT_DIFF
2541 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2542 match = -1;
2543#endif
2544 }
2545
2546 /*
2547 * Try four ways of matching a listed buffer:
2548 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002549 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 * attempt == 2: with '$' at end (only match at end)
2551 * attempt == 3: with '^' at start and '$' at end (only full match)
2552 * Repeat this for finding an unlisted buffer if there was no matching
2553 * listed buffer.
2554 */
2555 else
2556 {
2557 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2558 if (pat == NULL)
2559 return -1;
2560 patend = pat + STRLEN(pat) - 1;
2561 toggledollar = (patend > pat && *patend == '$');
2562
Bram Moolenaarc667da52019-11-30 20:52:27 +01002563 // First try finding a listed buffer. If not found and "unlisted"
2564 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 find_listed = TRUE;
2566 for (;;)
2567 {
2568 for (attempt = 0; attempt <= 3; ++attempt)
2569 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002570 regmatch_T regmatch;
2571
Bram Moolenaarc667da52019-11-30 20:52:27 +01002572 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002574 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002576 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002578 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2579 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 vim_free(pat);
2582 return -1;
2583 }
2584
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002585 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (buf->b_p_bl == find_listed
2587#ifdef FEAT_DIFF
2588 && (!diffmode || diff_mode_buf(buf))
2589#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002590 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002592 if (curtab_only)
2593 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002594 // Ignore the match if the buffer is not open in
2595 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002596 win_T *wp;
2597
Bram Moolenaar29323592016-07-24 22:04:11 +02002598 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002599 if (wp->w_buffer == buf)
2600 break;
2601 if (wp == NULL)
2602 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002603 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002604 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 match = -2;
2607 break;
2608 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002609 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
2611
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002612 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002613 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 break;
2615 }
2616
Bram Moolenaarc667da52019-11-30 20:52:27 +01002617 // Only search for unlisted buffers if there was no match with
2618 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (!unlisted || !find_listed || match != -1)
2620 break;
2621 find_listed = FALSE;
2622 }
2623
2624 vim_free(pat);
2625 }
2626
2627 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002628 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 return match;
2632}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Bram Moolenaar52410572019-10-27 05:12:45 +01002634#ifdef FEAT_VIMINFO
2635typedef struct {
2636 buf_T *buf;
2637 char_u *match;
2638} bufmatch_T;
2639#endif
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641/*
2642 * Find all buffer names that match.
2643 * For command line expansion of ":buf" and ":sbuf".
2644 * Return OK if matches found, FAIL otherwise.
2645 */
2646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002647ExpandBufnames(
2648 char_u *pat,
2649 int *num_file,
2650 char_u ***file,
2651 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 int count = 0;
2654 buf_T *buf;
2655 int round;
2656 char_u *p;
2657 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002658 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002659#ifdef FEAT_VIMINFO
2660 bufmatch_T *matches = NULL;
2661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
Bram Moolenaarc667da52019-11-30 20:52:27 +01002663 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 *file = NULL;
2665
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002666#ifdef FEAT_DIFF
2667 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2668 return FAIL;
2669#endif
2670
Bram Moolenaarc667da52019-11-30 20:52:27 +01002671 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002672 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002674 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002675 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002677 STRCPY(patc, "\\(^\\|[\\/]\\)");
2678 STRCPY(patc + 11, pat + 1);
2679 }
2680 else
2681 patc = pat;
2682
2683 /*
2684 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002685 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002686 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002687 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002688 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002689 regmatch_T regmatch;
2690
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002691 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002692 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002693 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2694 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002695 {
2696 if (patc != pat)
2697 vim_free(patc);
2698 return FAIL;
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 /*
2702 * round == 1: Count the matches.
2703 * round == 2: Build the array to keep the matches.
2704 */
2705 for (round = 1; round <= 2; ++round)
2706 {
2707 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002708 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002710 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002712#ifdef FEAT_DIFF
2713 if (options & BUF_DIFF_FILTER)
2714 // Skip buffers not suitable for
2715 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002716 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002717 continue;
2718#endif
2719
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002720 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (p != NULL)
2722 {
2723 if (round == 1)
2724 ++count;
2725 else
2726 {
2727 if (options & WILD_HOME_REPLACE)
2728 p = home_replace_save(buf, p);
2729 else
2730 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002731#ifdef FEAT_VIMINFO
2732 if (matches != NULL)
2733 {
2734 matches[count].buf = buf;
2735 matches[count].match = p;
2736 count++;
2737 }
2738 else
2739#endif
2740 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 }
2743 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002744 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 break;
2746 if (round == 1)
2747 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002748 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (*file == NULL)
2750 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002751 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002752 if (patc != pat)
2753 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 return FAIL;
2755 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002756#ifdef FEAT_VIMINFO
2757 if (options & WILD_BUFLASTUSED)
2758 matches = ALLOC_MULT(bufmatch_T, count);
2759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002762 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002763 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 break;
2765 }
2766
Bram Moolenaar05159a02005-02-26 23:04:13 +00002767 if (patc != pat)
2768 vim_free(patc);
2769
Bram Moolenaar52410572019-10-27 05:12:45 +01002770#ifdef FEAT_VIMINFO
2771 if (matches != NULL)
2772 {
2773 int i;
2774 if (count > 1)
2775 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2776 // if the current buffer is first in the list, place it at the end
2777 if (matches[0].buf == curbuf)
2778 {
2779 for (i = 1; i < count; i++)
2780 (*file)[i-1] = matches[i].match;
2781 (*file)[count-1] = matches[0].match;
2782 }
2783 else
2784 {
2785 for (i = 0; i < count; i++)
2786 (*file)[i] = matches[i].match;
2787 }
2788 vim_free(matches);
2789 }
2790#endif
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 *num_file = count;
2793 return (count == 0 ? FAIL : OK);
2794}
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796/*
2797 * Check for a match on the file name for buffer "buf" with regprog "prog".
2798 */
2799 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002800buflist_match(
2801 regmatch_T *rmp,
2802 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002803 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 char_u *match;
2806
Bram Moolenaarc667da52019-11-30 20:52:27 +01002807 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002808 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002810 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 return match;
2813}
2814
2815/*
2816 * Try matching the regexp in "prog" with file name "name".
2817 * Return "name" when there is a match, NULL when not.
2818 */
2819 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002820fname_match(
2821 regmatch_T *rmp,
2822 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002823 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 char_u *match = NULL;
2826 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 if (name != NULL)
2829 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002830 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002831 rmp->rm_ic = p_fic || ignore_case;
2832 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 match = name;
2834 else
2835 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002836 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002838 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 match = name;
2840 vim_free(p);
2841 }
2842 }
2843
2844 return match;
2845}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002848 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
2850 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002851buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002853 char_u key[VIM_SIZEOF_INT * 2 + 1];
2854 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 if (nr == 0)
2857 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002858 sprintf((char *)key, "%x", nr);
2859 hi = hash_find(&buf_hashtab, key);
2860
2861 if (!HASHITEM_EMPTY(hi))
2862 return (buf_T *)(hi->hi_key
2863 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 return NULL;
2865}
2866
2867/*
2868 * Get name of file 'n' in the buffer list.
2869 * When the file has no name an empty string is returned.
2870 * home_replace() is used to shorten the file name (used for marks).
2871 * Returns a pointer to allocated memory, of NULL when failed.
2872 */
2873 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002874buflist_nr2name(
2875 int n,
2876 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002877 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 buf_T *buf;
2880
2881 buf = buflist_findnr(n);
2882 if (buf == NULL)
2883 return NULL;
2884 return home_replace_save(helptail ? buf : NULL,
2885 fullname ? buf->b_ffname : buf->b_fname);
2886}
2887
2888/*
2889 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2890 * When "copy_options" is TRUE save the local window option values.
2891 * When "lnum" is 0 only do the options.
2892 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894buflist_setfpos(
2895 buf_T *buf,
2896 win_T *win,
2897 linenr_T lnum,
2898 colnr_T col,
2899 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 wininfo_T *wip;
2902
2903 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2904 if (wip->wi_win == win)
2905 break;
2906 if (wip == NULL)
2907 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002908 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002909 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (wip == NULL)
2911 return;
2912 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002913 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 lnum = 1;
2915 }
2916 else
2917 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002918 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (wip->wi_prev)
2920 wip->wi_prev->wi_next = wip->wi_next;
2921 else
2922 buf->b_wininfo = wip->wi_next;
2923 if (wip->wi_next)
2924 wip->wi_next->wi_prev = wip->wi_prev;
2925 if (copy_options && wip->wi_optset)
2926 {
2927 clear_winopt(&wip->wi_opt);
2928#ifdef FEAT_FOLDING
2929 deleteFoldRecurse(&wip->wi_folds);
2930#endif
2931 }
2932 }
2933 if (lnum != 0)
2934 {
2935 wip->wi_fpos.lnum = lnum;
2936 wip->wi_fpos.col = col;
2937 }
2938 if (copy_options)
2939 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002940 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2942#ifdef FEAT_FOLDING
2943 wip->wi_fold_manual = win->w_fold_manual;
2944 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2945#endif
2946 wip->wi_optset = TRUE;
2947 }
2948
Bram Moolenaarc667da52019-11-30 20:52:27 +01002949 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 wip->wi_next = buf->b_wininfo;
2951 buf->b_wininfo = wip;
2952 wip->wi_prev = NULL;
2953 if (wip->wi_next)
2954 wip->wi_next->wi_prev = wip;
2955
2956 return;
2957}
2958
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002959#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002960/*
2961 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2962 * page. That's because a diff is local to a tab page.
2963 */
2964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002966{
2967 win_T *wp;
2968
2969 if (wip->wi_opt.wo_diff)
2970 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002971 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002972 // return FALSE when it's a window in the current tab page, thus
2973 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002974 if (wip->wi_win == wp)
2975 return FALSE;
2976 return TRUE;
2977 }
2978 return FALSE;
2979}
2980#endif
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982/*
2983 * Find info for the current window in buffer "buf".
2984 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002985 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2986 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 * Returns NULL when there isn't any info.
2988 */
2989 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002990find_wininfo(
2991 buf_T *buf,
2992 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 wininfo_T *wip;
2995
2996 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002997 if (wip->wi_win == curwin
2998#ifdef FEAT_DIFF
2999 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3000#endif
3001 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003003
Bram Moolenaarc667da52019-11-30 20:52:27 +01003004 // If no wininfo for curwin, use the first in the list (that doesn't have
3005 // 'diff' set and is in another tab page).
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003006 if (wip == NULL)
3007 {
3008#ifdef FEAT_DIFF
3009 if (skip_diff_buffer)
3010 {
3011 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
3012 if (!wininfo_other_tab_diff(wip))
3013 break;
3014 }
3015 else
3016#endif
3017 wip = buf->b_wininfo;
3018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 return wip;
3020}
3021
3022/*
3023 * Reset the local window options to the values last used in this window.
3024 * If the buffer wasn't used in this window before, use the values from
3025 * the most recently used window. If the values were never set, use the
3026 * global values for the window.
3027 */
3028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003029get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 wininfo_T *wip;
3032
3033 clear_winopt(&curwin->w_onebuf_opt);
3034#ifdef FEAT_FOLDING
3035 clearFolding(curwin);
3036#endif
3037
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003038 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003039 if (wip != NULL && wip->wi_win != NULL
3040 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003042 // The buffer is currently displayed in the window: use the actual
3043 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003044 win_T *wp = wip->wi_win;
3045
3046 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3047#ifdef FEAT_FOLDING
3048 curwin->w_fold_manual = wp->w_fold_manual;
3049 curwin->w_foldinvalid = TRUE;
3050 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3051#endif
3052 }
3053 else if (wip != NULL && wip->wi_optset)
3054 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003055 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3057#ifdef FEAT_FOLDING
3058 curwin->w_fold_manual = wip->wi_fold_manual;
3059 curwin->w_foldinvalid = TRUE;
3060 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3061#endif
3062 }
3063 else
3064 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3065
3066#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003067 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (p_fdls >= 0)
3069 curwin->w_p_fdl = p_fdls;
3070#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003071 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074/*
3075 * Find the position (lnum and col) for the buffer 'buf' for the current
3076 * window.
3077 * Returns a pointer to no_position if no position is found.
3078 */
3079 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003083 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003085 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (wip != NULL)
3087 return &(wip->wi_fpos);
3088 else
3089 return &no_position;
3090}
3091
3092/*
3093 * Find the lnum for the buffer 'buf' for the current window.
3094 */
3095 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003096buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097{
3098 return buflist_findfpos(buf)->lnum;
3099}
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003102 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaar52410572019-10-27 05:12:45 +01003107 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 int len;
3109 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003110 int ro_char;
3111 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003112#ifdef FEAT_TERMINAL
3113 int job_running;
3114 int job_none_open;
3115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaar52410572019-10-27 05:12:45 +01003117#ifdef FEAT_VIMINFO
3118 garray_T buflist;
3119 buf_T **buflist_data = NULL, **p;
3120
3121 if (vim_strchr(eap->arg, 't'))
3122 {
3123 ga_init2(&buflist, sizeof(buf_T *), 50);
3124 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3125 {
3126 if (ga_grow(&buflist, 1) == OK)
3127 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3128 }
3129
3130 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3131 sizeof(buf_T *), buf_compare);
3132
Bram Moolenaar3b991522019-11-06 23:26:20 +01003133 buflist_data = (buf_T **)buflist.ga_data;
3134 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003135 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003136 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003137
Bram Moolenaar3b991522019-11-06 23:26:20 +01003138 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003139 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3140 : buf->b_next)
3141#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003145#ifdef FEAT_TERMINAL
3146 job_running = term_job_running(buf->b_term);
3147 job_none_open = job_running && term_none_open(buf->b_term);
3148#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003149 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003150 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3151 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3152 || (vim_strchr(eap->arg, '+')
3153 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3154 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003155 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003156 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003157 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3158#ifdef FEAT_TERMINAL
3159 || (vim_strchr(eap->arg, 'R')
3160 && (!job_running || (job_running && job_none_open)))
3161 || (vim_strchr(eap->arg, '?')
3162 && (!job_running || (job_running && !job_none_open)))
3163 || (vim_strchr(eap->arg, 'F')
3164 && (job_running || buf->b_term == NULL))
3165#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003166 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3167 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3168 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3169 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3170 || (vim_strchr(eap->arg, '#')
3171 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003174 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 else
3176 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003177 if (message_filtered(NameBuff))
3178 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003180 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3181 : (bufIsChanged(buf) ? '+' : ' ');
3182#ifdef FEAT_TERMINAL
3183 if (term_job_running(buf->b_term))
3184 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003185 if (term_none_open(buf->b_term))
3186 ro_char = '?';
3187 else
3188 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003189 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3190 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003191 }
3192 else if (buf->b_term != NULL)
3193 ro_char = 'F';
3194 else
3195#endif
3196 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3197
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003198 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003199 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 buf->b_fnum,
3201 buf->b_p_bl ? ' ' : 'u',
3202 buf == curbuf ? '%' :
3203 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3204 buf->b_ml.ml_mfp == NULL ? ' ' :
3205 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003206 ro_char,
3207 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003208 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003209 if (len > IOSIZE - 20)
3210 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
Bram Moolenaarc667da52019-11-30 20:52:27 +01003212 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 i = 40 - vim_strsize(IObuff);
3214 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003216 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003217#ifdef FEAT_VIMINFO
3218 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3219 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3220 else
3221#endif
3222 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3223 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003224 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003226 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 ui_breakcheck();
3228 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003229
3230#ifdef FEAT_VIMINFO
3231 if (buflist_data)
3232 ga_clear(&buflist);
3233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236/*
3237 * Get file name and line number for file 'fnum'.
3238 * Used by DoOneCmd() for translating '%' and '#'.
3239 * Used by insert_reg() and cmdline_paste() for '#' register.
3240 * Return FAIL if not found, OK for success.
3241 */
3242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243buflist_name_nr(
3244 int fnum,
3245 char_u **fname,
3246 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 buf_T *buf;
3249
3250 buf = buflist_findnr(fnum);
3251 if (buf == NULL || buf->b_fname == NULL)
3252 return FAIL;
3253
3254 *fname = buf->b_fname;
3255 *lnum = buflist_findlnum(buf);
3256
3257 return OK;
3258}
3259
3260/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003261 * Set the file name for "buf"' to "ffname_arg", short file name to
3262 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 * The file name with the full path is also remembered, for when :cd is used.
3264 * Returns FAIL for failure (file name already in use by other buffer)
3265 * OK otherwise.
3266 */
3267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268setfname(
3269 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003270 char_u *ffname_arg,
3271 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003272 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003274 char_u *ffname = ffname_arg;
3275 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003276 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003278 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280
3281 if (ffname == NULL || *ffname == NUL)
3282 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003283 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003284 if (buf->b_sfname != buf->b_ffname)
3285 VIM_CLEAR(buf->b_sfname);
3286 else
3287 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003288 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#ifdef UNIX
3290 st.st_dev = (dev_T)-1;
3291#endif
3292 }
3293 else
3294 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003295 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3296 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return FAIL;
3298
3299 /*
3300 * if the file name is already used in another buffer:
3301 * - if the buffer is loaded, fail
3302 * - if the buffer is not loaded, delete it from the list
3303 */
3304#ifdef UNIX
3305 if (mch_stat((char *)ffname, &st) < 0)
3306 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003307#endif
3308 if (!(buf->b_flags & BF_DUMMY))
3309#ifdef UNIX
3310 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003312 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#endif
3314 if (obuf != NULL && obuf != buf)
3315 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003316 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003319 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 vim_free(ffname);
3321 return FAIL;
3322 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003323 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003324 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326 sfname = vim_strsave(sfname);
3327 if (ffname == NULL || sfname == NULL)
3328 {
3329 vim_free(sfname);
3330 vim_free(ffname);
3331 return FAIL;
3332 }
3333#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003334 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003336 if (buf->b_sfname != buf->b_ffname)
3337 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 buf->b_ffname = ffname;
3340 buf->b_sfname = sfname;
3341 }
3342 buf->b_fname = buf->b_sfname;
3343#ifdef UNIX
3344 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003345 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else
3347 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003348 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 buf->b_dev = st.st_dev;
3350 buf->b_ino = st.st_ino;
3351 }
3352#endif
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 buf_name_changed(buf);
3357 return OK;
3358}
3359
3360/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003361 * Crude way of changing the name of a buffer. Use with care!
3362 * The name should be relative to the current directory.
3363 */
3364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003366{
3367 buf_T *buf;
3368
3369 buf = buflist_findnr(fnum);
3370 if (buf != NULL)
3371 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003372 if (buf->b_sfname != buf->b_ffname)
3373 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003374 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003375 buf->b_ffname = vim_strsave(name);
3376 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003377 // Allocate ffname and expand into full path. Also resolves .lnk
3378 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003379 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003380 buf->b_fname = buf->b_sfname;
3381 }
3382}
3383
3384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * Take care of what needs to be done when the name of buffer "buf" has
3386 * changed.
3387 */
3388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 /*
3392 * If the file name changed, also change the name of the swapfile
3393 */
3394 if (buf->b_ml.ml_mfp != NULL)
3395 ml_setname(buf);
3396
3397 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003398 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003400 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003402 status_redraw_all(); // status lines need to be redrawn
3403 fmarks_check_names(buf); // check named file marks
3404 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405}
3406
3407/*
3408 * set alternate file name for current window
3409 *
3410 * Used by do_one_cmd(), do_write() and do_ecmd().
3411 * Return the buffer.
3412 */
3413 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414setaltfname(
3415 char_u *ffname,
3416 char_u *sfname,
3417 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 buf_T *buf;
3420
Bram Moolenaarc667da52019-11-30 20:52:27 +01003421 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003423 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 curwin->w_alt_fnum = buf->b_fnum;
3425 return buf;
3426}
3427
3428/*
3429 * Get alternate file name for current window.
3430 * Return NULL if there isn't any, and give error message if requested.
3431 */
3432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003433getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003434 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
3436 char_u *fname;
3437 linenr_T dummy;
3438
3439 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3440 {
3441 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003442 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 return NULL;
3444 }
3445 return fname;
3446}
3447
3448/*
3449 * Add a file name to the buflist and return its number.
3450 * Uses same flags as buflist_new(), except BLN_DUMMY.
3451 *
3452 * used by qf_init(), main() and doarglist()
3453 */
3454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 buf_T *buf;
3458
3459 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3460 if (buf != NULL)
3461 return buf->b_fnum;
3462 return 0;
3463}
3464
3465#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3466/*
3467 * Adjust slashes in file names. Called after 'shellslash' was set.
3468 */
3469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003470buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 buf_T *bp;
3473
Bram Moolenaar29323592016-07-24 22:04:11 +02003474 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 if (bp->b_ffname != NULL)
3477 slash_adjust(bp->b_ffname);
3478 if (bp->b_sfname != NULL)
3479 slash_adjust(bp->b_sfname);
3480 }
3481}
3482#endif
3483
3484/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003485 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 * Also save the local window option values.
3487 */
3488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003489buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003491 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492}
3493
3494/*
3495 * Return TRUE if 'ffname' is not the same file as current file.
3496 * Fname must have a full path (expanded by mch_FullName()).
3497 */
3498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003499otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 return otherfile_buf(curbuf, ffname
3502#ifdef UNIX
3503 , NULL
3504#endif
3505 );
3506}
3507
3508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003509otherfile_buf(
3510 buf_T *buf,
3511 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003513 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003517 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3519 return TRUE;
3520 if (fnamecmp(ffname, buf->b_ffname) == 0)
3521 return FALSE;
3522#ifdef UNIX
3523 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003524 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaarc667da52019-11-30 20:52:27 +01003526 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (stp == NULL)
3528 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003529 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 st.st_dev = (dev_T)-1;
3531 stp = &st;
3532 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003533 // Use dev/ino to check if the files are the same, even when the names
3534 // are different (possible with links). Still need to compare the
3535 // name above, for when the file doesn't exist yet.
3536 // Problem: The dev/ino changes when a file is deleted (and created
3537 // again) and remains the same when renamed/moved. We don't want to
3538 // mch_stat() each buffer each time, that would be too slow. Get the
3539 // dev/ino again when they appear to match, but not when they appear
3540 // to be different: Could skip a buffer when it's actually the same
3541 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (buf_same_ino(buf, stp))
3543 {
3544 buf_setino(buf);
3545 if (buf_same_ino(buf, stp))
3546 return FALSE;
3547 }
3548 }
3549#endif
3550 return TRUE;
3551}
3552
3553#if defined(UNIX) || defined(PROTO)
3554/*
3555 * Set inode and device number for a buffer.
3556 * Must always be called when b_fname is changed!.
3557 */
3558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003559buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003561 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3564 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003565 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 buf->b_dev = st.st_dev;
3567 buf->b_ino = st.st_ino;
3568 }
3569 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003570 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
3574 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3575 */
3576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577buf_same_ino(
3578 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003579 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003581 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 && stp->st_dev == buf->b_dev
3583 && stp->st_ino == buf->b_ino);
3584}
3585#endif
3586
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587/*
3588 * Print info about the current buffer.
3589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003591fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003592 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003593 int shorthelp,
3594 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 char_u *name;
3597 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003598 char *p;
3599 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003600 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003602 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (buffer == NULL)
3604 return;
3605
Bram Moolenaarc667da52019-11-30 20:52:27 +01003606 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003608 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 p = buffer + STRLEN(buffer);
3610 }
3611 else
3612 p = buffer;
3613
3614 *p++ = '"';
3615 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003616 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 else
3618 {
3619 if (!fullname && curbuf->b_fname != NULL)
3620 name = curbuf->b_fname;
3621 else
3622 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003623 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 (int)(IOSIZE - (p - buffer)), TRUE);
3625 }
3626
Bram Moolenaar32526b32019-01-19 17:43:09 +01003627 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 curbufIsChanged() ? (shortmess(SHM_MOD)
3629 ? " [+]" : _(" [Modified]")) : " ",
3630 (curbuf->b_flags & BF_NOTEDITED)
3631#ifdef FEAT_QUICKFIX
3632 && !bt_dontwrite(curbuf)
3633#endif
3634 ? _("[Not edited]") : "",
3635 (curbuf->b_flags & BF_NEW)
3636#ifdef FEAT_QUICKFIX
3637 && !bt_dontwrite(curbuf)
3638#endif
3639 ? _("[New file]") : "",
3640 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003641 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 : _("[readonly]")) : "",
3643 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3644 || curbuf->b_p_ro) ?
3645 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003646 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3647 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (curwin->w_cursor.lnum > 1000000L)
3649 n = (int)(((long)curwin->w_cursor.lnum) /
3650 ((long)curbuf->b_ml.ml_line_count / 100L));
3651 else
3652 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3653 (long)curbuf->b_ml.ml_line_count);
3654 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003655 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656#ifdef FEAT_CMDL_INFO
3657 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003658 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003659 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003660 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3661 curbuf->b_ml.ml_line_count),
3662 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663#endif
3664 else
3665 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003666 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 _("line %ld of %ld --%d%%-- col "),
3668 (long)curwin->w_cursor.lnum,
3669 (long)curbuf->b_ml.ml_line_count,
3670 n);
3671 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003672 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003673 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3675 }
3676
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3678 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 if (dont_truncate)
3681 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003682 // Temporarily set msg_scroll to avoid the message being truncated.
3683 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 msg_start();
3685 n = msg_scroll;
3686 msg_scroll = TRUE;
3687 msg(buffer);
3688 msg_scroll = n;
3689 }
3690 else
3691 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003692 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003694 // Need to repeat the message after redrawing when:
3695 // - When restart_edit is set (otherwise there will be a delay
3696 // before redrawing).
3697 // - When the screen was scrolled but there is no wait-return
3698 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003699 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701
3702 vim_free(buffer);
3703}
3704
3705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706col_print(
3707 char_u *buf,
3708 size_t buflen,
3709 int col,
3710 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003713 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003715 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716}
3717
3718#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719static char_u *lasttitle = NULL;
3720static char_u *lasticon = NULL;
3721
Bram Moolenaar84a93082018-06-16 22:58:15 +02003722/*
3723 * Put the file name in the title bar and icon of the window.
3724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003726maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
3728 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003729 char_u *title_str = NULL;
3730 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 int maxlen = 0;
3732 int len;
3733 int mustset;
3734 char_u buf[IOSIZE];
3735 int off;
3736
3737 if (!redrawing())
3738 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003739 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 need_maketitle = TRUE;
3741 return;
3742 }
3743
3744 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003745 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003746 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 if (p_title)
3749 {
3750 if (p_titlelen > 0)
3751 {
3752 maxlen = p_titlelen * Columns / 100;
3753 if (maxlen < 10)
3754 maxlen = 10;
3755 }
3756
Bram Moolenaar84a93082018-06-16 22:58:15 +02003757 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 if (*p_titlestring != NUL)
3759 {
3760#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003761 if (stl_syntax & STL_IN_TITLE)
3762 {
3763 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003764 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003765
3766# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003767 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003768# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003769 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003770 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003771 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003772 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003773 set_string_option_direct((char_u *)"titlestring", -1,
3774 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 else
3777#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003778 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 else
3781 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003782 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
Bram Moolenaar2c666692012-09-05 13:30:40 +02003784#define SPACE_FOR_FNAME (IOSIZE - 100)
3785#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003786#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003788 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003789#ifdef FEAT_TERMINAL
3790 else if (curbuf->b_term != NULL)
3791 {
3792 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3793 SPACE_FOR_FNAME);
3794 }
3795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 else
3797 {
3798 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003799 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802
Bram Moolenaar21554412017-07-24 21:44:43 +02003803#ifdef FEAT_TERMINAL
3804 if (curbuf->b_term == NULL)
3805#endif
3806 switch (bufIsChanged(curbuf)
3807 + (curbuf->b_p_ro * 2)
3808 + (!curbuf->b_p_ma * 4))
3809 {
3810 case 1: STRCAT(buf, " +"); break;
3811 case 2: STRCAT(buf, " ="); break;
3812 case 3: STRCAT(buf, " =+"); break;
3813 case 4:
3814 case 6: STRCAT(buf, " -"); break;
3815 case 5:
3816 case 7: STRCAT(buf, " -+"); break;
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaar21554412017-07-24 21:44:43 +02003819 if (curbuf->b_fname != NULL
3820#ifdef FEAT_TERMINAL
3821 && curbuf->b_term == NULL
3822#endif
3823 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003825 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 off = (int)STRLEN(buf);
3827 buf[off++] = ' ';
3828 buf[off++] = '(';
3829 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003830 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003832 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (isalpha(buf[off]) && buf[off + 1] == ':')
3834 off += 2;
3835#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003836 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003837 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003839 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003840 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003841 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003842 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003846
Bram Moolenaarc667da52019-11-30 20:52:27 +01003847 // Translate unprintable chars and concatenate. Keep some
3848 // room for the server name. When there is no room (very long
3849 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003850 if (off < SPACE_FOR_DIR)
3851 {
3852 p = transstr(buf + off);
3853 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3854 vim_free(p);
3855 }
3856 else
3857 {
3858 vim_strncpy(buf + off, (char_u *)"...",
3859 (size_t)(SPACE_FOR_ARGNR - off));
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 STRCAT(buf, ")");
3862 }
3863
Bram Moolenaar2c666692012-09-05 13:30:40 +02003864 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866#if defined(FEAT_CLIENTSERVER)
3867 if (serverName != NULL)
3868 {
3869 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003870 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872 else
3873#endif
3874 STRCAT(buf, " - VIM");
3875
3876 if (maxlen > 0)
3877 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003878 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003879 if (vim_strsize(buf) > maxlen)
3880 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 }
3883 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003884 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
3886 if (p_icon)
3887 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003888 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (*p_iconstring != NUL)
3890 {
3891#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003892 if (stl_syntax & STL_IN_ICON)
3893 {
3894 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003895 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003896
3897# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003898 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003899# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003900 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003901 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003902 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003903 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003904 set_string_option_direct((char_u *)"iconstring", -1,
3905 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 else
3908#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003909 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 else
3912 {
3913 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003914 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003915 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003916 p = gettail(curbuf->b_ffname);
3917 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003918 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003919 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (len > 100)
3921 {
3922 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003924 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003925 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003927 STRCPY(icon_str, p);
3928 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930 }
3931
Bram Moolenaar84a93082018-06-16 22:58:15 +02003932 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 if (mustset)
3935 resettitle();
3936}
3937
3938/*
3939 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3940 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003941 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 */
3943 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003944value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
3946 if ((str == NULL) != (*last == NULL)
3947 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3948 {
3949 vim_free(*last);
3950 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003953 mch_restore_title(
3954 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003959 return TRUE;
3960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 return FALSE;
3963}
3964
3965/*
3966 * Put current window title back (used after calling a shell)
3967 */
3968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 mch_settitle(lasttitle, lasticon);
3972}
Bram Moolenaarea408852005-06-25 22:49:46 +00003973
3974# if defined(EXITFREE) || defined(PROTO)
3975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003976free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003977{
3978 vim_free(lasttitle);
3979 vim_free(lasticon);
3980}
3981# endif
3982
Bram Moolenaarc667da52019-11-30 20:52:27 +01003983#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003985#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003987 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * Return length of string in screen cells.
3989 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003990 * Normally works for window "wp", except when working for 'tabline' then it
3991 * is "curwin".
3992 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * Items are drawn interspersed with the text that surrounds it
3994 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3995 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3996 *
3997 * If maxwidth is not zero, the string will be filled at any middle marker
3998 * or truncated if too long, fillchar is used for all whitespace.
3999 */
4000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001build_stl_str_hl(
4002 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004003 char_u *out, // buffer to write into != NameBuff
4004 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004005 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004006 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004007 int fillchar,
4008 int maxwidth,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004009 struct stl_hlrec *hltab, // return: HL attributes (can be NULL)
4010 struct stl_hlrec *tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaar10772302019-01-20 18:25:54 +01004012 linenr_T lnum;
4013 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 char_u *p;
4015 char_u *s;
4016 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004017 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004019 win_T *save_curwin;
4020 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004021 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#endif
4023 int empty_line;
4024 colnr_T virtcol;
4025 long l;
4026 long n;
4027 int prevchar_isflag;
4028 int prevchar_isitem;
4029 int itemisflag;
4030 int fillable;
4031 char_u *str;
4032 long num;
4033 int width;
4034 int itemcnt;
4035 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004036 int group_end_userhl;
4037 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 int groupitem[STL_MAX_ITEM];
4039 int groupdepth;
4040 struct stl_item
4041 {
4042 char_u *start;
4043 int minwid;
4044 int maxwid;
4045 enum
4046 {
4047 Normal,
4048 Empty,
4049 Group,
4050 Middle,
4051 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004052 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 Trunc
4054 } type;
4055 } item[STL_MAX_ITEM];
4056 int minwid;
4057 int maxwid;
4058 int zeropad;
4059 char_u base;
4060 char_u opt;
4061#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004062 char_u buf_tmp[TMPLEN];
4063 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004064 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004065 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004066 int save_must_redraw = must_redraw;
4067 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004068
4069#ifdef FEAT_EVAL
4070 /*
4071 * When the format starts with "%!" then evaluate it as an expression and
4072 * use the result as the actual format string.
4073 */
4074 if (fmt[0] == '%' && fmt[1] == '!')
4075 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004076 typval_T tv;
4077
4078 tv.v_type = VAR_NUMBER;
4079 tv.vval.v_number = wp->w_id;
4080 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4081
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004082 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
4083 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004084 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004085
4086 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004087 }
4088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 if (fillchar == 0)
4091 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004092 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004093 else if (mb_char2len(fillchar) > 1)
4094 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004095
Bram Moolenaar10772302019-01-20 18:25:54 +01004096 // The cursor in windows other than the current one isn't always
4097 // up-to-date, esp. because of autocommands and timers.
4098 lnum = wp->w_cursor.lnum;
4099 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4100 {
4101 lnum = wp->w_buffer->b_ml.ml_line_count;
4102 wp->w_cursor.lnum = lnum;
4103 }
4104
4105 // Get line & check if empty (cursorpos will show "0-1"). Note that
4106 // p will become invalid when getting another buffer line.
4107 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004108 empty_line = (*p == NUL);
4109
Bram Moolenaar10772302019-01-20 18:25:54 +01004110 // Get the byte value now, in case we need it below. This is more efficient
4111 // than making a copy of the line.
4112 len = STRLEN(p);
4113 if (wp->w_cursor.col > (colnr_T)len)
4114 {
4115 // Line may have changed since checking the cursor column, or the lnum
4116 // was adjusted above.
4117 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004118 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004119 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004120 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004121 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004122 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 groupdepth = 0;
4125 p = out;
4126 curitem = 0;
4127 prevchar_isflag = TRUE;
4128 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004129 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004131 if (curitem == STL_MAX_ITEM)
4132 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004133 // There are too many items. Add the error code to the statusline
4134 // to give the user a hint about what went wrong.
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004135 if (p + 6 < out + outlen)
4136 {
4137 mch_memmove(p, " E541", (size_t)5);
4138 p += 5;
4139 }
4140 break;
4141 }
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (*s != NUL && *s != '%')
4144 prevchar_isflag = prevchar_isitem = FALSE;
4145
4146 /*
4147 * Handle up to the next '%' or the end.
4148 */
4149 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4150 *p++ = *s++;
4151 if (*s == NUL || p + 1 >= out + outlen)
4152 break;
4153
4154 /*
4155 * Handle one '%' item.
4156 */
4157 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004158 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (*s == '%')
4161 {
4162 if (p + 1 >= out + outlen)
4163 break;
4164 *p++ = *s++;
4165 prevchar_isflag = prevchar_isitem = FALSE;
4166 continue;
4167 }
4168 if (*s == STL_MIDDLEMARK)
4169 {
4170 s++;
4171 if (groupdepth > 0)
4172 continue;
4173 item[curitem].type = Middle;
4174 item[curitem++].start = p;
4175 continue;
4176 }
4177 if (*s == STL_TRUNCMARK)
4178 {
4179 s++;
4180 item[curitem].type = Trunc;
4181 item[curitem++].start = p;
4182 continue;
4183 }
4184 if (*s == ')')
4185 {
4186 s++;
4187 if (groupdepth < 1)
4188 continue;
4189 groupdepth--;
4190
4191 t = item[groupitem[groupdepth]].start;
4192 *p = NUL;
4193 l = vim_strsize(t);
4194 if (curitem > groupitem[groupdepth] + 1
4195 && item[groupitem[groupdepth]].minwid == 0)
4196 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004197 // remove group if all items are empty and highlight group
4198 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004199 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004200 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4201 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004202 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004203 {
4204 group_start_userhl = group_end_userhl = item[n].minwid;
4205 break;
4206 }
4207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004209 {
4210 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004212 if (item[n].type == Highlight)
4213 group_end_userhl = item[n].minwid;
4214 }
4215 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 p = t;
4218 l = 0;
Bram Moolenaardbe5d362020-02-08 18:35:31 +01004219 // do not use the highlighting from the removed group
4220 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4221 if (item[n].type == Highlight)
4222 item[n].type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 }
4225 if (l > item[groupitem[groupdepth]].maxwid)
4226 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004227 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (has_mbyte)
4229 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004230 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 n = 0;
4232 while (l >= item[groupitem[groupdepth]].maxwid)
4233 {
4234 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004235 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 }
4238 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004239 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004242 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004244
4245 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 while (++l < item[groupitem[groupdepth]].minwid)
4247 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
Bram Moolenaarc667da52019-11-30 20:52:27 +01004249 // correct the start of the items for the truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4251 {
4252 item[l].start -= n;
4253 if (item[l].start < t)
4254 item[l].start = t;
4255 }
4256 }
4257 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4258 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004259 // fill
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 n = item[groupitem[groupdepth]].minwid;
4261 if (n < 0)
4262 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004263 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 n = 0 - n;
4265 while (l++ < n && p + 1 < out + outlen)
4266 *p++ = fillchar;
4267 }
4268 else
4269 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004270 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004271 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 l = n - l;
4273 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004274 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 p += l;
4276 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4277 item[n].start += l;
4278 for ( ; l > 0; l--)
4279 *t++ = fillchar;
4280 }
4281 }
4282 continue;
4283 }
4284 minwid = 0;
4285 maxwid = 9999;
4286 zeropad = FALSE;
4287 l = 1;
4288 if (*s == '0')
4289 {
4290 s++;
4291 zeropad = TRUE;
4292 }
4293 if (*s == '-')
4294 {
4295 s++;
4296 l = -1;
4297 }
4298 if (VIM_ISDIGIT(*s))
4299 {
4300 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004301 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 minwid = 0;
4303 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004304 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
4306 item[curitem].type = Highlight;
4307 item[curitem].start = p;
4308 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4309 s++;
4310 curitem++;
4311 continue;
4312 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004313 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4314 {
4315 if (*s == STL_TABCLOSENR)
4316 {
4317 if (minwid == 0)
4318 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004319 // %X ends the close label, go back to the previously
4320 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004321 for (n = curitem - 1; n >= 0; --n)
4322 if (item[n].type == TabPage && item[n].minwid >= 0)
4323 {
4324 minwid = item[n].minwid;
4325 break;
4326 }
4327 }
4328 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004329 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004330 minwid = - minwid;
4331 }
4332 item[curitem].type = TabPage;
4333 item[curitem].start = p;
4334 item[curitem].minwid = minwid;
4335 s++;
4336 curitem++;
4337 continue;
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (*s == '.')
4340 {
4341 s++;
4342 if (VIM_ISDIGIT(*s))
4343 {
4344 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004345 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 maxwid = 50;
4347 }
4348 }
4349 minwid = (minwid > 50 ? 50 : minwid) * l;
4350 if (*s == '(')
4351 {
4352 groupitem[groupdepth++] = curitem;
4353 item[curitem].type = Group;
4354 item[curitem].start = p;
4355 item[curitem].minwid = minwid;
4356 item[curitem].maxwid = maxwid;
4357 s++;
4358 curitem++;
4359 continue;
4360 }
4361 if (vim_strchr(STL_ALL, *s) == NULL)
4362 {
4363 s++;
4364 continue;
4365 }
4366 opt = *s++;
4367
Bram Moolenaarc667da52019-11-30 20:52:27 +01004368 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 base = 'D';
4370 itemisflag = FALSE;
4371 fillable = TRUE;
4372 num = -1;
4373 str = NULL;
4374 switch (opt)
4375 {
4376 case STL_FILEPATH:
4377 case STL_FULLPATH:
4378 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004379 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004381 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 else
4383 {
4384 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004385 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4387 }
4388 trans_characters(NameBuff, MAXPATHL);
4389 if (opt != STL_FILENAME)
4390 str = NameBuff;
4391 else
4392 str = gettail(NameBuff);
4393 break;
4394
Bram Moolenaarc667da52019-11-30 20:52:27 +01004395 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 itemisflag = TRUE;
4397 t = p;
4398 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4399 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004400 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 break;
4402 s++;
4403 *p = 0;
4404 p = t;
4405
4406#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004407 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4408 "%d", curbuf->b_fnum);
4409 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4410 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4411 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004413 save_curbuf = curbuf;
4414 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004415 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 curwin = wp;
4417 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004418 // Visual mode is only valid in the current window.
4419 if (curwin != save_curwin)
4420 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004422 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004424 curwin = save_curwin;
4425 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004426 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004427 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004428 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 if (str != NULL && *str != 0)
4431 {
4432 if (*skipdigits(str) == NUL)
4433 {
4434 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004435 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 itemisflag = FALSE;
4437 }
4438 }
4439#endif
4440 break;
4441
4442 case STL_LINE:
4443 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4444 ? 0L : (long)(wp->w_cursor.lnum);
4445 break;
4446
4447 case STL_NUMLINES:
4448 num = wp->w_buffer->b_ml.ml_line_count;
4449 break;
4450
4451 case STL_COLUMN:
4452 num = !(State & INSERT) && empty_line
4453 ? 0 : (int)wp->w_cursor.col + 1;
4454 break;
4455
4456 case STL_VIRTCOL:
4457 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004458 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 virtcol = wp->w_virtcol;
4460 if (wp->w_p_list && lcs_tab1 == NUL)
4461 {
4462 wp->w_p_list = FALSE;
4463 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4464 wp->w_p_list = TRUE;
4465 }
4466 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004467 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (opt == STL_VIRTCOL_ALT
4469 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4470 ? 0 : (int)wp->w_cursor.col + 1)))
4471 break;
4472 num = (long)virtcol;
4473 break;
4474
4475 case STL_PERCENTAGE:
4476 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4477 (long)wp->w_buffer->b_ml.ml_line_count);
4478 break;
4479
4480 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004481 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004482 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 break;
4484
4485 case STL_ARGLISTSTAT:
4486 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004487 buf_tmp[0] = 0;
4488 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4489 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 break;
4491
4492 case STL_KEYMAP:
4493 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004494 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4495 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 break;
4497 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004498#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004499 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500#else
4501 num = 0;
4502#endif
4503 break;
4504
4505 case STL_BUFNO:
4506 num = wp->w_buffer->b_fnum;
4507 break;
4508
4509 case STL_OFFSET_X:
4510 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004511 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 case STL_OFFSET:
4513#ifdef FEAT_BYTEOFF
4514 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4515 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4516 0L : l + 1 + (!(State & INSERT) && empty_line ?
4517 0 : (int)wp->w_cursor.col);
4518#endif
4519 break;
4520
4521 case STL_BYTEVAL_X:
4522 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004523 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004525 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (num == NL)
4527 num = 0;
4528 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4529 num = NL;
4530 break;
4531
4532 case STL_ROFLAG:
4533 case STL_ROFLAG_ALT:
4534 itemisflag = TRUE;
4535 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004536 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 break;
4538
4539 case STL_HELPFLAG:
4540 case STL_HELPFLAG_ALT:
4541 itemisflag = TRUE;
4542 if (wp->w_buffer->b_help)
4543 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004544 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 break;
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 case STL_FILETYPE:
4548 if (*wp->w_buffer->b_p_ft != NUL
4549 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4550 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004551 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004552 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004553 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 break;
4556
4557 case STL_FILETYPE_ALT:
4558 itemisflag = TRUE;
4559 if (*wp->w_buffer->b_p_ft != NUL
4560 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4561 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004562 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004563 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004564 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004566 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
Bram Moolenaar4033c552017-09-16 20:54:51 +02004570#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 case STL_PREVIEWFLAG:
4572 case STL_PREVIEWFLAG_ALT:
4573 itemisflag = TRUE;
4574 if (wp->w_p_pvw)
4575 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4576 : _("[Preview]"));
4577 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004578
4579 case STL_QUICKFIX:
4580 if (bt_quickfix(wp->w_buffer))
4581 str = (char_u *)(wp->w_llist_ref
4582 ? _(msg_loclist)
4583 : _(msg_qflist));
4584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#endif
4586
4587 case STL_MODIFIED:
4588 case STL_MODIFIED_ALT:
4589 itemisflag = TRUE;
4590 switch ((opt == STL_MODIFIED_ALT)
4591 + bufIsChanged(wp->w_buffer) * 2
4592 + (!wp->w_buffer->b_p_ma) * 4)
4593 {
4594 case 2: str = (char_u *)"[+]"; break;
4595 case 3: str = (char_u *)",+"; break;
4596 case 4: str = (char_u *)"[-]"; break;
4597 case 5: str = (char_u *)",-"; break;
4598 case 6: str = (char_u *)"[+-]"; break;
4599 case 7: str = (char_u *)",+-"; break;
4600 }
4601 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004602
4603 case STL_HIGHLIGHT:
4604 t = s;
4605 while (*s != '#' && *s != NUL)
4606 ++s;
4607 if (*s == '#')
4608 {
4609 item[curitem].type = Highlight;
4610 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004611 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004612 curitem++;
4613 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004614 if (*s != NUL)
4615 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004616 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618
4619 item[curitem].start = p;
4620 item[curitem].type = Normal;
4621 if (str != NULL && *str)
4622 {
4623 t = str;
4624 if (itemisflag)
4625 {
4626 if ((t[0] && t[1])
4627 && ((!prevchar_isitem && *t == ',')
4628 || (prevchar_isflag && *t == ' ')))
4629 t++;
4630 prevchar_isflag = TRUE;
4631 }
4632 l = vim_strsize(t);
4633 if (l > 0)
4634 prevchar_isitem = TRUE;
4635 if (l > maxwid)
4636 {
4637 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (has_mbyte)
4639 {
4640 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004641 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 l -= byte2cells(*t++);
4645 if (p + 1 >= out + outlen)
4646 break;
4647 *p++ = '<';
4648 }
4649 if (minwid > 0)
4650 {
4651 for (; l < minwid && p + 1 < out + outlen; l++)
4652 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004653 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4655 *p++ = ' ';
4656 else
4657 *p++ = fillchar;
4658 }
4659 minwid = 0;
4660 }
4661 else
4662 minwid *= -1;
4663 while (*t && p + 1 < out + outlen)
4664 {
4665 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004666 // Change a space by fillchar, unless fillchar is '-' and a
4667 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (fillable && p[-1] == ' '
4669 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4670 p[-1] = fillchar;
4671 }
4672 for (; l < minwid && p + 1 < out + outlen; l++)
4673 *p++ = fillchar;
4674 }
4675 else if (num >= 0)
4676 {
4677 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4678 char_u nstr[20];
4679
4680 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004681 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 prevchar_isitem = TRUE;
4683 t = nstr;
4684 if (opt == STL_VIRTCOL_ALT)
4685 {
4686 *t++ = '-';
4687 minwid--;
4688 }
4689 *t++ = '%';
4690 if (zeropad)
4691 *t++ = '0';
4692 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004693 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 *t = 0;
4695
4696 for (n = num, l = 1; n >= nbase; n /= nbase)
4697 l++;
4698 if (opt == STL_VIRTCOL_ALT)
4699 l++;
4700 if (l > maxwid)
4701 {
4702 l += 2;
4703 n = l - maxwid;
4704 while (l-- > maxwid)
4705 num /= nbase;
4706 *t++ = '>';
4707 *t++ = '%';
4708 *t = t[-3];
4709 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004710 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4711 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004714 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4715 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 p += STRLEN(p);
4717 }
4718 else
4719 item[curitem].type = Empty;
4720
4721 if (opt == STL_VIM_EXPR)
4722 vim_free(str);
4723
4724 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004725 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 curitem++;
4727 }
4728 *p = NUL;
4729 itemcnt = curitem;
4730
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004731#ifdef FEAT_EVAL
4732 if (usefmt != fmt)
4733 vim_free(usefmt);
4734#endif
4735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 width = vim_strsize(out);
4737 if (maxwidth > 0 && width > maxwidth)
4738 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004739 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 l = 0;
4741 if (itemcnt == 0)
4742 s = out;
4743 else
4744 {
4745 for ( ; l < itemcnt; l++)
4746 if (item[l].type == Trunc)
4747 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004748 // Truncate at %< item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 s = item[l].start;
4750 break;
4751 }
4752 if (l == itemcnt)
4753 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004754 // No %< item, truncate first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 s = item[0].start;
4756 l = 0;
4757 }
4758 }
4759
4760 if (width - vim_strsize(s) >= maxwidth)
4761 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004762 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (has_mbyte)
4764 {
4765 s = out;
4766 width = 0;
4767 for (;;)
4768 {
4769 width += ptr2cells(s);
4770 if (width >= maxwidth)
4771 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004772 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004774 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 while (++width < maxwidth)
4776 *s++ = fillchar;
4777 }
4778 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 s = out + maxwidth - 1;
4780 for (l = 0; l < itemcnt; l++)
4781 if (item[l].start > s)
4782 break;
4783 itemcnt = l;
4784 *s++ = '>';
4785 *s = 0;
4786 }
4787 else
4788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (has_mbyte)
4790 {
4791 n = 0;
4792 while (width >= maxwidth)
4793 {
4794 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004795 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 n = width - maxwidth + 1;
4800 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004801 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 *s = '<';
4803
Bram Moolenaarc667da52019-11-30 20:52:27 +01004804 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 while (++width < maxwidth)
4806 {
4807 s = s + STRLEN(s);
4808 *s++ = fillchar;
4809 *s = NUL;
4810 }
4811
Bram Moolenaarc667da52019-11-30 20:52:27 +01004812 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 for (; l < itemcnt; l++)
4814 {
4815 if (item[l].start - n >= s)
4816 item[l].start -= n;
4817 else
4818 item[l].start = s;
4819 }
4820 }
4821 width = maxwidth;
4822 }
4823 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4824 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004825 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 for (l = 0; l < itemcnt; l++)
4827 if (item[l].type == Middle)
4828 break;
4829 if (l < itemcnt)
4830 {
4831 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004832 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 for (s = item[l].start; s < p; s++)
4834 *s = fillchar;
4835 for (l++; l < itemcnt; l++)
4836 item[l].start += maxwidth - width;
4837 width = maxwidth;
4838 }
4839 }
4840
Bram Moolenaarc667da52019-11-30 20:52:27 +01004841 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004842 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004844 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 for (l = 0; l < itemcnt; l++)
4846 {
4847 if (item[l].type == Highlight)
4848 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004849 sp->start = item[l].start;
4850 sp->userhl = item[l].minwid;
4851 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004854 sp->start = NULL;
4855 sp->userhl = 0;
4856 }
4857
Bram Moolenaarc667da52019-11-30 20:52:27 +01004858 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004859 if (tabtab != NULL)
4860 {
4861 sp = tabtab;
4862 for (l = 0; l < itemcnt; l++)
4863 {
4864 if (item[l].type == TabPage)
4865 {
4866 sp->start = item[l].start;
4867 sp->userhl = item[l].minwid;
4868 sp++;
4869 }
4870 }
4871 sp->start = NULL;
4872 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874
Bram Moolenaarc667da52019-11-30 20:52:27 +01004875 // When inside update_screen we do not want redrawing a stausline, ruler,
4876 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004877 if (updating_screen)
4878 {
4879 must_redraw = save_must_redraw;
4880 curwin->w_redr_type = save_redr_type;
4881 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004882
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return width;
4884}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004885#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004887#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4888 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004890 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4891 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
4893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004894get_rel_pos(
4895 win_T *wp,
4896 char_u *buf,
4897 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004899 long above; // number of lines above window
4900 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901
Bram Moolenaarc667da52019-11-30 20:52:27 +01004902 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004903 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 above = wp->w_topline - 1;
4905#ifdef FEAT_DIFF
4906 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004907 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004908 above = 0; // All buffer lines are displayed and there is an
4909 // indication of filler lines, that can be considered
4910 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#endif
4912 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4913 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004914 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4915 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004917 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004919 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 ? (int)(above / ((above + below) / 100L))
4921 : (int)(above * 100L / (above + below)));
4922}
4923#endif
4924
4925/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004926 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 * Return TRUE if it was appended.
4928 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004930append_arg_number(
4931 win_T *wp,
4932 char_u *buf,
4933 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004934 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 char_u *p;
4937
Bram Moolenaarc667da52019-11-30 20:52:27 +01004938 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 return FALSE;
4940
Bram Moolenaarc667da52019-11-30 20:52:27 +01004941 p = buf + STRLEN(buf); // go to the end of the buffer
4942 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return FALSE;
4944 *p++ = ' ';
4945 *p++ = '(';
4946 if (add_file)
4947 {
4948 STRCPY(p, "file ");
4949 p += 5;
4950 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004951 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4952 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4954 return TRUE;
4955}
4956
4957/*
4958 * If fname is not a full path, make it a full path.
4959 * Returns pointer to allocated memory (NULL for failure).
4960 */
4961 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004962fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963{
4964 /*
4965 * Force expanding the path always for Unix, because symbolic links may
4966 * mess up the full path name, even though it starts with a '/'.
4967 * Also expand when there is ".." in the file name, try to remove it,
4968 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004969 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 * For MS-Windows also expand names like "longna~1" to "longname".
4971 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004972#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 return FullName_save(fname, TRUE);
4974#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004975 if (!vim_isAbsName(fname)
4976 || strstr((char *)fname, "..") != NULL
4977 || strstr((char *)fname, "//") != NULL
4978# ifdef BACKSLASH_IN_FILENAME
4979 || strstr((char *)fname, "\\\\") != NULL
4980# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004981# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 )
4985 return FullName_save(fname, FALSE);
4986
4987 fname = vim_strsave(fname);
4988
Bram Moolenaar9b942202007-10-03 12:31:33 +00004989# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004990 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004991 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00004992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
4994 return fname;
4995#endif
4996}
4997
4998/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004999 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5000 * "*ffname" becomes a pointer to allocated memory (or NULL).
5001 * When resolving a link both "*sfname" and "*ffname" will point to the same
5002 * allocated memory.
5003 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005004 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005007fname_expand(
5008 buf_T *buf UNUSED,
5009 char_u **ffname,
5010 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005012 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005014 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005016 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018#ifdef FEAT_SHORTCUT
5019 if (!buf->b_p_bin)
5020 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005021 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005023 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005024 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005025 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 vim_free(*ffname);
5028 *ffname = rfname;
5029 *sfname = rfname;
5030 }
5031 }
5032#endif
5033}
5034
5035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 * Open a window for a number of buffers.
5037 */
5038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
5041 buf_T *buf;
5042 win_T *wp, *wpnext;
5043 int split_ret = OK;
5044 int p_ea_save;
5045 int open_wins = 0;
5046 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005047 int count; // Maximum number of windows to open.
5048 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005049 int had_tab = cmdmod.tab;
5050 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaarc667da52019-11-30 20:52:27 +01005052 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 count = 9999;
5054 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005055 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5057 all = FALSE;
5058 else
5059 all = TRUE;
5060
5061 setpcmark();
5062
5063#ifdef FEAT_GUI
5064 need_mouse_correct = TRUE;
5065#endif
5066
5067 /*
5068 * Close superfluous windows (two windows for the same buffer).
5069 * Also close windows that are not full-width.
5070 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005071 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005072 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005073 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005075 tpnext = curtab->tp_next;
5076 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005078 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005079 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005080 || ((cmdmod.split & WSP_VERT)
5081 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005082 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005083 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005084 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005085 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005086 {
5087 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005088 wpnext = firstwin; // just in case an autocommand does
5089 // something strange with windows
5090 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005091 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005092 }
5093 else
5094 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005096
Bram Moolenaarc667da52019-11-30 20:52:27 +01005097 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005098 if (had_tab == 0 || tpnext == NULL)
5099 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005100 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102
5103 /*
5104 * Go through the buffer list. When a buffer doesn't have a window yet,
5105 * open one. Otherwise move the window to the right position.
5106 * Watch out for autocommands that delete buffers or windows!
5107 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005108 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5113 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005114 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5116 continue;
5117
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005118 if (had_tab != 0)
5119 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005120 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005121 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005122 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005123 else
5124 wp = NULL;
5125 }
5126 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005127 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005128 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005129 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005130 if (wp->w_buffer == buf)
5131 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005132 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005133 if (wp != NULL)
5134 win_move_after(wp, curwin);
5135 }
5136
5137 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005139 bufref_T bufref;
5140
5141 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005142
Bram Moolenaarc667da52019-11-30 20:52:27 +01005143 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005145 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5147 ++open_wins;
5148 p_ea = p_ea_save;
5149 if (split_ret == FAIL)
5150 continue;
5151
Bram Moolenaarc667da52019-11-30 20:52:27 +01005152 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005155 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005157 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 break;
5160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (swap_exists_action == SEA_QUIT)
5162 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005163#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005164 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005165
Bram Moolenaarc667da52019-11-30 20:52:27 +01005166 // Reset the error/interrupt/exception state here so that
5167 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005168 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005169#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005170
Bram Moolenaarc667da52019-11-30 20:52:27 +01005171 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 win_close(curwin, TRUE);
5173 --open_wins;
5174 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005175 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005176
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005177#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005178 // Restore the error/interrupt/exception state if not
5179 // discarded by a new aborting error, interrupt, or uncaught
5180 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005181 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 else
5185 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187
5188 ui_breakcheck();
5189 if (got_int)
5190 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005191 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005194#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005195 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005196 if (aborting())
5197 break;
5198#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005199 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005200 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5201 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005204 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 /*
5208 * Close superfluous windows.
5209 */
5210 for (wp = lastwin; open_wins > count; )
5211 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005212 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (!win_valid(wp))
5215 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005216 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 wp = lastwin;
5218 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005219 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005221 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 --open_wins;
5223 wp = lastwin;
5224 }
5225 else
5226 {
5227 wp = wp->w_prev;
5228 if (wp == NULL)
5229 break;
5230 }
5231 }
5232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005235static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005236
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237/*
5238 * do_modelines() - process mode lines for the current file
5239 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005240 * "flags" can be:
5241 * OPT_WINONLY only set options local to window
5242 * OPT_NOWIN don't set options local to window
5243 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 * Returns immediately if the "ml" option isn't set.
5245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005247do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005249 linenr_T lnum;
5250 int nmlines;
5251 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5254 return;
5255
Bram Moolenaarc667da52019-11-30 20:52:27 +01005256 // Disallow recursive entry here. Can happen when executing a modeline
5257 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 if (entered)
5259 return;
5260
5261 ++entered;
5262 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5263 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005264 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 nmlines = 0;
5266
5267 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5268 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005269 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 nmlines = 0;
5271 --entered;
5272}
5273
Bram Moolenaarc667da52019-11-30 20:52:27 +01005274#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276/*
5277 * chk_modeline() - check a single line for a mode string
5278 * Return FAIL if an error encountered.
5279 */
5280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005281chk_modeline(
5282 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005283 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
5285 char_u *s;
5286 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005287 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 int prev;
5289 int vers;
5290 int end;
5291 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005293 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01005295 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 prev = -1;
5298 for (s = ml_get(lnum); *s != NUL; ++s)
5299 {
5300 if (prev == -1 || vim_isspace(prev))
5301 {
5302 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5303 || STRNCMP(s, "vi:", (size_t)3) == 0)
5304 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005305 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005306 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5309 e = s + 4;
5310 else
5311 e = s + 3;
5312 vers = getdigits(&e);
5313 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005314 && (s[0] != 'V'
5315 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 && (s[3] == ':'
5317 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5318 || (VIM_VERSION_100 < vers && s[3] == '<')
5319 || (VIM_VERSION_100 > vers && s[3] == '>')
5320 || (VIM_VERSION_100 == vers && s[3] == '=')))
5321 break;
5322 }
5323 }
5324 prev = *s;
5325 }
5326
5327 if (*s)
5328 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005329 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ++s;
5331 while (s[-1] != ':');
5332
Bram Moolenaarc667da52019-11-30 20:52:27 +01005333 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (linecopy == NULL)
5335 return FAIL;
5336
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005337 // prepare for emsg()
5338 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005339 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 end = FALSE;
5342 while (end == FALSE)
5343 {
5344 s = skipwhite(s);
5345 if (*s == NUL)
5346 break;
5347
5348 /*
5349 * Find end of set command: ':' or end of line.
5350 * Skip over "\:", replacing it with ":".
5351 */
5352 for (e = s; *e != ':' && *e != NUL; ++e)
5353 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005354 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (*e == NUL)
5356 end = TRUE;
5357
5358 /*
5359 * If there is a "set" command, require a terminating ':' and
5360 * ignore the stuff after the ':'.
5361 * "vi:set opt opt opt: foo" -- foo not interpreted
5362 * "vi:opt opt opt: foo" -- foo interpreted
5363 * Accept "se" for compatibility with Elvis.
5364 */
5365 if (STRNCMP(s, "set ", (size_t)4) == 0
5366 || STRNCMP(s, "se ", (size_t)3) == 0)
5367 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005368 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 break;
5370 end = TRUE;
5371 s = vim_strchr(s, ' ') + 1;
5372 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005373 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
Bram Moolenaarc667da52019-11-30 20:52:27 +01005375 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005377 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005379 save_current_sctx = current_sctx;
5380 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005381 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005382 current_sctx.sc_lnum = lnum;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005383 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005385 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005386 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005387
Bram Moolenaara3227e22006-03-08 21:32:40 +00005388 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005389
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005390 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005392 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005394 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 break;
5396 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005397 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399
Bram Moolenaare31ee862020-01-07 20:59:34 +01005400 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005401 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 vim_free(linecopy);
5403 }
5404 return retval;
5405}
5406
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005407/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005408 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5409 */
5410 int
5411bt_normal(buf_T *buf)
5412{
5413 return buf != NULL && buf->b_p_bt[0] == NUL;
5414}
5415
Bram Moolenaar113e1072019-01-20 15:30:40 +01005416#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005417/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005418 * Return TRUE if "buf" is the quickfix buffer.
5419 */
5420 int
5421bt_quickfix(buf_T *buf)
5422{
5423 return buf != NULL && buf->b_p_bt[0] == 'q';
5424}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005425#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005426
Bram Moolenaar113e1072019-01-20 15:30:40 +01005427#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005428/*
5429 * Return TRUE if "buf" is a terminal buffer.
5430 */
5431 int
5432bt_terminal(buf_T *buf)
5433{
5434 return buf != NULL && buf->b_p_bt[0] == 't';
5435}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005436#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005437
5438/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005439 * Return TRUE if "buf" is a help buffer.
5440 */
5441 int
5442bt_help(buf_T *buf)
5443{
5444 return buf != NULL && buf->b_help;
5445}
5446
5447/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005448 * Return TRUE if "buf" is a prompt buffer.
5449 */
5450 int
5451bt_prompt(buf_T *buf)
5452{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005453 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5454}
5455
5456/*
5457 * Return TRUE if "buf" is a buffer for a popup window.
5458 */
5459 int
5460bt_popup(buf_T *buf)
5461{
5462 return buf != NULL && buf->b_p_bt != NULL
5463 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005464}
5465
5466/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005467 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5468 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005469 */
5470 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005471bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005472{
5473 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5474 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005475 || buf->b_p_bt[0] == 't'
5476 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005477}
5478
5479/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005480 * Return TRUE if "buf" has 'buftype' set to "nofile".
5481 */
5482 int
5483bt_nofile(buf_T *buf)
5484{
5485 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5486}
5487
5488/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005489 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5490 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005491 */
5492 int
5493bt_dontwrite(buf_T *buf)
5494{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005495 return buf != NULL && (buf->b_p_bt[0] == 'n'
5496 || buf->b_p_bt[0] == 't'
5497 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005498}
5499
Bram Moolenaar113e1072019-01-20 15:30:40 +01005500#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005501 int
5502bt_dontwrite_msg(buf_T *buf)
5503{
5504 if (bt_dontwrite(buf))
5505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005506 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005507 return TRUE;
5508 }
5509 return FALSE;
5510}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005511#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005512
5513/*
5514 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5515 * and 'bufhidden'.
5516 */
5517 int
5518buf_hide(buf_T *buf)
5519{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005520 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005521 switch (buf->b_p_bh[0])
5522 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005523 case 'u': // "unload"
5524 case 'w': // "wipe"
5525 case 'd': return FALSE; // "delete"
5526 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005527 }
5528 return (p_hid || cmdmod.hide);
5529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531/*
5532 * Return special buffer name.
5533 * Returns NULL when the buffer has a normal file name.
5534 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005536buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005538#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005540 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005541 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005542 * Differentiate between the quickfix and location list buffers using
5543 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005544 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005545 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005546 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005547 else
5548 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005551
Bram Moolenaarc667da52019-11-30 20:52:27 +01005552 // There is no _file_ when 'buftype' is "nofile", b_sfname
5553 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005554 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005556#ifdef FEAT_TERMINAL
5557 if (buf->b_term != NULL)
5558 return term_get_status_text(buf->b_term);
5559#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005560 if (buf->b_fname != NULL)
5561 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005562#ifdef FEAT_JOB_CHANNEL
5563 if (bt_prompt(buf))
5564 return (char_u *)_("[Prompt]");
5565#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005566#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005567 if (bt_popup(buf))
5568 return (char_u *)_("[Popup]");
5569#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005570 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005574 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 return NULL;
5576}
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578/*
5579 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5580 */
5581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005582set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 if (on != curbuf->b_p_bl)
5585 {
5586 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 if (on)
5588 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5589 else
5590 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592}
5593
5594/*
5595 * Read the file for "buf" again and check if the contents changed.
5596 * Return TRUE if it changed or this could not be checked.
5597 */
5598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005599buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 buf_T *newbuf;
5602 int differ = TRUE;
5603 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 exarg_T ea;
5606
Bram Moolenaarc667da52019-11-30 20:52:27 +01005607 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5609 if (newbuf == NULL)
5610 return TRUE;
5611
Bram Moolenaarc667da52019-11-30 20:52:27 +01005612 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if (prep_exarg(&ea, buf) == FAIL)
5614 {
5615 wipe_buffer(newbuf, FALSE);
5616 return TRUE;
5617 }
5618
Bram Moolenaarc667da52019-11-30 20:52:27 +01005619 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar4770d092006-01-12 23:22:24 +00005622 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 && readfile(buf->b_ffname, buf->b_fname,
5624 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5625 &ea, READ_NEW | READ_DUMMY) == OK)
5626 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005627 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5629 {
5630 differ = FALSE;
5631 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5632 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5633 {
5634 differ = TRUE;
5635 break;
5636 }
5637 }
5638 }
5639 vim_free(ea.cmd);
5640
Bram Moolenaarc667da52019-11-30 20:52:27 +01005641 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaarc667da52019-11-30 20:52:27 +01005644 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 wipe_buffer(newbuf, FALSE);
5646
5647 return differ;
5648}
5649
5650/*
5651 * Wipe out a buffer and decrement the last buffer number if it was used for
5652 * this buffer. Call this to wipe out a temp buffer that does not contain any
5653 * marks.
5654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005656wipe_buffer(
5657 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005658 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 if (buf->b_fnum == top_file_num - 1)
5661 --top_file_num;
5662
Bram Moolenaarc667da52019-11-30 20:52:27 +01005663 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005664 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005665
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005666 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005669 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670}