blob: c14aefb0e3cf137aab9cd4e65470121e7f43abf6 [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 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100555 else if (buf->b_p_bh[0] == 'h' && !del_buf)
556 {
557 // Hide a terminal buffer.
558 unload_buf = FALSE;
559 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200560 else
561 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100562 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200563 del_buf = TRUE;
564 unload_buf = TRUE;
565 wipe_buf = TRUE;
566 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100567 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200568 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // Disallow deleting the buffer when it is locked (already being closed or
572 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200573 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200574 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200575
Bram Moolenaarc667da52019-11-30 20:52:27 +0100576 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200577 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100579 // Set b_last_cursor when closing the last window for the buffer.
580 // Remember the last cursor position and window options of the buffer.
581 // This used to be only for the current window, but then options like
582 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (buf->b_nwindows == 1)
584 set_last_cursor(win);
585 buflist_setfpos(buf, win,
586 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
587 win->w_cursor.col, TRUE);
588 }
589
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200590 set_bufref(&bufref, buf);
591
Bram Moolenaarc667da52019-11-30 20:52:27 +0100592 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (buf->b_nwindows == 1)
594 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200595 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200596 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
597 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200598 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100599 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100600 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200601aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100602 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100604 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200605 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200606 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200608 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaarc667da52019-11-30 20:52:27 +0100610 // When the buffer becomes hidden, but is not unloaded, trigger
611 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (!unload_buf)
613 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200614 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200615 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
616 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200617 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100618 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200619 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200620 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200621 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100622 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200623 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100625#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100626 // autocmds may abort script processing
627 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200631
Bram Moolenaarc667da52019-11-30 20:52:27 +0100632 // If the buffer was in curwin and the window has changed, go back to that
633 // window, if it still exists. This avoids that ":edit x" triggering a
634 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200635 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
636 {
637 block_autocmds();
638 goto_tabpage_win(the_curtab, the_curwin);
639 unblock_autocmds();
640 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaarc667da52019-11-30 20:52:27 +0100644 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (buf->b_nwindows > 0)
646 --buf->b_nwindows;
647
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100648#ifdef FEAT_DIFF
649 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100650 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100651#endif
652
Bram Moolenaarc667da52019-11-30 20:52:27 +0100653 // Return when a window is displaying the buffer or when it's not
654 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
Bram Moolenaarc667da52019-11-30 20:52:27 +0100658 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (buf->b_ffname == NULL)
660 del_buf = TRUE;
661
Bram Moolenaarc667da52019-11-30 20:52:27 +0100662 // When closing the current buffer stop Visual mode before freeing
663 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200664 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200665#if defined(EXITFREE)
666 && !entered_free_all_mem
667#endif
668 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200669 end_visual_mode();
670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /*
672 * Free all things allocated for this buffer.
673 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
674 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100675 // Remember if we are closing the current buffer. Restore the number of
676 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 is_curbuf = (buf == curbuf);
678 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100680 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100681 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100682 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200685 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100687#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100688 // autocmds may abort script processing
689 if (!ignore_abort && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 /*
694 * It's possible that autocommands change curbuf to the one being deleted.
695 * This might cause the previous curbuf to be deleted unexpectedly. But
696 * in some cases it's OK to delete the curbuf, because a new one is
697 * obtained anyway. Therefore only return if curbuf changed to the
698 * deleted buffer.
699 */
700 if (buf == curbuf && !is_curbuf)
701 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200702
Bram Moolenaar4033c552017-09-16 20:54:51 +0200703 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100704 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200705
Bram Moolenaarc667da52019-11-30 20:52:27 +0100706 // Autocommands may have opened or closed windows for this buffer.
707 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200708 if (buf->b_nwindows > 0)
709 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 /*
712 * Remove the buffer from the list.
713 */
714 if (wipe_buf)
715 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200716 if (action == DOBUF_WIPE_REUSE)
717 {
718 // we can re-use this buffer number, store it
719 if (buf_reuse.ga_itemsize == 0)
720 ga_init2(&buf_reuse, sizeof(int), 50);
721 if (ga_grow(&buf_reuse, 1) == OK)
722 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
723 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200724 if (buf->b_sfname != buf->b_ffname)
725 VIM_CLEAR(buf->b_sfname);
726 else
727 buf->b_sfname = NULL;
728 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (buf->b_prev == NULL)
730 firstbuf = buf->b_next;
731 else
732 buf->b_prev->b_next = buf->b_next;
733 if (buf->b_next == NULL)
734 lastbuf = buf->b_prev;
735 else
736 buf->b_next->b_prev = buf->b_prev;
737 free_buffer(buf);
738 }
739 else
740 {
741 if (del_buf)
742 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100743 // Free all internal variables and reset option values, to make
744 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 free_buffer_stuff(buf, TRUE);
746
Bram Moolenaarc667da52019-11-30 20:52:27 +0100747 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
749
Bram Moolenaarc667da52019-11-30 20:52:27 +0100750 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 buf->b_p_initialized = FALSE;
752 }
753 buf_clear_file(buf);
754 if (del_buf)
755 buf->b_p_bl = FALSE;
756 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100757 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758}
759
760/*
761 * Make buffer not contain a file.
762 */
763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100764buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200767 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 buf->b_p_eol = TRUE;
770 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000772 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100774 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_NETBEANS_INTG
776 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778}
779
780/*
781 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200782 * the file. Careful: get here with "curwin" NULL when exiting.
783 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100784 * BFA_DEL buffer is going to be deleted
785 * BFA_WIPE buffer is going to be wiped out
786 * BFA_KEEP_UNDO do not free undo information
787 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100790buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200793 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200794 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200795 win_T *the_curwin = curwin;
796 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaarc667da52019-11-30 20:52:27 +0100798 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200799 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200800 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200801 if (buf->b_ml.ml_mfp != NULL)
802 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200803 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
804 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200805 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100806 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200807 return;
808 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200809 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200811 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
812 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200813 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100814 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return;
816 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200817 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200819 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
820 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200821 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100822 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 return;
824 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200825 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200826
Bram Moolenaarc667da52019-11-30 20:52:27 +0100827 // If the buffer was in curwin and the window has changed, go back to that
828 // window, if it still exists. This avoids that ":edit x" triggering a
829 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200830 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
831 {
832 block_autocmds();
833 goto_tabpage_win(the_curtab, the_curwin);
834 unblock_autocmds();
835 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200836
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100837#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100838 // autocmds may abort script processing
839 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843 /*
844 * It's possible that autocommands change curbuf to the one being deleted.
845 * This might cause curbuf to be deleted unexpectedly. But in some cases
846 * it's OK to delete the curbuf, because a new one is obtained anyway.
847 * Therefore only return if curbuf changed to the deleted buffer.
848 */
849 if (buf == curbuf && !is_curbuf)
850 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100852 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200854#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100855 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200856 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100857 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200858#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000859
860#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100861 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000862 {
863 win_T *win;
864 tabpage_T *tp;
865
866 FOR_ALL_TAB_WINDOWS(tp, win)
867 if (win->w_buffer == buf)
868 clearFolding(win);
869 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000870#endif
871
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#ifdef FEAT_TCL
873 tcl_buffer_free(buf);
874#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100875 ml_close(buf, TRUE); // close and delete the memline/memfile
876 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200877 if ((flags & BFA_KEEP_UNDO) == 0)
878 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100879 u_blockfree(buf); // free the memory allocated for undo
880 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100883 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100885#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100886 clear_buf_prop_types(buf);
887#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100888 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889}
890
891/*
892 * Free a buffer structure and the things it contains related to the buffer
893 * itself (not the file, that must have been done already).
894 */
895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100896free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200898 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200900#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100901 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200902 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200903 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200904 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200905#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200906#ifdef FEAT_LUA
907 lua_buffer_free(buf);
908#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000909#ifdef FEAT_MZSCHEME
910 mzscheme_buffer_free(buf);
911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#ifdef FEAT_PERL
913 perl_buf_free(buf);
914#endif
915#ifdef FEAT_PYTHON
916 python_buffer_free(buf);
917#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200918#ifdef FEAT_PYTHON3
919 python3_buffer_free(buf);
920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#ifdef FEAT_RUBY
922 ruby_buffer_free(buf);
923#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200924#ifdef FEAT_JOB_CHANNEL
925 channel_buffer_free(buf);
926#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200927#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200928 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200929#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200930#ifdef FEAT_JOB_CHANNEL
931 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200932 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200933 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200934#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200935
936 buf_hashtab_remove(buf);
937
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200938 aubuflocal_remove(buf);
939
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200940 if (autocmd_busy)
941 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100942 // Do not free the buffer structure while autocommands are executing,
943 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200944 buf->b_next = au_pending_free_buf;
945 au_pending_free_buf = buf;
946 }
947 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100948 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200949 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100950 if (curbuf == buf)
951 curbuf = NULL; // make clear it's not to be used
952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100956 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100957 */
958 static void
959init_changedtick(buf_T *buf)
960{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100961 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100962
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100963 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
964 di->di_tv.v_type = VAR_NUMBER;
965 di->di_tv.v_lock = VAR_FIXED;
966 di->di_tv.vval.v_number = 0;
967
Bram Moolenaar92769c32017-02-25 15:41:37 +0100968#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100969 STRCPY(buf->b_ct_di.di_key, "changedtick");
970 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100971#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100972}
973
974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
976 */
977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100978free_buffer_stuff(
979 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100980 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 if (free_options)
983 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100984 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200986#ifdef FEAT_SPELL
987 ga_clear(&buf->b_s.b_langp);
988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100991 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100992 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100993
Bram Moolenaarc667da52019-11-30 20:52:27 +0100994 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100995 hash_init(&buf->b_vars->dv_hashtab);
996 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100997 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +0100998 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001001 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001003 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001005#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001006 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001007#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001008 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1009 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001010 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011}
1012
1013/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001014 * Free one wininfo_T.
1015 */
1016 void
1017free_wininfo(wininfo_T *wip)
1018{
1019 if (wip->wi_optset)
1020 {
1021 clear_winopt(&wip->wi_opt);
1022#ifdef FEAT_FOLDING
1023 deleteFoldRecurse(&wip->wi_folds);
1024#endif
1025 }
1026 vim_free(wip);
1027}
1028
1029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 * Free the b_wininfo list for buffer "buf".
1031 */
1032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001033clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 wininfo_T *wip;
1036
1037 while (buf->b_wininfo != NULL)
1038 {
1039 wip = buf->b_wininfo;
1040 buf->b_wininfo = wip->wi_next;
Bram Moolenaar4882d982020-10-25 17:55:09 +01001041 free_wininfo(wip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043}
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045/*
1046 * Go to another buffer. Handles the result of the ATTENTION dialog.
1047 */
1048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049goto_buffer(
1050 exarg_T *eap,
1051 int start,
1052 int dir,
1053 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001055 bufref_T old_curbuf;
1056
1057 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1061 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1063 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001064#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001065 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001066
Bram Moolenaarc667da52019-11-30 20:52:27 +01001067 // Reset the error/interrupt/exception state here so that
1068 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001070#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001071
Bram Moolenaarc667da52019-11-30 20:52:27 +01001072 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 win_close(curwin, TRUE);
1074 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001075 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001076
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001077#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001078 // Restore the error/interrupt/exception state if not discarded by a
1079 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001080 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001084 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087/*
1088 * Handle the situation of swap_exists_action being set.
1089 * It is allowed for "old_curbuf" to be NULL or invalid.
1090 */
1091 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001092handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001094#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001095 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001096#endif
1097#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001098 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001099#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001100 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (swap_exists_action == SEA_QUIT)
1103 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001104#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001105 // Reset the error/interrupt/exception state here so that
1106 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001107 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001108#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001109
Bram Moolenaarc667da52019-11-30 20:52:27 +01001110 // User selected Quit at ATTENTION prompt. Go back to previous
1111 // buffer. If that buffer is gone or the same as the current one,
1112 // open a new, empty buffer.
1113 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001114 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001115 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001116 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1117 || old_curbuf->br_buf == curbuf)
1118 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1119 else
1120 buf = old_curbuf->br_buf;
1121 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001122 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001123 int old_msg_silent = msg_silent;
1124
1125 if (shortmess(SHM_FILEINFO))
1126 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001127 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001128 // restore msg_silent, so that the command line will be shown
1129 msg_silent = old_msg_silent;
1130
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001131#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001132 if (old_tw != curbuf->b_p_tw)
1133 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001134#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001135 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001136 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001137
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001138#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001139 // Restore the error/interrupt/exception state if not discarded by a
1140 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001141 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144 else if (swap_exists_action == SEA_RECOVER)
1145 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001146#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001147 // Reset the error/interrupt/exception state here so that
1148 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001149 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001150#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001151
Bram Moolenaarc667da52019-11-30 20:52:27 +01001152 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001154 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001155 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001157 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001159#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001160 // Restore the error/interrupt/exception state if not discarded by a
1161 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001162 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 swap_exists_action = SEA_NONE;
1166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168/*
1169 * do_bufdel() - delete or unload buffer(s)
1170 *
1171 * addr_count == 0: ":bdel" - delete current buffer
1172 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1173 * buffer "end_bnr", then any other arguments.
1174 * addr_count == 2: ":N,N bdel" - delete buffers in range
1175 *
1176 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1177 * DOBUF_DEL (":bdel")
1178 *
1179 * Returns error message or NULL
1180 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182do_bufdel(
1183 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001184 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001185 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001186 int start_bnr, // first buffer number in a range
1187 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001188 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001190 int do_current = 0; // delete current buffer?
1191 int deleted = 0; // number of buffers deleted
1192 char *errormsg = NULL; // return value
1193 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 char_u *p;
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (addr_count == 0)
1197 {
1198 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1199 }
1200 else
1201 {
1202 if (addr_count == 2)
1203 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001204 if (*arg) // both range and argument is not allowed
Bram Moolenaar8930caa2020-07-23 16:37:03 +02001205 return ex_errmsg(e_trailing_arg, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 bnr = start_bnr;
1207 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001208 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 bnr = end_bnr;
1210
1211 for ( ;!got_int; ui_breakcheck())
1212 {
1213 /*
1214 * delete the current buffer last, otherwise when the
1215 * current buffer is deleted, the next buffer becomes
1216 * the current one and will be loaded, which may then
1217 * also be deleted, etc.
1218 */
1219 if (bnr == curbuf->b_fnum)
1220 do_current = bnr;
1221 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1222 forceit) == OK)
1223 ++deleted;
1224
1225 /*
1226 * find next buffer number to delete/unload
1227 */
1228 if (addr_count == 2)
1229 {
1230 if (++bnr > end_bnr)
1231 break;
1232 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001233 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 arg = skipwhite(arg);
1236 if (*arg == NUL)
1237 break;
1238 if (!VIM_ISDIGIT(*arg))
1239 {
1240 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001241 bnr = buflist_findpat(arg, p,
1242 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001243 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001244 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 break;
1246 arg = p;
1247 }
1248 else
1249 bnr = getdigits(&arg);
1250 }
1251 }
1252 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1253 FORWARD, do_current, forceit) == OK)
1254 ++deleted;
1255
1256 if (deleted == 0)
1257 {
1258 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001261 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001263 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001264 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
1266 else if (deleted >= p_report)
1267 {
1268 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001269 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001270 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001272 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001273 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001275 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001276 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 }
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 return errormsg;
1282}
1283
Bram Moolenaara02471e2014-01-10 16:43:14 +01001284/*
1285 * Make the current buffer empty.
1286 * Used when it is wiped out and it's the last buffer.
1287 */
1288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289empty_curbuf(
1290 int close_others,
1291 int forceit,
1292 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001293{
1294 int retval;
1295 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001296 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001297
1298 if (action == DOBUF_UNLOAD)
1299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001300 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001301 return FAIL;
1302 }
1303
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001304 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001305 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001306 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001307 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001308
1309 setpcmark();
1310 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1311 forceit ? ECMD_FORCEIT : 0, curwin);
1312
1313 /*
1314 * do_ecmd() may create a new buffer, then we have to delete
1315 * the old one. But do_ecmd() may have done that already, check
1316 * if the buffer still exists.
1317 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001318 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001319 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001320 if (!close_others)
1321 need_fileinfo = FALSE;
1322 return retval;
1323}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325/*
1326 * Implementation of the commands for the buffer list.
1327 *
1328 * action == DOBUF_GOTO go to specified buffer
1329 * action == DOBUF_SPLIT split window and go to specified buffer
1330 * action == DOBUF_UNLOAD unload specified buffer(s)
1331 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1332 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001333 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 *
1335 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1336 * start == DOBUF_FIRST go to "count" buffer from first buffer
1337 * start == DOBUF_LAST go to "count" buffer from last buffer
1338 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1339 *
1340 * Return FAIL or OK.
1341 */
1342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001343do_buffer(
1344 int action,
1345 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001346 int dir, // FORWARD or BACKWARD
1347 int count, // buffer number or number of buffers
1348 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
1350 buf_T *buf;
1351 buf_T *bp;
1352 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001353 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
1355 switch (start)
1356 {
1357 case DOBUF_FIRST: buf = firstbuf; break;
1358 case DOBUF_LAST: buf = lastbuf; break;
1359 default: buf = curbuf; break;
1360 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001361 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
1363 while (count-- > 0)
1364 {
1365 do
1366 {
1367 buf = buf->b_next;
1368 if (buf == NULL)
1369 buf = firstbuf;
1370 }
1371 while (buf != curbuf && !bufIsChanged(buf));
1372 }
1373 if (!bufIsChanged(buf))
1374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001375 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 return FAIL;
1377 }
1378 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001379 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
1381 while (buf != NULL && buf->b_fnum != count)
1382 buf = buf->b_next;
1383 }
1384 else
1385 {
1386 bp = NULL;
1387 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1388 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001389 // remember the buffer where we start, we come back there when all
1390 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (bp == NULL)
1392 bp = buf;
1393 if (dir == FORWARD)
1394 {
1395 buf = buf->b_next;
1396 if (buf == NULL)
1397 buf = firstbuf;
1398 }
1399 else
1400 {
1401 buf = buf->b_prev;
1402 if (buf == NULL)
1403 buf = lastbuf;
1404 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001405 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (unload || buf->b_p_bl)
1407 {
1408 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001409 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 if (bp == buf)
1412 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001413 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 return FAIL;
1416 }
1417 }
1418 }
1419
Bram Moolenaarc667da52019-11-30 20:52:27 +01001420 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
1422 if (start == DOBUF_FIRST)
1423 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001424 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001426 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001429 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001431 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return FAIL;
1433 }
1434
1435#ifdef FEAT_GUI
1436 need_mouse_correct = TRUE;
1437#endif
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 /*
1440 * delete buffer buf from memory and/or the list
1441 */
1442 if (unload)
1443 {
1444 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001445 bufref_T bufref;
1446
Bram Moolenaar94f01952018-09-01 15:30:03 +02001447 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001448 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001449
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001450 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaarc667da52019-11-30 20:52:27 +01001452 // When unloading or deleting a buffer that's already unloaded and
1453 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001454 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1455 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 return FAIL;
1457
1458 if (!forceit && bufIsChanged(buf))
1459 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001460#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001461 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
1463 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001464 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001465 // Autocommand deleted buffer, oops! It's not changed
1466 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001468 // If it's still changed fail silently, the dialog already
1469 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001470 if (bufIsChanged(buf))
1471 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001473 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001476 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 buf->b_fnum);
1478 return FAIL;
1479 }
1480 }
1481
Bram Moolenaarc667da52019-11-30 20:52:27 +01001482 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001483 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001484 end_visual_mode();
1485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 /*
1487 * If deleting the last (listed) buffer, make it empty.
1488 * The last (listed) buffer cannot be unloaded.
1489 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001490 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (bp->b_p_bl && bp != buf)
1492 break;
1493 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001494 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /*
1497 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001498 * (unless it's the only window). Repeat this so long as we end up in
1499 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001501 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001502 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001503 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001504 {
1505 if (win_close(curwin, FALSE) == FAIL)
1506 break;
1507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509 /*
1510 * If the buffer to be deleted is not the current one, delete it here.
1511 */
1512 if (buf != curbuf)
1513 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001514 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001515 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001516 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 return OK;
1518 }
1519
1520 /*
1521 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001522 * There should be another, otherwise it would have been handled
1523 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001524 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 * Then prefer the buffer we most recently visited.
1526 * Else try to find one that is loaded, after the current buffer,
1527 * then before the current buffer.
1528 * Finally use any buffer.
1529 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001530 buf = NULL; // selected buffer
1531 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001532 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1533 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001535 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 int jumpidx;
1538
1539 jumpidx = curwin->w_jumplistidx - 1;
1540 if (jumpidx < 0)
1541 jumpidx = curwin->w_jumplistlen - 1;
1542
1543 forward = jumpidx;
1544 while (jumpidx != curwin->w_jumplistidx)
1545 {
1546 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1547 if (buf != NULL)
1548 {
1549 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001550 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 else if (buf->b_ml.ml_mfp == NULL)
1552 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001553 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (bp == NULL)
1555 bp = buf;
1556 buf = NULL;
1557 }
1558 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001559 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001561 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1563 break;
1564 if (--jumpidx < 0)
1565 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001566 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 break;
1568 }
1569 }
1570#endif
1571
Bram Moolenaarc667da52019-11-30 20:52:27 +01001572 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 forward = TRUE;
1575 buf = curbuf->b_next;
1576 for (;;)
1577 {
1578 if (buf == NULL)
1579 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001580 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 break;
1582 buf = curbuf->b_prev;
1583 forward = FALSE;
1584 continue;
1585 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001586 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1588 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001589 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001591 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 bp = buf;
1593 }
1594 if (forward)
1595 buf = buf->b_next;
1596 else
1597 buf = buf->b_prev;
1598 }
1599 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001600 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001602 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001604 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (buf->b_p_bl && buf != curbuf)
1606 break;
1607 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001608 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 if (curbuf->b_next != NULL)
1611 buf = curbuf->b_next;
1612 else
1613 buf = curbuf->b_prev;
1614 }
1615 }
1616
Bram Moolenaara02471e2014-01-10 16:43:14 +01001617 if (buf == NULL)
1618 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001619 // Autocommands must have wiped out all other buffers. Only option
1620 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001621 return empty_curbuf(FALSE, forceit, action);
1622 }
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 /*
1625 * make buf current buffer
1626 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001627 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001629 // If 'switchbuf' contains "useopen": jump to first window containing
1630 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001631 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001632 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001633 // If 'switchbuf' contains "usetab": jump to first window in any tab
1634 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001635 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 return OK;
1637 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 return FAIL;
1639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaarc667da52019-11-30 20:52:27 +01001641 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 if (buf == curbuf)
1643 return OK;
1644
1645 /*
1646 * Check if the current buffer may be abandoned.
1647 */
1648 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1649 {
1650#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001651 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001653 bufref_T bufref;
1654
1655 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001657 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001658 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 if (bufIsChanged(curbuf))
1662#endif
1663 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001664 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 return FAIL;
1666 }
1667 }
1668
Bram Moolenaarc667da52019-11-30 20:52:27 +01001669 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 set_curbuf(buf, action);
1671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001673 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001675#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001676 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 return FAIL;
1678#endif
1679
1680 return OK;
1681}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683/*
1684 * Set current buffer to "buf". Executes autocommands and closes current
1685 * buffer. "action" tells how to close the current buffer:
1686 * DOBUF_GOTO free or hide it
1687 * DOBUF_SPLIT nothing
1688 * DOBUF_UNLOAD unload it
1689 * DOBUF_DEL delete it
1690 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001691 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 */
1693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 buf_T *prevbuf;
1697 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001698 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001699#ifdef FEAT_SYN_HL
1700 long old_tw = curbuf->b_p_tw;
1701#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001702 bufref_T newbufref;
1703 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
1705 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001706 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001707 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1708 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaarc667da52019-11-30 20:52:27 +01001710 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaarc667da52019-11-30 20:52:27 +01001713 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001715 set_bufref(&prevbufref, prevbuf);
1716 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001718 // Autocommands may delete the current buffer and/or the buffer we want to go
Bram Moolenaarc667da52019-11-30 20:52:27 +01001719 // to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001720 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001721 || (bufref_valid(&prevbufref)
1722 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001723#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001724 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001726 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001728#ifdef FEAT_SYN_HL
1729 if (prevbuf == curwin->w_buffer)
1730 reset_synblock(curwin);
1731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001733 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001734#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001735 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001737 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001739 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001740 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001741
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001742 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001743 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1745 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001746 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001747 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1748 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001749 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001750 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001751 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001754 // An autocommand may have deleted "buf", already entered it (e.g., when
1755 // it did ":bunload") or aborted the script processing.
1756 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001757 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001758#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001759 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001764#ifdef FEAT_SYN_HL
1765 if (old_tw != curbuf->b_p_tw)
1766 check_colorcolumn(curwin);
1767#endif
1768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001773 * Old curbuf must have been abandoned already! This also means "curbuf" may
1774 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001779 // Get the buffer in the current window.
1780 curwin->w_buffer = buf;
1781 curbuf = buf;
1782 ++curbuf->b_nwindows;
1783
1784 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1786 if (!buf->b_help)
1787 get_winopts(buf);
1788#ifdef FEAT_FOLDING
1789 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001790 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001792 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
1794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001796 if (curwin->w_p_diff)
1797 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
1799
Bram Moolenaara971b822011-09-14 14:43:25 +02001800#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001801 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001802#endif
1803
Bram Moolenaarc667da52019-11-30 20:52:27 +01001804 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 curwin->w_cursor.lnum = 1;
1806 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001809 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaarc667da52019-11-30 20:52:27 +01001811 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001812 curwin->w_valid = 0;
1813
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001814 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1815 curbuf->b_last_cursor.col, TRUE);
1816
Bram Moolenaarc667da52019-11-30 20:52:27 +01001817 // Make sure the buffer is loaded.
1818 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001819 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001820 // If there is no filetype, allow for detecting one. Esp. useful for
1821 // ":ball" used in a autocommand. If there already is a filetype we
1822 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001823 if (*curbuf->b_p_ft == NUL)
1824 did_filetype = FALSE;
1825
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001826 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else
1829 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001830 if (!msg_silent && !shortmess(SHM_FILEINFO))
1831 need_fileinfo = TRUE; // display file info after redraw
1832
1833 // check if file changed
1834 (void)buf_check_timestamp(curbuf, FALSE);
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001837#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1841 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843
Bram Moolenaarc667da52019-11-30 20:52:27 +01001844 // If autocommands did not change the cursor position, restore cursor lnum
1845 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (curwin->w_cursor.lnum == 1 && inindent(0))
1847 buflist_getfpos();
1848
Bram Moolenaarc667da52019-11-30 20:52:27 +01001849 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#ifdef FEAT_TITLE
1851 maketitle();
1852#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001853 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001854 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001855 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
Bram Moolenaar009b2592004-10-24 19:18:58 +00001857#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001858 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001859 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001860#endif
1861
Bram Moolenaarc667da52019-11-30 20:52:27 +01001862 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001863 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865#ifdef FEAT_KEYMAP
1866 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001867 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001869#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001870 // May need to set the spell language. Can only do this after the buffer
1871 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001872 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1873 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001874#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001875#ifdef FEAT_VIMINFO
1876 curbuf->b_last_used = vim_time();
1877#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 redraw_later(NOT_VALID);
1880}
1881
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001882#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1883/*
1884 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001885 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001886 */
1887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001889{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001890 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001891 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001892 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001893 shorten_fnames(TRUE);
1894}
1895#endif
1896
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001897 void
1898no_write_message(void)
1899{
1900#ifdef FEAT_TERMINAL
1901 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001903 else
1904#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001906}
1907
1908 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001909no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001910{
1911#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001912 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001913 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001914 else
1915#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001916 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001917}
1918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919/*
1920 * functions for dealing with the buffer list
1921 */
1922
1923/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001924 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1925 * only one window. That means it can be re-used.
1926 */
1927 int
1928curbuf_reusable(void)
1929{
1930 return (curbuf != NULL
1931 && curbuf->b_ffname == NULL
1932 && curbuf->b_nwindows <= 1
1933 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001934#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001935 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001936#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001937 && !curbufIsChanged());
1938}
1939
1940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 * Add a file name to the buffer list. Return a pointer to the buffer.
1942 * If the same file name already exists return a pointer to that buffer.
1943 * If it does not exist, or if fname == NULL, a new entry is created.
1944 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1945 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1946 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001947 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001948 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1949 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001950 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 * This is the ONLY way to create a new buffer.
1952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001954buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001955 char_u *ffname_arg, // full path of fname or relative
1956 char_u *sfname_arg, // short fname or NULL
1957 linenr_T lnum, // preferred cursor line
1958 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001960 char_u *ffname = ffname_arg;
1961 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 buf_T *buf;
1963#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001964 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#endif
1966
Bram Moolenaar480778b2016-07-14 22:09:39 +02001967 if (top_file_num == 1)
1968 hash_init(&buf_hashtab);
1969
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001970 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 /*
1973 * If file name already exists in the list, update the entry.
1974 */
1975#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001976 // On Unix we can use inode numbers when the file exists. Works better
1977 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1979 st.st_dev = (dev_T)-1;
1980#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001981 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#ifdef UNIX
1983 buflist_findname_stat(ffname, &st)
1984#else
1985 buflist_findname(ffname)
1986#endif
1987 ) != NULL)
1988 {
1989 vim_free(ffname);
1990 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01001991 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
1992 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001993
1994 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001995 // copy the options now, if 'cpo' doesn't have 's' and not done
1996 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02001997 buf_copy_options(buf, 0);
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2000 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002001 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002004 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002006 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002007 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002008 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002009 return NULL;
2010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 return buf;
2013 }
2014
2015 /*
2016 * If the current buffer has no name and no contents, use the current
2017 * buffer. Otherwise: Need to allocate a new buffer structure.
2018 *
2019 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002020 * (A spell file buffer is allocated in spell.c, but that's not a normal
2021 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 */
2023 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002024 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002027 // It's like this buffer is deleted. Watch out for autocommands that
2028 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (curbuf->b_p_bl)
2030 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2031 if (buf == curbuf)
2032 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002033#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002034 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002035 {
2036 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002042 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 clear_string_option(&buf->b_p_bh);
2044 clear_string_option(&buf->b_p_bt);
2045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 if (buf != curbuf || curbuf == NULL)
2048 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002049 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (buf == NULL)
2051 {
2052 vim_free(ffname);
2053 return NULL;
2054 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002055#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002056 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002057 buf->b_vars = dict_alloc();
2058 if (buf->b_vars == NULL)
2059 {
2060 vim_free(ffname);
2061 vim_free(buf);
2062 return NULL;
2063 }
2064 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2065#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002066 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
2069 if (ffname != NULL)
2070 {
2071 buf->b_ffname = ffname;
2072 buf->b_sfname = vim_strsave(sfname);
2073 }
2074
2075 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002076 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2079 || buf->b_wininfo == NULL)
2080 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002081 if (buf->b_sfname != buf->b_ffname)
2082 VIM_CLEAR(buf->b_sfname);
2083 else
2084 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002085 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (buf != curbuf)
2087 free_buffer(buf);
2088 return NULL;
2089 }
2090
2091 if (buf == curbuf)
2092 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002093 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002094 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002095 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002097#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002098 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 return NULL;
2100#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002101 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002102
Bram Moolenaarc667da52019-11-30 20:52:27 +01002103 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002104 buf->b_p_initialized = FALSE;
2105 buf_copy_options(buf, BCO_ENTER);
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002108 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 curbuf->b_kmap_state |= KEYMAP_INIT;
2110#endif
2111 }
2112 else
2113 {
2114 /*
2115 * put new buffer at the end of the buffer list
2116 */
2117 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002118 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 buf->b_prev = NULL;
2121 firstbuf = buf;
2122 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002123 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
2125 lastbuf->b_next = buf;
2126 buf->b_prev = lastbuf;
2127 }
2128 lastbuf = buf;
2129
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002130 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2131 {
2132 // Recycle a previously used buffer number. Used for buffers which
2133 // are normally hidden, e.g. in a popup window. Avoids that the
2134 // buffer number grows rapidly.
2135 --buf_reuse.ga_len;
2136 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002137
2138 // Move buffer to the right place in the buffer list.
2139 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2140 {
2141 buf_T *prev = buf->b_prev;
2142
2143 prev->b_next = buf->b_next;
2144 if (prev->b_next != NULL)
2145 prev->b_next->b_prev = prev;
2146 buf->b_next = prev;
2147 buf->b_prev = prev->b_prev;
2148 if (buf->b_prev != NULL)
2149 buf->b_prev->b_next = buf;
2150 prev->b_prev = buf;
2151 if (lastbuf == buf)
2152 lastbuf = prev;
2153 if (firstbuf == prev)
2154 firstbuf = buf;
2155 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002156 }
2157 else
2158 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002159 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002162 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002165 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 top_file_num = 1;
2168 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002169 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 /*
2172 * Always copy the options from the current buffer.
2173 */
2174 buf_copy_options(buf, BCO_ALWAYS);
2175 }
2176
2177 buf->b_wininfo->wi_fpos.lnum = lnum;
2178 buf->b_wininfo->wi_win = curwin;
2179
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002180#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002181 hash_init(&buf->b_s.b_keywtab);
2182 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#endif
2184
2185 buf->b_fname = buf->b_sfname;
2186#ifdef UNIX
2187 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002188 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 else
2190 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002191 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 buf->b_dev = st.st_dev;
2193 buf->b_ino = st.st_ino;
2194 }
2195#endif
2196 buf->b_u_synced = TRUE;
2197 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002198 if (flags & BLN_DUMMY)
2199 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002201 clrallmarks(buf); // clear marks
2202 fmarks_check_names(buf); // check file marks for this file
2203 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (!(flags & BLN_DUMMY))
2205 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002206 bufref_T bufref;
2207
Bram Moolenaarc667da52019-11-30 20:52:27 +01002208 // Tricky: these autocommands may change the buffer list. They could
2209 // also split the window with re-using the one empty buffer. This may
2210 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002211 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002212 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002213 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002214 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002216 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002217 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002218 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002219 return NULL;
2220 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002221#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002222 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 return buf;
2228}
2229
2230/*
2231 * Free the memory for the options of a buffer.
2232 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2233 * 'fileencoding'.
2234 */
2235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236free_buf_options(
2237 buf_T *buf,
2238 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 if (free_p_ff)
2241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 clear_string_option(&buf->b_p_bh);
2245 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247#ifdef FEAT_FIND_ID
2248 clear_string_option(&buf->b_p_def);
2249 clear_string_option(&buf->b_p_inc);
2250# ifdef FEAT_EVAL
2251 clear_string_option(&buf->b_p_inex);
2252# endif
2253#endif
2254#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2255 clear_string_option(&buf->b_p_inde);
2256 clear_string_option(&buf->b_p_indk);
2257#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002258#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2259 clear_string_option(&buf->b_p_bexpr);
2260#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002261#if defined(FEAT_CRYPT)
2262 clear_string_option(&buf->b_p_cm);
2263#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002264 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002265#if defined(FEAT_EVAL)
2266 clear_string_option(&buf->b_p_fex);
2267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#ifdef FEAT_CRYPT
2269 clear_string_option(&buf->b_p_key);
2270#endif
2271 clear_string_option(&buf->b_p_kp);
2272 clear_string_option(&buf->b_p_mps);
2273 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002274 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002276#ifdef FEAT_VARTABS
2277 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002278 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002279 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002280 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002281 buf->b_p_vsts_array = NULL;
2282 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002283 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#ifdef FEAT_KEYMAP
2286 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002287 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 ga_clear(&buf->b_kmap_ga);
2289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#ifdef FEAT_FOLDING
2292 clear_string_option(&buf->b_p_cms);
2293#endif
2294 clear_string_option(&buf->b_p_nf);
2295#ifdef FEAT_SYN_HL
2296 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002297 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002298#endif
2299#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002300 clear_string_option(&buf->b_s.b_p_spc);
2301 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002302 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002303 buf->b_s.b_cap_prog = NULL;
2304 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002305 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#endif
2307#ifdef FEAT_SEARCHPATH
2308 clear_string_option(&buf->b_p_sua);
2309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#ifdef FEAT_CINDENT
2312 clear_string_option(&buf->b_p_cink);
2313 clear_string_option(&buf->b_p_cino);
2314#endif
2315#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2316 clear_string_option(&buf->b_p_cinw);
2317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002319#ifdef FEAT_COMPL_FUNC
2320 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002321 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#ifdef FEAT_QUICKFIX
2324 clear_string_option(&buf->b_p_gp);
2325 clear_string_option(&buf->b_p_mp);
2326 clear_string_option(&buf->b_p_efm);
2327#endif
2328 clear_string_option(&buf->b_p_ep);
2329 clear_string_option(&buf->b_p_path);
2330 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002331 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002332#ifdef FEAT_EVAL
2333 clear_string_option(&buf->b_p_tfu);
2334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 clear_string_option(&buf->b_p_dict);
2336 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002337#ifdef FEAT_TEXTOBJ
2338 clear_string_option(&buf->b_p_qe);
2339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002341 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002342#ifdef FEAT_LISP
2343 clear_string_option(&buf->b_p_lw);
2344#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002345 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002346 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347}
2348
2349/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002350 * Get alternate file "n".
2351 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2352 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 * if (options & GETF_SETMARK) call setpcmark()
2354 * if (options & GETF_ALT) we are jumping to an alternate file.
2355 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2356 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002357 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 */
2359 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002360buflist_getfile(
2361 int n,
2362 linenr_T lnum,
2363 int options,
2364 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 pos_T *fpos;
2369 colnr_T col;
2370
2371 buf = buflist_findnr(n);
2372 if (buf == NULL)
2373 {
2374 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002375 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002377 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 return FAIL;
2379 }
2380
Bram Moolenaarc667da52019-11-30 20:52:27 +01002381 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (buf == curbuf)
2383 return OK;
2384
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002385 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002386 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002387 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002389 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002390 if (curbuf_locked())
2391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
Bram Moolenaarc667da52019-11-30 20:52:27 +01002393 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (lnum == 0)
2395 {
2396 fpos = buflist_findfpos(buf);
2397 lnum = fpos->lnum;
2398 col = fpos->col;
2399 }
2400 else
2401 col = 0;
2402
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (options & GETF_SWITCH)
2404 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002405 // If 'switchbuf' contains "useopen": jump to first window containing
2406 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002407 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002409
Bram Moolenaarc667da52019-11-30 20:52:27 +01002410 // If 'switchbuf' contains "usetab": jump to first window in any tab
2411 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002412 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002413 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002414
Bram Moolenaarc667da52019-11-30 20:52:27 +01002415 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2416 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002417 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002418 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002420 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002421 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002422 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2423 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002425 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002430 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2431 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433 --RedrawingDisabled;
2434
Bram Moolenaarc667da52019-11-30 20:52:27 +01002435 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (!p_sol && col != 0)
2437 {
2438 curwin->w_cursor.col = col;
2439 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 curwin->w_set_curswant = TRUE;
2442 }
2443 return OK;
2444 }
2445 --RedrawingDisabled;
2446 return FAIL;
2447}
2448
2449/*
2450 * go to the last know line number for the current buffer
2451 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 pos_T *fpos;
2456
2457 fpos = buflist_findfpos(curbuf);
2458
2459 curwin->w_cursor.lnum = fpos->lnum;
2460 check_cursor_lnum();
2461
2462 if (p_sol)
2463 curwin->w_cursor.col = 0;
2464 else
2465 {
2466 curwin->w_cursor.col = fpos->col;
2467 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 curwin->w_set_curswant = TRUE;
2470 }
2471}
2472
Bram Moolenaar81695252004-12-29 20:58:21 +00002473#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2474/*
2475 * Find file in buffer list by name (it has to be for the current window).
2476 * Returns NULL if not found.
2477 */
2478 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002479buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002480{
2481 char_u *ffname;
2482 buf_T *buf = NULL;
2483
Bram Moolenaarc667da52019-11-30 20:52:27 +01002484 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002485 ffname = FullName_save(fname,
2486#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002487 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002488#else
2489 FALSE
2490#endif
2491 );
2492 if (ffname != NULL)
2493 {
2494 buf = buflist_findname(ffname);
2495 vim_free(ffname);
2496 }
2497 return buf;
2498}
2499#endif
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * Find file in buffer list by name (it has to be for the current window).
2503 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002504 * Skips dummy buffers.
2505 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002508buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002511 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 if (mch_stat((char *)ffname, &st) < 0)
2514 st.st_dev = (dev_T)-1;
2515 return buflist_findname_stat(ffname, &st);
2516}
2517
2518/*
2519 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2520 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002521 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
2523 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002524buflist_findname_stat(
2525 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002526 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528#endif
2529 buf_T *buf;
2530
Bram Moolenaarc667da52019-11-30 20:52:27 +01002531 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002532 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002533 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#ifdef UNIX
2535 , stp
2536#endif
2537 ))
2538 return buf;
2539 return NULL;
2540}
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542/*
2543 * Find file in buffer list by a regexp pattern.
2544 * Return fnum of the found buffer.
2545 * Return < 0 for error.
2546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548buflist_findpat(
2549 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002550 char_u *pattern_end, // pointer to first char after pattern
2551 int unlisted, // find unlisted buffers
2552 int diffmode UNUSED, // find diff-mode buffers only
2553 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 int match = -1;
2557 int find_listed;
2558 char_u *pat;
2559 char_u *patend;
2560 int attempt;
2561 char_u *p;
2562 int toggledollar;
2563
2564 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2565 {
2566 if (*pattern == '%')
2567 match = curbuf->b_fnum;
2568 else
2569 match = curwin->w_alt_fnum;
2570#ifdef FEAT_DIFF
2571 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2572 match = -1;
2573#endif
2574 }
2575
2576 /*
2577 * Try four ways of matching a listed buffer:
2578 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002579 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * attempt == 2: with '$' at end (only match at end)
2581 * attempt == 3: with '^' at start and '$' at end (only full match)
2582 * Repeat this for finding an unlisted buffer if there was no matching
2583 * listed buffer.
2584 */
2585 else
2586 {
2587 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2588 if (pat == NULL)
2589 return -1;
2590 patend = pat + STRLEN(pat) - 1;
2591 toggledollar = (patend > pat && *patend == '$');
2592
Bram Moolenaarc667da52019-11-30 20:52:27 +01002593 // First try finding a listed buffer. If not found and "unlisted"
2594 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 find_listed = TRUE;
2596 for (;;)
2597 {
2598 for (attempt = 0; attempt <= 3; ++attempt)
2599 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002600 regmatch_T regmatch;
2601
Bram Moolenaarc667da52019-11-30 20:52:27 +01002602 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002604 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002606 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002608 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002609 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 {
2611 vim_free(pat);
2612 return -1;
2613 }
2614
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002615 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 if (buf->b_p_bl == find_listed
2617#ifdef FEAT_DIFF
2618 && (!diffmode || diff_mode_buf(buf))
2619#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002620 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002622 if (curtab_only)
2623 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002624 // Ignore the match if the buffer is not open in
2625 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002626 win_T *wp;
2627
Bram Moolenaar29323592016-07-24 22:04:11 +02002628 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002629 if (wp->w_buffer == buf)
2630 break;
2631 if (wp == NULL)
2632 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002633 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002634 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 match = -2;
2637 break;
2638 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002639 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002642 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002643 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 break;
2645 }
2646
Bram Moolenaarc667da52019-11-30 20:52:27 +01002647 // Only search for unlisted buffers if there was no match with
2648 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (!unlisted || !find_listed || match != -1)
2650 break;
2651 find_listed = FALSE;
2652 }
2653
2654 vim_free(pat);
2655 }
2656
2657 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002658 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 return match;
2662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar52410572019-10-27 05:12:45 +01002664#ifdef FEAT_VIMINFO
2665typedef struct {
2666 buf_T *buf;
2667 char_u *match;
2668} bufmatch_T;
2669#endif
2670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671/*
2672 * Find all buffer names that match.
2673 * For command line expansion of ":buf" and ":sbuf".
2674 * Return OK if matches found, FAIL otherwise.
2675 */
2676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677ExpandBufnames(
2678 char_u *pat,
2679 int *num_file,
2680 char_u ***file,
2681 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 int count = 0;
2684 buf_T *buf;
2685 int round;
2686 char_u *p;
2687 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002688 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002689#ifdef FEAT_VIMINFO
2690 bufmatch_T *matches = NULL;
2691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
Bram Moolenaarc667da52019-11-30 20:52:27 +01002693 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 *file = NULL;
2695
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002696#ifdef FEAT_DIFF
2697 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2698 return FAIL;
2699#endif
2700
Bram Moolenaarc667da52019-11-30 20:52:27 +01002701 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002702 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002704 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002705 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002707 STRCPY(patc, "\\(^\\|[\\/]\\)");
2708 STRCPY(patc + 11, pat + 1);
2709 }
2710 else
2711 patc = pat;
2712
2713 /*
2714 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002715 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002716 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002717 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002718 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002719 regmatch_T regmatch;
2720
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002721 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002722 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002723 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2724 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002725 {
2726 if (patc != pat)
2727 vim_free(patc);
2728 return FAIL;
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * round == 1: Count the matches.
2733 * round == 2: Build the array to keep the matches.
2734 */
2735 for (round = 1; round <= 2; ++round)
2736 {
2737 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002738 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002740 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002742#ifdef FEAT_DIFF
2743 if (options & BUF_DIFF_FILTER)
2744 // Skip buffers not suitable for
2745 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002746 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002747 continue;
2748#endif
2749
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002750 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (p != NULL)
2752 {
2753 if (round == 1)
2754 ++count;
2755 else
2756 {
2757 if (options & WILD_HOME_REPLACE)
2758 p = home_replace_save(buf, p);
2759 else
2760 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002761#ifdef FEAT_VIMINFO
2762 if (matches != NULL)
2763 {
2764 matches[count].buf = buf;
2765 matches[count].match = p;
2766 count++;
2767 }
2768 else
2769#endif
2770 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 }
2773 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002774 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 break;
2776 if (round == 1)
2777 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002778 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (*file == NULL)
2780 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002781 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002782 if (patc != pat)
2783 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 return FAIL;
2785 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002786#ifdef FEAT_VIMINFO
2787 if (options & WILD_BUFLASTUSED)
2788 matches = ALLOC_MULT(bufmatch_T, count);
2789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002792 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002793 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 break;
2795 }
2796
Bram Moolenaar05159a02005-02-26 23:04:13 +00002797 if (patc != pat)
2798 vim_free(patc);
2799
Bram Moolenaar52410572019-10-27 05:12:45 +01002800#ifdef FEAT_VIMINFO
2801 if (matches != NULL)
2802 {
2803 int i;
2804 if (count > 1)
2805 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2806 // if the current buffer is first in the list, place it at the end
2807 if (matches[0].buf == curbuf)
2808 {
2809 for (i = 1; i < count; i++)
2810 (*file)[i-1] = matches[i].match;
2811 (*file)[count-1] = matches[0].match;
2812 }
2813 else
2814 {
2815 for (i = 0; i < count; i++)
2816 (*file)[i] = matches[i].match;
2817 }
2818 vim_free(matches);
2819 }
2820#endif
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 *num_file = count;
2823 return (count == 0 ? FAIL : OK);
2824}
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826/*
2827 * Check for a match on the file name for buffer "buf" with regprog "prog".
2828 */
2829 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002830buflist_match(
2831 regmatch_T *rmp,
2832 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002833 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 char_u *match;
2836
Bram Moolenaarc667da52019-11-30 20:52:27 +01002837 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002838 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002840 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 return match;
2843}
2844
2845/*
2846 * Try matching the regexp in "prog" with file name "name".
2847 * Return "name" when there is a match, NULL when not.
2848 */
2849 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002850fname_match(
2851 regmatch_T *rmp,
2852 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002853 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 char_u *match = NULL;
2856 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 if (name != NULL)
2859 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002860 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002861 rmp->rm_ic = p_fic || ignore_case;
2862 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 match = name;
2864 else
2865 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002866 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002868 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 match = name;
2870 vim_free(p);
2871 }
2872 }
2873
2874 return match;
2875}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
2877/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002878 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 */
2880 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002881buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002883 char_u key[VIM_SIZEOF_INT * 2 + 1];
2884 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
2886 if (nr == 0)
2887 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002888 sprintf((char *)key, "%x", nr);
2889 hi = hash_find(&buf_hashtab, key);
2890
2891 if (!HASHITEM_EMPTY(hi))
2892 return (buf_T *)(hi->hi_key
2893 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 return NULL;
2895}
2896
2897/*
2898 * Get name of file 'n' in the buffer list.
2899 * When the file has no name an empty string is returned.
2900 * home_replace() is used to shorten the file name (used for marks).
2901 * Returns a pointer to allocated memory, of NULL when failed.
2902 */
2903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904buflist_nr2name(
2905 int n,
2906 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002907 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 buf_T *buf;
2910
2911 buf = buflist_findnr(n);
2912 if (buf == NULL)
2913 return NULL;
2914 return home_replace_save(helptail ? buf : NULL,
2915 fullname ? buf->b_ffname : buf->b_fname);
2916}
2917
2918/*
2919 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2920 * When "copy_options" is TRUE save the local window option values.
2921 * When "lnum" is 0 only do the options.
2922 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924buflist_setfpos(
2925 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002926 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927 linenr_T lnum,
2928 colnr_T col,
2929 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 wininfo_T *wip;
2932
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002933 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (wip->wi_win == win)
2935 break;
2936 if (wip == NULL)
2937 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002938 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002939 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (wip == NULL)
2941 return;
2942 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002943 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 lnum = 1;
2945 }
2946 else
2947 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002948 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (wip->wi_prev)
2950 wip->wi_prev->wi_next = wip->wi_next;
2951 else
2952 buf->b_wininfo = wip->wi_next;
2953 if (wip->wi_next)
2954 wip->wi_next->wi_prev = wip->wi_prev;
2955 if (copy_options && wip->wi_optset)
2956 {
2957 clear_winopt(&wip->wi_opt);
2958#ifdef FEAT_FOLDING
2959 deleteFoldRecurse(&wip->wi_folds);
2960#endif
2961 }
2962 }
2963 if (lnum != 0)
2964 {
2965 wip->wi_fpos.lnum = lnum;
2966 wip->wi_fpos.col = col;
2967 }
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002968 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002970 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2972#ifdef FEAT_FOLDING
2973 wip->wi_fold_manual = win->w_fold_manual;
2974 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2975#endif
2976 wip->wi_optset = TRUE;
2977 }
2978
Bram Moolenaarc667da52019-11-30 20:52:27 +01002979 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 wip->wi_next = buf->b_wininfo;
2981 buf->b_wininfo = wip;
2982 wip->wi_prev = NULL;
2983 if (wip->wi_next)
2984 wip->wi_next->wi_prev = wip;
2985
2986 return;
2987}
2988
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002989#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002990/*
2991 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2992 * page. That's because a diff is local to a tab page.
2993 */
2994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002995wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002996{
2997 win_T *wp;
2998
2999 if (wip->wi_opt.wo_diff)
3000 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003001 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003002 // return FALSE when it's a window in the current tab page, thus
3003 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003004 if (wip->wi_win == wp)
3005 return FALSE;
3006 return TRUE;
3007 }
3008 return FALSE;
3009}
3010#endif
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012/*
3013 * Find info for the current window in buffer "buf".
3014 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003015 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003016 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3017 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 * Returns NULL when there isn't any info.
3019 */
3020 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003021find_wininfo(
3022 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003023 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003024 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 wininfo_T *wip;
3027
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003028 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003029 if (wip->wi_win == curwin
3030#ifdef FEAT_DIFF
3031 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3032#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003033
3034 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003036
Bram Moolenaarc667da52019-11-30 20:52:27 +01003037 // If no wininfo for curwin, use the first in the list (that doesn't have
3038 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003039 // If "need_options" is TRUE skip entries that don't have options set,
3040 // unless the window is editing "buf", so we can copy from the window
3041 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003042 if (wip == NULL)
3043 {
3044#ifdef FEAT_DIFF
3045 if (skip_diff_buffer)
3046 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003047 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003048 if (!wininfo_other_tab_diff(wip)
3049 && (!need_options || wip->wi_optset
3050 || (wip->wi_win != NULL
3051 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003052 break;
3053 }
3054 else
3055#endif
3056 wip = buf->b_wininfo;
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return wip;
3059}
3060
3061/*
3062 * Reset the local window options to the values last used in this window.
3063 * If the buffer wasn't used in this window before, use the values from
3064 * the most recently used window. If the values were never set, use the
3065 * global values for the window.
3066 */
3067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003068get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 wininfo_T *wip;
3071
3072 clear_winopt(&curwin->w_onebuf_opt);
3073#ifdef FEAT_FOLDING
3074 clearFolding(curwin);
3075#endif
3076
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003077 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003078 if (wip != NULL && wip->wi_win != NULL
3079 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003081 // The buffer is currently displayed in the window: use the actual
3082 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003083 win_T *wp = wip->wi_win;
3084
3085 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3086#ifdef FEAT_FOLDING
3087 curwin->w_fold_manual = wp->w_fold_manual;
3088 curwin->w_foldinvalid = TRUE;
3089 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3090#endif
3091 }
3092 else if (wip != NULL && wip->wi_optset)
3093 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003094 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3096#ifdef FEAT_FOLDING
3097 curwin->w_fold_manual = wip->wi_fold_manual;
3098 curwin->w_foldinvalid = TRUE;
3099 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3100#endif
3101 }
3102 else
3103 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3104
3105#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003106 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (p_fdls >= 0)
3108 curwin->w_p_fdl = p_fdls;
3109#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003110 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111}
3112
3113/*
3114 * Find the position (lnum and col) for the buffer 'buf' for the current
3115 * window.
3116 * Returns a pointer to no_position if no position is found.
3117 */
3118 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003119buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003122 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003124 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (wip != NULL)
3126 return &(wip->wi_fpos);
3127 else
3128 return &no_position;
3129}
3130
3131/*
3132 * Find the lnum for the buffer 'buf' for the current window.
3133 */
3134 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003135buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 return buflist_findfpos(buf)->lnum;
3138}
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003141 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003144buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
Bram Moolenaar52410572019-10-27 05:12:45 +01003146 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int len;
3148 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003149 int ro_char;
3150 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003151#ifdef FEAT_TERMINAL
3152 int job_running;
3153 int job_none_open;
3154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaar52410572019-10-27 05:12:45 +01003156#ifdef FEAT_VIMINFO
3157 garray_T buflist;
3158 buf_T **buflist_data = NULL, **p;
3159
3160 if (vim_strchr(eap->arg, 't'))
3161 {
3162 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003163 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003164 {
3165 if (ga_grow(&buflist, 1) == OK)
3166 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3167 }
3168
3169 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3170 sizeof(buf_T *), buf_compare);
3171
Bram Moolenaar3b991522019-11-06 23:26:20 +01003172 buflist_data = (buf_T **)buflist.ga_data;
3173 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003174 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003175 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003176
Bram Moolenaar3b991522019-11-06 23:26:20 +01003177 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003178 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3179 : buf->b_next)
3180#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003184#ifdef FEAT_TERMINAL
3185 job_running = term_job_running(buf->b_term);
3186 job_none_open = job_running && term_none_open(buf->b_term);
3187#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003188 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003189 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3190 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3191 || (vim_strchr(eap->arg, '+')
3192 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3193 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003194 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003195 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003196 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3197#ifdef FEAT_TERMINAL
3198 || (vim_strchr(eap->arg, 'R')
3199 && (!job_running || (job_running && job_none_open)))
3200 || (vim_strchr(eap->arg, '?')
3201 && (!job_running || (job_running && !job_none_open)))
3202 || (vim_strchr(eap->arg, 'F')
3203 && (job_running || buf->b_term == NULL))
3204#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003205 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3206 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3207 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3208 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3209 || (vim_strchr(eap->arg, '#')
3210 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003213 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 else
3215 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003216 if (message_filtered(NameBuff))
3217 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003219 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3220 : (bufIsChanged(buf) ? '+' : ' ');
3221#ifdef FEAT_TERMINAL
3222 if (term_job_running(buf->b_term))
3223 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003224 if (term_none_open(buf->b_term))
3225 ro_char = '?';
3226 else
3227 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003228 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3229 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003230 }
3231 else if (buf->b_term != NULL)
3232 ro_char = 'F';
3233 else
3234#endif
3235 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3236
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003237 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003238 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 buf->b_fnum,
3240 buf->b_p_bl ? ' ' : 'u',
3241 buf == curbuf ? '%' :
3242 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3243 buf->b_ml.ml_mfp == NULL ? ' ' :
3244 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003245 ro_char,
3246 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003247 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003248 if (len > IOSIZE - 20)
3249 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaarc667da52019-11-30 20:52:27 +01003251 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 i = 40 - vim_strsize(IObuff);
3253 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003255 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003256#ifdef FEAT_VIMINFO
3257 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3258 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3259 else
3260#endif
3261 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3262 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003263 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003265 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 ui_breakcheck();
3267 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003268
3269#ifdef FEAT_VIMINFO
3270 if (buflist_data)
3271 ga_clear(&buflist);
3272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275/*
3276 * Get file name and line number for file 'fnum'.
3277 * Used by DoOneCmd() for translating '%' and '#'.
3278 * Used by insert_reg() and cmdline_paste() for '#' register.
3279 * Return FAIL if not found, OK for success.
3280 */
3281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282buflist_name_nr(
3283 int fnum,
3284 char_u **fname,
3285 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 buf_T *buf;
3288
3289 buf = buflist_findnr(fnum);
3290 if (buf == NULL || buf->b_fname == NULL)
3291 return FAIL;
3292
3293 *fname = buf->b_fname;
3294 *lnum = buflist_findlnum(buf);
3295
3296 return OK;
3297}
3298
3299/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003300 * Set the file name for "buf"' to "ffname_arg", short file name to
3301 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 * The file name with the full path is also remembered, for when :cd is used.
3303 * Returns FAIL for failure (file name already in use by other buffer)
3304 * OK otherwise.
3305 */
3306 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003307setfname(
3308 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003309 char_u *ffname_arg,
3310 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003311 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003313 char_u *ffname = ffname_arg;
3314 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003315 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003317 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#endif
3319
3320 if (ffname == NULL || *ffname == NUL)
3321 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003322 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003323 if (buf->b_sfname != buf->b_ffname)
3324 VIM_CLEAR(buf->b_sfname);
3325 else
3326 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003327 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#ifdef UNIX
3329 st.st_dev = (dev_T)-1;
3330#endif
3331 }
3332 else
3333 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003334 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3335 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return FAIL;
3337
3338 /*
3339 * if the file name is already used in another buffer:
3340 * - if the buffer is loaded, fail
3341 * - if the buffer is not loaded, delete it from the list
3342 */
3343#ifdef UNIX
3344 if (mch_stat((char *)ffname, &st) < 0)
3345 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003346#endif
3347 if (!(buf->b_flags & BF_DUMMY))
3348#ifdef UNIX
3349 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003351 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
3353 if (obuf != NULL && obuf != buf)
3354 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003355 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003358 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 vim_free(ffname);
3360 return FAIL;
3361 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003362 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003363 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 sfname = vim_strsave(sfname);
3366 if (ffname == NULL || sfname == NULL)
3367 {
3368 vim_free(sfname);
3369 vim_free(ffname);
3370 return FAIL;
3371 }
3372#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003373 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003375 if (buf->b_sfname != buf->b_ffname)
3376 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 buf->b_ffname = ffname;
3379 buf->b_sfname = sfname;
3380 }
3381 buf->b_fname = buf->b_sfname;
3382#ifdef UNIX
3383 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003384 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else
3386 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003387 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 buf->b_dev = st.st_dev;
3389 buf->b_ino = st.st_ino;
3390 }
3391#endif
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 buf_name_changed(buf);
3396 return OK;
3397}
3398
3399/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003400 * Crude way of changing the name of a buffer. Use with care!
3401 * The name should be relative to the current directory.
3402 */
3403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003404buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003405{
3406 buf_T *buf;
3407
3408 buf = buflist_findnr(fnum);
3409 if (buf != NULL)
3410 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003411 if (buf->b_sfname != buf->b_ffname)
3412 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003413 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003414 buf->b_ffname = vim_strsave(name);
3415 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003416 // Allocate ffname and expand into full path. Also resolves .lnk
3417 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003418 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003419 buf->b_fname = buf->b_sfname;
3420 }
3421}
3422
3423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Take care of what needs to be done when the name of buffer "buf" has
3425 * changed.
3426 */
3427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003428buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 /*
3431 * If the file name changed, also change the name of the swapfile
3432 */
3433 if (buf->b_ml.ml_mfp != NULL)
3434 ml_setname(buf);
3435
3436 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003437 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003439 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003441 status_redraw_all(); // status lines need to be redrawn
3442 fmarks_check_names(buf); // check named file marks
3443 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446/*
3447 * set alternate file name for current window
3448 *
3449 * Used by do_one_cmd(), do_write() and do_ecmd().
3450 * Return the buffer.
3451 */
3452 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003453setaltfname(
3454 char_u *ffname,
3455 char_u *sfname,
3456 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 buf_T *buf;
3459
Bram Moolenaarc667da52019-11-30 20:52:27 +01003460 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003462 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 curwin->w_alt_fnum = buf->b_fnum;
3464 return buf;
3465}
3466
3467/*
3468 * Get alternate file name for current window.
3469 * Return NULL if there isn't any, and give error message if requested.
3470 */
3471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003473 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 char_u *fname;
3476 linenr_T dummy;
3477
3478 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3479 {
3480 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003481 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return NULL;
3483 }
3484 return fname;
3485}
3486
3487/*
3488 * Add a file name to the buflist and return its number.
3489 * Uses same flags as buflist_new(), except BLN_DUMMY.
3490 *
3491 * used by qf_init(), main() and doarglist()
3492 */
3493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 buf_T *buf;
3497
3498 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3499 if (buf != NULL)
3500 return buf->b_fnum;
3501 return 0;
3502}
3503
3504#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3505/*
3506 * Adjust slashes in file names. Called after 'shellslash' was set.
3507 */
3508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003509buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
3511 buf_T *bp;
3512
Bram Moolenaar29323592016-07-24 22:04:11 +02003513 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 if (bp->b_ffname != NULL)
3516 slash_adjust(bp->b_ffname);
3517 if (bp->b_sfname != NULL)
3518 slash_adjust(bp->b_sfname);
3519 }
3520}
3521#endif
3522
3523/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003524 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 * Also save the local window option values.
3526 */
3527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003528buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003530 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531}
3532
3533/*
3534 * Return TRUE if 'ffname' is not the same file as current file.
3535 * Fname must have a full path (expanded by mch_FullName()).
3536 */
3537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003538otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
3540 return otherfile_buf(curbuf, ffname
3541#ifdef UNIX
3542 , NULL
3543#endif
3544 );
3545}
3546
3547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548otherfile_buf(
3549 buf_T *buf,
3550 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003552 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003556 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3558 return TRUE;
3559 if (fnamecmp(ffname, buf->b_ffname) == 0)
3560 return FALSE;
3561#ifdef UNIX
3562 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003563 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaarc667da52019-11-30 20:52:27 +01003565 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (stp == NULL)
3567 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003568 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 st.st_dev = (dev_T)-1;
3570 stp = &st;
3571 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003572 // Use dev/ino to check if the files are the same, even when the names
3573 // are different (possible with links). Still need to compare the
3574 // name above, for when the file doesn't exist yet.
3575 // Problem: The dev/ino changes when a file is deleted (and created
3576 // again) and remains the same when renamed/moved. We don't want to
3577 // mch_stat() each buffer each time, that would be too slow. Get the
3578 // dev/ino again when they appear to match, but not when they appear
3579 // to be different: Could skip a buffer when it's actually the same
3580 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (buf_same_ino(buf, stp))
3582 {
3583 buf_setino(buf);
3584 if (buf_same_ino(buf, stp))
3585 return FALSE;
3586 }
3587 }
3588#endif
3589 return TRUE;
3590}
3591
3592#if defined(UNIX) || defined(PROTO)
3593/*
3594 * Set inode and device number for a buffer.
3595 * Must always be called when b_fname is changed!.
3596 */
3597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003600 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3603 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003604 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 buf->b_dev = st.st_dev;
3606 buf->b_ino = st.st_ino;
3607 }
3608 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003609 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610}
3611
3612/*
3613 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3614 */
3615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616buf_same_ino(
3617 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003618 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003620 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 && stp->st_dev == buf->b_dev
3622 && stp->st_ino == buf->b_ino);
3623}
3624#endif
3625
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003626/*
3627 * Print info about the current buffer.
3628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003631 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632 int shorthelp,
3633 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
3635 char_u *name;
3636 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003637 char *p;
3638 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003639 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003641 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (buffer == NULL)
3643 return;
3644
Bram Moolenaarc667da52019-11-30 20:52:27 +01003645 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003647 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 p = buffer + STRLEN(buffer);
3649 }
3650 else
3651 p = buffer;
3652
3653 *p++ = '"';
3654 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003655 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 else
3657 {
3658 if (!fullname && curbuf->b_fname != NULL)
3659 name = curbuf->b_fname;
3660 else
3661 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003662 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 (int)(IOSIZE - (p - buffer)), TRUE);
3664 }
3665
Bram Moolenaar32526b32019-01-19 17:43:09 +01003666 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 curbufIsChanged() ? (shortmess(SHM_MOD)
3668 ? " [+]" : _(" [Modified]")) : " ",
3669 (curbuf->b_flags & BF_NOTEDITED)
3670#ifdef FEAT_QUICKFIX
3671 && !bt_dontwrite(curbuf)
3672#endif
3673 ? _("[Not edited]") : "",
3674 (curbuf->b_flags & BF_NEW)
3675#ifdef FEAT_QUICKFIX
3676 && !bt_dontwrite(curbuf)
3677#endif
Bram Moolenaar722e5052020-06-12 22:31:00 +02003678 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003680 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 : _("[readonly]")) : "",
3682 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3683 || curbuf->b_p_ro) ?
3684 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003685 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3686 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (curwin->w_cursor.lnum > 1000000L)
3688 n = (int)(((long)curwin->w_cursor.lnum) /
3689 ((long)curbuf->b_ml.ml_line_count / 100L));
3690 else
3691 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3692 (long)curbuf->b_ml.ml_line_count);
3693 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003694 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695#ifdef FEAT_CMDL_INFO
3696 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003697 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003698 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003699 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3700 curbuf->b_ml.ml_line_count),
3701 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702#endif
3703 else
3704 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003705 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 _("line %ld of %ld --%d%%-- col "),
3707 (long)curwin->w_cursor.lnum,
3708 (long)curbuf->b_ml.ml_line_count,
3709 n);
3710 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003711 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003712 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3714 }
3715
Bram Moolenaar32526b32019-01-19 17:43:09 +01003716 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3717 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 if (dont_truncate)
3720 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003721 // Temporarily set msg_scroll to avoid the message being truncated.
3722 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 msg_start();
3724 n = msg_scroll;
3725 msg_scroll = TRUE;
3726 msg(buffer);
3727 msg_scroll = n;
3728 }
3729 else
3730 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003731 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003733 // Need to repeat the message after redrawing when:
3734 // - When restart_edit is set (otherwise there will be a delay
3735 // before redrawing).
3736 // - When the screen was scrolled but there is no wait-return
3737 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003738 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740
3741 vim_free(buffer);
3742}
3743
3744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745col_print(
3746 char_u *buf,
3747 size_t buflen,
3748 int col,
3749 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003752 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003754 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755}
3756
3757#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758static char_u *lasttitle = NULL;
3759static char_u *lasticon = NULL;
3760
Bram Moolenaar84a93082018-06-16 22:58:15 +02003761/*
3762 * Put the file name in the title bar and icon of the window.
3763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003765maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003768 char_u *title_str = NULL;
3769 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 int maxlen = 0;
3771 int len;
3772 int mustset;
3773 char_u buf[IOSIZE];
3774 int off;
3775
3776 if (!redrawing())
3777 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003778 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 need_maketitle = TRUE;
3780 return;
3781 }
3782
3783 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003784 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003785 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 if (p_title)
3788 {
3789 if (p_titlelen > 0)
3790 {
3791 maxlen = p_titlelen * Columns / 100;
3792 if (maxlen < 10)
3793 maxlen = 10;
3794 }
3795
Bram Moolenaar84a93082018-06-16 22:58:15 +02003796 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (*p_titlestring != NUL)
3798 {
3799#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003800 if (stl_syntax & STL_IN_TITLE)
3801 {
3802 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003803 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003804
3805# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003806 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003807# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003808 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003809 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003810 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003811 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003812 set_string_option_direct((char_u *)"titlestring", -1,
3813 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 else
3816#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003817 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 else
3820 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003821 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
Bram Moolenaar2c666692012-09-05 13:30:40 +02003823#define SPACE_FOR_FNAME (IOSIZE - 100)
3824#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003825#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003827 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003828#ifdef FEAT_TERMINAL
3829 else if (curbuf->b_term != NULL)
3830 {
3831 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3832 SPACE_FOR_FNAME);
3833 }
3834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 else
3836 {
3837 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003838 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
Bram Moolenaar21554412017-07-24 21:44:43 +02003842#ifdef FEAT_TERMINAL
3843 if (curbuf->b_term == NULL)
3844#endif
3845 switch (bufIsChanged(curbuf)
3846 + (curbuf->b_p_ro * 2)
3847 + (!curbuf->b_p_ma * 4))
3848 {
3849 case 1: STRCAT(buf, " +"); break;
3850 case 2: STRCAT(buf, " ="); break;
3851 case 3: STRCAT(buf, " =+"); break;
3852 case 4:
3853 case 6: STRCAT(buf, " -"); break;
3854 case 5:
3855 case 7: STRCAT(buf, " -+"); break;
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaar21554412017-07-24 21:44:43 +02003858 if (curbuf->b_fname != NULL
3859#ifdef FEAT_TERMINAL
3860 && curbuf->b_term == NULL
3861#endif
3862 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003864 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 off = (int)STRLEN(buf);
3866 buf[off++] = ' ';
3867 buf[off++] = '(';
3868 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003869 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003871 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (isalpha(buf[off]) && buf[off + 1] == ':')
3873 off += 2;
3874#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003875 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003876 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003878 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003879 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003880 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003881 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003885
Bram Moolenaarc667da52019-11-30 20:52:27 +01003886 // Translate unprintable chars and concatenate. Keep some
3887 // room for the server name. When there is no room (very long
3888 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003889 if (off < SPACE_FOR_DIR)
3890 {
3891 p = transstr(buf + off);
3892 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3893 vim_free(p);
3894 }
3895 else
3896 {
3897 vim_strncpy(buf + off, (char_u *)"...",
3898 (size_t)(SPACE_FOR_ARGNR - off));
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 STRCAT(buf, ")");
3901 }
3902
Bram Moolenaar2c666692012-09-05 13:30:40 +02003903 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905#if defined(FEAT_CLIENTSERVER)
3906 if (serverName != NULL)
3907 {
3908 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003909 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 else
3912#endif
3913 STRCAT(buf, " - VIM");
3914
3915 if (maxlen > 0)
3916 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003917 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003918 if (vim_strsize(buf) > maxlen)
3919 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 }
3922 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003923 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 if (p_icon)
3926 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003927 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (*p_iconstring != NUL)
3929 {
3930#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003931 if (stl_syntax & STL_IN_ICON)
3932 {
3933 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003934 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003935
3936# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003937 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003938# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003939 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003940 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003941 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003942 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003943 set_string_option_direct((char_u *)"iconstring", -1,
3944 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
3947#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003948 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 else
3951 {
3952 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003953 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003954 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003955 p = gettail(curbuf->b_ffname);
3956 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003957 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003958 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (len > 100)
3960 {
3961 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003963 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003964 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003966 STRCPY(icon_str, p);
3967 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970
Bram Moolenaar84a93082018-06-16 22:58:15 +02003971 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
3973 if (mustset)
3974 resettitle();
3975}
3976
3977/*
3978 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3979 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003980 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003983value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985 if ((str == NULL) != (*last == NULL)
3986 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3987 {
3988 vim_free(*last);
3989 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003992 mch_restore_title(
3993 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003998 return TRUE;
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 return FALSE;
4002}
4003
4004/*
4005 * Put current window title back (used after calling a shell)
4006 */
4007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004008resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 mch_settitle(lasttitle, lasticon);
4011}
Bram Moolenaarea408852005-06-25 22:49:46 +00004012
4013# if defined(EXITFREE) || defined(PROTO)
4014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004015free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004016{
4017 vim_free(lasttitle);
4018 vim_free(lasticon);
4019}
4020# endif
4021
Bram Moolenaarc667da52019-11-30 20:52:27 +01004022#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004024#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004025
4026/*
4027 * Used for building in the status line.
4028 */
4029typedef struct
4030{
4031 char_u *stl_start;
4032 int stl_minwid;
4033 int stl_maxwid;
4034 enum {
4035 Normal,
4036 Empty,
4037 Group,
4038 Middle,
4039 Highlight,
4040 TabPage,
4041 Trunc
4042 } stl_type;
4043} stl_item_T;
4044
4045static size_t stl_items_len = 20; // Initial value, grows as needed.
4046static stl_item_T *stl_items = NULL;
4047static int *stl_groupitem = NULL;
4048static stl_hlrec_T *stl_hltab = NULL;
4049static stl_hlrec_T *stl_tabtab = NULL;
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004052 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 * Return length of string in screen cells.
4054 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004055 * Normally works for window "wp", except when working for 'tabline' then it
4056 * is "curwin".
4057 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 * Items are drawn interspersed with the text that surrounds it
4059 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4060 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4061 *
4062 * If maxwidth is not zero, the string will be filled at any middle marker
4063 * or truncated if too long, fillchar is used for all whitespace.
4064 */
4065 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004066build_stl_str_hl(
4067 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004068 char_u *out, // buffer to write into != NameBuff
4069 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004070 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004071 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004072 int fillchar,
4073 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004074 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4075 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
Bram Moolenaar10772302019-01-20 18:25:54 +01004077 linenr_T lnum;
4078 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 char_u *p;
4080 char_u *s;
4081 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004082 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004084 win_T *save_curwin;
4085 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004086 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#endif
4088 int empty_line;
4089 colnr_T virtcol;
4090 long l;
4091 long n;
4092 int prevchar_isflag;
4093 int prevchar_isitem;
4094 int itemisflag;
4095 int fillable;
4096 char_u *str;
4097 long num;
4098 int width;
4099 int itemcnt;
4100 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004101 int group_end_userhl;
4102 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 int groupdepth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 int minwid;
4105 int maxwid;
4106 int zeropad;
4107 char_u base;
4108 char_u opt;
4109#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004110 char_u buf_tmp[TMPLEN];
4111 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004112 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004113 stl_hlrec_T *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004114 int save_must_redraw = must_redraw;
4115 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004116
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004117 if (stl_items == NULL)
4118 {
4119 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4120 stl_groupitem = ALLOC_MULT(int, stl_items_len);
4121 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4122 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4123 }
4124
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004125#ifdef FEAT_EVAL
4126 /*
4127 * When the format starts with "%!" then evaluate it as an expression and
4128 * use the result as the actual format string.
4129 */
4130 if (fmt[0] == '%' && fmt[1] == '!')
4131 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004132 typval_T tv;
4133
4134 tv.v_type = VAR_NUMBER;
4135 tv.vval.v_number = wp->w_id;
4136 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4137
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004138 usefmt = eval_to_string_safe(fmt + 2, use_sandbox);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004139 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004140 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004141
4142 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004143 }
4144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 if (fillchar == 0)
4147 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004148 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004149 else if (mb_char2len(fillchar) > 1)
4150 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004151
Bram Moolenaar10772302019-01-20 18:25:54 +01004152 // The cursor in windows other than the current one isn't always
4153 // up-to-date, esp. because of autocommands and timers.
4154 lnum = wp->w_cursor.lnum;
4155 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4156 {
4157 lnum = wp->w_buffer->b_ml.ml_line_count;
4158 wp->w_cursor.lnum = lnum;
4159 }
4160
4161 // Get line & check if empty (cursorpos will show "0-1"). Note that
4162 // p will become invalid when getting another buffer line.
4163 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004164 empty_line = (*p == NUL);
4165
Bram Moolenaar10772302019-01-20 18:25:54 +01004166 // Get the byte value now, in case we need it below. This is more efficient
4167 // than making a copy of the line.
4168 len = STRLEN(p);
4169 if (wp->w_cursor.col > (colnr_T)len)
4170 {
4171 // Line may have changed since checking the cursor column, or the lnum
4172 // was adjusted above.
4173 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004174 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004175 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004176 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004177 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004178 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 groupdepth = 0;
4181 p = out;
4182 curitem = 0;
4183 prevchar_isflag = TRUE;
4184 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004185 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004187 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004188 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004189 size_t new_len = stl_items_len * 3 / 2;
4190 stl_item_T *new_items;
4191 int *new_groupitem;
4192 stl_hlrec_T *new_hlrec;
4193
4194 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4195 if (new_items == NULL)
4196 break;
4197 stl_items = new_items;
4198 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4199 if (new_groupitem == NULL)
4200 break;
4201 stl_groupitem = new_groupitem;
4202 new_hlrec = vim_realloc(stl_hltab, sizeof(stl_hlrec_T) * new_len);
4203 if (new_hlrec == NULL)
4204 break;
4205 stl_hltab = new_hlrec;
4206 new_hlrec = vim_realloc(stl_tabtab, sizeof(stl_hlrec_T) * new_len);
4207 if (new_hlrec == NULL)
4208 break;
4209 stl_tabtab = new_hlrec;
4210 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004211 }
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (*s != NUL && *s != '%')
4214 prevchar_isflag = prevchar_isitem = FALSE;
4215
4216 /*
4217 * Handle up to the next '%' or the end.
4218 */
4219 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4220 *p++ = *s++;
4221 if (*s == NUL || p + 1 >= out + outlen)
4222 break;
4223
4224 /*
4225 * Handle one '%' item.
4226 */
4227 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004228 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (*s == '%')
4231 {
4232 if (p + 1 >= out + outlen)
4233 break;
4234 *p++ = *s++;
4235 prevchar_isflag = prevchar_isitem = FALSE;
4236 continue;
4237 }
4238 if (*s == STL_MIDDLEMARK)
4239 {
4240 s++;
4241 if (groupdepth > 0)
4242 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004243 stl_items[curitem].stl_type = Middle;
4244 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 continue;
4246 }
4247 if (*s == STL_TRUNCMARK)
4248 {
4249 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004250 stl_items[curitem].stl_type = Trunc;
4251 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 continue;
4253 }
4254 if (*s == ')')
4255 {
4256 s++;
4257 if (groupdepth < 1)
4258 continue;
4259 groupdepth--;
4260
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004261 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 *p = NUL;
4263 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004264 if (curitem > stl_groupitem[groupdepth] + 1
4265 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004267 // remove group if all items are empty and highlight group
4268 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004269 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004270 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004271 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004272 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004273 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004274 group_start_userhl = group_end_userhl =
4275 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004276 break;
4277 }
4278 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004279 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004280 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004281 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004283 if (stl_items[n].stl_type == Highlight)
4284 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004285 }
4286 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004288 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 p = t;
4290 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004291 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004292 {
4293 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004294 if (stl_items[n].stl_type == Highlight)
4295 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004296 // adjust the start position of TabPage to the next
4297 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004298 if (stl_items[n].stl_type == TabPage)
4299 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004303 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004305 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (has_mbyte)
4307 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004308 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004310 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004313 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
4316 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004317 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4318 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004321 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004323
4324 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004325 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
Bram Moolenaarc667da52019-11-30 20:52:27 +01004328 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004329 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004331 stl_items[l].stl_start -= n;
4332 if (stl_items[l].stl_start < t)
4333 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004336 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004338 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004339 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 if (n < 0)
4341 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004342 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 n = 0 - n;
4344 while (l++ < n && p + 1 < out + outlen)
4345 *p++ = fillchar;
4346 }
4347 else
4348 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004349 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004350 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 l = n - l;
4352 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004353 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004355 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4356 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 for ( ; l > 0; l--)
4358 *t++ = fillchar;
4359 }
4360 }
4361 continue;
4362 }
4363 minwid = 0;
4364 maxwid = 9999;
4365 zeropad = FALSE;
4366 l = 1;
4367 if (*s == '0')
4368 {
4369 s++;
4370 zeropad = TRUE;
4371 }
4372 if (*s == '-')
4373 {
4374 s++;
4375 l = -1;
4376 }
4377 if (VIM_ISDIGIT(*s))
4378 {
4379 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004380 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 minwid = 0;
4382 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004383 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004385 stl_items[curitem].stl_type = Highlight;
4386 stl_items[curitem].stl_start = p;
4387 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 s++;
4389 curitem++;
4390 continue;
4391 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004392 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4393 {
4394 if (*s == STL_TABCLOSENR)
4395 {
4396 if (minwid == 0)
4397 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004398 // %X ends the close label, go back to the previously
4399 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004400 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004401 if (stl_items[n].stl_type == TabPage
4402 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004403 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004404 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004405 break;
4406 }
4407 }
4408 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004409 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004410 minwid = - minwid;
4411 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004412 stl_items[curitem].stl_type = TabPage;
4413 stl_items[curitem].stl_start = p;
4414 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004415 s++;
4416 curitem++;
4417 continue;
4418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (*s == '.')
4420 {
4421 s++;
4422 if (VIM_ISDIGIT(*s))
4423 {
4424 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004425 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 maxwid = 50;
4427 }
4428 }
4429 minwid = (minwid > 50 ? 50 : minwid) * l;
4430 if (*s == '(')
4431 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004432 stl_groupitem[groupdepth++] = curitem;
4433 stl_items[curitem].stl_type = Group;
4434 stl_items[curitem].stl_start = p;
4435 stl_items[curitem].stl_minwid = minwid;
4436 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 s++;
4438 curitem++;
4439 continue;
4440 }
4441 if (vim_strchr(STL_ALL, *s) == NULL)
4442 {
4443 s++;
4444 continue;
4445 }
4446 opt = *s++;
4447
Bram Moolenaarc667da52019-11-30 20:52:27 +01004448 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 base = 'D';
4450 itemisflag = FALSE;
4451 fillable = TRUE;
4452 num = -1;
4453 str = NULL;
4454 switch (opt)
4455 {
4456 case STL_FILEPATH:
4457 case STL_FULLPATH:
4458 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004459 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004461 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
4463 {
4464 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004465 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4467 }
4468 trans_characters(NameBuff, MAXPATHL);
4469 if (opt != STL_FILENAME)
4470 str = NameBuff;
4471 else
4472 str = gettail(NameBuff);
4473 break;
4474
Bram Moolenaarc667da52019-11-30 20:52:27 +01004475 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 itemisflag = TRUE;
4477 t = p;
4478 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4479 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004480 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 break;
4482 s++;
4483 *p = 0;
4484 p = t;
4485
4486#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004487 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4488 "%d", curbuf->b_fnum);
4489 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4490 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4491 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004493 save_curbuf = curbuf;
4494 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004495 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 curwin = wp;
4497 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004498 // Visual mode is only valid in the current window.
4499 if (curwin != save_curwin)
4500 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004502 str = eval_to_string_safe(p, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004504 curwin = save_curwin;
4505 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004506 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004507 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004508 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 if (str != NULL && *str != 0)
4511 {
4512 if (*skipdigits(str) == NUL)
4513 {
4514 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004515 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 itemisflag = FALSE;
4517 }
4518 }
4519#endif
4520 break;
4521
4522 case STL_LINE:
4523 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4524 ? 0L : (long)(wp->w_cursor.lnum);
4525 break;
4526
4527 case STL_NUMLINES:
4528 num = wp->w_buffer->b_ml.ml_line_count;
4529 break;
4530
4531 case STL_COLUMN:
4532 num = !(State & INSERT) && empty_line
4533 ? 0 : (int)wp->w_cursor.col + 1;
4534 break;
4535
4536 case STL_VIRTCOL:
4537 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004538 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 virtcol = wp->w_virtcol;
4540 if (wp->w_p_list && lcs_tab1 == NUL)
4541 {
4542 wp->w_p_list = FALSE;
4543 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4544 wp->w_p_list = TRUE;
4545 }
4546 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004547 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (opt == STL_VIRTCOL_ALT
4549 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4550 ? 0 : (int)wp->w_cursor.col + 1)))
4551 break;
4552 num = (long)virtcol;
4553 break;
4554
4555 case STL_PERCENTAGE:
4556 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4557 (long)wp->w_buffer->b_ml.ml_line_count);
4558 break;
4559
4560 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004561 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004562 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 break;
4564
4565 case STL_ARGLISTSTAT:
4566 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004567 buf_tmp[0] = 0;
4568 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4569 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571
4572 case STL_KEYMAP:
4573 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004574 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4575 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 break;
4577 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004578#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004579 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#else
4581 num = 0;
4582#endif
4583 break;
4584
4585 case STL_BUFNO:
4586 num = wp->w_buffer->b_fnum;
4587 break;
4588
4589 case STL_OFFSET_X:
4590 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004591 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 case STL_OFFSET:
4593#ifdef FEAT_BYTEOFF
4594 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4595 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4596 0L : l + 1 + (!(State & INSERT) && empty_line ?
4597 0 : (int)wp->w_cursor.col);
4598#endif
4599 break;
4600
4601 case STL_BYTEVAL_X:
4602 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004603 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004605 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (num == NL)
4607 num = 0;
4608 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4609 num = NL;
4610 break;
4611
4612 case STL_ROFLAG:
4613 case STL_ROFLAG_ALT:
4614 itemisflag = TRUE;
4615 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004616 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618
4619 case STL_HELPFLAG:
4620 case STL_HELPFLAG_ALT:
4621 itemisflag = TRUE;
4622 if (wp->w_buffer->b_help)
4623 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004624 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 case STL_FILETYPE:
4628 if (*wp->w_buffer->b_p_ft != NUL
4629 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4630 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004631 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004632 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004633 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 break;
4636
4637 case STL_FILETYPE_ALT:
4638 itemisflag = TRUE;
4639 if (*wp->w_buffer->b_p_ft != NUL
4640 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4641 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004642 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004643 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004644 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004646 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaar4033c552017-09-16 20:54:51 +02004650#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 case STL_PREVIEWFLAG:
4652 case STL_PREVIEWFLAG_ALT:
4653 itemisflag = TRUE;
4654 if (wp->w_p_pvw)
4655 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4656 : _("[Preview]"));
4657 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004658
4659 case STL_QUICKFIX:
4660 if (bt_quickfix(wp->w_buffer))
4661 str = (char_u *)(wp->w_llist_ref
4662 ? _(msg_loclist)
4663 : _(msg_qflist));
4664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
4666
4667 case STL_MODIFIED:
4668 case STL_MODIFIED_ALT:
4669 itemisflag = TRUE;
4670 switch ((opt == STL_MODIFIED_ALT)
4671 + bufIsChanged(wp->w_buffer) * 2
4672 + (!wp->w_buffer->b_p_ma) * 4)
4673 {
4674 case 2: str = (char_u *)"[+]"; break;
4675 case 3: str = (char_u *)",+"; break;
4676 case 4: str = (char_u *)"[-]"; break;
4677 case 5: str = (char_u *)",-"; break;
4678 case 6: str = (char_u *)"[+-]"; break;
4679 case 7: str = (char_u *)",+-"; break;
4680 }
4681 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004682
4683 case STL_HIGHLIGHT:
4684 t = s;
4685 while (*s != '#' && *s != NUL)
4686 ++s;
4687 if (*s == '#')
4688 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004689 stl_items[curitem].stl_type = Highlight;
4690 stl_items[curitem].stl_start = p;
4691 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004692 curitem++;
4693 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004694 if (*s != NUL)
4695 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004696 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004699 stl_items[curitem].stl_start = p;
4700 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 if (str != NULL && *str)
4702 {
4703 t = str;
4704 if (itemisflag)
4705 {
4706 if ((t[0] && t[1])
4707 && ((!prevchar_isitem && *t == ',')
4708 || (prevchar_isflag && *t == ' ')))
4709 t++;
4710 prevchar_isflag = TRUE;
4711 }
4712 l = vim_strsize(t);
4713 if (l > 0)
4714 prevchar_isitem = TRUE;
4715 if (l > maxwid)
4716 {
4717 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (has_mbyte)
4719 {
4720 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004721 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 l -= byte2cells(*t++);
4725 if (p + 1 >= out + outlen)
4726 break;
4727 *p++ = '<';
4728 }
4729 if (minwid > 0)
4730 {
4731 for (; l < minwid && p + 1 < out + outlen; l++)
4732 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004733 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4735 *p++ = ' ';
4736 else
4737 *p++ = fillchar;
4738 }
4739 minwid = 0;
4740 }
4741 else
4742 minwid *= -1;
4743 while (*t && p + 1 < out + outlen)
4744 {
4745 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004746 // Change a space by fillchar, unless fillchar is '-' and a
4747 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (fillable && p[-1] == ' '
4749 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4750 p[-1] = fillchar;
4751 }
4752 for (; l < minwid && p + 1 < out + outlen; l++)
4753 *p++ = fillchar;
4754 }
4755 else if (num >= 0)
4756 {
4757 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4758 char_u nstr[20];
4759
4760 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004761 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 prevchar_isitem = TRUE;
4763 t = nstr;
4764 if (opt == STL_VIRTCOL_ALT)
4765 {
4766 *t++ = '-';
4767 minwid--;
4768 }
4769 *t++ = '%';
4770 if (zeropad)
4771 *t++ = '0';
4772 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004773 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 *t = 0;
4775
4776 for (n = num, l = 1; n >= nbase; n /= nbase)
4777 l++;
4778 if (opt == STL_VIRTCOL_ALT)
4779 l++;
4780 if (l > maxwid)
4781 {
4782 l += 2;
4783 n = l - maxwid;
4784 while (l-- > maxwid)
4785 num /= nbase;
4786 *t++ = '>';
4787 *t++ = '%';
4788 *t = t[-3];
4789 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004790 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4791 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004794 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4795 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 p += STRLEN(p);
4797 }
4798 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004799 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 if (opt == STL_VIM_EXPR)
4802 vim_free(str);
4803
4804 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004805 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 curitem++;
4807 }
4808 *p = NUL;
4809 itemcnt = curitem;
4810
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004811#ifdef FEAT_EVAL
4812 if (usefmt != fmt)
4813 vim_free(usefmt);
4814#endif
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 width = vim_strsize(out);
4817 if (maxwidth > 0 && width > maxwidth)
4818 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004819 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 l = 0;
4821 if (itemcnt == 0)
4822 s = out;
4823 else
4824 {
4825 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004826 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004828 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004829 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 break;
4831 }
4832 if (l == itemcnt)
4833 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004834 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004835 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 l = 0;
4837 }
4838 }
4839
4840 if (width - vim_strsize(s) >= maxwidth)
4841 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004842 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (has_mbyte)
4844 {
4845 s = out;
4846 width = 0;
4847 for (;;)
4848 {
4849 width += ptr2cells(s);
4850 if (width >= maxwidth)
4851 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004852 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004854 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 while (++width < maxwidth)
4856 *s++ = fillchar;
4857 }
4858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 s = out + maxwidth - 1;
4860 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004861 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 break;
4863 itemcnt = l;
4864 *s++ = '>';
4865 *s = 0;
4866 }
4867 else
4868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 if (has_mbyte)
4870 {
4871 n = 0;
4872 while (width >= maxwidth)
4873 {
4874 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004875 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 }
4878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 n = width - maxwidth + 1;
4880 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004881 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 *s = '<';
4883
Bram Moolenaarc667da52019-11-30 20:52:27 +01004884 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 while (++width < maxwidth)
4886 {
4887 s = s + STRLEN(s);
4888 *s++ = fillchar;
4889 *s = NUL;
4890 }
4891
Bram Moolenaarc667da52019-11-30 20:52:27 +01004892 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 for (; l < itemcnt; l++)
4894 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004895 if (stl_items[l].stl_start - n >= s)
4896 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004898 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 }
4901 width = maxwidth;
4902 }
4903 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4904 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004905 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004907 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 break;
4909 if (l < itemcnt)
4910 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004911 p = stl_items[l].stl_start + maxwidth - width;
4912 STRMOVE(p, stl_items[l].stl_start);
4913 for (s = stl_items[l].stl_start; s < p; s++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 *s = fillchar;
4915 for (l++; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004916 stl_items[l].stl_start += maxwidth - width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 width = maxwidth;
4918 }
4919 }
4920
Bram Moolenaarc667da52019-11-30 20:52:27 +01004921 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004922 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004924 *hltab = stl_hltab;
4925 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 for (l = 0; l < itemcnt; l++)
4927 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004928 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004930 sp->start = stl_items[l].stl_start;
4931 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004932 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004935 sp->start = NULL;
4936 sp->userhl = 0;
4937 }
4938
Bram Moolenaarc667da52019-11-30 20:52:27 +01004939 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004940 if (tabtab != NULL)
4941 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004942 *tabtab = stl_tabtab;
4943 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004944 for (l = 0; l < itemcnt; l++)
4945 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004946 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004947 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004948 sp->start = stl_items[l].stl_start;
4949 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004950 sp++;
4951 }
4952 }
4953 sp->start = NULL;
4954 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956
Bram Moolenaarc667da52019-11-30 20:52:27 +01004957 // When inside update_screen we do not want redrawing a stausline, ruler,
4958 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004959 if (updating_screen)
4960 {
4961 must_redraw = save_must_redraw;
4962 curwin->w_redr_type = save_redr_type;
4963 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 return width;
4966}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004967#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004969#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4970 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004972 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4973 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 */
4975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004976get_rel_pos(
4977 win_T *wp,
4978 char_u *buf,
4979 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004981 long above; // number of lines above window
4982 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaarc667da52019-11-30 20:52:27 +01004984 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004985 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 above = wp->w_topline - 1;
4987#ifdef FEAT_DIFF
4988 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004989 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004990 above = 0; // All buffer lines are displayed and there is an
4991 // indication of filler lines, that can be considered
4992 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
4994 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4995 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004996 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4997 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004999 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005001 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 ? (int)(above / ((above + below) / 100L))
5003 : (int)(above * 100L / (above + below)));
5004}
5005#endif
5006
5007/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005008 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 * Return TRUE if it was appended.
5010 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005012append_arg_number(
5013 win_T *wp,
5014 char_u *buf,
5015 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005016 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018 char_u *p;
5019
Bram Moolenaarc667da52019-11-30 20:52:27 +01005020 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 return FALSE;
5022
Bram Moolenaarc667da52019-11-30 20:52:27 +01005023 p = buf + STRLEN(buf); // go to the end of the buffer
5024 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 return FALSE;
5026 *p++ = ' ';
5027 *p++ = '(';
5028 if (add_file)
5029 {
5030 STRCPY(p, "file ");
5031 p += 5;
5032 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005033 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5034 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5036 return TRUE;
5037}
5038
5039/*
5040 * If fname is not a full path, make it a full path.
5041 * Returns pointer to allocated memory (NULL for failure).
5042 */
5043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005044fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046 /*
5047 * Force expanding the path always for Unix, because symbolic links may
5048 * mess up the full path name, even though it starts with a '/'.
5049 * Also expand when there is ".." in the file name, try to remove it,
5050 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005051 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * For MS-Windows also expand names like "longna~1" to "longname".
5053 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005054#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 return FullName_save(fname, TRUE);
5056#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005057 if (!vim_isAbsName(fname)
5058 || strstr((char *)fname, "..") != NULL
5059 || strstr((char *)fname, "//") != NULL
5060# ifdef BACKSLASH_IN_FILENAME
5061 || strstr((char *)fname, "\\\\") != NULL
5062# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005063# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 )
5067 return FullName_save(fname, FALSE);
5068
5069 fname = vim_strsave(fname);
5070
Bram Moolenaar9b942202007-10-03 12:31:33 +00005071# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005072 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005073 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
5076 return fname;
5077#endif
5078}
5079
5080/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005081 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5082 * "*ffname" becomes a pointer to allocated memory (or NULL).
5083 * When resolving a link both "*sfname" and "*ffname" will point to the same
5084 * allocated memory.
5085 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005086 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005089fname_expand(
5090 buf_T *buf UNUSED,
5091 char_u **ffname,
5092 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005094 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005096 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005098 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100#ifdef FEAT_SHORTCUT
5101 if (!buf->b_p_bin)
5102 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005103 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005105 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005106 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005107 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
5109 vim_free(*ffname);
5110 *ffname = rfname;
5111 *sfname = rfname;
5112 }
5113 }
5114#endif
5115}
5116
5117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 * Open a window for a number of buffers.
5119 */
5120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005121ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 buf_T *buf;
5124 win_T *wp, *wpnext;
5125 int split_ret = OK;
5126 int p_ea_save;
5127 int open_wins = 0;
5128 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005129 int count; // Maximum number of windows to open.
5130 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005131 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005132 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaarc667da52019-11-30 20:52:27 +01005134 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 count = 9999;
5136 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005137 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5139 all = FALSE;
5140 else
5141 all = TRUE;
5142
5143 setpcmark();
5144
5145#ifdef FEAT_GUI
5146 need_mouse_correct = TRUE;
5147#endif
5148
5149 /*
5150 * Close superfluous windows (two windows for the same buffer).
5151 * Also close windows that are not full-width.
5152 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005153 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005154 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005155 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005157 tpnext = curtab->tp_next;
5158 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005160 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005161 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1004402020-10-24 20:49:43 +02005162 || ((cmdmod.cmod_split & WSP_VERT)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005163 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005164 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005165 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005166 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005167 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005168 {
5169 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005170 wpnext = firstwin; // just in case an autocommand does
5171 // something strange with windows
5172 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005173 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005174 }
5175 else
5176 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005178
Bram Moolenaarc667da52019-11-30 20:52:27 +01005179 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005180 if (had_tab == 0 || tpnext == NULL)
5181 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005182 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184
5185 /*
5186 * Go through the buffer list. When a buffer doesn't have a window yet,
5187 * open one. Otherwise move the window to the right position.
5188 * Watch out for autocommands that delete buffers or windows!
5189 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005190 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5195 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005196 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5198 continue;
5199
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005200 if (had_tab != 0)
5201 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005202 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005203 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005204 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005205 else
5206 wp = NULL;
5207 }
5208 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005209 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005210 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005211 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005212 if (wp->w_buffer == buf)
5213 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005214 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005215 if (wp != NULL)
5216 win_move_after(wp, curwin);
5217 }
5218
5219 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005221 bufref_T bufref;
5222
5223 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005224
Bram Moolenaarc667da52019-11-30 20:52:27 +01005225 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005227 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5229 ++open_wins;
5230 p_ea = p_ea_save;
5231 if (split_ret == FAIL)
5232 continue;
5233
Bram Moolenaarc667da52019-11-30 20:52:27 +01005234 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005237 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005239 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 break;
5242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (swap_exists_action == SEA_QUIT)
5244 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005245#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005246 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005247
Bram Moolenaarc667da52019-11-30 20:52:27 +01005248 // Reset the error/interrupt/exception state here so that
5249 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005250 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005251#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005252
Bram Moolenaarc667da52019-11-30 20:52:27 +01005253 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 win_close(curwin, TRUE);
5255 --open_wins;
5256 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005257 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005258
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005259#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005260 // Restore the error/interrupt/exception state if not
5261 // discarded by a new aborting error, interrupt, or uncaught
5262 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005263 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 else
5267 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269
5270 ui_breakcheck();
5271 if (got_int)
5272 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005273 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 break;
5275 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005276#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005277 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005278 if (aborting())
5279 break;
5280#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005281 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005282 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005283 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005286 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * Close superfluous windows.
5291 */
5292 for (wp = lastwin; open_wins > count; )
5293 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005294 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (!win_valid(wp))
5297 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005298 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 wp = lastwin;
5300 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005301 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005303 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 --open_wins;
5305 wp = lastwin;
5306 }
5307 else
5308 {
5309 wp = wp->w_prev;
5310 if (wp == NULL)
5311 break;
5312 }
5313 }
5314}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005317static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319/*
5320 * do_modelines() - process mode lines for the current file
5321 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005322 * "flags" can be:
5323 * OPT_WINONLY only set options local to window
5324 * OPT_NOWIN don't set options local to window
5325 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 * Returns immediately if the "ml" option isn't set.
5327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005329do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005331 linenr_T lnum;
5332 int nmlines;
5333 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5336 return;
5337
Bram Moolenaarc667da52019-11-30 20:52:27 +01005338 // Disallow recursive entry here. Can happen when executing a modeline
5339 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 if (entered)
5341 return;
5342
5343 ++entered;
5344 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5345 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005346 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 nmlines = 0;
5348
5349 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5350 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005351 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 nmlines = 0;
5353 --entered;
5354}
5355
Bram Moolenaarc667da52019-11-30 20:52:27 +01005356#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358/*
5359 * chk_modeline() - check a single line for a mode string
5360 * Return FAIL if an error encountered.
5361 */
5362 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005363chk_modeline(
5364 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005365 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 char_u *s;
5368 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005369 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 int prev;
5371 int vers;
5372 int end;
5373 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005374 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005375
Bram Moolenaare31ee862020-01-07 20:59:34 +01005376 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
5378 prev = -1;
5379 for (s = ml_get(lnum); *s != NUL; ++s)
5380 {
5381 if (prev == -1 || vim_isspace(prev))
5382 {
5383 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5384 || STRNCMP(s, "vi:", (size_t)3) == 0)
5385 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005386 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005387 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
5389 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5390 e = s + 4;
5391 else
5392 e = s + 3;
5393 vers = getdigits(&e);
5394 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005395 && (s[0] != 'V'
5396 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 && (s[3] == ':'
5398 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5399 || (VIM_VERSION_100 < vers && s[3] == '<')
5400 || (VIM_VERSION_100 > vers && s[3] == '>')
5401 || (VIM_VERSION_100 == vers && s[3] == '=')))
5402 break;
5403 }
5404 }
5405 prev = *s;
5406 }
5407
5408 if (*s)
5409 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005410 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 ++s;
5412 while (s[-1] != ':');
5413
Bram Moolenaarc667da52019-11-30 20:52:27 +01005414 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (linecopy == NULL)
5416 return FAIL;
5417
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005418 // prepare for emsg()
5419 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005420 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 end = FALSE;
5423 while (end == FALSE)
5424 {
5425 s = skipwhite(s);
5426 if (*s == NUL)
5427 break;
5428
5429 /*
5430 * Find end of set command: ':' or end of line.
5431 * Skip over "\:", replacing it with ":".
5432 */
5433 for (e = s; *e != ':' && *e != NUL; ++e)
5434 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005435 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 if (*e == NUL)
5437 end = TRUE;
5438
5439 /*
5440 * If there is a "set" command, require a terminating ':' and
5441 * ignore the stuff after the ':'.
5442 * "vi:set opt opt opt: foo" -- foo not interpreted
5443 * "vi:opt opt opt: foo" -- foo interpreted
5444 * Accept "se" for compatibility with Elvis.
5445 */
5446 if (STRNCMP(s, "set ", (size_t)4) == 0
5447 || STRNCMP(s, "se ", (size_t)3) == 0)
5448 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005449 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 break;
5451 end = TRUE;
5452 s = vim_strchr(s, ' ') + 1;
5453 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005454 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaarc667da52019-11-30 20:52:27 +01005456 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005458 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005459
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005460 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005461 current_sctx.sc_version = 1;
5462#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005463 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005464 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005465 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005467
Bram Moolenaar5958f952018-11-20 04:25:21 +01005468 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005469 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005470
Bram Moolenaara3227e22006-03-08 21:32:40 +00005471 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005472
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005473 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005474 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005475 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 break;
5477 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005478 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480
Bram Moolenaare31ee862020-01-07 20:59:34 +01005481 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005482 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 vim_free(linecopy);
5484 }
5485 return retval;
5486}
5487
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005488/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005489 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5490 */
5491 int
5492bt_normal(buf_T *buf)
5493{
5494 return buf != NULL && buf->b_p_bt[0] == NUL;
5495}
5496
Bram Moolenaar113e1072019-01-20 15:30:40 +01005497#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005498/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005499 * Return TRUE if "buf" is the quickfix buffer.
5500 */
5501 int
5502bt_quickfix(buf_T *buf)
5503{
5504 return buf != NULL && buf->b_p_bt[0] == 'q';
5505}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005506#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005507
Bram Moolenaar113e1072019-01-20 15:30:40 +01005508#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005509/*
5510 * Return TRUE if "buf" is a terminal buffer.
5511 */
5512 int
5513bt_terminal(buf_T *buf)
5514{
5515 return buf != NULL && buf->b_p_bt[0] == 't';
5516}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005517#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005518
5519/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005520 * Return TRUE if "buf" is a help buffer.
5521 */
5522 int
5523bt_help(buf_T *buf)
5524{
5525 return buf != NULL && buf->b_help;
5526}
5527
5528/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005529 * Return TRUE if "buf" is a prompt buffer.
5530 */
5531 int
5532bt_prompt(buf_T *buf)
5533{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005534 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5535}
5536
5537/*
5538 * Return TRUE if "buf" is a buffer for a popup window.
5539 */
5540 int
5541bt_popup(buf_T *buf)
5542{
5543 return buf != NULL && buf->b_p_bt != NULL
5544 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005545}
5546
5547/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005548 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5549 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005550 */
5551 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005552bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005553{
5554 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5555 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005556 || buf->b_p_bt[0] == 't'
5557 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005558}
5559
5560/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005561 * Return TRUE if "buf" has 'buftype' set to "nofile".
5562 */
5563 int
5564bt_nofile(buf_T *buf)
5565{
5566 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5567}
5568
5569/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005570 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5571 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005572 */
5573 int
5574bt_dontwrite(buf_T *buf)
5575{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005576 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005577 || buf->b_p_bt[0] == 't'
5578 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005579}
5580
Bram Moolenaar113e1072019-01-20 15:30:40 +01005581#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005582 int
5583bt_dontwrite_msg(buf_T *buf)
5584{
5585 if (bt_dontwrite(buf))
5586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005588 return TRUE;
5589 }
5590 return FALSE;
5591}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005592#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005593
5594/*
5595 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5596 * and 'bufhidden'.
5597 */
5598 int
5599buf_hide(buf_T *buf)
5600{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005601 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005602 switch (buf->b_p_bh[0])
5603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005604 case 'u': // "unload"
5605 case 'w': // "wipe"
5606 case 'd': return FALSE; // "delete"
5607 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005608 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005609 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612/*
5613 * Return special buffer name.
5614 * Returns NULL when the buffer has a normal file name.
5615 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005616 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005617buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005619#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005621 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005622 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005623 * Differentiate between the quickfix and location list buffers using
5624 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005625 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005626 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005627 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005628 else
5629 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005632
Bram Moolenaarc667da52019-11-30 20:52:27 +01005633 // There is no _file_ when 'buftype' is "nofile", b_sfname
5634 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005635 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005637#ifdef FEAT_TERMINAL
5638 if (buf->b_term != NULL)
5639 return term_get_status_text(buf->b_term);
5640#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005641 if (buf->b_fname != NULL)
5642 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005643#ifdef FEAT_JOB_CHANNEL
5644 if (bt_prompt(buf))
5645 return (char_u *)_("[Prompt]");
5646#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005647#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005648 if (bt_popup(buf))
5649 return (char_u *)_("[Popup]");
5650#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005651 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005655 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return NULL;
5657}
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005660 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5661 */
5662 char_u *
5663buf_get_fname(buf_T *buf)
5664{
5665 if (buf->b_fname == NULL)
5666 return (char_u *)_("[No Name]");
5667 return buf->b_fname;
5668}
5669
5670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5672 */
5673 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005674set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 if (on != curbuf->b_p_bl)
5677 {
5678 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (on)
5680 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5681 else
5682 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684}
5685
5686/*
5687 * Read the file for "buf" again and check if the contents changed.
5688 * Return TRUE if it changed or this could not be checked.
5689 */
5690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005691buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 buf_T *newbuf;
5694 int differ = TRUE;
5695 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 exarg_T ea;
5698
Bram Moolenaarc667da52019-11-30 20:52:27 +01005699 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5701 if (newbuf == NULL)
5702 return TRUE;
5703
Bram Moolenaarc667da52019-11-30 20:52:27 +01005704 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 if (prep_exarg(&ea, buf) == FAIL)
5706 {
5707 wipe_buffer(newbuf, FALSE);
5708 return TRUE;
5709 }
5710
Bram Moolenaarc667da52019-11-30 20:52:27 +01005711 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
Bram Moolenaar4770d092006-01-12 23:22:24 +00005714 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 && readfile(buf->b_ffname, buf->b_fname,
5716 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5717 &ea, READ_NEW | READ_DUMMY) == OK)
5718 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005719 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5721 {
5722 differ = FALSE;
5723 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5724 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5725 {
5726 differ = TRUE;
5727 break;
5728 }
5729 }
5730 }
5731 vim_free(ea.cmd);
5732
Bram Moolenaarc667da52019-11-30 20:52:27 +01005733 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
Bram Moolenaarc667da52019-11-30 20:52:27 +01005736 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 wipe_buffer(newbuf, FALSE);
5738
5739 return differ;
5740}
5741
5742/*
5743 * Wipe out a buffer and decrement the last buffer number if it was used for
5744 * this buffer. Call this to wipe out a temp buffer that does not contain any
5745 * marks.
5746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005748wipe_buffer(
5749 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005750 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751{
5752 if (buf->b_fnum == top_file_num - 1)
5753 --top_file_num;
5754
Bram Moolenaarc667da52019-11-30 20:52:27 +01005755 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005756 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005757
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005758 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005761 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762}