blob: 318ce7f9885f80645ac106c3c71ad7776b4702e2 [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 Moolenaar00d253e2020-04-06 22:13:01 +020055#define FOR_ALL_BUFS_FROM_LAST(buf) \
56 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
57
Bram Moolenaar4033c552017-09-16 20:54:51 +020058#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059static char *msg_loclist = N_("[Location List]");
60static char *msg_qflist = N_("[Quickfix List]");
61#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010062static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020064// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020065static int buf_free_count = 0;
66
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020067static int top_file_num = 1; // highest file number
68static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
69
70/*
71 * Return the highest possible buffer number.
72 */
73 int
74get_highest_fnum(void)
75{
76 return top_file_num - 1;
77}
78
Bram Moolenaarc024b462019-06-08 18:07:21 +020079/*
80 * Read data from buffer for retrying.
81 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020082 static int
83read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010084 int read_stdin, // read file from stdin, otherwise fifo
85 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
86 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020087{
88 int retval = OK;
89 linenr_T line_count;
90
91 /*
92 * Read from the buffer which the text is already filled in and append at
93 * the end. This makes it possible to retry when 'fileformat' or
94 * 'fileencoding' was guessed wrong.
95 */
96 line_count = curbuf->b_ml.ml_line_count;
97 retval = readfile(
98 read_stdin ? NULL : curbuf->b_ffname,
99 read_stdin ? NULL : curbuf->b_fname,
100 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
101 flags | READ_BUFFER);
102 if (retval == OK)
103 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100104 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200105 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200106 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200107 }
108 else
109 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100110 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200111 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200112 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200113 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100114 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 curwin->w_cursor.lnum = 1;
116 curwin->w_cursor.col = 0;
117
118 if (read_stdin)
119 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100120 // Set or reset 'modified' before executing autocommands, so that
121 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100122 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200123 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100124 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200125 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100127 if (retval == OK)
128 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100129#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100130 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100131 curbuf, &retval);
132#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100133 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200134#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100135 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200136 }
137 return retval;
138}
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200141 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
142 */
143 void
144buffer_ensure_loaded(buf_T *buf)
145{
146 if (buf->b_ml.ml_mfp == NULL)
147 {
148 aco_save_T aco;
149
150 aucmd_prepbuf(&aco, buf);
151 swap_exists_action = SEA_NONE;
152 open_buffer(FALSE, NULL, 0);
153 aucmd_restbuf(&aco);
154 }
155}
156
157/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200158 * Open current buffer, that is: open the memfile and read the file into
159 * memory.
160 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 */
162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100163open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100164 int read_stdin, // read file from stdin
165 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
166 int flags) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167{
168 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200169 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100170#ifdef FEAT_SYN_HL
171 long old_tw = curbuf->b_p_tw;
172#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200173 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
175 /*
176 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
177 * When re-entering the same buffer, it should not change, because the
178 * user may have reset the flag by hand.
179 */
180 if (readonlymode && curbuf->b_ffname != NULL
181 && (curbuf->b_flags & BF_NEVERLOADED))
182 curbuf->b_p_ro = TRUE;
183
Bram Moolenaar4770d092006-01-12 23:22:24 +0000184 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 {
186 /*
187 * There MUST be a memfile, otherwise we can't do anything
188 * If we can't create one for the current buffer, take another buffer
189 */
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100190 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200191 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (curbuf->b_ml.ml_mfp != NULL)
193 break;
194 /*
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200195 * If there is no memfile at all, exit.
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000196 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 */
198 if (curbuf == NULL)
199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100200 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200201
202 // Don't try to do any saving, with "curbuf" NULL almost nothing
203 // will work.
204 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 getout(2);
206 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200207
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100208 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100210#ifdef FEAT_SYN_HL
211 if (old_tw != curbuf->b_p_tw)
212 check_colorcolumn(curwin);
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 return FAIL;
215 }
216
Bram Moolenaarc667da52019-11-30 20:52:27 +0100217 // The autocommands in readfile() may change the buffer, but only AFTER
218 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200219 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
Bram Moolenaarc667da52019-11-30 20:52:27 +0100222 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100223 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
225 if (curbuf->b_ffname != NULL
226#ifdef FEAT_NETBEANS_INTG
227 && netbeansReadFile
228#endif
229 )
230 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100231 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200232#ifdef UNIX
233 int save_bin = curbuf->b_p_bin;
234 int perm;
235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef FEAT_NETBEANS_INTG
237 int oldFire = netbeansFireChanges;
238
239 netbeansFireChanges = 0;
240#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200241#ifdef UNIX
242 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200243 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200244 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200245# ifdef OPEN_CHR_FILES
246 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
247# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200248 ))
249 read_fifo = TRUE;
250 if (read_fifo)
251 curbuf->b_p_bin = TRUE;
252#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100253 if (shortmess(SHM_FILEINFO))
254 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200256 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200257 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
258#ifdef UNIX
259 if (read_fifo)
260 {
261 curbuf->b_p_bin = save_bin;
262 if (retval == OK)
263 retval = read_buffer(FALSE, eap, flags);
264 }
265#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100266 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#ifdef FEAT_NETBEANS_INTG
268 netbeansFireChanges = oldFire;
269#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100270 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200271 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 fix_help_buffer();
273 }
274 else if (read_stdin)
275 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200276 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278 /*
279 * First read the text in binary mode into the buffer.
280 * Then read from that same buffer and append at the end. This makes
281 * it possible to retry when 'fileformat' or 'fileencoding' was
282 * guessed wrong.
283 */
284 curbuf->b_p_bin = TRUE;
285 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200286 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
287 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 curbuf->b_p_bin = save_bin;
289 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200290 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292
Bram Moolenaarc667da52019-11-30 20:52:27 +0100293 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100297#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100298 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100299#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302 /*
303 * Set/reset the Changed flag first, autocmds may change the buffer.
304 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200305 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100307 // When reading stdin, the buffer contents always needs writing, so set
308 // the changed flag. Unless in readonly mode: "ls | gview -".
309 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000310 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100311 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000315 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100317 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200318 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100319 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaarc667da52019-11-30 20:52:27 +0100321 // Set last_changedtick to avoid triggering a TextChanged autocommand right
322 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100323 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100324 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100325
Bram Moolenaarc667da52019-11-30 20:52:27 +0100326 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifdef FEAT_EVAL
328 if (aborting())
329#else
330 if (got_int)
331#endif
332 curbuf->b_flags |= BF_READERR;
333
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000334#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100335 // Need to update automatic folding. Do this before the autocommands,
336 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000337 foldUpdateAll(curwin);
338#endif
339
Bram Moolenaarc667da52019-11-30 20:52:27 +0100340 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (!(curwin->w_valid & VALID_TOPLINE))
342 {
343 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100344#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100348#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#endif
353
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100354 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 /*
357 * The autocommands may have changed the current buffer. Apply the
358 * modelines to the correct buffer, if it still exists and is loaded.
359 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200360 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 aco_save_T aco;
363
Bram Moolenaarc667da52019-11-30 20:52:27 +0100364 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200365 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000366 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
368
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100369#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100371 &retval);
372#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
Bram Moolenaarc667da52019-11-30 20:52:27 +0100376 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 aucmd_restbuf(&aco);
378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 }
380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 return retval;
382}
383
384/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200385 * Store "buf" in "bufref" and set the free count.
386 */
387 void
388set_bufref(bufref_T *bufref, buf_T *buf)
389{
390 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200391 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392 bufref->br_buf_free_count = buf_free_count;
393}
394
395/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200396 * Return TRUE if "bufref->br_buf" points to the same buffer as when
397 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200398 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200399 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
400 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200401 */
402 int
403bufref_valid(bufref_T *bufref)
404{
405 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200406 ? TRUE : buf_valid(bufref->br_buf)
407 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200408}
409
410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200412 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 */
414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100415buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
417 buf_T *bp;
418
Bram Moolenaarc667da52019-11-30 20:52:27 +0100419 // Assume that we more often have a recent buffer, start with the last
420 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200421 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 if (bp == buf)
423 return TRUE;
424 return FALSE;
425}
426
427/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200428 * A hash table used to quickly lookup a buffer by its number.
429 */
430static hashtab_T buf_hashtab;
431
432 static void
433buf_hashtab_add(buf_T *buf)
434{
435 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
436 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100437 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200438}
439
440 static void
441buf_hashtab_remove(buf_T *buf)
442{
443 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
444
445 if (!HASHITEM_EMPTY(hi))
446 hash_remove(&buf_hashtab, hi);
447}
448
Bram Moolenaar94f01952018-09-01 15:30:03 +0200449/*
450 * Return TRUE when buffer "buf" can be unloaded.
451 * Give an error message and return FALSE when the buffer is locked or the
452 * screen is being redrawn and the buffer is in a window.
453 */
454 static int
455can_unload_buffer(buf_T *buf)
456{
457 int can_unload = !buf->b_locked;
458
459 if (can_unload && updating_screen)
460 {
461 win_T *wp;
462
463 FOR_ALL_WINDOWS(wp)
464 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200465 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200466 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200467 break;
468 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200469 }
470 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200471 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
472 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200473 return can_unload;
474}
Bram Moolenaara997b452018-04-17 23:24:06 +0200475
Bram Moolenaar480778b2016-07-14 22:09:39 +0200476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Close the link to a buffer.
478 * "action" is used when there is no longer a window for the buffer.
479 * It can be:
480 * 0 buffer becomes hidden
481 * DOBUF_UNLOAD buffer is unloaded
482 * DOBUF_DELETE buffer is unloaded and removed from buffer list
483 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200484 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * When doing all but the first one on the current buffer, the caller should
486 * get a new buffer very soon!
487 *
488 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100489 *
490 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
491 * cause there to be only one window with this buffer. e.g. when ":quit" is
492 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100493 *
494 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 */
496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100497close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100498 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100499 buf_T *buf,
500 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100501 int abort_if_last,
502 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100505 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200506 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100507 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200508 win_T *the_curwin = curwin;
509 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200511 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
512 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaarcee52202020-03-11 14:19:58 +0100514 CHECK_CURBUF;
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200515 /*
516 * Force unloading or deleting when 'bufhidden' says so.
517 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
518 * "hide" (otherwise we could never free or delete a buffer).
519 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100520 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200521 {
522 del_buf = TRUE;
523 unload_buf = TRUE;
524 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100525 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200526 {
527 del_buf = TRUE;
528 unload_buf = TRUE;
529 wipe_buf = TRUE;
530 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100531 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200532 unload_buf = TRUE;
533
Bram Moolenaar94053a52017-08-01 21:44:33 +0200534#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200535 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200536 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100537 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200538 if (term_job_running(buf->b_term))
539 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200540 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200541 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200542 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200543 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200544
Bram Moolenaarc667da52019-11-30 20:52:27 +0100545 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200546 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200547 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200548 else
549 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100550 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200551 del_buf = FALSE;
552 unload_buf = FALSE;
553 }
554 }
555 else
556 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100557 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200558 del_buf = TRUE;
559 unload_buf = TRUE;
560 wipe_buf = TRUE;
561 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100562 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200563 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaarc667da52019-11-30 20:52:27 +0100566 // Disallow deleting the buffer when it is locked (already being closed or
567 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200568 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200569 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200570
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200572 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100574 // Set b_last_cursor when closing the last window for the buffer.
575 // Remember the last cursor position and window options of the buffer.
576 // This used to be only for the current window, but then options like
577 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (buf->b_nwindows == 1)
579 set_last_cursor(win);
580 buflist_setfpos(buf, win,
581 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
582 win->w_cursor.col, TRUE);
583 }
584
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200585 set_bufref(&bufref, buf);
586
Bram Moolenaarc667da52019-11-30 20:52:27 +0100587 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (buf->b_nwindows == 1)
589 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200590 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200591 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
592 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200593 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100594 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100595 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200596aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100597 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100599 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200600 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200601 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100602 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200603 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaarc667da52019-11-30 20:52:27 +0100605 // When the buffer becomes hidden, but is not unloaded, trigger
606 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (!unload_buf)
608 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200609 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200610 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
611 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200612 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100613 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200614 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200615 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200616 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100617 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200618 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100620#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100621 // autocmds may abort script processing
622 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200626
Bram Moolenaarc667da52019-11-30 20:52:27 +0100627 // If the buffer was in curwin and the window has changed, go back to that
628 // window, if it still exists. This avoids that ":edit x" triggering a
629 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200630 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
631 {
632 block_autocmds();
633 goto_tabpage_win(the_curtab, the_curwin);
634 unblock_autocmds();
635 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaarc667da52019-11-30 20:52:27 +0100639 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (buf->b_nwindows > 0)
641 --buf->b_nwindows;
642
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100643#ifdef FEAT_DIFF
644 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100645 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100646#endif
647
Bram Moolenaarc667da52019-11-30 20:52:27 +0100648 // Return when a window is displaying the buffer or when it's not
649 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaarc667da52019-11-30 20:52:27 +0100653 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (buf->b_ffname == NULL)
655 del_buf = TRUE;
656
Bram Moolenaarc667da52019-11-30 20:52:27 +0100657 // When closing the current buffer stop Visual mode before freeing
658 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200659 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200660#if defined(EXITFREE)
661 && !entered_free_all_mem
662#endif
663 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200664 end_visual_mode();
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 /*
667 * Free all things allocated for this buffer.
668 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
669 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100670 // Remember if we are closing the current buffer. Restore the number of
671 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 is_curbuf = (buf == curbuf);
673 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100675 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
676 + (wipe_buf ? BFA_WIPE : 0)
677 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaarc667da52019-11-30 20:52:27 +0100679 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200680 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100682#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100683 // autocmds may abort script processing
684 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 /*
689 * It's possible that autocommands change curbuf to the one being deleted.
690 * This might cause the previous curbuf to be deleted unexpectedly. But
691 * in some cases it's OK to delete the curbuf, because a new one is
692 * obtained anyway. Therefore only return if curbuf changed to the
693 * deleted buffer.
694 */
695 if (buf == curbuf && !is_curbuf)
696 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200697
Bram Moolenaar4033c552017-09-16 20:54:51 +0200698 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100699 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200700
Bram Moolenaarc667da52019-11-30 20:52:27 +0100701 // Autocommands may have opened or closed windows for this buffer.
702 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200703 if (buf->b_nwindows > 0)
704 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 /*
707 * Remove the buffer from the list.
708 */
709 if (wipe_buf)
710 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200711 if (action == DOBUF_WIPE_REUSE)
712 {
713 // we can re-use this buffer number, store it
714 if (buf_reuse.ga_itemsize == 0)
715 ga_init2(&buf_reuse, sizeof(int), 50);
716 if (ga_grow(&buf_reuse, 1) == OK)
717 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
718 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200719 if (buf->b_sfname != buf->b_ffname)
720 VIM_CLEAR(buf->b_sfname);
721 else
722 buf->b_sfname = NULL;
723 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (buf->b_prev == NULL)
725 firstbuf = buf->b_next;
726 else
727 buf->b_prev->b_next = buf->b_next;
728 if (buf->b_next == NULL)
729 lastbuf = buf->b_prev;
730 else
731 buf->b_next->b_prev = buf->b_prev;
732 free_buffer(buf);
733 }
734 else
735 {
736 if (del_buf)
737 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100738 // Free all internal variables and reset option values, to make
739 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 free_buffer_stuff(buf, TRUE);
741
Bram Moolenaarc667da52019-11-30 20:52:27 +0100742 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
744
Bram Moolenaarc667da52019-11-30 20:52:27 +0100745 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 buf->b_p_initialized = FALSE;
747 }
748 buf_clear_file(buf);
749 if (del_buf)
750 buf->b_p_bl = FALSE;
751 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100752 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753}
754
755/*
756 * Make buffer not contain a file.
757 */
758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100759buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200762 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 buf->b_p_eol = TRUE;
765 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000767 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100769 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#ifdef FEAT_NETBEANS_INTG
771 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773}
774
775/*
776 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200777 * the file. Careful: get here with "curwin" NULL when exiting.
778 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100779 * BFA_DEL buffer is going to be deleted
780 * BFA_WIPE buffer is going to be wiped out
781 * BFA_KEEP_UNDO do not free undo information
782 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100785buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200788 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200789 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200790 win_T *the_curwin = curwin;
791 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaarc667da52019-11-30 20:52:27 +0100793 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200794 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200795 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200796 if (buf->b_ml.ml_mfp != NULL)
797 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200798 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
799 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200800 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100801 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200802 return;
803 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200804 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200806 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
807 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200808 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100809 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return;
811 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200812 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200814 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
815 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200816 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100817 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 return;
819 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200820 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200821
Bram Moolenaarc667da52019-11-30 20:52:27 +0100822 // If the buffer was in curwin and the window has changed, go back to that
823 // window, if it still exists. This avoids that ":edit x" triggering a
824 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200825 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
826 {
827 block_autocmds();
828 goto_tabpage_win(the_curtab, the_curwin);
829 unblock_autocmds();
830 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200831
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100832#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100833 // autocmds may abort script processing
834 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838 /*
839 * It's possible that autocommands change curbuf to the one being deleted.
840 * This might cause curbuf to be deleted unexpectedly. But in some cases
841 * it's OK to delete the curbuf, because a new one is obtained anyway.
842 * Therefore only return if curbuf changed to the deleted buffer.
843 */
844 if (buf == curbuf && !is_curbuf)
845 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100847 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200849#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100850 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200851 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100852 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200853#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000854
855#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100856 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000857 {
858 win_T *win;
859 tabpage_T *tp;
860
861 FOR_ALL_TAB_WINDOWS(tp, win)
862 if (win->w_buffer == buf)
863 clearFolding(win);
864 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000865#endif
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#ifdef FEAT_TCL
868 tcl_buffer_free(buf);
869#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100870 ml_close(buf, TRUE); // close and delete the memline/memfile
871 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200872 if ((flags & BFA_KEEP_UNDO) == 0)
873 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100874 u_blockfree(buf); // free the memory allocated for undo
875 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100878 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100880#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100881 clear_buf_prop_types(buf);
882#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100883 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885
886/*
887 * Free a buffer structure and the things it contains related to the buffer
888 * itself (not the file, that must have been done already).
889 */
890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100891free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200893 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200895#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100896 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200897 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200898 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200899 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200900#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200901#ifdef FEAT_LUA
902 lua_buffer_free(buf);
903#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000904#ifdef FEAT_MZSCHEME
905 mzscheme_buffer_free(buf);
906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#ifdef FEAT_PERL
908 perl_buf_free(buf);
909#endif
910#ifdef FEAT_PYTHON
911 python_buffer_free(buf);
912#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913#ifdef FEAT_PYTHON3
914 python3_buffer_free(buf);
915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#ifdef FEAT_RUBY
917 ruby_buffer_free(buf);
918#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200919#ifdef FEAT_JOB_CHANNEL
920 channel_buffer_free(buf);
921#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200922#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200923 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200924#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200925#ifdef FEAT_JOB_CHANNEL
926 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200927 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200928 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200929#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200930
931 buf_hashtab_remove(buf);
932
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200933 aubuflocal_remove(buf);
934
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200935 if (autocmd_busy)
936 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100937 // Do not free the buffer structure while autocommands are executing,
938 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200939 buf->b_next = au_pending_free_buf;
940 au_pending_free_buf = buf;
941 }
942 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100943 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200944 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100945 if (curbuf == buf)
946 curbuf = NULL; // make clear it's not to be used
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100951 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100952 */
953 static void
954init_changedtick(buf_T *buf)
955{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100956 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100957
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100958 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
959 di->di_tv.v_type = VAR_NUMBER;
960 di->di_tv.v_lock = VAR_FIXED;
961 di->di_tv.vval.v_number = 0;
962
Bram Moolenaar92769c32017-02-25 15:41:37 +0100963#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100964 STRCPY(buf->b_ct_di.di_key, "changedtick");
965 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100966#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100967}
968
969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
971 */
972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100973free_buffer_stuff(
974 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100975 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 if (free_options)
978 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100979 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200981#ifdef FEAT_SPELL
982 ga_clear(&buf->b_s.b_langp);
983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100986 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100987 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100988
Bram Moolenaarc667da52019-11-30 20:52:27 +0100989 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100990 hash_init(&buf->b_vars->dv_hashtab);
991 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100992 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +0100993 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200996 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200998 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001000#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001001 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001002#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001003 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1004 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001005 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007
1008/*
1009 * Free the b_wininfo list for buffer "buf".
1010 */
1011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001012clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 wininfo_T *wip;
1015
1016 while (buf->b_wininfo != NULL)
1017 {
1018 wip = buf->b_wininfo;
1019 buf->b_wininfo = wip->wi_next;
1020 if (wip->wi_optset)
1021 {
1022 clear_winopt(&wip->wi_opt);
1023#ifdef FEAT_FOLDING
1024 deleteFoldRecurse(&wip->wi_folds);
1025#endif
1026 }
1027 vim_free(wip);
1028 }
1029}
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031/*
1032 * Go to another buffer. Handles the result of the ATTENTION dialog.
1033 */
1034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001035goto_buffer(
1036 exarg_T *eap,
1037 int start,
1038 int dir,
1039 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001041 bufref_T old_curbuf;
1042
1043 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
1045 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1047 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1049 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001050#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001051 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001052
Bram Moolenaarc667da52019-11-30 20:52:27 +01001053 // Reset the error/interrupt/exception state here so that
1054 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001055 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001056#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001057
Bram Moolenaarc667da52019-11-30 20:52:27 +01001058 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 win_close(curwin, TRUE);
1060 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001061 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001062
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001063#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001064 // Restore the error/interrupt/exception state if not discarded by a
1065 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001070 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073/*
1074 * Handle the situation of swap_exists_action being set.
1075 * It is allowed for "old_curbuf" to be NULL or invalid.
1076 */
1077 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001078handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001080#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001081 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001082#endif
1083#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001084 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001085#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001086 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (swap_exists_action == SEA_QUIT)
1089 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001090#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001091 // Reset the error/interrupt/exception state here so that
1092 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001093 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001094#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001095
Bram Moolenaarc667da52019-11-30 20:52:27 +01001096 // User selected Quit at ATTENTION prompt. Go back to previous
1097 // buffer. If that buffer is gone or the same as the current one,
1098 // open a new, empty buffer.
1099 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001100 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001101 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001102 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1103 || old_curbuf->br_buf == curbuf)
1104 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1105 else
1106 buf = old_curbuf->br_buf;
1107 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001108 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001109 int old_msg_silent = msg_silent;
1110
1111 if (shortmess(SHM_FILEINFO))
1112 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001113 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001114 // restore msg_silent, so that the command line will be shown
1115 msg_silent = old_msg_silent;
1116
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001118 if (old_tw != curbuf->b_p_tw)
1119 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001120#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001121 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001122 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001123
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001124#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001125 // Restore the error/interrupt/exception state if not discarded by a
1126 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001127 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 else if (swap_exists_action == SEA_RECOVER)
1131 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001132#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001133 // Reset the error/interrupt/exception state here so that
1134 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001135 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001136#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001137
Bram Moolenaarc667da52019-11-30 20:52:27 +01001138 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001140 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001141 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001143 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001144
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001145#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001146 // Restore the error/interrupt/exception state if not discarded by a
1147 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001148 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 }
1151 swap_exists_action = SEA_NONE;
1152}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154/*
1155 * do_bufdel() - delete or unload buffer(s)
1156 *
1157 * addr_count == 0: ":bdel" - delete current buffer
1158 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1159 * buffer "end_bnr", then any other arguments.
1160 * addr_count == 2: ":N,N bdel" - delete buffers in range
1161 *
1162 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1163 * DOBUF_DEL (":bdel")
1164 *
1165 * Returns error message or NULL
1166 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001167 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001168do_bufdel(
1169 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001170 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001172 int start_bnr, // first buffer number in a range
1173 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001174 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001176 int do_current = 0; // delete current buffer?
1177 int deleted = 0; // number of buffers deleted
1178 char *errormsg = NULL; // return value
1179 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 char_u *p;
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (addr_count == 0)
1183 {
1184 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1185 }
1186 else
1187 {
1188 if (addr_count == 2)
1189 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001190 if (*arg) // both range and argument is not allowed
Bram Moolenaar8930caa2020-07-23 16:37:03 +02001191 return ex_errmsg(e_trailing_arg, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 bnr = start_bnr;
1193 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001194 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 bnr = end_bnr;
1196
1197 for ( ;!got_int; ui_breakcheck())
1198 {
1199 /*
1200 * delete the current buffer last, otherwise when the
1201 * current buffer is deleted, the next buffer becomes
1202 * the current one and will be loaded, which may then
1203 * also be deleted, etc.
1204 */
1205 if (bnr == curbuf->b_fnum)
1206 do_current = bnr;
1207 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1208 forceit) == OK)
1209 ++deleted;
1210
1211 /*
1212 * find next buffer number to delete/unload
1213 */
1214 if (addr_count == 2)
1215 {
1216 if (++bnr > end_bnr)
1217 break;
1218 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001219 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 {
1221 arg = skipwhite(arg);
1222 if (*arg == NUL)
1223 break;
1224 if (!VIM_ISDIGIT(*arg))
1225 {
1226 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001227 bnr = buflist_findpat(arg, p,
1228 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001229 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001230 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 break;
1232 arg = p;
1233 }
1234 else
1235 bnr = getdigits(&arg);
1236 }
1237 }
1238 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1239 FORWARD, do_current, forceit) == OK)
1240 ++deleted;
1241
1242 if (deleted == 0)
1243 {
1244 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001245 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252 else if (deleted >= p_report)
1253 {
1254 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001255 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001256 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001259 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001261 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001262 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 }
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 return errormsg;
1268}
1269
Bram Moolenaara02471e2014-01-10 16:43:14 +01001270/*
1271 * Make the current buffer empty.
1272 * Used when it is wiped out and it's the last buffer.
1273 */
1274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001275empty_curbuf(
1276 int close_others,
1277 int forceit,
1278 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001279{
1280 int retval;
1281 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001282 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001283
1284 if (action == DOBUF_UNLOAD)
1285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001286 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001287 return FAIL;
1288 }
1289
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001290 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001291 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001292 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001293 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001294
1295 setpcmark();
1296 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1297 forceit ? ECMD_FORCEIT : 0, curwin);
1298
1299 /*
1300 * do_ecmd() may create a new buffer, then we have to delete
1301 * the old one. But do_ecmd() may have done that already, check
1302 * if the buffer still exists.
1303 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001304 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001305 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001306 if (!close_others)
1307 need_fileinfo = FALSE;
1308 return retval;
1309}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
1312 * Implementation of the commands for the buffer list.
1313 *
1314 * action == DOBUF_GOTO go to specified buffer
1315 * action == DOBUF_SPLIT split window and go to specified buffer
1316 * action == DOBUF_UNLOAD unload specified buffer(s)
1317 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1318 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001319 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 *
1321 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1322 * start == DOBUF_FIRST go to "count" buffer from first buffer
1323 * start == DOBUF_LAST go to "count" buffer from last buffer
1324 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1325 *
1326 * Return FAIL or OK.
1327 */
1328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329do_buffer(
1330 int action,
1331 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001332 int dir, // FORWARD or BACKWARD
1333 int count, // buffer number or number of buffers
1334 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 buf_T *buf;
1337 buf_T *bp;
1338 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001339 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 switch (start)
1342 {
1343 case DOBUF_FIRST: buf = firstbuf; break;
1344 case DOBUF_LAST: buf = lastbuf; break;
1345 default: buf = curbuf; break;
1346 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001347 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 while (count-- > 0)
1350 {
1351 do
1352 {
1353 buf = buf->b_next;
1354 if (buf == NULL)
1355 buf = firstbuf;
1356 }
1357 while (buf != curbuf && !bufIsChanged(buf));
1358 }
1359 if (!bufIsChanged(buf))
1360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001361 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 return FAIL;
1363 }
1364 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001365 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
1367 while (buf != NULL && buf->b_fnum != count)
1368 buf = buf->b_next;
1369 }
1370 else
1371 {
1372 bp = NULL;
1373 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1374 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001375 // remember the buffer where we start, we come back there when all
1376 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (bp == NULL)
1378 bp = buf;
1379 if (dir == FORWARD)
1380 {
1381 buf = buf->b_next;
1382 if (buf == NULL)
1383 buf = firstbuf;
1384 }
1385 else
1386 {
1387 buf = buf->b_prev;
1388 if (buf == NULL)
1389 buf = lastbuf;
1390 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001391 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (unload || buf->b_p_bl)
1393 {
1394 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001395 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 }
1397 if (bp == buf)
1398 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001399 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001400 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 return FAIL;
1402 }
1403 }
1404 }
1405
Bram Moolenaarc667da52019-11-30 20:52:27 +01001406 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 if (start == DOBUF_FIRST)
1409 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001410 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001412 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001415 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001417 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 return FAIL;
1419 }
1420
1421#ifdef FEAT_GUI
1422 need_mouse_correct = TRUE;
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 /*
1426 * delete buffer buf from memory and/or the list
1427 */
1428 if (unload)
1429 {
1430 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001431 bufref_T bufref;
1432
Bram Moolenaar94f01952018-09-01 15:30:03 +02001433 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001434 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001435
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001436 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaarc667da52019-11-30 20:52:27 +01001438 // When unloading or deleting a buffer that's already unloaded and
1439 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001440 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1441 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 return FAIL;
1443
1444 if (!forceit && bufIsChanged(buf))
1445 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001446#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001447 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
1449 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001450 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001451 // Autocommand deleted buffer, oops! It's not changed
1452 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001454 // If it's still changed fail silently, the dialog already
1455 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001456 if (bufIsChanged(buf))
1457 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001459 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001462 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 buf->b_fnum);
1464 return FAIL;
1465 }
1466 }
1467
Bram Moolenaarc667da52019-11-30 20:52:27 +01001468 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001469 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001470 end_visual_mode();
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 /*
1473 * If deleting the last (listed) buffer, make it empty.
1474 * The last (listed) buffer cannot be unloaded.
1475 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001476 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (bp->b_p_bl && bp != buf)
1478 break;
1479 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001480 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 /*
1483 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001484 * (unless it's the only window). Repeat this so long as we end up in
1485 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001487 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001488 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001489 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001490 {
1491 if (win_close(curwin, FALSE) == FAIL)
1492 break;
1493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 /*
1496 * If the buffer to be deleted is not the current one, delete it here.
1497 */
1498 if (buf != curbuf)
1499 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001500 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001501 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001502 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 return OK;
1504 }
1505
1506 /*
1507 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001508 * There should be another, otherwise it would have been handled
1509 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001510 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 * Then prefer the buffer we most recently visited.
1512 * Else try to find one that is loaded, after the current buffer,
1513 * then before the current buffer.
1514 * Finally use any buffer.
1515 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001516 buf = NULL; // selected buffer
1517 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001518 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1519 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001521 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 int jumpidx;
1524
1525 jumpidx = curwin->w_jumplistidx - 1;
1526 if (jumpidx < 0)
1527 jumpidx = curwin->w_jumplistlen - 1;
1528
1529 forward = jumpidx;
1530 while (jumpidx != curwin->w_jumplistidx)
1531 {
1532 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1533 if (buf != NULL)
1534 {
1535 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001536 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 else if (buf->b_ml.ml_mfp == NULL)
1538 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001539 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (bp == NULL)
1541 bp = buf;
1542 buf = NULL;
1543 }
1544 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001545 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001547 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1549 break;
1550 if (--jumpidx < 0)
1551 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001552 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 break;
1554 }
1555 }
1556#endif
1557
Bram Moolenaarc667da52019-11-30 20:52:27 +01001558 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
1560 forward = TRUE;
1561 buf = curbuf->b_next;
1562 for (;;)
1563 {
1564 if (buf == NULL)
1565 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001566 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 break;
1568 buf = curbuf->b_prev;
1569 forward = FALSE;
1570 continue;
1571 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001572 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1574 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001577 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 bp = buf;
1579 }
1580 if (forward)
1581 buf = buf->b_next;
1582 else
1583 buf = buf->b_prev;
1584 }
1585 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001586 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001588 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001590 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (buf->b_p_bl && buf != curbuf)
1592 break;
1593 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001594 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 if (curbuf->b_next != NULL)
1597 buf = curbuf->b_next;
1598 else
1599 buf = curbuf->b_prev;
1600 }
1601 }
1602
Bram Moolenaara02471e2014-01-10 16:43:14 +01001603 if (buf == NULL)
1604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001605 // Autocommands must have wiped out all other buffers. Only option
1606 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001607 return empty_curbuf(FALSE, forceit, action);
1608 }
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /*
1611 * make buf current buffer
1612 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001613 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001615 // If 'switchbuf' contains "useopen": jump to first window containing
1616 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001617 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001618 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001619 // If 'switchbuf' contains "usetab": jump to first window in any tab
1620 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001621 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 return OK;
1623 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 return FAIL;
1625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaarc667da52019-11-30 20:52:27 +01001627 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (buf == curbuf)
1629 return OK;
1630
1631 /*
1632 * Check if the current buffer may be abandoned.
1633 */
1634 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1635 {
1636#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001637 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001639 bufref_T bufref;
1640
1641 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001643 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001644 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647 if (bufIsChanged(curbuf))
1648#endif
1649 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001650 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 return FAIL;
1652 }
1653 }
1654
Bram Moolenaarc667da52019-11-30 20:52:27 +01001655 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 set_curbuf(buf, action);
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001659 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001661#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001662 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 return FAIL;
1664#endif
1665
1666 return OK;
1667}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669/*
1670 * Set current buffer to "buf". Executes autocommands and closes current
1671 * buffer. "action" tells how to close the current buffer:
1672 * DOBUF_GOTO free or hide it
1673 * DOBUF_SPLIT nothing
1674 * DOBUF_UNLOAD unload it
1675 * DOBUF_DEL delete it
1676 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001677 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
1679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 buf_T *prevbuf;
1683 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001684 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001685#ifdef FEAT_SYN_HL
1686 long old_tw = curbuf->b_p_tw;
1687#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001688 bufref_T newbufref;
1689 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001692 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001693 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1694 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaarc667da52019-11-30 20:52:27 +01001696 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaarc667da52019-11-30 20:52:27 +01001699 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001701 set_bufref(&prevbufref, prevbuf);
1702 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001704 // Autocommands may delete the current buffer and/or the buffer we want to go
Bram Moolenaarc667da52019-11-30 20:52:27 +01001705 // to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001706 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001707 || (bufref_valid(&prevbufref)
1708 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001710 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001712 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001714#ifdef FEAT_SYN_HL
1715 if (prevbuf == curwin->w_buffer)
1716 reset_synblock(curwin);
1717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001719 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001721 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001723 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001725 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001726 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001727
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001728 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001729 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1731 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001732 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001733 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1734 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001735 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001736 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001737 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001740 // An autocommand may have deleted "buf", already entered it (e.g., when
1741 // it did ":bunload") or aborted the script processing.
1742 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001743 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001744#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001745 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001747 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001750#ifdef FEAT_SYN_HL
1751 if (old_tw != curbuf->b_p_tw)
1752 check_colorcolumn(curwin);
1753#endif
1754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
1758 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001759 * Old curbuf must have been abandoned already! This also means "curbuf" may
1760 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001765 // Get the buffer in the current window.
1766 curwin->w_buffer = buf;
1767 curbuf = buf;
1768 ++curbuf->b_nwindows;
1769
1770 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1772 if (!buf->b_help)
1773 get_winopts(buf);
1774#ifdef FEAT_FOLDING
1775 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001776 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001778 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
1780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001782 if (curwin->w_p_diff)
1783 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
1785
Bram Moolenaara971b822011-09-14 14:43:25 +02001786#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001787 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001788#endif
1789
Bram Moolenaarc667da52019-11-30 20:52:27 +01001790 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 curwin->w_cursor.lnum = 1;
1792 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001795 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
Bram Moolenaarc667da52019-11-30 20:52:27 +01001797 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001798 curwin->w_valid = 0;
1799
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001800 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1801 curbuf->b_last_cursor.col, TRUE);
1802
Bram Moolenaarc667da52019-11-30 20:52:27 +01001803 // Make sure the buffer is loaded.
1804 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001805 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001806 // If there is no filetype, allow for detecting one. Esp. useful for
1807 // ":ball" used in a autocommand. If there already is a filetype we
1808 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001809 if (*curbuf->b_p_ft == NUL)
1810 did_filetype = FALSE;
1811
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001812 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 else
1815 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001816 if (!msg_silent && !shortmess(SHM_FILEINFO))
1817 need_fileinfo = TRUE; // display file info after redraw
1818
1819 // check if file changed
1820 (void)buf_check_timestamp(curbuf, FALSE);
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001823#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1827 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829
Bram Moolenaarc667da52019-11-30 20:52:27 +01001830 // If autocommands did not change the cursor position, restore cursor lnum
1831 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (curwin->w_cursor.lnum == 1 && inindent(0))
1833 buflist_getfpos();
1834
Bram Moolenaarc667da52019-11-30 20:52:27 +01001835 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836#ifdef FEAT_TITLE
1837 maketitle();
1838#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001839 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001840 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001841 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar009b2592004-10-24 19:18:58 +00001843#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001844 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001845 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001846#endif
1847
Bram Moolenaarc667da52019-11-30 20:52:27 +01001848 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001849 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851#ifdef FEAT_KEYMAP
1852 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001853 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001855#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001856 // May need to set the spell language. Can only do this after the buffer
1857 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001858 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1859 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001860#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001861#ifdef FEAT_VIMINFO
1862 curbuf->b_last_used = vim_time();
1863#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 redraw_later(NOT_VALID);
1866}
1867
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001868#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1869/*
1870 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001871 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001872 */
1873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001874do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001875{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001876 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001877 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001878 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001879 shorten_fnames(TRUE);
1880}
1881#endif
1882
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001883 void
1884no_write_message(void)
1885{
1886#ifdef FEAT_TERMINAL
1887 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 emsg(_("E948: Job still running (add ! to end the job)"));
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 (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001892}
1893
1894 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001895no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001896{
1897#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001898 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001900 else
1901#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001903}
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905/*
1906 * functions for dealing with the buffer list
1907 */
1908
1909/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001910 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1911 * only one window. That means it can be re-used.
1912 */
1913 int
1914curbuf_reusable(void)
1915{
1916 return (curbuf != NULL
1917 && curbuf->b_ffname == NULL
1918 && curbuf->b_nwindows <= 1
1919 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001920#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001921 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001922#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001923 && !curbufIsChanged());
1924}
1925
1926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 * Add a file name to the buffer list. Return a pointer to the buffer.
1928 * If the same file name already exists return a pointer to that buffer.
1929 * If it does not exist, or if fname == NULL, a new entry is created.
1930 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1931 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1932 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001933 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001934 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1935 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001936 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 * This is the ONLY way to create a new buffer.
1938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001941 char_u *ffname_arg, // full path of fname or relative
1942 char_u *sfname_arg, // short fname or NULL
1943 linenr_T lnum, // preferred cursor line
1944 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001946 char_u *ffname = ffname_arg;
1947 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 buf_T *buf;
1949#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001950 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#endif
1952
Bram Moolenaar480778b2016-07-14 22:09:39 +02001953 if (top_file_num == 1)
1954 hash_init(&buf_hashtab);
1955
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001956 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 /*
1959 * If file name already exists in the list, update the entry.
1960 */
1961#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001962 // On Unix we can use inode numbers when the file exists. Works better
1963 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1965 st.st_dev = (dev_T)-1;
1966#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001967 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#ifdef UNIX
1969 buflist_findname_stat(ffname, &st)
1970#else
1971 buflist_findname(ffname)
1972#endif
1973 ) != NULL)
1974 {
1975 vim_free(ffname);
1976 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01001977 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
1978 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001979
1980 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001981 // copy the options now, if 'cpo' doesn't have 's' and not done
1982 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02001983 buf_copy_options(buf, 0);
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1986 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001987 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001990 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001992 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001993 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001994 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001995 return NULL;
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998 return buf;
1999 }
2000
2001 /*
2002 * If the current buffer has no name and no contents, use the current
2003 * buffer. Otherwise: Need to allocate a new buffer structure.
2004 *
2005 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002006 * (A spell file buffer is allocated in spell.c, but that's not a normal
2007 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 */
2009 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002010 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002013 // It's like this buffer is deleted. Watch out for autocommands that
2014 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (curbuf->b_p_bl)
2016 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2017 if (buf == curbuf)
2018 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002019#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002020 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002021 {
2022 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002028 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 clear_string_option(&buf->b_p_bh);
2030 clear_string_option(&buf->b_p_bt);
2031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033 if (buf != curbuf || curbuf == NULL)
2034 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002035 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (buf == NULL)
2037 {
2038 vim_free(ffname);
2039 return NULL;
2040 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002041#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002042 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002043 buf->b_vars = dict_alloc();
2044 if (buf->b_vars == NULL)
2045 {
2046 vim_free(ffname);
2047 vim_free(buf);
2048 return NULL;
2049 }
2050 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2051#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002052 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054
2055 if (ffname != NULL)
2056 {
2057 buf->b_ffname = ffname;
2058 buf->b_sfname = vim_strsave(sfname);
2059 }
2060
2061 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002062 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2065 || buf->b_wininfo == NULL)
2066 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002067 if (buf->b_sfname != buf->b_ffname)
2068 VIM_CLEAR(buf->b_sfname);
2069 else
2070 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002071 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (buf != curbuf)
2073 free_buffer(buf);
2074 return NULL;
2075 }
2076
2077 if (buf == curbuf)
2078 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002079 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002080 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002081 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002083#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002084 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 return NULL;
2086#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002087 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002088
Bram Moolenaarc667da52019-11-30 20:52:27 +01002089 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002090 buf->b_p_initialized = FALSE;
2091 buf_copy_options(buf, BCO_ENTER);
2092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002094 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 curbuf->b_kmap_state |= KEYMAP_INIT;
2096#endif
2097 }
2098 else
2099 {
2100 /*
2101 * put new buffer at the end of the buffer list
2102 */
2103 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002104 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106 buf->b_prev = NULL;
2107 firstbuf = buf;
2108 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002109 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
2111 lastbuf->b_next = buf;
2112 buf->b_prev = lastbuf;
2113 }
2114 lastbuf = buf;
2115
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002116 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2117 {
2118 // Recycle a previously used buffer number. Used for buffers which
2119 // are normally hidden, e.g. in a popup window. Avoids that the
2120 // buffer number grows rapidly.
2121 --buf_reuse.ga_len;
2122 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002123
2124 // Move buffer to the right place in the buffer list.
2125 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2126 {
2127 buf_T *prev = buf->b_prev;
2128
2129 prev->b_next = buf->b_next;
2130 if (prev->b_next != NULL)
2131 prev->b_next->b_prev = prev;
2132 buf->b_next = prev;
2133 buf->b_prev = prev->b_prev;
2134 if (buf->b_prev != NULL)
2135 buf->b_prev->b_next = buf;
2136 prev->b_prev = buf;
2137 if (lastbuf == buf)
2138 lastbuf = prev;
2139 if (firstbuf == prev)
2140 firstbuf = buf;
2141 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002142 }
2143 else
2144 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002145 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (emsg_silent == 0)
2149 {
2150 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002151 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 top_file_num = 1;
2154 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002155 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 /*
2158 * Always copy the options from the current buffer.
2159 */
2160 buf_copy_options(buf, BCO_ALWAYS);
2161 }
2162
2163 buf->b_wininfo->wi_fpos.lnum = lnum;
2164 buf->b_wininfo->wi_win = curwin;
2165
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002166#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002167 hash_init(&buf->b_s.b_keywtab);
2168 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#endif
2170
2171 buf->b_fname = buf->b_sfname;
2172#ifdef UNIX
2173 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002174 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 else
2176 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002177 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 buf->b_dev = st.st_dev;
2179 buf->b_ino = st.st_ino;
2180 }
2181#endif
2182 buf->b_u_synced = TRUE;
2183 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002184 if (flags & BLN_DUMMY)
2185 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002187 clrallmarks(buf); // clear marks
2188 fmarks_check_names(buf); // check file marks for this file
2189 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (!(flags & BLN_DUMMY))
2191 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002192 bufref_T bufref;
2193
Bram Moolenaarc667da52019-11-30 20:52:27 +01002194 // Tricky: these autocommands may change the buffer list. They could
2195 // also split the window with re-using the one empty buffer. This may
2196 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002197 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002198 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002199 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002200 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002202 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002203 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002204 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002205 return NULL;
2206 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002207#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002208 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213 return buf;
2214}
2215
2216/*
2217 * Free the memory for the options of a buffer.
2218 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2219 * 'fileencoding'.
2220 */
2221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222free_buf_options(
2223 buf_T *buf,
2224 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226 if (free_p_ff)
2227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 clear_string_option(&buf->b_p_bh);
2231 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233#ifdef FEAT_FIND_ID
2234 clear_string_option(&buf->b_p_def);
2235 clear_string_option(&buf->b_p_inc);
2236# ifdef FEAT_EVAL
2237 clear_string_option(&buf->b_p_inex);
2238# endif
2239#endif
2240#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2241 clear_string_option(&buf->b_p_inde);
2242 clear_string_option(&buf->b_p_indk);
2243#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002244#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2245 clear_string_option(&buf->b_p_bexpr);
2246#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002247#if defined(FEAT_CRYPT)
2248 clear_string_option(&buf->b_p_cm);
2249#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002250 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002251#if defined(FEAT_EVAL)
2252 clear_string_option(&buf->b_p_fex);
2253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#ifdef FEAT_CRYPT
2255 clear_string_option(&buf->b_p_key);
2256#endif
2257 clear_string_option(&buf->b_p_kp);
2258 clear_string_option(&buf->b_p_mps);
2259 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002260 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002262#ifdef FEAT_VARTABS
2263 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002264 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002265 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002266 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002267 buf->b_p_vsts_array = NULL;
2268 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002269 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#ifdef FEAT_KEYMAP
2272 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002273 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 ga_clear(&buf->b_kmap_ga);
2275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#ifdef FEAT_FOLDING
2278 clear_string_option(&buf->b_p_cms);
2279#endif
2280 clear_string_option(&buf->b_p_nf);
2281#ifdef FEAT_SYN_HL
2282 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002283 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002284#endif
2285#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002286 clear_string_option(&buf->b_s.b_p_spc);
2287 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002288 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002289 buf->b_s.b_cap_prog = NULL;
2290 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002291 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292#endif
2293#ifdef FEAT_SEARCHPATH
2294 clear_string_option(&buf->b_p_sua);
2295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297#ifdef FEAT_CINDENT
2298 clear_string_option(&buf->b_p_cink);
2299 clear_string_option(&buf->b_p_cino);
2300#endif
2301#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2302 clear_string_option(&buf->b_p_cinw);
2303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002305#ifdef FEAT_COMPL_FUNC
2306 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002307 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#ifdef FEAT_QUICKFIX
2310 clear_string_option(&buf->b_p_gp);
2311 clear_string_option(&buf->b_p_mp);
2312 clear_string_option(&buf->b_p_efm);
2313#endif
2314 clear_string_option(&buf->b_p_ep);
2315 clear_string_option(&buf->b_p_path);
2316 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002317 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002318#ifdef FEAT_EVAL
2319 clear_string_option(&buf->b_p_tfu);
2320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 clear_string_option(&buf->b_p_dict);
2322 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002323#ifdef FEAT_TEXTOBJ
2324 clear_string_option(&buf->b_p_qe);
2325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002327 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002328#ifdef FEAT_LISP
2329 clear_string_option(&buf->b_p_lw);
2330#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002331 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002332 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333}
2334
2335/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002336 * Get alternate file "n".
2337 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2338 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 * if (options & GETF_SETMARK) call setpcmark()
2340 * if (options & GETF_ALT) we are jumping to an alternate file.
2341 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2342 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002343 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 */
2345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002346buflist_getfile(
2347 int n,
2348 linenr_T lnum,
2349 int options,
2350 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 pos_T *fpos;
2355 colnr_T col;
2356
2357 buf = buflist_findnr(n);
2358 if (buf == NULL)
2359 {
2360 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002361 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002363 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 return FAIL;
2365 }
2366
Bram Moolenaarc667da52019-11-30 20:52:27 +01002367 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (buf == curbuf)
2369 return OK;
2370
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002371 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002372 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002373 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002375 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002376 if (curbuf_locked())
2377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
Bram Moolenaarc667da52019-11-30 20:52:27 +01002379 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (lnum == 0)
2381 {
2382 fpos = buflist_findfpos(buf);
2383 lnum = fpos->lnum;
2384 col = fpos->col;
2385 }
2386 else
2387 col = 0;
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (options & GETF_SWITCH)
2390 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002391 // If 'switchbuf' contains "useopen": jump to first window containing
2392 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002393 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002395
Bram Moolenaarc667da52019-11-30 20:52:27 +01002396 // If 'switchbuf' contains "usetab": jump to first window in any tab
2397 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002398 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002399 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002400
Bram Moolenaarc667da52019-11-30 20:52:27 +01002401 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2402 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002403 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002404 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002406 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002407 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002408 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2409 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002411 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002416 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2417 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 --RedrawingDisabled;
2420
Bram Moolenaarc667da52019-11-30 20:52:27 +01002421 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (!p_sol && col != 0)
2423 {
2424 curwin->w_cursor.col = col;
2425 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 curwin->w_set_curswant = TRUE;
2428 }
2429 return OK;
2430 }
2431 --RedrawingDisabled;
2432 return FAIL;
2433}
2434
2435/*
2436 * go to the last know line number for the current buffer
2437 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002439buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 pos_T *fpos;
2442
2443 fpos = buflist_findfpos(curbuf);
2444
2445 curwin->w_cursor.lnum = fpos->lnum;
2446 check_cursor_lnum();
2447
2448 if (p_sol)
2449 curwin->w_cursor.col = 0;
2450 else
2451 {
2452 curwin->w_cursor.col = fpos->col;
2453 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 curwin->w_set_curswant = TRUE;
2456 }
2457}
2458
Bram Moolenaar81695252004-12-29 20:58:21 +00002459#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2460/*
2461 * Find file in buffer list by name (it has to be for the current window).
2462 * Returns NULL if not found.
2463 */
2464 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002465buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002466{
2467 char_u *ffname;
2468 buf_T *buf = NULL;
2469
Bram Moolenaarc667da52019-11-30 20:52:27 +01002470 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002471 ffname = FullName_save(fname,
2472#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002473 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002474#else
2475 FALSE
2476#endif
2477 );
2478 if (ffname != NULL)
2479 {
2480 buf = buflist_findname(ffname);
2481 vim_free(ffname);
2482 }
2483 return buf;
2484}
2485#endif
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487/*
2488 * Find file in buffer list by name (it has to be for the current window).
2489 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002490 * Skips dummy buffers.
2491 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002494buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002497 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499 if (mch_stat((char *)ffname, &st) < 0)
2500 st.st_dev = (dev_T)-1;
2501 return buflist_findname_stat(ffname, &st);
2502}
2503
2504/*
2505 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2506 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002507 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
2509 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510buflist_findname_stat(
2511 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002512 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514#endif
2515 buf_T *buf;
2516
Bram Moolenaarc667da52019-11-30 20:52:27 +01002517 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002518 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002519 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520#ifdef UNIX
2521 , stp
2522#endif
2523 ))
2524 return buf;
2525 return NULL;
2526}
2527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528/*
2529 * Find file in buffer list by a regexp pattern.
2530 * Return fnum of the found buffer.
2531 * Return < 0 for error.
2532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002534buflist_findpat(
2535 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002536 char_u *pattern_end, // pointer to first char after pattern
2537 int unlisted, // find unlisted buffers
2538 int diffmode UNUSED, // find diff-mode buffers only
2539 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 int match = -1;
2543 int find_listed;
2544 char_u *pat;
2545 char_u *patend;
2546 int attempt;
2547 char_u *p;
2548 int toggledollar;
2549
2550 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2551 {
2552 if (*pattern == '%')
2553 match = curbuf->b_fnum;
2554 else
2555 match = curwin->w_alt_fnum;
2556#ifdef FEAT_DIFF
2557 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2558 match = -1;
2559#endif
2560 }
2561
2562 /*
2563 * Try four ways of matching a listed buffer:
2564 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002565 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 * attempt == 2: with '$' at end (only match at end)
2567 * attempt == 3: with '^' at start and '$' at end (only full match)
2568 * Repeat this for finding an unlisted buffer if there was no matching
2569 * listed buffer.
2570 */
2571 else
2572 {
2573 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2574 if (pat == NULL)
2575 return -1;
2576 patend = pat + STRLEN(pat) - 1;
2577 toggledollar = (patend > pat && *patend == '$');
2578
Bram Moolenaarc667da52019-11-30 20:52:27 +01002579 // First try finding a listed buffer. If not found and "unlisted"
2580 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 find_listed = TRUE;
2582 for (;;)
2583 {
2584 for (attempt = 0; attempt <= 3; ++attempt)
2585 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002586 regmatch_T regmatch;
2587
Bram Moolenaarc667da52019-11-30 20:52:27 +01002588 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002590 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002592 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002594 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2595 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 vim_free(pat);
2598 return -1;
2599 }
2600
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002601 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 if (buf->b_p_bl == find_listed
2603#ifdef FEAT_DIFF
2604 && (!diffmode || diff_mode_buf(buf))
2605#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002606 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002608 if (curtab_only)
2609 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002610 // Ignore the match if the buffer is not open in
2611 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002612 win_T *wp;
2613
Bram Moolenaar29323592016-07-24 22:04:11 +02002614 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002615 if (wp->w_buffer == buf)
2616 break;
2617 if (wp == NULL)
2618 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002619 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002620 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
2622 match = -2;
2623 break;
2624 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002625 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002628 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002629 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 break;
2631 }
2632
Bram Moolenaarc667da52019-11-30 20:52:27 +01002633 // Only search for unlisted buffers if there was no match with
2634 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (!unlisted || !find_listed || match != -1)
2636 break;
2637 find_listed = FALSE;
2638 }
2639
2640 vim_free(pat);
2641 }
2642
2643 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002644 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 return match;
2648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar52410572019-10-27 05:12:45 +01002650#ifdef FEAT_VIMINFO
2651typedef struct {
2652 buf_T *buf;
2653 char_u *match;
2654} bufmatch_T;
2655#endif
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657/*
2658 * Find all buffer names that match.
2659 * For command line expansion of ":buf" and ":sbuf".
2660 * Return OK if matches found, FAIL otherwise.
2661 */
2662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002663ExpandBufnames(
2664 char_u *pat,
2665 int *num_file,
2666 char_u ***file,
2667 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 int count = 0;
2670 buf_T *buf;
2671 int round;
2672 char_u *p;
2673 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002674 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002675#ifdef FEAT_VIMINFO
2676 bufmatch_T *matches = NULL;
2677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaarc667da52019-11-30 20:52:27 +01002679 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 *file = NULL;
2681
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002682#ifdef FEAT_DIFF
2683 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2684 return FAIL;
2685#endif
2686
Bram Moolenaarc667da52019-11-30 20:52:27 +01002687 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002688 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002690 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002691 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002693 STRCPY(patc, "\\(^\\|[\\/]\\)");
2694 STRCPY(patc + 11, pat + 1);
2695 }
2696 else
2697 patc = pat;
2698
2699 /*
2700 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002701 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002702 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002703 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002704 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002705 regmatch_T regmatch;
2706
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002707 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002708 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002709 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2710 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002711 {
2712 if (patc != pat)
2713 vim_free(patc);
2714 return FAIL;
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 /*
2718 * round == 1: Count the matches.
2719 * round == 2: Build the array to keep the matches.
2720 */
2721 for (round = 1; round <= 2; ++round)
2722 {
2723 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002724 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002726 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002728#ifdef FEAT_DIFF
2729 if (options & BUF_DIFF_FILTER)
2730 // Skip buffers not suitable for
2731 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002732 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002733 continue;
2734#endif
2735
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002736 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (p != NULL)
2738 {
2739 if (round == 1)
2740 ++count;
2741 else
2742 {
2743 if (options & WILD_HOME_REPLACE)
2744 p = home_replace_save(buf, p);
2745 else
2746 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002747#ifdef FEAT_VIMINFO
2748 if (matches != NULL)
2749 {
2750 matches[count].buf = buf;
2751 matches[count].match = p;
2752 count++;
2753 }
2754 else
2755#endif
2756 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 }
2759 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002760 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 break;
2762 if (round == 1)
2763 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002764 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (*file == NULL)
2766 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002767 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002768 if (patc != pat)
2769 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 return FAIL;
2771 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002772#ifdef FEAT_VIMINFO
2773 if (options & WILD_BUFLASTUSED)
2774 matches = ALLOC_MULT(bufmatch_T, count);
2775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002778 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002779 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 break;
2781 }
2782
Bram Moolenaar05159a02005-02-26 23:04:13 +00002783 if (patc != pat)
2784 vim_free(patc);
2785
Bram Moolenaar52410572019-10-27 05:12:45 +01002786#ifdef FEAT_VIMINFO
2787 if (matches != NULL)
2788 {
2789 int i;
2790 if (count > 1)
2791 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2792 // if the current buffer is first in the list, place it at the end
2793 if (matches[0].buf == curbuf)
2794 {
2795 for (i = 1; i < count; i++)
2796 (*file)[i-1] = matches[i].match;
2797 (*file)[count-1] = matches[0].match;
2798 }
2799 else
2800 {
2801 for (i = 0; i < count; i++)
2802 (*file)[i] = matches[i].match;
2803 }
2804 vim_free(matches);
2805 }
2806#endif
2807
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 *num_file = count;
2809 return (count == 0 ? FAIL : OK);
2810}
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812/*
2813 * Check for a match on the file name for buffer "buf" with regprog "prog".
2814 */
2815 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816buflist_match(
2817 regmatch_T *rmp,
2818 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002819 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 char_u *match;
2822
Bram Moolenaarc667da52019-11-30 20:52:27 +01002823 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002824 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002826 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 return match;
2829}
2830
2831/*
2832 * Try matching the regexp in "prog" with file name "name".
2833 * Return "name" when there is a match, NULL when not.
2834 */
2835 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002836fname_match(
2837 regmatch_T *rmp,
2838 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002839 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 char_u *match = NULL;
2842 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 if (name != NULL)
2845 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002846 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002847 rmp->rm_ic = p_fic || ignore_case;
2848 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 match = name;
2850 else
2851 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002852 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002854 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 match = name;
2856 vim_free(p);
2857 }
2858 }
2859
2860 return match;
2861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002864 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 */
2866 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002867buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002869 char_u key[VIM_SIZEOF_INT * 2 + 1];
2870 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 if (nr == 0)
2873 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002874 sprintf((char *)key, "%x", nr);
2875 hi = hash_find(&buf_hashtab, key);
2876
2877 if (!HASHITEM_EMPTY(hi))
2878 return (buf_T *)(hi->hi_key
2879 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 return NULL;
2881}
2882
2883/*
2884 * Get name of file 'n' in the buffer list.
2885 * When the file has no name an empty string is returned.
2886 * home_replace() is used to shorten the file name (used for marks).
2887 * Returns a pointer to allocated memory, of NULL when failed.
2888 */
2889 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002890buflist_nr2name(
2891 int n,
2892 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002893 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 buf_T *buf;
2896
2897 buf = buflist_findnr(n);
2898 if (buf == NULL)
2899 return NULL;
2900 return home_replace_save(helptail ? buf : NULL,
2901 fullname ? buf->b_ffname : buf->b_fname);
2902}
2903
2904/*
2905 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2906 * When "copy_options" is TRUE save the local window option values.
2907 * When "lnum" is 0 only do the options.
2908 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002910buflist_setfpos(
2911 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002912 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913 linenr_T lnum,
2914 colnr_T col,
2915 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 wininfo_T *wip;
2918
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002919 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (wip->wi_win == win)
2921 break;
2922 if (wip == NULL)
2923 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002924 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002925 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (wip == NULL)
2927 return;
2928 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002929 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 lnum = 1;
2931 }
2932 else
2933 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002934 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (wip->wi_prev)
2936 wip->wi_prev->wi_next = wip->wi_next;
2937 else
2938 buf->b_wininfo = wip->wi_next;
2939 if (wip->wi_next)
2940 wip->wi_next->wi_prev = wip->wi_prev;
2941 if (copy_options && wip->wi_optset)
2942 {
2943 clear_winopt(&wip->wi_opt);
2944#ifdef FEAT_FOLDING
2945 deleteFoldRecurse(&wip->wi_folds);
2946#endif
2947 }
2948 }
2949 if (lnum != 0)
2950 {
2951 wip->wi_fpos.lnum = lnum;
2952 wip->wi_fpos.col = col;
2953 }
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002954 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002956 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2958#ifdef FEAT_FOLDING
2959 wip->wi_fold_manual = win->w_fold_manual;
2960 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2961#endif
2962 wip->wi_optset = TRUE;
2963 }
2964
Bram Moolenaarc667da52019-11-30 20:52:27 +01002965 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 wip->wi_next = buf->b_wininfo;
2967 buf->b_wininfo = wip;
2968 wip->wi_prev = NULL;
2969 if (wip->wi_next)
2970 wip->wi_next->wi_prev = wip;
2971
2972 return;
2973}
2974
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002975#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002976/*
2977 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2978 * page. That's because a diff is local to a tab page.
2979 */
2980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002981wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002982{
2983 win_T *wp;
2984
2985 if (wip->wi_opt.wo_diff)
2986 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002987 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002988 // return FALSE when it's a window in the current tab page, thus
2989 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002990 if (wip->wi_win == wp)
2991 return FALSE;
2992 return TRUE;
2993 }
2994 return FALSE;
2995}
2996#endif
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
2999 * Find info for the current window in buffer "buf".
3000 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003001 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003002 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3003 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 * Returns NULL when there isn't any info.
3005 */
3006 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007find_wininfo(
3008 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003009 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003010 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 wininfo_T *wip;
3013
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003014 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003015 if (wip->wi_win == curwin
3016#ifdef FEAT_DIFF
3017 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3018#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003019
3020 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003022
Bram Moolenaarc667da52019-11-30 20:52:27 +01003023 // If no wininfo for curwin, use the first in the list (that doesn't have
3024 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003025 // If "need_options" is TRUE skip entries that don't have options set,
3026 // unless the window is editing "buf", so we can copy from the window
3027 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003028 if (wip == NULL)
3029 {
3030#ifdef FEAT_DIFF
3031 if (skip_diff_buffer)
3032 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003033 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003034 if (!wininfo_other_tab_diff(wip)
3035 && (!need_options || wip->wi_optset
3036 || (wip->wi_win != NULL
3037 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003038 break;
3039 }
3040 else
3041#endif
3042 wip = buf->b_wininfo;
3043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 return wip;
3045}
3046
3047/*
3048 * Reset the local window options to the values last used in this window.
3049 * If the buffer wasn't used in this window before, use the values from
3050 * the most recently used window. If the values were never set, use the
3051 * global values for the window.
3052 */
3053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003054get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
3056 wininfo_T *wip;
3057
3058 clear_winopt(&curwin->w_onebuf_opt);
3059#ifdef FEAT_FOLDING
3060 clearFolding(curwin);
3061#endif
3062
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003063 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003064 if (wip != NULL && wip->wi_win != NULL
3065 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003067 // The buffer is currently displayed in the window: use the actual
3068 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003069 win_T *wp = wip->wi_win;
3070
3071 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3072#ifdef FEAT_FOLDING
3073 curwin->w_fold_manual = wp->w_fold_manual;
3074 curwin->w_foldinvalid = TRUE;
3075 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3076#endif
3077 }
3078 else if (wip != NULL && wip->wi_optset)
3079 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003080 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3082#ifdef FEAT_FOLDING
3083 curwin->w_fold_manual = wip->wi_fold_manual;
3084 curwin->w_foldinvalid = TRUE;
3085 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3086#endif
3087 }
3088 else
3089 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3090
3091#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003092 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (p_fdls >= 0)
3094 curwin->w_p_fdl = p_fdls;
3095#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003096 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098
3099/*
3100 * Find the position (lnum and col) for the buffer 'buf' for the current
3101 * window.
3102 * Returns a pointer to no_position if no position is found.
3103 */
3104 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003108 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003110 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (wip != NULL)
3112 return &(wip->wi_fpos);
3113 else
3114 return &no_position;
3115}
3116
3117/*
3118 * Find the lnum for the buffer 'buf' for the current window.
3119 */
3120 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003121buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 return buflist_findfpos(buf)->lnum;
3124}
3125
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003127 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003130buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
Bram Moolenaar52410572019-10-27 05:12:45 +01003132 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 int len;
3134 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003135 int ro_char;
3136 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003137#ifdef FEAT_TERMINAL
3138 int job_running;
3139 int job_none_open;
3140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaar52410572019-10-27 05:12:45 +01003142#ifdef FEAT_VIMINFO
3143 garray_T buflist;
3144 buf_T **buflist_data = NULL, **p;
3145
3146 if (vim_strchr(eap->arg, 't'))
3147 {
3148 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003149 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003150 {
3151 if (ga_grow(&buflist, 1) == OK)
3152 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3153 }
3154
3155 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3156 sizeof(buf_T *), buf_compare);
3157
Bram Moolenaar3b991522019-11-06 23:26:20 +01003158 buflist_data = (buf_T **)buflist.ga_data;
3159 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003160 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003161 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003162
Bram Moolenaar3b991522019-11-06 23:26:20 +01003163 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003164 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3165 : buf->b_next)
3166#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003170#ifdef FEAT_TERMINAL
3171 job_running = term_job_running(buf->b_term);
3172 job_none_open = job_running && term_none_open(buf->b_term);
3173#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003174 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003175 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3176 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3177 || (vim_strchr(eap->arg, '+')
3178 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3179 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003180 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003181 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003182 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3183#ifdef FEAT_TERMINAL
3184 || (vim_strchr(eap->arg, 'R')
3185 && (!job_running || (job_running && job_none_open)))
3186 || (vim_strchr(eap->arg, '?')
3187 && (!job_running || (job_running && !job_none_open)))
3188 || (vim_strchr(eap->arg, 'F')
3189 && (job_running || buf->b_term == NULL))
3190#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003191 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3192 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3193 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3194 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3195 || (vim_strchr(eap->arg, '#')
3196 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003199 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
3201 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003202 if (message_filtered(NameBuff))
3203 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003205 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3206 : (bufIsChanged(buf) ? '+' : ' ');
3207#ifdef FEAT_TERMINAL
3208 if (term_job_running(buf->b_term))
3209 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003210 if (term_none_open(buf->b_term))
3211 ro_char = '?';
3212 else
3213 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003214 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3215 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003216 }
3217 else if (buf->b_term != NULL)
3218 ro_char = 'F';
3219 else
3220#endif
3221 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3222
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003223 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003224 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 buf->b_fnum,
3226 buf->b_p_bl ? ' ' : 'u',
3227 buf == curbuf ? '%' :
3228 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3229 buf->b_ml.ml_mfp == NULL ? ' ' :
3230 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003231 ro_char,
3232 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003233 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003234 if (len > IOSIZE - 20)
3235 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaarc667da52019-11-30 20:52:27 +01003237 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 i = 40 - vim_strsize(IObuff);
3239 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003241 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003242#ifdef FEAT_VIMINFO
3243 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3244 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3245 else
3246#endif
3247 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3248 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003249 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003251 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 ui_breakcheck();
3253 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003254
3255#ifdef FEAT_VIMINFO
3256 if (buflist_data)
3257 ga_clear(&buflist);
3258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261/*
3262 * Get file name and line number for file 'fnum'.
3263 * Used by DoOneCmd() for translating '%' and '#'.
3264 * Used by insert_reg() and cmdline_paste() for '#' register.
3265 * Return FAIL if not found, OK for success.
3266 */
3267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268buflist_name_nr(
3269 int fnum,
3270 char_u **fname,
3271 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 buf_T *buf;
3274
3275 buf = buflist_findnr(fnum);
3276 if (buf == NULL || buf->b_fname == NULL)
3277 return FAIL;
3278
3279 *fname = buf->b_fname;
3280 *lnum = buflist_findlnum(buf);
3281
3282 return OK;
3283}
3284
3285/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003286 * Set the file name for "buf"' to "ffname_arg", short file name to
3287 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * The file name with the full path is also remembered, for when :cd is used.
3289 * Returns FAIL for failure (file name already in use by other buffer)
3290 * OK otherwise.
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293setfname(
3294 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003295 char_u *ffname_arg,
3296 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003297 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003299 char_u *ffname = ffname_arg;
3300 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003301 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003303 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
3305
3306 if (ffname == NULL || *ffname == NUL)
3307 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003308 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003309 if (buf->b_sfname != buf->b_ffname)
3310 VIM_CLEAR(buf->b_sfname);
3311 else
3312 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003313 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#ifdef UNIX
3315 st.st_dev = (dev_T)-1;
3316#endif
3317 }
3318 else
3319 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003320 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3321 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return FAIL;
3323
3324 /*
3325 * if the file name is already used in another buffer:
3326 * - if the buffer is loaded, fail
3327 * - if the buffer is not loaded, delete it from the list
3328 */
3329#ifdef UNIX
3330 if (mch_stat((char *)ffname, &st) < 0)
3331 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003332#endif
3333 if (!(buf->b_flags & BF_DUMMY))
3334#ifdef UNIX
3335 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003337 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#endif
3339 if (obuf != NULL && obuf != buf)
3340 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003341 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003344 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 vim_free(ffname);
3346 return FAIL;
3347 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003348 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003349 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
3351 sfname = vim_strsave(sfname);
3352 if (ffname == NULL || sfname == NULL)
3353 {
3354 vim_free(sfname);
3355 vim_free(ffname);
3356 return FAIL;
3357 }
3358#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003359 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003361 if (buf->b_sfname != buf->b_ffname)
3362 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 buf->b_ffname = ffname;
3365 buf->b_sfname = sfname;
3366 }
3367 buf->b_fname = buf->b_sfname;
3368#ifdef UNIX
3369 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003370 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003373 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 buf->b_dev = st.st_dev;
3375 buf->b_ino = st.st_ino;
3376 }
3377#endif
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 buf_name_changed(buf);
3382 return OK;
3383}
3384
3385/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003386 * Crude way of changing the name of a buffer. Use with care!
3387 * The name should be relative to the current directory.
3388 */
3389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003390buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003391{
3392 buf_T *buf;
3393
3394 buf = buflist_findnr(fnum);
3395 if (buf != NULL)
3396 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003397 if (buf->b_sfname != buf->b_ffname)
3398 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003399 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003400 buf->b_ffname = vim_strsave(name);
3401 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003402 // Allocate ffname and expand into full path. Also resolves .lnk
3403 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003404 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003405 buf->b_fname = buf->b_sfname;
3406 }
3407}
3408
3409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * Take care of what needs to be done when the name of buffer "buf" has
3411 * changed.
3412 */
3413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 /*
3417 * If the file name changed, also change the name of the swapfile
3418 */
3419 if (buf->b_ml.ml_mfp != NULL)
3420 ml_setname(buf);
3421
3422 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003423 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003425 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003427 status_redraw_all(); // status lines need to be redrawn
3428 fmarks_check_names(buf); // check named file marks
3429 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430}
3431
3432/*
3433 * set alternate file name for current window
3434 *
3435 * Used by do_one_cmd(), do_write() and do_ecmd().
3436 * Return the buffer.
3437 */
3438 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003439setaltfname(
3440 char_u *ffname,
3441 char_u *sfname,
3442 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 buf_T *buf;
3445
Bram Moolenaarc667da52019-11-30 20:52:27 +01003446 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003448 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 curwin->w_alt_fnum = buf->b_fnum;
3450 return buf;
3451}
3452
3453/*
3454 * Get alternate file name for current window.
3455 * Return NULL if there isn't any, and give error message if requested.
3456 */
3457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003458getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003459 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 char_u *fname;
3462 linenr_T dummy;
3463
3464 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3465 {
3466 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003467 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return NULL;
3469 }
3470 return fname;
3471}
3472
3473/*
3474 * Add a file name to the buflist and return its number.
3475 * Uses same flags as buflist_new(), except BLN_DUMMY.
3476 *
3477 * used by qf_init(), main() and doarglist()
3478 */
3479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003480buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482 buf_T *buf;
3483
3484 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3485 if (buf != NULL)
3486 return buf->b_fnum;
3487 return 0;
3488}
3489
3490#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3491/*
3492 * Adjust slashes in file names. Called after 'shellslash' was set.
3493 */
3494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003495buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 buf_T *bp;
3498
Bram Moolenaar29323592016-07-24 22:04:11 +02003499 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 if (bp->b_ffname != NULL)
3502 slash_adjust(bp->b_ffname);
3503 if (bp->b_sfname != NULL)
3504 slash_adjust(bp->b_sfname);
3505 }
3506}
3507#endif
3508
3509/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003510 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 * Also save the local window option values.
3512 */
3513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003514buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003516 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517}
3518
3519/*
3520 * Return TRUE if 'ffname' is not the same file as current file.
3521 * Fname must have a full path (expanded by mch_FullName()).
3522 */
3523 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003524otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 return otherfile_buf(curbuf, ffname
3527#ifdef UNIX
3528 , NULL
3529#endif
3530 );
3531}
3532
3533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534otherfile_buf(
3535 buf_T *buf,
3536 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003538 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003542 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3544 return TRUE;
3545 if (fnamecmp(ffname, buf->b_ffname) == 0)
3546 return FALSE;
3547#ifdef UNIX
3548 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003549 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
Bram Moolenaarc667da52019-11-30 20:52:27 +01003551 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (stp == NULL)
3553 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003554 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 st.st_dev = (dev_T)-1;
3556 stp = &st;
3557 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003558 // Use dev/ino to check if the files are the same, even when the names
3559 // are different (possible with links). Still need to compare the
3560 // name above, for when the file doesn't exist yet.
3561 // Problem: The dev/ino changes when a file is deleted (and created
3562 // again) and remains the same when renamed/moved. We don't want to
3563 // mch_stat() each buffer each time, that would be too slow. Get the
3564 // dev/ino again when they appear to match, but not when they appear
3565 // to be different: Could skip a buffer when it's actually the same
3566 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 if (buf_same_ino(buf, stp))
3568 {
3569 buf_setino(buf);
3570 if (buf_same_ino(buf, stp))
3571 return FALSE;
3572 }
3573 }
3574#endif
3575 return TRUE;
3576}
3577
3578#if defined(UNIX) || defined(PROTO)
3579/*
3580 * Set inode and device number for a buffer.
3581 * Must always be called when b_fname is changed!.
3582 */
3583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003586 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
3588 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3589 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003590 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 buf->b_dev = st.st_dev;
3592 buf->b_ino = st.st_ino;
3593 }
3594 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003595 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596}
3597
3598/*
3599 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3600 */
3601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003602buf_same_ino(
3603 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003604 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003606 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 && stp->st_dev == buf->b_dev
3608 && stp->st_ino == buf->b_ino);
3609}
3610#endif
3611
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003612/*
3613 * Print info about the current buffer.
3614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003617 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003618 int shorthelp,
3619 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 char_u *name;
3622 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003623 char *p;
3624 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003625 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003627 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (buffer == NULL)
3629 return;
3630
Bram Moolenaarc667da52019-11-30 20:52:27 +01003631 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003633 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 p = buffer + STRLEN(buffer);
3635 }
3636 else
3637 p = buffer;
3638
3639 *p++ = '"';
3640 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003641 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 else
3643 {
3644 if (!fullname && curbuf->b_fname != NULL)
3645 name = curbuf->b_fname;
3646 else
3647 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003648 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 (int)(IOSIZE - (p - buffer)), TRUE);
3650 }
3651
Bram Moolenaar32526b32019-01-19 17:43:09 +01003652 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 curbufIsChanged() ? (shortmess(SHM_MOD)
3654 ? " [+]" : _(" [Modified]")) : " ",
3655 (curbuf->b_flags & BF_NOTEDITED)
3656#ifdef FEAT_QUICKFIX
3657 && !bt_dontwrite(curbuf)
3658#endif
3659 ? _("[Not edited]") : "",
3660 (curbuf->b_flags & BF_NEW)
3661#ifdef FEAT_QUICKFIX
3662 && !bt_dontwrite(curbuf)
3663#endif
Bram Moolenaar722e5052020-06-12 22:31:00 +02003664 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003666 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 : _("[readonly]")) : "",
3668 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3669 || curbuf->b_p_ro) ?
3670 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003671 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3672 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (curwin->w_cursor.lnum > 1000000L)
3674 n = (int)(((long)curwin->w_cursor.lnum) /
3675 ((long)curbuf->b_ml.ml_line_count / 100L));
3676 else
3677 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3678 (long)curbuf->b_ml.ml_line_count);
3679 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003680 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#ifdef FEAT_CMDL_INFO
3682 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003683 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003684 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003685 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3686 curbuf->b_ml.ml_line_count),
3687 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#endif
3689 else
3690 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003691 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 _("line %ld of %ld --%d%%-- col "),
3693 (long)curwin->w_cursor.lnum,
3694 (long)curbuf->b_ml.ml_line_count,
3695 n);
3696 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003697 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003698 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3700 }
3701
Bram Moolenaar32526b32019-01-19 17:43:09 +01003702 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3703 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (dont_truncate)
3706 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003707 // Temporarily set msg_scroll to avoid the message being truncated.
3708 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 msg_start();
3710 n = msg_scroll;
3711 msg_scroll = TRUE;
3712 msg(buffer);
3713 msg_scroll = n;
3714 }
3715 else
3716 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003717 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003719 // Need to repeat the message after redrawing when:
3720 // - When restart_edit is set (otherwise there will be a delay
3721 // before redrawing).
3722 // - When the screen was scrolled but there is no wait-return
3723 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003724 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726
3727 vim_free(buffer);
3728}
3729
3730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003731col_print(
3732 char_u *buf,
3733 size_t buflen,
3734 int col,
3735 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003738 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003740 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741}
3742
3743#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744static char_u *lasttitle = NULL;
3745static char_u *lasticon = NULL;
3746
Bram Moolenaar84a93082018-06-16 22:58:15 +02003747/*
3748 * Put the file name in the title bar and icon of the window.
3749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003751maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003754 char_u *title_str = NULL;
3755 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 int maxlen = 0;
3757 int len;
3758 int mustset;
3759 char_u buf[IOSIZE];
3760 int off;
3761
3762 if (!redrawing())
3763 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003764 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 need_maketitle = TRUE;
3766 return;
3767 }
3768
3769 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003770 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003771 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 if (p_title)
3774 {
3775 if (p_titlelen > 0)
3776 {
3777 maxlen = p_titlelen * Columns / 100;
3778 if (maxlen < 10)
3779 maxlen = 10;
3780 }
3781
Bram Moolenaar84a93082018-06-16 22:58:15 +02003782 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 if (*p_titlestring != NUL)
3784 {
3785#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003786 if (stl_syntax & STL_IN_TITLE)
3787 {
3788 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003789 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003790
3791# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003792 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003793# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003794 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003795 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003796 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003797 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003798 set_string_option_direct((char_u *)"titlestring", -1,
3799 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 else
3802#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003803 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 else
3806 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003807 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
Bram Moolenaar2c666692012-09-05 13:30:40 +02003809#define SPACE_FOR_FNAME (IOSIZE - 100)
3810#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003811#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003813 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003814#ifdef FEAT_TERMINAL
3815 else if (curbuf->b_term != NULL)
3816 {
3817 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3818 SPACE_FOR_FNAME);
3819 }
3820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 else
3822 {
3823 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003824 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827
Bram Moolenaar21554412017-07-24 21:44:43 +02003828#ifdef FEAT_TERMINAL
3829 if (curbuf->b_term == NULL)
3830#endif
3831 switch (bufIsChanged(curbuf)
3832 + (curbuf->b_p_ro * 2)
3833 + (!curbuf->b_p_ma * 4))
3834 {
3835 case 1: STRCAT(buf, " +"); break;
3836 case 2: STRCAT(buf, " ="); break;
3837 case 3: STRCAT(buf, " =+"); break;
3838 case 4:
3839 case 6: STRCAT(buf, " -"); break;
3840 case 5:
3841 case 7: STRCAT(buf, " -+"); break;
3842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaar21554412017-07-24 21:44:43 +02003844 if (curbuf->b_fname != NULL
3845#ifdef FEAT_TERMINAL
3846 && curbuf->b_term == NULL
3847#endif
3848 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003850 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 off = (int)STRLEN(buf);
3852 buf[off++] = ' ';
3853 buf[off++] = '(';
3854 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003855 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003857 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (isalpha(buf[off]) && buf[off + 1] == ':')
3859 off += 2;
3860#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003861 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003862 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003864 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003865 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003866 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003867 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003871
Bram Moolenaarc667da52019-11-30 20:52:27 +01003872 // Translate unprintable chars and concatenate. Keep some
3873 // room for the server name. When there is no room (very long
3874 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003875 if (off < SPACE_FOR_DIR)
3876 {
3877 p = transstr(buf + off);
3878 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3879 vim_free(p);
3880 }
3881 else
3882 {
3883 vim_strncpy(buf + off, (char_u *)"...",
3884 (size_t)(SPACE_FOR_ARGNR - off));
3885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 STRCAT(buf, ")");
3887 }
3888
Bram Moolenaar2c666692012-09-05 13:30:40 +02003889 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891#if defined(FEAT_CLIENTSERVER)
3892 if (serverName != NULL)
3893 {
3894 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003895 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 else
3898#endif
3899 STRCAT(buf, " - VIM");
3900
3901 if (maxlen > 0)
3902 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003903 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003904 if (vim_strsize(buf) > maxlen)
3905 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 }
3908 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003909 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 if (p_icon)
3912 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003913 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (*p_iconstring != NUL)
3915 {
3916#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003917 if (stl_syntax & STL_IN_ICON)
3918 {
3919 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003920 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003921
3922# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003923 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003924# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003925 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003926 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003927 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003928 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003929 set_string_option_direct((char_u *)"iconstring", -1,
3930 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 else
3933#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003934 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 else
3937 {
3938 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003939 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003940 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003941 p = gettail(curbuf->b_ffname);
3942 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003943 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003944 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (len > 100)
3946 {
3947 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003949 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003950 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003952 STRCPY(icon_str, p);
3953 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955 }
3956
Bram Moolenaar84a93082018-06-16 22:58:15 +02003957 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 if (mustset)
3960 resettitle();
3961}
3962
3963/*
3964 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3965 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003966 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
3968 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003969value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 if ((str == NULL) != (*last == NULL)
3972 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3973 {
3974 vim_free(*last);
3975 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003978 mch_restore_title(
3979 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003984 return TRUE;
3985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 return FALSE;
3988}
3989
3990/*
3991 * Put current window title back (used after calling a shell)
3992 */
3993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003994resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
3996 mch_settitle(lasttitle, lasticon);
3997}
Bram Moolenaarea408852005-06-25 22:49:46 +00003998
3999# if defined(EXITFREE) || defined(PROTO)
4000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004002{
4003 vim_free(lasttitle);
4004 vim_free(lasticon);
4005}
4006# endif
4007
Bram Moolenaarc667da52019-11-30 20:52:27 +01004008#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004010#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004012 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * Return length of string in screen cells.
4014 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004015 * Normally works for window "wp", except when working for 'tabline' then it
4016 * is "curwin".
4017 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * Items are drawn interspersed with the text that surrounds it
4019 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4020 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4021 *
4022 * If maxwidth is not zero, the string will be filled at any middle marker
4023 * or truncated if too long, fillchar is used for all whitespace.
4024 */
4025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026build_stl_str_hl(
4027 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004028 char_u *out, // buffer to write into != NameBuff
4029 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004030 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004031 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004032 int fillchar,
4033 int maxwidth,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004034 struct stl_hlrec *hltab, // return: HL attributes (can be NULL)
4035 struct stl_hlrec *tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
Bram Moolenaar10772302019-01-20 18:25:54 +01004037 linenr_T lnum;
4038 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *p;
4040 char_u *s;
4041 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004042 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004044 win_T *save_curwin;
4045 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004046 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047#endif
4048 int empty_line;
4049 colnr_T virtcol;
4050 long l;
4051 long n;
4052 int prevchar_isflag;
4053 int prevchar_isitem;
4054 int itemisflag;
4055 int fillable;
4056 char_u *str;
4057 long num;
4058 int width;
4059 int itemcnt;
4060 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004061 int group_end_userhl;
4062 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 int groupitem[STL_MAX_ITEM];
4064 int groupdepth;
4065 struct stl_item
4066 {
4067 char_u *start;
4068 int minwid;
4069 int maxwid;
4070 enum
4071 {
4072 Normal,
4073 Empty,
4074 Group,
4075 Middle,
4076 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004077 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 Trunc
4079 } type;
4080 } item[STL_MAX_ITEM];
4081 int minwid;
4082 int maxwid;
4083 int zeropad;
4084 char_u base;
4085 char_u opt;
4086#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004087 char_u buf_tmp[TMPLEN];
4088 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004089 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004090 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004091 int save_must_redraw = must_redraw;
4092 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004093
4094#ifdef FEAT_EVAL
4095 /*
4096 * When the format starts with "%!" then evaluate it as an expression and
4097 * use the result as the actual format string.
4098 */
4099 if (fmt[0] == '%' && fmt[1] == '!')
4100 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004101 typval_T tv;
4102
4103 tv.v_type = VAR_NUMBER;
4104 tv.vval.v_number = wp->w_id;
4105 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4106
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004107 usefmt = eval_to_string_safe(fmt + 2, use_sandbox);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004108 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004109 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004110
4111 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004112 }
4113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 if (fillchar == 0)
4116 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004117 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004118 else if (mb_char2len(fillchar) > 1)
4119 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004120
Bram Moolenaar10772302019-01-20 18:25:54 +01004121 // The cursor in windows other than the current one isn't always
4122 // up-to-date, esp. because of autocommands and timers.
4123 lnum = wp->w_cursor.lnum;
4124 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4125 {
4126 lnum = wp->w_buffer->b_ml.ml_line_count;
4127 wp->w_cursor.lnum = lnum;
4128 }
4129
4130 // Get line & check if empty (cursorpos will show "0-1"). Note that
4131 // p will become invalid when getting another buffer line.
4132 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004133 empty_line = (*p == NUL);
4134
Bram Moolenaar10772302019-01-20 18:25:54 +01004135 // Get the byte value now, in case we need it below. This is more efficient
4136 // than making a copy of the line.
4137 len = STRLEN(p);
4138 if (wp->w_cursor.col > (colnr_T)len)
4139 {
4140 // Line may have changed since checking the cursor column, or the lnum
4141 // was adjusted above.
4142 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004143 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004144 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004145 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004146 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004147 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 groupdepth = 0;
4150 p = out;
4151 curitem = 0;
4152 prevchar_isflag = TRUE;
4153 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004154 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004156 if (curitem == STL_MAX_ITEM)
4157 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004158 // There are too many items. Add the error code to the statusline
4159 // to give the user a hint about what went wrong.
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004160 if (p + 6 < out + outlen)
4161 {
4162 mch_memmove(p, " E541", (size_t)5);
4163 p += 5;
4164 }
4165 break;
4166 }
4167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (*s != NUL && *s != '%')
4169 prevchar_isflag = prevchar_isitem = FALSE;
4170
4171 /*
4172 * Handle up to the next '%' or the end.
4173 */
4174 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4175 *p++ = *s++;
4176 if (*s == NUL || p + 1 >= out + outlen)
4177 break;
4178
4179 /*
4180 * Handle one '%' item.
4181 */
4182 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004183 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004184 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 if (*s == '%')
4186 {
4187 if (p + 1 >= out + outlen)
4188 break;
4189 *p++ = *s++;
4190 prevchar_isflag = prevchar_isitem = FALSE;
4191 continue;
4192 }
4193 if (*s == STL_MIDDLEMARK)
4194 {
4195 s++;
4196 if (groupdepth > 0)
4197 continue;
4198 item[curitem].type = Middle;
4199 item[curitem++].start = p;
4200 continue;
4201 }
4202 if (*s == STL_TRUNCMARK)
4203 {
4204 s++;
4205 item[curitem].type = Trunc;
4206 item[curitem++].start = p;
4207 continue;
4208 }
4209 if (*s == ')')
4210 {
4211 s++;
4212 if (groupdepth < 1)
4213 continue;
4214 groupdepth--;
4215
4216 t = item[groupitem[groupdepth]].start;
4217 *p = NUL;
4218 l = vim_strsize(t);
4219 if (curitem > groupitem[groupdepth] + 1
4220 && item[groupitem[groupdepth]].minwid == 0)
4221 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004222 // remove group if all items are empty and highlight group
4223 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004224 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004225 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4226 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004227 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004228 {
4229 group_start_userhl = group_end_userhl = item[n].minwid;
4230 break;
4231 }
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004234 {
4235 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004237 if (item[n].type == Highlight)
4238 group_end_userhl = item[n].minwid;
4239 }
4240 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004242 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 p = t;
4244 l = 0;
Bram Moolenaardbe5d362020-02-08 18:35:31 +01004245 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004246 {
4247 // do not use the highlighting from the removed group
Bram Moolenaardbe5d362020-02-08 18:35:31 +01004248 if (item[n].type == Highlight)
4249 item[n].type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004250 // adjust the start position of TabPage to the next
4251 // item position
4252 if (item[n].type == TabPage)
4253 item[n].start = p;
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
4257 if (l > item[groupitem[groupdepth]].maxwid)
4258 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004259 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (has_mbyte)
4261 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004262 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 n = 0;
4264 while (l >= item[groupitem[groupdepth]].maxwid)
4265 {
4266 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004267 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004271 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004274 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004276
4277 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 while (++l < item[groupitem[groupdepth]].minwid)
4279 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaarc667da52019-11-30 20:52:27 +01004281 // correct the start of the items for the truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4283 {
4284 item[l].start -= n;
4285 if (item[l].start < t)
4286 item[l].start = t;
4287 }
4288 }
4289 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4290 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004291 // fill
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 n = item[groupitem[groupdepth]].minwid;
4293 if (n < 0)
4294 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004295 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 n = 0 - n;
4297 while (l++ < n && p + 1 < out + outlen)
4298 *p++ = fillchar;
4299 }
4300 else
4301 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004302 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004303 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 l = n - l;
4305 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004306 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 p += l;
4308 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4309 item[n].start += l;
4310 for ( ; l > 0; l--)
4311 *t++ = fillchar;
4312 }
4313 }
4314 continue;
4315 }
4316 minwid = 0;
4317 maxwid = 9999;
4318 zeropad = FALSE;
4319 l = 1;
4320 if (*s == '0')
4321 {
4322 s++;
4323 zeropad = TRUE;
4324 }
4325 if (*s == '-')
4326 {
4327 s++;
4328 l = -1;
4329 }
4330 if (VIM_ISDIGIT(*s))
4331 {
4332 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004333 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 minwid = 0;
4335 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004336 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338 item[curitem].type = Highlight;
4339 item[curitem].start = p;
4340 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4341 s++;
4342 curitem++;
4343 continue;
4344 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004345 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4346 {
4347 if (*s == STL_TABCLOSENR)
4348 {
4349 if (minwid == 0)
4350 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004351 // %X ends the close label, go back to the previously
4352 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004353 for (n = curitem - 1; n >= 0; --n)
4354 if (item[n].type == TabPage && item[n].minwid >= 0)
4355 {
4356 minwid = item[n].minwid;
4357 break;
4358 }
4359 }
4360 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004361 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004362 minwid = - minwid;
4363 }
4364 item[curitem].type = TabPage;
4365 item[curitem].start = p;
4366 item[curitem].minwid = minwid;
4367 s++;
4368 curitem++;
4369 continue;
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (*s == '.')
4372 {
4373 s++;
4374 if (VIM_ISDIGIT(*s))
4375 {
4376 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004377 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 maxwid = 50;
4379 }
4380 }
4381 minwid = (minwid > 50 ? 50 : minwid) * l;
4382 if (*s == '(')
4383 {
4384 groupitem[groupdepth++] = curitem;
4385 item[curitem].type = Group;
4386 item[curitem].start = p;
4387 item[curitem].minwid = minwid;
4388 item[curitem].maxwid = maxwid;
4389 s++;
4390 curitem++;
4391 continue;
4392 }
4393 if (vim_strchr(STL_ALL, *s) == NULL)
4394 {
4395 s++;
4396 continue;
4397 }
4398 opt = *s++;
4399
Bram Moolenaarc667da52019-11-30 20:52:27 +01004400 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 base = 'D';
4402 itemisflag = FALSE;
4403 fillable = TRUE;
4404 num = -1;
4405 str = NULL;
4406 switch (opt)
4407 {
4408 case STL_FILEPATH:
4409 case STL_FULLPATH:
4410 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004411 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004413 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 else
4415 {
4416 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004417 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4419 }
4420 trans_characters(NameBuff, MAXPATHL);
4421 if (opt != STL_FILENAME)
4422 str = NameBuff;
4423 else
4424 str = gettail(NameBuff);
4425 break;
4426
Bram Moolenaarc667da52019-11-30 20:52:27 +01004427 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 itemisflag = TRUE;
4429 t = p;
4430 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4431 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004432 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434 s++;
4435 *p = 0;
4436 p = t;
4437
4438#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004439 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4440 "%d", curbuf->b_fnum);
4441 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4442 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4443 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004445 save_curbuf = curbuf;
4446 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004447 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 curwin = wp;
4449 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004450 // Visual mode is only valid in the current window.
4451 if (curwin != save_curwin)
4452 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004454 str = eval_to_string_safe(p, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004456 curwin = save_curwin;
4457 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004458 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004459 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004460 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 if (str != NULL && *str != 0)
4463 {
4464 if (*skipdigits(str) == NUL)
4465 {
4466 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004467 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 itemisflag = FALSE;
4469 }
4470 }
4471#endif
4472 break;
4473
4474 case STL_LINE:
4475 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4476 ? 0L : (long)(wp->w_cursor.lnum);
4477 break;
4478
4479 case STL_NUMLINES:
4480 num = wp->w_buffer->b_ml.ml_line_count;
4481 break;
4482
4483 case STL_COLUMN:
4484 num = !(State & INSERT) && empty_line
4485 ? 0 : (int)wp->w_cursor.col + 1;
4486 break;
4487
4488 case STL_VIRTCOL:
4489 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004490 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 virtcol = wp->w_virtcol;
4492 if (wp->w_p_list && lcs_tab1 == NUL)
4493 {
4494 wp->w_p_list = FALSE;
4495 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4496 wp->w_p_list = TRUE;
4497 }
4498 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004499 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (opt == STL_VIRTCOL_ALT
4501 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4502 ? 0 : (int)wp->w_cursor.col + 1)))
4503 break;
4504 num = (long)virtcol;
4505 break;
4506
4507 case STL_PERCENTAGE:
4508 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4509 (long)wp->w_buffer->b_ml.ml_line_count);
4510 break;
4511
4512 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004513 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004514 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516
4517 case STL_ARGLISTSTAT:
4518 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004519 buf_tmp[0] = 0;
4520 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4521 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 break;
4523
4524 case STL_KEYMAP:
4525 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004526 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4527 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 break;
4529 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004530#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004531 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532#else
4533 num = 0;
4534#endif
4535 break;
4536
4537 case STL_BUFNO:
4538 num = wp->w_buffer->b_fnum;
4539 break;
4540
4541 case STL_OFFSET_X:
4542 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004543 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 case STL_OFFSET:
4545#ifdef FEAT_BYTEOFF
4546 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4547 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4548 0L : l + 1 + (!(State & INSERT) && empty_line ?
4549 0 : (int)wp->w_cursor.col);
4550#endif
4551 break;
4552
4553 case STL_BYTEVAL_X:
4554 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004555 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004557 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (num == NL)
4559 num = 0;
4560 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4561 num = NL;
4562 break;
4563
4564 case STL_ROFLAG:
4565 case STL_ROFLAG_ALT:
4566 itemisflag = TRUE;
4567 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004568 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 break;
4570
4571 case STL_HELPFLAG:
4572 case STL_HELPFLAG_ALT:
4573 itemisflag = TRUE;
4574 if (wp->w_buffer->b_help)
4575 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004576 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 break;
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 case STL_FILETYPE:
4580 if (*wp->w_buffer->b_p_ft != NUL
4581 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4582 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004583 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004584 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004585 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587 break;
4588
4589 case STL_FILETYPE_ALT:
4590 itemisflag = TRUE;
4591 if (*wp->w_buffer->b_p_ft != NUL
4592 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4593 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004594 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004595 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004596 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004598 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar4033c552017-09-16 20:54:51 +02004602#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 case STL_PREVIEWFLAG:
4604 case STL_PREVIEWFLAG_ALT:
4605 itemisflag = TRUE;
4606 if (wp->w_p_pvw)
4607 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4608 : _("[Preview]"));
4609 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004610
4611 case STL_QUICKFIX:
4612 if (bt_quickfix(wp->w_buffer))
4613 str = (char_u *)(wp->w_llist_ref
4614 ? _(msg_loclist)
4615 : _(msg_qflist));
4616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617#endif
4618
4619 case STL_MODIFIED:
4620 case STL_MODIFIED_ALT:
4621 itemisflag = TRUE;
4622 switch ((opt == STL_MODIFIED_ALT)
4623 + bufIsChanged(wp->w_buffer) * 2
4624 + (!wp->w_buffer->b_p_ma) * 4)
4625 {
4626 case 2: str = (char_u *)"[+]"; break;
4627 case 3: str = (char_u *)",+"; break;
4628 case 4: str = (char_u *)"[-]"; break;
4629 case 5: str = (char_u *)",-"; break;
4630 case 6: str = (char_u *)"[+-]"; break;
4631 case 7: str = (char_u *)",+-"; break;
4632 }
4633 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004634
4635 case STL_HIGHLIGHT:
4636 t = s;
4637 while (*s != '#' && *s != NUL)
4638 ++s;
4639 if (*s == '#')
4640 {
4641 item[curitem].type = Highlight;
4642 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004643 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004644 curitem++;
4645 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004646 if (*s != NUL)
4647 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004648 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650
4651 item[curitem].start = p;
4652 item[curitem].type = Normal;
4653 if (str != NULL && *str)
4654 {
4655 t = str;
4656 if (itemisflag)
4657 {
4658 if ((t[0] && t[1])
4659 && ((!prevchar_isitem && *t == ',')
4660 || (prevchar_isflag && *t == ' ')))
4661 t++;
4662 prevchar_isflag = TRUE;
4663 }
4664 l = vim_strsize(t);
4665 if (l > 0)
4666 prevchar_isitem = TRUE;
4667 if (l > maxwid)
4668 {
4669 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (has_mbyte)
4671 {
4672 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004673 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 l -= byte2cells(*t++);
4677 if (p + 1 >= out + outlen)
4678 break;
4679 *p++ = '<';
4680 }
4681 if (minwid > 0)
4682 {
4683 for (; l < minwid && p + 1 < out + outlen; l++)
4684 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004685 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4687 *p++ = ' ';
4688 else
4689 *p++ = fillchar;
4690 }
4691 minwid = 0;
4692 }
4693 else
4694 minwid *= -1;
4695 while (*t && p + 1 < out + outlen)
4696 {
4697 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004698 // Change a space by fillchar, unless fillchar is '-' and a
4699 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (fillable && p[-1] == ' '
4701 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4702 p[-1] = fillchar;
4703 }
4704 for (; l < minwid && p + 1 < out + outlen; l++)
4705 *p++ = fillchar;
4706 }
4707 else if (num >= 0)
4708 {
4709 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4710 char_u nstr[20];
4711
4712 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004713 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 prevchar_isitem = TRUE;
4715 t = nstr;
4716 if (opt == STL_VIRTCOL_ALT)
4717 {
4718 *t++ = '-';
4719 minwid--;
4720 }
4721 *t++ = '%';
4722 if (zeropad)
4723 *t++ = '0';
4724 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004725 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 *t = 0;
4727
4728 for (n = num, l = 1; n >= nbase; n /= nbase)
4729 l++;
4730 if (opt == STL_VIRTCOL_ALT)
4731 l++;
4732 if (l > maxwid)
4733 {
4734 l += 2;
4735 n = l - maxwid;
4736 while (l-- > maxwid)
4737 num /= nbase;
4738 *t++ = '>';
4739 *t++ = '%';
4740 *t = t[-3];
4741 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004742 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4743 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004746 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4747 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 p += STRLEN(p);
4749 }
4750 else
4751 item[curitem].type = Empty;
4752
4753 if (opt == STL_VIM_EXPR)
4754 vim_free(str);
4755
4756 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004757 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 curitem++;
4759 }
4760 *p = NUL;
4761 itemcnt = curitem;
4762
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004763#ifdef FEAT_EVAL
4764 if (usefmt != fmt)
4765 vim_free(usefmt);
4766#endif
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 width = vim_strsize(out);
4769 if (maxwidth > 0 && width > maxwidth)
4770 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004771 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 l = 0;
4773 if (itemcnt == 0)
4774 s = out;
4775 else
4776 {
4777 for ( ; l < itemcnt; l++)
4778 if (item[l].type == Trunc)
4779 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004780 // Truncate at %< item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 s = item[l].start;
4782 break;
4783 }
4784 if (l == itemcnt)
4785 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004786 // No %< item, truncate first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 s = item[0].start;
4788 l = 0;
4789 }
4790 }
4791
4792 if (width - vim_strsize(s) >= maxwidth)
4793 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004794 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (has_mbyte)
4796 {
4797 s = out;
4798 width = 0;
4799 for (;;)
4800 {
4801 width += ptr2cells(s);
4802 if (width >= maxwidth)
4803 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004804 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004806 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 while (++width < maxwidth)
4808 *s++ = fillchar;
4809 }
4810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 s = out + maxwidth - 1;
4812 for (l = 0; l < itemcnt; l++)
4813 if (item[l].start > s)
4814 break;
4815 itemcnt = l;
4816 *s++ = '>';
4817 *s = 0;
4818 }
4819 else
4820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (has_mbyte)
4822 {
4823 n = 0;
4824 while (width >= maxwidth)
4825 {
4826 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004827 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 }
4830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 n = width - maxwidth + 1;
4832 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004833 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 *s = '<';
4835
Bram Moolenaarc667da52019-11-30 20:52:27 +01004836 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 while (++width < maxwidth)
4838 {
4839 s = s + STRLEN(s);
4840 *s++ = fillchar;
4841 *s = NUL;
4842 }
4843
Bram Moolenaarc667da52019-11-30 20:52:27 +01004844 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 for (; l < itemcnt; l++)
4846 {
4847 if (item[l].start - n >= s)
4848 item[l].start -= n;
4849 else
4850 item[l].start = s;
4851 }
4852 }
4853 width = maxwidth;
4854 }
4855 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4856 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004857 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 for (l = 0; l < itemcnt; l++)
4859 if (item[l].type == Middle)
4860 break;
4861 if (l < itemcnt)
4862 {
4863 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004864 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 for (s = item[l].start; s < p; s++)
4866 *s = fillchar;
4867 for (l++; l < itemcnt; l++)
4868 item[l].start += maxwidth - width;
4869 width = maxwidth;
4870 }
4871 }
4872
Bram Moolenaarc667da52019-11-30 20:52:27 +01004873 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004874 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004876 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 for (l = 0; l < itemcnt; l++)
4878 {
4879 if (item[l].type == Highlight)
4880 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004881 sp->start = item[l].start;
4882 sp->userhl = item[l].minwid;
4883 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004886 sp->start = NULL;
4887 sp->userhl = 0;
4888 }
4889
Bram Moolenaarc667da52019-11-30 20:52:27 +01004890 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004891 if (tabtab != NULL)
4892 {
4893 sp = tabtab;
4894 for (l = 0; l < itemcnt; l++)
4895 {
4896 if (item[l].type == TabPage)
4897 {
4898 sp->start = item[l].start;
4899 sp->userhl = item[l].minwid;
4900 sp++;
4901 }
4902 }
4903 sp->start = NULL;
4904 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906
Bram Moolenaarc667da52019-11-30 20:52:27 +01004907 // When inside update_screen we do not want redrawing a stausline, ruler,
4908 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004909 if (updating_screen)
4910 {
4911 must_redraw = save_must_redraw;
4912 curwin->w_redr_type = save_redr_type;
4913 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 return width;
4916}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004917#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004919#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4920 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004922 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4923 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 */
4925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004926get_rel_pos(
4927 win_T *wp,
4928 char_u *buf,
4929 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004931 long above; // number of lines above window
4932 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
Bram Moolenaarc667da52019-11-30 20:52:27 +01004934 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004935 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 above = wp->w_topline - 1;
4937#ifdef FEAT_DIFF
4938 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004939 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004940 above = 0; // All buffer lines are displayed and there is an
4941 // indication of filler lines, that can be considered
4942 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#endif
4944 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4945 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004946 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4947 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004949 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004951 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 ? (int)(above / ((above + below) / 100L))
4953 : (int)(above * 100L / (above + below)));
4954}
4955#endif
4956
4957/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004958 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * Return TRUE if it was appended.
4960 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004962append_arg_number(
4963 win_T *wp,
4964 char_u *buf,
4965 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004966 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 char_u *p;
4969
Bram Moolenaarc667da52019-11-30 20:52:27 +01004970 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 return FALSE;
4972
Bram Moolenaarc667da52019-11-30 20:52:27 +01004973 p = buf + STRLEN(buf); // go to the end of the buffer
4974 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return FALSE;
4976 *p++ = ' ';
4977 *p++ = '(';
4978 if (add_file)
4979 {
4980 STRCPY(p, "file ");
4981 p += 5;
4982 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004983 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4984 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4986 return TRUE;
4987}
4988
4989/*
4990 * If fname is not a full path, make it a full path.
4991 * Returns pointer to allocated memory (NULL for failure).
4992 */
4993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004994fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
4996 /*
4997 * Force expanding the path always for Unix, because symbolic links may
4998 * mess up the full path name, even though it starts with a '/'.
4999 * Also expand when there is ".." in the file name, try to remove it,
5000 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005001 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 * For MS-Windows also expand names like "longna~1" to "longname".
5003 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005004#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 return FullName_save(fname, TRUE);
5006#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005007 if (!vim_isAbsName(fname)
5008 || strstr((char *)fname, "..") != NULL
5009 || strstr((char *)fname, "//") != NULL
5010# ifdef BACKSLASH_IN_FILENAME
5011 || strstr((char *)fname, "\\\\") != NULL
5012# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005013# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 )
5017 return FullName_save(fname, FALSE);
5018
5019 fname = vim_strsave(fname);
5020
Bram Moolenaar9b942202007-10-03 12:31:33 +00005021# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005022 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005023 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
5026 return fname;
5027#endif
5028}
5029
5030/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005031 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5032 * "*ffname" becomes a pointer to allocated memory (or NULL).
5033 * When resolving a link both "*sfname" and "*ffname" will point to the same
5034 * allocated memory.
5035 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005036 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039fname_expand(
5040 buf_T *buf UNUSED,
5041 char_u **ffname,
5042 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005044 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005046 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005048 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050#ifdef FEAT_SHORTCUT
5051 if (!buf->b_p_bin)
5052 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005053 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005055 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005056 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005057 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 vim_free(*ffname);
5060 *ffname = rfname;
5061 *sfname = rfname;
5062 }
5063 }
5064#endif
5065}
5066
5067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 * Open a window for a number of buffers.
5069 */
5070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005071ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073 buf_T *buf;
5074 win_T *wp, *wpnext;
5075 int split_ret = OK;
5076 int p_ea_save;
5077 int open_wins = 0;
5078 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005079 int count; // Maximum number of windows to open.
5080 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005081 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005082 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaarc667da52019-11-30 20:52:27 +01005084 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 count = 9999;
5086 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005087 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5089 all = FALSE;
5090 else
5091 all = TRUE;
5092
5093 setpcmark();
5094
5095#ifdef FEAT_GUI
5096 need_mouse_correct = TRUE;
5097#endif
5098
5099 /*
5100 * Close superfluous windows (two windows for the same buffer).
5101 * Also close windows that are not full-width.
5102 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005103 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005104 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005105 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005107 tpnext = curtab->tp_next;
5108 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005110 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005111 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1004402020-10-24 20:49:43 +02005112 || ((cmdmod.cmod_split & WSP_VERT)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005113 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005114 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005115 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005116 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005117 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005118 {
5119 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005120 wpnext = firstwin; // just in case an autocommand does
5121 // something strange with windows
5122 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005123 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005124 }
5125 else
5126 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005128
Bram Moolenaarc667da52019-11-30 20:52:27 +01005129 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005130 if (had_tab == 0 || tpnext == NULL)
5131 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005132 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134
5135 /*
5136 * Go through the buffer list. When a buffer doesn't have a window yet,
5137 * open one. Otherwise move the window to the right position.
5138 * Watch out for autocommands that delete buffers or windows!
5139 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005140 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5145 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005146 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5148 continue;
5149
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005150 if (had_tab != 0)
5151 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005152 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005153 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005154 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005155 else
5156 wp = NULL;
5157 }
5158 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005159 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005160 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005161 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005162 if (wp->w_buffer == buf)
5163 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005164 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005165 if (wp != NULL)
5166 win_move_after(wp, curwin);
5167 }
5168
5169 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005171 bufref_T bufref;
5172
5173 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005174
Bram Moolenaarc667da52019-11-30 20:52:27 +01005175 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005177 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5179 ++open_wins;
5180 p_ea = p_ea_save;
5181 if (split_ret == FAIL)
5182 continue;
5183
Bram Moolenaarc667da52019-11-30 20:52:27 +01005184 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005187 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005189 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (swap_exists_action == SEA_QUIT)
5194 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005195#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005196 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005197
Bram Moolenaarc667da52019-11-30 20:52:27 +01005198 // Reset the error/interrupt/exception state here so that
5199 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005200 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005201#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005202
Bram Moolenaarc667da52019-11-30 20:52:27 +01005203 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 win_close(curwin, TRUE);
5205 --open_wins;
5206 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005207 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005208
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005209#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005210 // Restore the error/interrupt/exception state if not
5211 // discarded by a new aborting error, interrupt, or uncaught
5212 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005213 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 else
5217 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219
5220 ui_breakcheck();
5221 if (got_int)
5222 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005223 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 break;
5225 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005226#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005227 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005228 if (aborting())
5229 break;
5230#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005231 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005232 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005233 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005236 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 /*
5240 * Close superfluous windows.
5241 */
5242 for (wp = lastwin; open_wins > count; )
5243 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005244 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (!win_valid(wp))
5247 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005248 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 wp = lastwin;
5250 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005251 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005253 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 --open_wins;
5255 wp = lastwin;
5256 }
5257 else
5258 {
5259 wp = wp->w_prev;
5260 if (wp == NULL)
5261 break;
5262 }
5263 }
5264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005267static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
5270 * do_modelines() - process mode lines for the current file
5271 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005272 * "flags" can be:
5273 * OPT_WINONLY only set options local to window
5274 * OPT_NOWIN don't set options local to window
5275 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 * Returns immediately if the "ml" option isn't set.
5277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005279do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005281 linenr_T lnum;
5282 int nmlines;
5283 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5286 return;
5287
Bram Moolenaarc667da52019-11-30 20:52:27 +01005288 // Disallow recursive entry here. Can happen when executing a modeline
5289 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 if (entered)
5291 return;
5292
5293 ++entered;
5294 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5295 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005296 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 nmlines = 0;
5298
5299 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5300 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005301 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 nmlines = 0;
5303 --entered;
5304}
5305
Bram Moolenaarc667da52019-11-30 20:52:27 +01005306#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
5308/*
5309 * chk_modeline() - check a single line for a mode string
5310 * Return FAIL if an error encountered.
5311 */
5312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313chk_modeline(
5314 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005315 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 char_u *s;
5318 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005319 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 int prev;
5321 int vers;
5322 int end;
5323 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005325 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01005327 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 prev = -1;
5330 for (s = ml_get(lnum); *s != NUL; ++s)
5331 {
5332 if (prev == -1 || vim_isspace(prev))
5333 {
5334 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5335 || STRNCMP(s, "vi:", (size_t)3) == 0)
5336 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005337 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005338 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
5340 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5341 e = s + 4;
5342 else
5343 e = s + 3;
5344 vers = getdigits(&e);
5345 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005346 && (s[0] != 'V'
5347 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 && (s[3] == ':'
5349 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5350 || (VIM_VERSION_100 < vers && s[3] == '<')
5351 || (VIM_VERSION_100 > vers && s[3] == '>')
5352 || (VIM_VERSION_100 == vers && s[3] == '=')))
5353 break;
5354 }
5355 }
5356 prev = *s;
5357 }
5358
5359 if (*s)
5360 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005361 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ++s;
5363 while (s[-1] != ':');
5364
Bram Moolenaarc667da52019-11-30 20:52:27 +01005365 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (linecopy == NULL)
5367 return FAIL;
5368
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005369 // prepare for emsg()
5370 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005371 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 end = FALSE;
5374 while (end == FALSE)
5375 {
5376 s = skipwhite(s);
5377 if (*s == NUL)
5378 break;
5379
5380 /*
5381 * Find end of set command: ':' or end of line.
5382 * Skip over "\:", replacing it with ":".
5383 */
5384 for (e = s; *e != ':' && *e != NUL; ++e)
5385 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005386 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (*e == NUL)
5388 end = TRUE;
5389
5390 /*
5391 * If there is a "set" command, require a terminating ':' and
5392 * ignore the stuff after the ':'.
5393 * "vi:set opt opt opt: foo" -- foo not interpreted
5394 * "vi:opt opt opt: foo" -- foo interpreted
5395 * Accept "se" for compatibility with Elvis.
5396 */
5397 if (STRNCMP(s, "set ", (size_t)4) == 0
5398 || STRNCMP(s, "se ", (size_t)3) == 0)
5399 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005400 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 break;
5402 end = TRUE;
5403 s = vim_strchr(s, ' ') + 1;
5404 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005405 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
Bram Moolenaarc667da52019-11-30 20:52:27 +01005407 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005409 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005411 save_current_sctx = current_sctx;
5412 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005413 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005414 current_sctx.sc_lnum = lnum;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005415 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005417 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005418 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005419
Bram Moolenaara3227e22006-03-08 21:32:40 +00005420 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005421
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005422 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005424 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005426 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 break;
5428 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005429 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
Bram Moolenaare31ee862020-01-07 20:59:34 +01005432 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005433 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 vim_free(linecopy);
5435 }
5436 return retval;
5437}
5438
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005439/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005440 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5441 */
5442 int
5443bt_normal(buf_T *buf)
5444{
5445 return buf != NULL && buf->b_p_bt[0] == NUL;
5446}
5447
Bram Moolenaar113e1072019-01-20 15:30:40 +01005448#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005449/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005450 * Return TRUE if "buf" is the quickfix buffer.
5451 */
5452 int
5453bt_quickfix(buf_T *buf)
5454{
5455 return buf != NULL && buf->b_p_bt[0] == 'q';
5456}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005457#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005458
Bram Moolenaar113e1072019-01-20 15:30:40 +01005459#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005460/*
5461 * Return TRUE if "buf" is a terminal buffer.
5462 */
5463 int
5464bt_terminal(buf_T *buf)
5465{
5466 return buf != NULL && buf->b_p_bt[0] == 't';
5467}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005468#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005469
5470/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005471 * Return TRUE if "buf" is a help buffer.
5472 */
5473 int
5474bt_help(buf_T *buf)
5475{
5476 return buf != NULL && buf->b_help;
5477}
5478
5479/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005480 * Return TRUE if "buf" is a prompt buffer.
5481 */
5482 int
5483bt_prompt(buf_T *buf)
5484{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005485 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5486}
5487
5488/*
5489 * Return TRUE if "buf" is a buffer for a popup window.
5490 */
5491 int
5492bt_popup(buf_T *buf)
5493{
5494 return buf != NULL && buf->b_p_bt != NULL
5495 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005496}
5497
5498/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005499 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5500 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005501 */
5502 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005503bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005504{
5505 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5506 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005507 || buf->b_p_bt[0] == 't'
5508 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005509}
5510
5511/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005512 * Return TRUE if "buf" has 'buftype' set to "nofile".
5513 */
5514 int
5515bt_nofile(buf_T *buf)
5516{
5517 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5518}
5519
5520/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005521 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5522 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005523 */
5524 int
5525bt_dontwrite(buf_T *buf)
5526{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005527 return buf != NULL && (buf->b_p_bt[0] == 'n'
5528 || buf->b_p_bt[0] == 't'
5529 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005530}
5531
Bram Moolenaar113e1072019-01-20 15:30:40 +01005532#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005533 int
5534bt_dontwrite_msg(buf_T *buf)
5535{
5536 if (bt_dontwrite(buf))
5537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005538 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005539 return TRUE;
5540 }
5541 return FALSE;
5542}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005543#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005544
5545/*
5546 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5547 * and 'bufhidden'.
5548 */
5549 int
5550buf_hide(buf_T *buf)
5551{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005552 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005553 switch (buf->b_p_bh[0])
5554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005555 case 'u': // "unload"
5556 case 'w': // "wipe"
5557 case 'd': return FALSE; // "delete"
5558 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005559 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005560 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563/*
5564 * Return special buffer name.
5565 * Returns NULL when the buffer has a normal file name.
5566 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005567 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005568buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005570#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005572 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005573 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005574 * Differentiate between the quickfix and location list buffers using
5575 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005576 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005577 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005578 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005579 else
5580 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005583
Bram Moolenaarc667da52019-11-30 20:52:27 +01005584 // There is no _file_ when 'buftype' is "nofile", b_sfname
5585 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005586 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005588#ifdef FEAT_TERMINAL
5589 if (buf->b_term != NULL)
5590 return term_get_status_text(buf->b_term);
5591#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005592 if (buf->b_fname != NULL)
5593 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005594#ifdef FEAT_JOB_CHANNEL
5595 if (bt_prompt(buf))
5596 return (char_u *)_("[Prompt]");
5597#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005598#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005599 if (bt_popup(buf))
5600 return (char_u *)_("[Popup]");
5601#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005602 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005606 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 return NULL;
5608}
5609
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610/*
5611 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5612 */
5613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005614set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 if (on != curbuf->b_p_bl)
5617 {
5618 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 if (on)
5620 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5621 else
5622 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624}
5625
5626/*
5627 * Read the file for "buf" again and check if the contents changed.
5628 * Return TRUE if it changed or this could not be checked.
5629 */
5630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005631buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 buf_T *newbuf;
5634 int differ = TRUE;
5635 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 exarg_T ea;
5638
Bram Moolenaarc667da52019-11-30 20:52:27 +01005639 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5641 if (newbuf == NULL)
5642 return TRUE;
5643
Bram Moolenaarc667da52019-11-30 20:52:27 +01005644 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (prep_exarg(&ea, buf) == FAIL)
5646 {
5647 wipe_buffer(newbuf, FALSE);
5648 return TRUE;
5649 }
5650
Bram Moolenaarc667da52019-11-30 20:52:27 +01005651 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar4770d092006-01-12 23:22:24 +00005654 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 && readfile(buf->b_ffname, buf->b_fname,
5656 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5657 &ea, READ_NEW | READ_DUMMY) == OK)
5658 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005659 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5661 {
5662 differ = FALSE;
5663 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5664 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5665 {
5666 differ = TRUE;
5667 break;
5668 }
5669 }
5670 }
5671 vim_free(ea.cmd);
5672
Bram Moolenaarc667da52019-11-30 20:52:27 +01005673 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaarc667da52019-11-30 20:52:27 +01005676 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 wipe_buffer(newbuf, FALSE);
5678
5679 return differ;
5680}
5681
5682/*
5683 * Wipe out a buffer and decrement the last buffer number if it was used for
5684 * this buffer. Call this to wipe out a temp buffer that does not contain any
5685 * marks.
5686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005688wipe_buffer(
5689 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005690 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 if (buf->b_fnum == top_file_num - 1)
5693 --top_file_num;
5694
Bram Moolenaarc667da52019-11-30 20:52:27 +01005695 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005696 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005697
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005698 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005701 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702}