blob: ca1fd36efe0ab9058345febcd256765e2535ad2f [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 Moolenaar797e63b2021-01-15 16:22:52 +0100495 *
496 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100499close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100500 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100501 buf_T *buf,
502 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100503 int abort_if_last,
504 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100507 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200508 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100509 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200510 win_T *the_curwin = curwin;
511 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200513 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
514 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaarcee52202020-03-11 14:19:58 +0100516 CHECK_CURBUF;
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200517 /*
518 * Force unloading or deleting when 'bufhidden' says so.
519 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
520 * "hide" (otherwise we could never free or delete a buffer).
521 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100522 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200523 {
524 del_buf = TRUE;
525 unload_buf = TRUE;
526 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100527 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200528 {
529 del_buf = TRUE;
530 unload_buf = TRUE;
531 wipe_buf = TRUE;
532 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100533 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200534 unload_buf = TRUE;
535
Bram Moolenaar94053a52017-08-01 21:44:33 +0200536#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200537 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200538 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100539 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200540 if (term_job_running(buf->b_term))
541 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200542 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200543 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200544 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100545 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200546
Bram Moolenaarc667da52019-11-30 20:52:27 +0100547 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200548 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200549 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200550 else
551 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100552 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200553 del_buf = FALSE;
554 unload_buf = FALSE;
555 }
556 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100557 else if (buf->b_p_bh[0] == 'h' && !del_buf)
558 {
559 // Hide a terminal buffer.
560 unload_buf = FALSE;
561 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200562 else
563 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100564 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200565 del_buf = TRUE;
566 unload_buf = TRUE;
567 wipe_buf = TRUE;
568 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100569 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200570 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaarc667da52019-11-30 20:52:27 +0100573 // Disallow deleting the buffer when it is locked (already being closed or
574 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200575 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100576 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200577
Bram Moolenaarc667da52019-11-30 20:52:27 +0100578 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200579 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100581 // Set b_last_cursor when closing the last window for the buffer.
582 // Remember the last cursor position and window options of the buffer.
583 // This used to be only for the current window, but then options like
584 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (buf->b_nwindows == 1)
586 set_last_cursor(win);
587 buflist_setfpos(buf, win,
588 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
589 win->w_cursor.col, TRUE);
590 }
591
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200592 set_bufref(&bufref, buf);
593
Bram Moolenaarc667da52019-11-30 20:52:27 +0100594 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (buf->b_nwindows == 1)
596 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200597 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100598 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200599 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
600 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200601 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100602 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100603 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200604aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100605 emsg(_(e_auabort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100606 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100607 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200608 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100609 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200610 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100611 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200612 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 // When the buffer becomes hidden, but is not unloaded, trigger
615 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (!unload_buf)
617 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200618 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100619 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200620 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
621 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200622 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100623 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200624 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200625 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100626 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200627 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100628 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200629 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100631#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100632 // autocmds may abort script processing
633 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100634 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200637
Bram Moolenaarc667da52019-11-30 20:52:27 +0100638 // If the buffer was in curwin and the window has changed, go back to that
639 // window, if it still exists. This avoids that ":edit x" triggering a
640 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200641 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
642 {
643 block_autocmds();
644 goto_tabpage_win(the_curtab, the_curwin);
645 unblock_autocmds();
646 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200647
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaarc667da52019-11-30 20:52:27 +0100650 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (buf->b_nwindows > 0)
652 --buf->b_nwindows;
653
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100654#ifdef FEAT_DIFF
655 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100656 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100657#endif
658
Bram Moolenaarc667da52019-11-30 20:52:27 +0100659 // Return when a window is displaying the buffer or when it's not
660 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100662 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaarc667da52019-11-30 20:52:27 +0100664 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if (buf->b_ffname == NULL)
666 del_buf = TRUE;
667
Bram Moolenaarc667da52019-11-30 20:52:27 +0100668 // When closing the current buffer stop Visual mode before freeing
669 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200670 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200671#if defined(EXITFREE)
672 && !entered_free_all_mem
673#endif
674 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200675 end_visual_mode();
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /*
678 * Free all things allocated for this buffer.
679 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
680 */
Bram Moolenaarc667da52019-11-30 20:52:27 +0100681 // Remember if we are closing the current buffer. Restore the number of
682 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 is_curbuf = (buf == curbuf);
684 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100686 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100687 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100688 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
Bram Moolenaarc667da52019-11-30 20:52:27 +0100690 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200691 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100692 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100693#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100694 // autocmds may abort script processing
695 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100696 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 /*
700 * It's possible that autocommands change curbuf to the one being deleted.
701 * This might cause the previous curbuf to be deleted unexpectedly. But
702 * in some cases it's OK to delete the curbuf, because a new one is
703 * obtained anyway. Therefore only return if curbuf changed to the
704 * deleted buffer.
705 */
706 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100707 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200708
Bram Moolenaar4033c552017-09-16 20:54:51 +0200709 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100710 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200711
Bram Moolenaarc667da52019-11-30 20:52:27 +0100712 // Autocommands may have opened or closed windows for this buffer.
713 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200714 if (buf->b_nwindows > 0)
715 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 /*
718 * Remove the buffer from the list.
719 */
720 if (wipe_buf)
721 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200722 if (action == DOBUF_WIPE_REUSE)
723 {
724 // we can re-use this buffer number, store it
725 if (buf_reuse.ga_itemsize == 0)
726 ga_init2(&buf_reuse, sizeof(int), 50);
727 if (ga_grow(&buf_reuse, 1) == OK)
728 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
729 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200730 if (buf->b_sfname != buf->b_ffname)
731 VIM_CLEAR(buf->b_sfname);
732 else
733 buf->b_sfname = NULL;
734 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (buf->b_prev == NULL)
736 firstbuf = buf->b_next;
737 else
738 buf->b_prev->b_next = buf->b_next;
739 if (buf->b_next == NULL)
740 lastbuf = buf->b_prev;
741 else
742 buf->b_next->b_prev = buf->b_prev;
743 free_buffer(buf);
744 }
745 else
746 {
747 if (del_buf)
748 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100749 // Free all internal variables and reset option values, to make
750 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 free_buffer_stuff(buf, TRUE);
752
Bram Moolenaarc667da52019-11-30 20:52:27 +0100753 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
755
Bram Moolenaarc667da52019-11-30 20:52:27 +0100756 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 buf->b_p_initialized = FALSE;
758 }
759 buf_clear_file(buf);
760 if (del_buf)
761 buf->b_p_bl = FALSE;
762 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100763 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100764 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765}
766
767/*
768 * Make buffer not contain a file.
769 */
770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100771buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772{
773 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200774 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 buf->b_p_eol = TRUE;
777 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000779 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100781 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#ifdef FEAT_NETBEANS_INTG
783 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#endif
785}
786
787/*
788 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200789 * the file. Careful: get here with "curwin" NULL when exiting.
790 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100791 * BFA_DEL buffer is going to be deleted
792 * BFA_WIPE buffer is going to be wiped out
793 * BFA_KEEP_UNDO do not free undo information
794 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200800 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200801 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200802 win_T *the_curwin = curwin;
803 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaarc667da52019-11-30 20:52:27 +0100805 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200806 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100807 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200808 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200809 if (buf->b_ml.ml_mfp != NULL)
810 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200811 if (apply_autocmds(EVENT_BUFUNLOAD, 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 Moolenaarc67e8922016-05-24 16:07:40 +0200815 return;
816 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200817 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200819 if (apply_autocmds(EVENT_BUFDELETE, 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 Moolenaar59f931e2010-07-24 20:27:03 +0200825 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200827 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
828 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200829 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100830 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 return;
832 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200833 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100834 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200835
Bram Moolenaarc667da52019-11-30 20:52:27 +0100836 // If the buffer was in curwin and the window has changed, go back to that
837 // window, if it still exists. This avoids that ":edit x" triggering a
838 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200839 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
840 {
841 block_autocmds();
842 goto_tabpage_win(the_curtab, the_curwin);
843 unblock_autocmds();
844 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200845
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100846#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100847 // autocmds may abort script processing
848 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852 /*
853 * It's possible that autocommands change curbuf to the one being deleted.
854 * This might cause curbuf to be deleted unexpectedly. But in some cases
855 * it's OK to delete the curbuf, because a new one is obtained anyway.
856 * Therefore only return if curbuf changed to the deleted buffer.
857 */
858 if (buf == curbuf && !is_curbuf)
859 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100861 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200863#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100864 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200865 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100866 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200867#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000868
869#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100870 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000871 {
872 win_T *win;
873 tabpage_T *tp;
874
875 FOR_ALL_TAB_WINDOWS(tp, win)
876 if (win->w_buffer == buf)
877 clearFolding(win);
878 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000879#endif
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#ifdef FEAT_TCL
882 tcl_buffer_free(buf);
883#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100884 ml_close(buf, TRUE); // close and delete the memline/memfile
885 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200886 if ((flags & BFA_KEEP_UNDO) == 0)
887 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100888 u_blockfree(buf); // free the memory allocated for undo
889 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100892 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100894#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100895 clear_buf_prop_types(buf);
896#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100897 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898}
899
900/*
901 * Free a buffer structure and the things it contains related to the buffer
902 * itself (not the file, that must have been done already).
903 */
904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100905free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200907 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200909#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100910 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200911 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200912 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200913 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200914#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200915#ifdef FEAT_LUA
916 lua_buffer_free(buf);
917#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000918#ifdef FEAT_MZSCHEME
919 mzscheme_buffer_free(buf);
920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#ifdef FEAT_PERL
922 perl_buf_free(buf);
923#endif
924#ifdef FEAT_PYTHON
925 python_buffer_free(buf);
926#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200927#ifdef FEAT_PYTHON3
928 python3_buffer_free(buf);
929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#ifdef FEAT_RUBY
931 ruby_buffer_free(buf);
932#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200933#ifdef FEAT_JOB_CHANNEL
934 channel_buffer_free(buf);
935#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200936#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200937 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200938#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200939#ifdef FEAT_JOB_CHANNEL
940 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200941 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200942 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200943#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200944
945 buf_hashtab_remove(buf);
946
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200947 aubuflocal_remove(buf);
948
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200949 if (autocmd_busy)
950 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100951 // Do not free the buffer structure while autocommands are executing,
952 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200953 buf->b_next = au_pending_free_buf;
954 au_pending_free_buf = buf;
955 }
956 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100957 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200958 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100959 if (curbuf == buf)
960 curbuf = NULL; // make clear it's not to be used
961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100965 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100966 */
967 static void
968init_changedtick(buf_T *buf)
969{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100970 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100971
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100972 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
973 di->di_tv.v_type = VAR_NUMBER;
974 di->di_tv.v_lock = VAR_FIXED;
975 di->di_tv.vval.v_number = 0;
976
Bram Moolenaar92769c32017-02-25 15:41:37 +0100977#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100978 STRCPY(buf->b_ct_di.di_key, "changedtick");
979 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100980#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100981}
982
983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
985 */
986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100987free_buffer_stuff(
988 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100989 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
991 if (free_options)
992 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100993 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200995#ifdef FEAT_SPELL
996 ga_clear(&buf->b_s.b_langp);
997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
999#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001000 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001001 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001002
Bram Moolenaarc667da52019-11-30 20:52:27 +01001003 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001004 hash_init(&buf->b_vars->dv_hashtab);
1005 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001006 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001007 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001010 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001012 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001014#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001015 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001016#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001017 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1018 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001019 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020}
1021
1022/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001023 * Free one wininfo_T.
1024 */
1025 void
1026free_wininfo(wininfo_T *wip)
1027{
1028 if (wip->wi_optset)
1029 {
1030 clear_winopt(&wip->wi_opt);
1031#ifdef FEAT_FOLDING
1032 deleteFoldRecurse(&wip->wi_folds);
1033#endif
1034 }
1035 vim_free(wip);
1036}
1037
1038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 * Free the b_wininfo list for buffer "buf".
1040 */
1041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001042clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 wininfo_T *wip;
1045
1046 while (buf->b_wininfo != NULL)
1047 {
1048 wip = buf->b_wininfo;
1049 buf->b_wininfo = wip->wi_next;
Bram Moolenaar4882d982020-10-25 17:55:09 +01001050 free_wininfo(wip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 }
1052}
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054/*
1055 * Go to another buffer. Handles the result of the ATTENTION dialog.
1056 */
1057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001058goto_buffer(
1059 exarg_T *eap,
1060 int start,
1061 int dir,
1062 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001064 bufref_T old_curbuf;
1065
1066 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1070 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1072 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001073#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001075
Bram Moolenaarc667da52019-11-30 20:52:27 +01001076 // Reset the error/interrupt/exception state here so that
1077 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001078 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001079#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001080
Bram Moolenaarc667da52019-11-30 20:52:27 +01001081 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 win_close(curwin, TRUE);
1083 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001084 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001085
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001086#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001087 // Restore the error/interrupt/exception state if not discarded by a
1088 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001089 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001093 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001094}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096/*
1097 * Handle the situation of swap_exists_action being set.
1098 * It is allowed for "old_curbuf" to be NULL or invalid.
1099 */
1100 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001103#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001104 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001105#endif
1106#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001107 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001108#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001109 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (swap_exists_action == SEA_QUIT)
1112 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001113#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001114 // Reset the error/interrupt/exception state here so that
1115 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001116 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001118
Bram Moolenaarc667da52019-11-30 20:52:27 +01001119 // User selected Quit at ATTENTION prompt. Go back to previous
1120 // buffer. If that buffer is gone or the same as the current one,
1121 // open a new, empty buffer.
1122 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001123 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001124 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001125 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1126 || old_curbuf->br_buf == curbuf)
1127 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1128 else
1129 buf = old_curbuf->br_buf;
1130 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001131 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001132 int old_msg_silent = msg_silent;
1133
1134 if (shortmess(SHM_FILEINFO))
1135 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001136 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001137 // restore msg_silent, so that the command line will be shown
1138 msg_silent = old_msg_silent;
1139
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001140#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001141 if (old_tw != curbuf->b_p_tw)
1142 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001143#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001144 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001145 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001146
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001147#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001148 // Restore the error/interrupt/exception state if not discarded by a
1149 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001150 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153 else if (swap_exists_action == SEA_RECOVER)
1154 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001155#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001156 // Reset the error/interrupt/exception state here so that
1157 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001159#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160
Bram Moolenaarc667da52019-11-30 20:52:27 +01001161 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001163 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001164 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001166 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001167
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001168#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001169 // Restore the error/interrupt/exception state if not discarded by a
1170 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001171 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
1174 swap_exists_action = SEA_NONE;
1175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177/*
1178 * do_bufdel() - delete or unload buffer(s)
1179 *
1180 * addr_count == 0: ":bdel" - delete current buffer
1181 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1182 * buffer "end_bnr", then any other arguments.
1183 * addr_count == 2: ":N,N bdel" - delete buffers in range
1184 *
1185 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1186 * DOBUF_DEL (":bdel")
1187 *
1188 * Returns error message or NULL
1189 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001190 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001191do_bufdel(
1192 int command,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001193 char_u *arg, // pointer to extra arguments
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194 int addr_count,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001195 int start_bnr, // first buffer number in a range
1196 int end_bnr, // buffer nr or last buffer nr in a range
Bram Moolenaar7454a062016-01-30 15:14:10 +01001197 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
Bram Moolenaarc667da52019-11-30 20:52:27 +01001199 int do_current = 0; // delete current buffer?
1200 int deleted = 0; // number of buffers deleted
1201 char *errormsg = NULL; // return value
1202 int bnr; // buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 char_u *p;
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (addr_count == 0)
1206 {
1207 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1208 }
1209 else
1210 {
1211 if (addr_count == 2)
1212 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001213 if (*arg) // both range and argument is not allowed
Bram Moolenaar8930caa2020-07-23 16:37:03 +02001214 return ex_errmsg(e_trailing_arg, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 bnr = start_bnr;
1216 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001217 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 bnr = end_bnr;
1219
1220 for ( ;!got_int; ui_breakcheck())
1221 {
1222 /*
1223 * delete the current buffer last, otherwise when the
1224 * current buffer is deleted, the next buffer becomes
1225 * the current one and will be loaded, which may then
1226 * also be deleted, etc.
1227 */
1228 if (bnr == curbuf->b_fnum)
1229 do_current = bnr;
1230 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1231 forceit) == OK)
1232 ++deleted;
1233
1234 /*
1235 * find next buffer number to delete/unload
1236 */
1237 if (addr_count == 2)
1238 {
1239 if (++bnr > end_bnr)
1240 break;
1241 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001242 else // addr_count == 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 arg = skipwhite(arg);
1245 if (*arg == NUL)
1246 break;
1247 if (!VIM_ISDIGIT(*arg))
1248 {
1249 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001250 bnr = buflist_findpat(arg, p,
1251 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001252 FALSE, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001253 if (bnr < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 break;
1255 arg = p;
1256 }
1257 else
1258 bnr = getdigits(&arg);
1259 }
1260 }
1261 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1262 FORWARD, do_current, forceit) == OK)
1263 ++deleted;
1264
1265 if (deleted == 0)
1266 {
1267 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001268 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001270 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001272 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001273 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 else if (deleted >= p_report)
1276 {
1277 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001278 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001279 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001281 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001282 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001284 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001285 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 }
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
1290 return errormsg;
1291}
1292
Bram Moolenaara02471e2014-01-10 16:43:14 +01001293/*
1294 * Make the current buffer empty.
1295 * Used when it is wiped out and it's the last buffer.
1296 */
1297 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001298empty_curbuf(
1299 int close_others,
1300 int forceit,
1301 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001302{
1303 int retval;
1304 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001305 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001306
1307 if (action == DOBUF_UNLOAD)
1308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001309 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001310 return FAIL;
1311 }
1312
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001313 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001314 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001315 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001316 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001317
1318 setpcmark();
1319 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1320 forceit ? ECMD_FORCEIT : 0, curwin);
1321
1322 /*
1323 * do_ecmd() may create a new buffer, then we have to delete
1324 * the old one. But do_ecmd() may have done that already, check
1325 * if the buffer still exists.
1326 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001327 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001328 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001329 if (!close_others)
1330 need_fileinfo = FALSE;
1331 return retval;
1332}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334/*
1335 * Implementation of the commands for the buffer list.
1336 *
1337 * action == DOBUF_GOTO go to specified buffer
1338 * action == DOBUF_SPLIT split window and go to specified buffer
1339 * action == DOBUF_UNLOAD unload specified buffer(s)
1340 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1341 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001342 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 *
1344 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1345 * start == DOBUF_FIRST go to "count" buffer from first buffer
1346 * start == DOBUF_LAST go to "count" buffer from last buffer
1347 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1348 *
1349 * Return FAIL or OK.
1350 */
1351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001352do_buffer(
1353 int action,
1354 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001355 int dir, // FORWARD or BACKWARD
1356 int count, // buffer number or number of buffers
1357 int forceit) // TRUE for :...!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359 buf_T *buf;
1360 buf_T *bp;
1361 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001362 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364 switch (start)
1365 {
1366 case DOBUF_FIRST: buf = firstbuf; break;
1367 case DOBUF_LAST: buf = lastbuf; break;
1368 default: buf = curbuf; break;
1369 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001370 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
1372 while (count-- > 0)
1373 {
1374 do
1375 {
1376 buf = buf->b_next;
1377 if (buf == NULL)
1378 buf = firstbuf;
1379 }
1380 while (buf != curbuf && !bufIsChanged(buf));
1381 }
1382 if (!bufIsChanged(buf))
1383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001384 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 return FAIL;
1386 }
1387 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001388 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 while (buf != NULL && buf->b_fnum != count)
1391 buf = buf->b_next;
1392 }
1393 else
1394 {
1395 bp = NULL;
1396 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1397 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001398 // remember the buffer where we start, we come back there when all
1399 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (bp == NULL)
1401 bp = buf;
1402 if (dir == FORWARD)
1403 {
1404 buf = buf->b_next;
1405 if (buf == NULL)
1406 buf = firstbuf;
1407 }
1408 else
1409 {
1410 buf = buf->b_prev;
1411 if (buf == NULL)
1412 buf = lastbuf;
1413 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001414 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (unload || buf->b_p_bl)
1416 {
1417 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001418 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 if (bp == buf)
1421 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001422 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001423 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 return FAIL;
1425 }
1426 }
1427 }
1428
Bram Moolenaarc667da52019-11-30 20:52:27 +01001429 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
1431 if (start == DOBUF_FIRST)
1432 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001433 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001435 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001438 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001440 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 return FAIL;
1442 }
1443
1444#ifdef FEAT_GUI
1445 need_mouse_correct = TRUE;
1446#endif
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 /*
1449 * delete buffer buf from memory and/or the list
1450 */
1451 if (unload)
1452 {
1453 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001454 bufref_T bufref;
1455
Bram Moolenaar94f01952018-09-01 15:30:03 +02001456 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001457 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001458
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001459 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaarc667da52019-11-30 20:52:27 +01001461 // When unloading or deleting a buffer that's already unloaded and
1462 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001463 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1464 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 return FAIL;
1466
1467 if (!forceit && bufIsChanged(buf))
1468 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001469#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001470 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
1472 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001473 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001474 // Autocommand deleted buffer, oops! It's not changed
1475 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001477 // If it's still changed fail silently, the dialog already
1478 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001479 if (bufIsChanged(buf))
1480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001482 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001485 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 buf->b_fnum);
1487 return FAIL;
1488 }
1489 }
1490
Bram Moolenaarc667da52019-11-30 20:52:27 +01001491 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001492 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001493 end_visual_mode();
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 /*
1496 * If deleting the last (listed) buffer, make it empty.
1497 * The last (listed) buffer cannot be unloaded.
1498 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001499 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (bp->b_p_bl && bp != buf)
1501 break;
1502 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001503 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 /*
1506 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001507 * (unless it's the only window). Repeat this so long as we end up in
1508 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001510 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001511 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001512 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001513 {
1514 if (win_close(curwin, FALSE) == FAIL)
1515 break;
1516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518 /*
1519 * If the buffer to be deleted is not the current one, delete it here.
1520 */
1521 if (buf != curbuf)
1522 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001523 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001524 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001525 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 return OK;
1527 }
1528
1529 /*
1530 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001531 * There should be another, otherwise it would have been handled
1532 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001533 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 * Then prefer the buffer we most recently visited.
1535 * Else try to find one that is loaded, after the current buffer,
1536 * then before the current buffer.
1537 * Finally use any buffer.
1538 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001539 buf = NULL; // selected buffer
1540 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001541 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1542 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001544 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
1546 int jumpidx;
1547
1548 jumpidx = curwin->w_jumplistidx - 1;
1549 if (jumpidx < 0)
1550 jumpidx = curwin->w_jumplistlen - 1;
1551
1552 forward = jumpidx;
1553 while (jumpidx != curwin->w_jumplistidx)
1554 {
1555 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1556 if (buf != NULL)
1557 {
1558 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001559 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 else if (buf->b_ml.ml_mfp == NULL)
1561 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001562 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (bp == NULL)
1564 bp = buf;
1565 buf = NULL;
1566 }
1567 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001568 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001570 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1572 break;
1573 if (--jumpidx < 0)
1574 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 break;
1577 }
1578 }
1579#endif
1580
Bram Moolenaarc667da52019-11-30 20:52:27 +01001581 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 forward = TRUE;
1584 buf = curbuf->b_next;
1585 for (;;)
1586 {
1587 if (buf == NULL)
1588 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001589 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 break;
1591 buf = curbuf->b_prev;
1592 forward = FALSE;
1593 continue;
1594 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001595 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1597 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001598 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001600 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 bp = buf;
1602 }
1603 if (forward)
1604 buf = buf->b_next;
1605 else
1606 buf = buf->b_prev;
1607 }
1608 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001609 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001611 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001613 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (buf->b_p_bl && buf != curbuf)
1615 break;
1616 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001617 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 if (curbuf->b_next != NULL)
1620 buf = curbuf->b_next;
1621 else
1622 buf = curbuf->b_prev;
1623 }
1624 }
1625
Bram Moolenaara02471e2014-01-10 16:43:14 +01001626 if (buf == NULL)
1627 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001628 // Autocommands must have wiped out all other buffers. Only option
1629 // now is to make the current buffer empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001630 return empty_curbuf(FALSE, forceit, action);
1631 }
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /*
1634 * make buf current buffer
1635 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001636 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001638 // If 'switchbuf' contains "useopen": jump to first window containing
1639 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001640 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001641 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001642 // If 'switchbuf' contains "usetab": jump to first window in any tab
1643 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001644 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 return OK;
1646 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 return FAIL;
1648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
Bram Moolenaarc667da52019-11-30 20:52:27 +01001650 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (buf == curbuf)
1652 return OK;
1653
1654 /*
1655 * Check if the current buffer may be abandoned.
1656 */
1657 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1658 {
1659#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001660 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001662 bufref_T bufref;
1663
1664 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001666 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001667 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 if (bufIsChanged(curbuf))
1671#endif
1672 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001673 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 return FAIL;
1675 }
1676 }
1677
Bram Moolenaarc667da52019-11-30 20:52:27 +01001678 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 set_curbuf(buf, action);
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001682 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001684#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001685 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 return FAIL;
1687#endif
1688
1689 return OK;
1690}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692/*
1693 * Set current buffer to "buf". Executes autocommands and closes current
1694 * buffer. "action" tells how to close the current buffer:
1695 * DOBUF_GOTO free or hide it
1696 * DOBUF_SPLIT nothing
1697 * DOBUF_UNLOAD unload it
1698 * DOBUF_DEL delete it
1699 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001700 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 */
1702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 buf_T *prevbuf;
1706 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001707 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001708#ifdef FEAT_SYN_HL
1709 long old_tw = curbuf->b_p_tw;
1710#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001711 bufref_T newbufref;
1712 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001715 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001716 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1717 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaarc667da52019-11-30 20:52:27 +01001719 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaarc667da52019-11-30 20:52:27 +01001722 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001724 set_bufref(&prevbufref, prevbuf);
1725 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001727 // Autocommands may delete the current buffer and/or the buffer we want to
1728 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001729 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001730 || (bufref_valid(&prevbufref)
1731 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001732#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001733 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001735 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001737#ifdef FEAT_SYN_HL
1738 if (prevbuf == curwin->w_buffer)
1739 reset_synblock(curwin);
1740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001742 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001743#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001744 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001746 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001748 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001749 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001750
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001751 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001752 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1754 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001755 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001756 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1757 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001758 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001759 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001760 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001763 // An autocommand may have deleted "buf", already entered it (e.g., when
1764 // it did ":bunload") or aborted the script processing.
1765 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001766 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001767#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001768 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001770 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001773#ifdef FEAT_SYN_HL
1774 if (old_tw != curbuf->b_p_tw)
1775 check_colorcolumn(curwin);
1776#endif
1777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778}
1779
1780/*
1781 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001782 * Old curbuf must have been abandoned already! This also means "curbuf" may
1783 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001788 // Get the buffer in the current window.
1789 curwin->w_buffer = buf;
1790 curbuf = buf;
1791 ++curbuf->b_nwindows;
1792
1793 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1795 if (!buf->b_help)
1796 get_winopts(buf);
1797#ifdef FEAT_FOLDING
1798 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001799 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001801 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#endif
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001805 if (curwin->w_p_diff)
1806 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
Bram Moolenaara971b822011-09-14 14:43:25 +02001809#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001810 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001811#endif
1812
Bram Moolenaarc667da52019-11-30 20:52:27 +01001813 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 curwin->w_cursor.lnum = 1;
1815 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001818 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaarc667da52019-11-30 20:52:27 +01001820 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001821 curwin->w_valid = 0;
1822
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001823 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1824 curbuf->b_last_cursor.col, TRUE);
1825
Bram Moolenaarc667da52019-11-30 20:52:27 +01001826 // Make sure the buffer is loaded.
1827 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001828 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001829 // If there is no filetype, allow for detecting one. Esp. useful for
1830 // ":ball" used in a autocommand. If there already is a filetype we
1831 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001832 if (*curbuf->b_p_ft == NUL)
1833 did_filetype = FALSE;
1834
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001835 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 else
1838 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001839 if (!msg_silent && !shortmess(SHM_FILEINFO))
1840 need_fileinfo = TRUE; // display file info after redraw
1841
1842 // check if file changed
1843 (void)buf_check_timestamp(curbuf, FALSE);
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001846#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1850 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852
Bram Moolenaarc667da52019-11-30 20:52:27 +01001853 // If autocommands did not change the cursor position, restore cursor lnum
1854 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (curwin->w_cursor.lnum == 1 && inindent(0))
1856 buflist_getfpos();
1857
Bram Moolenaarc667da52019-11-30 20:52:27 +01001858 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859#ifdef FEAT_TITLE
1860 maketitle();
1861#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001862 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001863 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001864 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaar009b2592004-10-24 19:18:58 +00001866#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001867 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001868 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001869#endif
1870
Bram Moolenaarc667da52019-11-30 20:52:27 +01001871 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001872 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874#ifdef FEAT_KEYMAP
1875 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001876 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001878#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001879 // May need to set the spell language. Can only do this after the buffer
1880 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001881 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1882 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001883#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001884#ifdef FEAT_VIMINFO
1885 curbuf->b_last_used = vim_time();
1886#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 redraw_later(NOT_VALID);
1889}
1890
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001891#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1892/*
1893 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001894 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001895 */
1896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001897do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001898{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001899 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001900 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001901 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001902 shorten_fnames(TRUE);
1903}
1904#endif
1905
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001906 void
1907no_write_message(void)
1908{
1909#ifdef FEAT_TERMINAL
1910 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001911 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001912 else
1913#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001915}
1916
1917 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001918no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001919{
1920#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001921 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001922 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001923 else
1924#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001925 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001926}
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
1929 * functions for dealing with the buffer list
1930 */
1931
1932/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001933 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1934 * only one window. That means it can be re-used.
1935 */
1936 int
1937curbuf_reusable(void)
1938{
1939 return (curbuf != NULL
1940 && curbuf->b_ffname == NULL
1941 && curbuf->b_nwindows <= 1
1942 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001943#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001944 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001945#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001946 && !curbufIsChanged());
1947}
1948
1949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 * Add a file name to the buffer list. Return a pointer to the buffer.
1951 * If the same file name already exists return a pointer to that buffer.
1952 * If it does not exist, or if fname == NULL, a new entry is created.
1953 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1954 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1955 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001956 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001957 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1958 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001959 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 * This is the ONLY way to create a new buffer.
1961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001963buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001964 char_u *ffname_arg, // full path of fname or relative
1965 char_u *sfname_arg, // short fname or NULL
1966 linenr_T lnum, // preferred cursor line
1967 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001969 char_u *ffname = ffname_arg;
1970 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 buf_T *buf;
1972#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001973 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974#endif
1975
Bram Moolenaar480778b2016-07-14 22:09:39 +02001976 if (top_file_num == 1)
1977 hash_init(&buf_hashtab);
1978
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001979 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 /*
1982 * If file name already exists in the list, update the entry.
1983 */
1984#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001985 // On Unix we can use inode numbers when the file exists. Works better
1986 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1988 st.st_dev = (dev_T)-1;
1989#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001990 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#ifdef UNIX
1992 buflist_findname_stat(ffname, &st)
1993#else
1994 buflist_findname(ffname)
1995#endif
1996 ) != NULL)
1997 {
1998 vim_free(ffname);
1999 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002000 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2001 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002002
2003 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002004 // copy the options now, if 'cpo' doesn't have 's' and not done
2005 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002006 buf_copy_options(buf, 0);
2007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2009 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002010 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002013 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002015 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002016 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002017 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002018 return NULL;
2019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021 return buf;
2022 }
2023
2024 /*
2025 * If the current buffer has no name and no contents, use the current
2026 * buffer. Otherwise: Need to allocate a new buffer structure.
2027 *
2028 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002029 * (A spell file buffer is allocated in spell.c, but that's not a normal
2030 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 */
2032 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002033 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002036 // It's like this buffer is deleted. Watch out for autocommands that
2037 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (curbuf->b_p_bl)
2039 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2040 if (buf == curbuf)
2041 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002042#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002043 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002044 {
2045 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002051 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 clear_string_option(&buf->b_p_bh);
2053 clear_string_option(&buf->b_p_bt);
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 if (buf != curbuf || curbuf == NULL)
2057 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002058 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (buf == NULL)
2060 {
2061 vim_free(ffname);
2062 return NULL;
2063 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002064#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002065 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002066 buf->b_vars = dict_alloc();
2067 if (buf->b_vars == NULL)
2068 {
2069 vim_free(ffname);
2070 vim_free(buf);
2071 return NULL;
2072 }
2073 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2074#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002075 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077
2078 if (ffname != NULL)
2079 {
2080 buf->b_ffname = ffname;
2081 buf->b_sfname = vim_strsave(sfname);
2082 }
2083
2084 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002085 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2088 || buf->b_wininfo == NULL)
2089 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002090 if (buf->b_sfname != buf->b_ffname)
2091 VIM_CLEAR(buf->b_sfname);
2092 else
2093 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002094 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (buf != curbuf)
2096 free_buffer(buf);
2097 return NULL;
2098 }
2099
2100 if (buf == curbuf)
2101 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002102 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002103 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002104 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002106#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002107 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 return NULL;
2109#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002110 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002111
Bram Moolenaarc667da52019-11-30 20:52:27 +01002112 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002113 buf->b_p_initialized = FALSE;
2114 buf_copy_options(buf, BCO_ENTER);
2115
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002117 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 curbuf->b_kmap_state |= KEYMAP_INIT;
2119#endif
2120 }
2121 else
2122 {
2123 /*
2124 * put new buffer at the end of the buffer list
2125 */
2126 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002127 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 buf->b_prev = NULL;
2130 firstbuf = buf;
2131 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002132 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 lastbuf->b_next = buf;
2135 buf->b_prev = lastbuf;
2136 }
2137 lastbuf = buf;
2138
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002139 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2140 {
2141 // Recycle a previously used buffer number. Used for buffers which
2142 // are normally hidden, e.g. in a popup window. Avoids that the
2143 // buffer number grows rapidly.
2144 --buf_reuse.ga_len;
2145 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002146
2147 // Move buffer to the right place in the buffer list.
2148 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2149 {
2150 buf_T *prev = buf->b_prev;
2151
2152 prev->b_next = buf->b_next;
2153 if (prev->b_next != NULL)
2154 prev->b_next->b_prev = prev;
2155 buf->b_next = prev;
2156 buf->b_prev = prev->b_prev;
2157 if (buf->b_prev != NULL)
2158 buf->b_prev->b_next = buf;
2159 prev->b_prev = buf;
2160 if (lastbuf == buf)
2161 lastbuf = prev;
2162 if (firstbuf == prev)
2163 firstbuf = buf;
2164 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002165 }
2166 else
2167 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002168 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002171 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002174 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 top_file_num = 1;
2177 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002178 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 /*
2181 * Always copy the options from the current buffer.
2182 */
2183 buf_copy_options(buf, BCO_ALWAYS);
2184 }
2185
2186 buf->b_wininfo->wi_fpos.lnum = lnum;
2187 buf->b_wininfo->wi_win = curwin;
2188
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002189#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002190 hash_init(&buf->b_s.b_keywtab);
2191 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#endif
2193
2194 buf->b_fname = buf->b_sfname;
2195#ifdef UNIX
2196 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002197 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 else
2199 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002200 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 buf->b_dev = st.st_dev;
2202 buf->b_ino = st.st_ino;
2203 }
2204#endif
2205 buf->b_u_synced = TRUE;
2206 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002207 if (flags & BLN_DUMMY)
2208 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002210 clrallmarks(buf); // clear marks
2211 fmarks_check_names(buf); // check file marks for this file
2212 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (!(flags & BLN_DUMMY))
2214 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002215 bufref_T bufref;
2216
Bram Moolenaarc667da52019-11-30 20:52:27 +01002217 // Tricky: these autocommands may change the buffer list. They could
2218 // also split the window with re-using the one empty buffer. This may
2219 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002220 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002221 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002222 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002223 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002225 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002226 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002227 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002228 return NULL;
2229 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002230#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002231 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 return buf;
2237}
2238
2239/*
2240 * Free the memory for the options of a buffer.
2241 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2242 * 'fileencoding'.
2243 */
2244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002245free_buf_options(
2246 buf_T *buf,
2247 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 if (free_p_ff)
2250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 clear_string_option(&buf->b_p_bh);
2254 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256#ifdef FEAT_FIND_ID
2257 clear_string_option(&buf->b_p_def);
2258 clear_string_option(&buf->b_p_inc);
2259# ifdef FEAT_EVAL
2260 clear_string_option(&buf->b_p_inex);
2261# endif
2262#endif
2263#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2264 clear_string_option(&buf->b_p_inde);
2265 clear_string_option(&buf->b_p_indk);
2266#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002267#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2268 clear_string_option(&buf->b_p_bexpr);
2269#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002270#if defined(FEAT_CRYPT)
2271 clear_string_option(&buf->b_p_cm);
2272#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002273 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002274#if defined(FEAT_EVAL)
2275 clear_string_option(&buf->b_p_fex);
2276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#ifdef FEAT_CRYPT
2278 clear_string_option(&buf->b_p_key);
2279#endif
2280 clear_string_option(&buf->b_p_kp);
2281 clear_string_option(&buf->b_p_mps);
2282 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002283 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002285#ifdef FEAT_VARTABS
2286 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002287 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002288 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002289 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002290 buf->b_p_vsts_array = NULL;
2291 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002292 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#ifdef FEAT_KEYMAP
2295 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002296 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ga_clear(&buf->b_kmap_ga);
2298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#ifdef FEAT_FOLDING
2301 clear_string_option(&buf->b_p_cms);
2302#endif
2303 clear_string_option(&buf->b_p_nf);
2304#ifdef FEAT_SYN_HL
2305 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002306 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002307#endif
2308#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002309 clear_string_option(&buf->b_s.b_p_spc);
2310 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002311 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002312 buf->b_s.b_cap_prog = NULL;
2313 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002314 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
2316#ifdef FEAT_SEARCHPATH
2317 clear_string_option(&buf->b_p_sua);
2318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#ifdef FEAT_CINDENT
2321 clear_string_option(&buf->b_p_cink);
2322 clear_string_option(&buf->b_p_cino);
2323#endif
2324#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2325 clear_string_option(&buf->b_p_cinw);
2326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002328#ifdef FEAT_COMPL_FUNC
2329 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002330 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#ifdef FEAT_QUICKFIX
2333 clear_string_option(&buf->b_p_gp);
2334 clear_string_option(&buf->b_p_mp);
2335 clear_string_option(&buf->b_p_efm);
2336#endif
2337 clear_string_option(&buf->b_p_ep);
2338 clear_string_option(&buf->b_p_path);
2339 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002340 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002341#ifdef FEAT_EVAL
2342 clear_string_option(&buf->b_p_tfu);
2343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 clear_string_option(&buf->b_p_dict);
2345 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002346#ifdef FEAT_TEXTOBJ
2347 clear_string_option(&buf->b_p_qe);
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002350 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002351#ifdef FEAT_LISP
2352 clear_string_option(&buf->b_p_lw);
2353#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002354 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002355 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356}
2357
2358/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002359 * Get alternate file "n".
2360 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2361 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 * if (options & GETF_SETMARK) call setpcmark()
2363 * if (options & GETF_ALT) we are jumping to an alternate file.
2364 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2365 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002366 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 */
2368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369buflist_getfile(
2370 int n,
2371 linenr_T lnum,
2372 int options,
2373 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 pos_T *fpos;
2378 colnr_T col;
2379
2380 buf = buflist_findnr(n);
2381 if (buf == NULL)
2382 {
2383 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002384 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002386 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 return FAIL;
2388 }
2389
Bram Moolenaarc667da52019-11-30 20:52:27 +01002390 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (buf == curbuf)
2392 return OK;
2393
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002394 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002395 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002396 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002398 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002399 if (curbuf_locked())
2400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaarc667da52019-11-30 20:52:27 +01002402 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (lnum == 0)
2404 {
2405 fpos = buflist_findfpos(buf);
2406 lnum = fpos->lnum;
2407 col = fpos->col;
2408 }
2409 else
2410 col = 0;
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (options & GETF_SWITCH)
2413 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002414 // If 'switchbuf' contains "useopen": jump to first window containing
2415 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002416 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002418
Bram Moolenaarc667da52019-11-30 20:52:27 +01002419 // If 'switchbuf' contains "usetab": jump to first window in any tab
2420 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002421 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002422 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002423
Bram Moolenaarc667da52019-11-30 20:52:27 +01002424 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2425 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002426 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002427 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002429 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002430 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002431 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2432 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002434 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002439 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2440 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 --RedrawingDisabled;
2443
Bram Moolenaarc667da52019-11-30 20:52:27 +01002444 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (!p_sol && col != 0)
2446 {
2447 curwin->w_cursor.col = col;
2448 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 curwin->w_set_curswant = TRUE;
2451 }
2452 return OK;
2453 }
2454 --RedrawingDisabled;
2455 return FAIL;
2456}
2457
2458/*
2459 * go to the last know line number for the current buffer
2460 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002462buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 pos_T *fpos;
2465
2466 fpos = buflist_findfpos(curbuf);
2467
2468 curwin->w_cursor.lnum = fpos->lnum;
2469 check_cursor_lnum();
2470
2471 if (p_sol)
2472 curwin->w_cursor.col = 0;
2473 else
2474 {
2475 curwin->w_cursor.col = fpos->col;
2476 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 curwin->w_set_curswant = TRUE;
2479 }
2480}
2481
Bram Moolenaar81695252004-12-29 20:58:21 +00002482#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2483/*
2484 * Find file in buffer list by name (it has to be for the current window).
2485 * Returns NULL if not found.
2486 */
2487 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002488buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002489{
2490 char_u *ffname;
2491 buf_T *buf = NULL;
2492
Bram Moolenaarc667da52019-11-30 20:52:27 +01002493 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002494 ffname = FullName_save(fname,
2495#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002496 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002497#else
2498 FALSE
2499#endif
2500 );
2501 if (ffname != NULL)
2502 {
2503 buf = buflist_findname(ffname);
2504 vim_free(ffname);
2505 }
2506 return buf;
2507}
2508#endif
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510/*
2511 * Find file in buffer list by name (it has to be for the current window).
2512 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002513 * Skips dummy buffers.
2514 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
2516 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002517buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002520 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522 if (mch_stat((char *)ffname, &st) < 0)
2523 st.st_dev = (dev_T)-1;
2524 return buflist_findname_stat(ffname, &st);
2525}
2526
2527/*
2528 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2529 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002530 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
2532 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002533buflist_findname_stat(
2534 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002535 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537#endif
2538 buf_T *buf;
2539
Bram Moolenaarc667da52019-11-30 20:52:27 +01002540 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002541 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002542 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef UNIX
2544 , stp
2545#endif
2546 ))
2547 return buf;
2548 return NULL;
2549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
2552 * Find file in buffer list by a regexp pattern.
2553 * Return fnum of the found buffer.
2554 * Return < 0 for error.
2555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002557buflist_findpat(
2558 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002559 char_u *pattern_end, // pointer to first char after pattern
2560 int unlisted, // find unlisted buffers
2561 int diffmode UNUSED, // find diff-mode buffers only
2562 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 int match = -1;
2566 int find_listed;
2567 char_u *pat;
2568 char_u *patend;
2569 int attempt;
2570 char_u *p;
2571 int toggledollar;
2572
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002573 // "%" is current file, "%%" or "#" is alternate file
2574 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2575 || (in_vim9script() && pattern_end == pattern + 2
2576 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002578 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002580 else
2581 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#ifdef FEAT_DIFF
2583 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2584 match = -1;
2585#endif
2586 }
2587
2588 /*
2589 * Try four ways of matching a listed buffer:
2590 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002591 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 * attempt == 2: with '$' at end (only match at end)
2593 * attempt == 3: with '^' at start and '$' at end (only full match)
2594 * Repeat this for finding an unlisted buffer if there was no matching
2595 * listed buffer.
2596 */
2597 else
2598 {
2599 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2600 if (pat == NULL)
2601 return -1;
2602 patend = pat + STRLEN(pat) - 1;
2603 toggledollar = (patend > pat && *patend == '$');
2604
Bram Moolenaarc667da52019-11-30 20:52:27 +01002605 // First try finding a listed buffer. If not found and "unlisted"
2606 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 find_listed = TRUE;
2608 for (;;)
2609 {
2610 for (attempt = 0; attempt <= 3; ++attempt)
2611 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002612 regmatch_T regmatch;
2613
Bram Moolenaarc667da52019-11-30 20:52:27 +01002614 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002616 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002618 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002620 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002621 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 vim_free(pat);
2624 return -1;
2625 }
2626
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002627 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (buf->b_p_bl == find_listed
2629#ifdef FEAT_DIFF
2630 && (!diffmode || diff_mode_buf(buf))
2631#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002632 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002634 if (curtab_only)
2635 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002636 // Ignore the match if the buffer is not open in
2637 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002638 win_T *wp;
2639
Bram Moolenaar29323592016-07-24 22:04:11 +02002640 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002641 if (wp->w_buffer == buf)
2642 break;
2643 if (wp == NULL)
2644 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002645 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002646 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 match = -2;
2649 break;
2650 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002651 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002654 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002655 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 break;
2657 }
2658
Bram Moolenaarc667da52019-11-30 20:52:27 +01002659 // Only search for unlisted buffers if there was no match with
2660 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (!unlisted || !find_listed || match != -1)
2662 break;
2663 find_listed = FALSE;
2664 }
2665
2666 vim_free(pat);
2667 }
2668
2669 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002670 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002672 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 return match;
2674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
Bram Moolenaar52410572019-10-27 05:12:45 +01002676#ifdef FEAT_VIMINFO
2677typedef struct {
2678 buf_T *buf;
2679 char_u *match;
2680} bufmatch_T;
2681#endif
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * Find all buffer names that match.
2685 * For command line expansion of ":buf" and ":sbuf".
2686 * Return OK if matches found, FAIL otherwise.
2687 */
2688 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002689ExpandBufnames(
2690 char_u *pat,
2691 int *num_file,
2692 char_u ***file,
2693 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 int count = 0;
2696 buf_T *buf;
2697 int round;
2698 char_u *p;
2699 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002700 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002701#ifdef FEAT_VIMINFO
2702 bufmatch_T *matches = NULL;
2703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaarc667da52019-11-30 20:52:27 +01002705 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 *file = NULL;
2707
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002708#ifdef FEAT_DIFF
2709 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2710 return FAIL;
2711#endif
2712
Bram Moolenaarc667da52019-11-30 20:52:27 +01002713 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002714 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002716 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002717 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002719 STRCPY(patc, "\\(^\\|[\\/]\\)");
2720 STRCPY(patc + 11, pat + 1);
2721 }
2722 else
2723 patc = pat;
2724
2725 /*
2726 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002727 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002728 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002729 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002730 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002731 regmatch_T regmatch;
2732
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002733 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002734 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002735 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2736 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002737 {
2738 if (patc != pat)
2739 vim_free(patc);
2740 return FAIL;
2741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 /*
2744 * round == 1: Count the matches.
2745 * round == 2: Build the array to keep the matches.
2746 */
2747 for (round = 1; round <= 2; ++round)
2748 {
2749 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002750 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002752 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002754#ifdef FEAT_DIFF
2755 if (options & BUF_DIFF_FILTER)
2756 // Skip buffers not suitable for
2757 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002758 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002759 continue;
2760#endif
2761
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002762 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (p != NULL)
2764 {
2765 if (round == 1)
2766 ++count;
2767 else
2768 {
2769 if (options & WILD_HOME_REPLACE)
2770 p = home_replace_save(buf, p);
2771 else
2772 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002773#ifdef FEAT_VIMINFO
2774 if (matches != NULL)
2775 {
2776 matches[count].buf = buf;
2777 matches[count].match = p;
2778 count++;
2779 }
2780 else
2781#endif
2782 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
2784 }
2785 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002786 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 break;
2788 if (round == 1)
2789 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002790 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (*file == NULL)
2792 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002793 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002794 if (patc != pat)
2795 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return FAIL;
2797 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002798#ifdef FEAT_VIMINFO
2799 if (options & WILD_BUFLASTUSED)
2800 matches = ALLOC_MULT(bufmatch_T, count);
2801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002804 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002805 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 break;
2807 }
2808
Bram Moolenaar05159a02005-02-26 23:04:13 +00002809 if (patc != pat)
2810 vim_free(patc);
2811
Bram Moolenaar52410572019-10-27 05:12:45 +01002812#ifdef FEAT_VIMINFO
2813 if (matches != NULL)
2814 {
2815 int i;
2816 if (count > 1)
2817 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2818 // if the current buffer is first in the list, place it at the end
2819 if (matches[0].buf == curbuf)
2820 {
2821 for (i = 1; i < count; i++)
2822 (*file)[i-1] = matches[i].match;
2823 (*file)[count-1] = matches[0].match;
2824 }
2825 else
2826 {
2827 for (i = 0; i < count; i++)
2828 (*file)[i] = matches[i].match;
2829 }
2830 vim_free(matches);
2831 }
2832#endif
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 *num_file = count;
2835 return (count == 0 ? FAIL : OK);
2836}
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838/*
2839 * Check for a match on the file name for buffer "buf" with regprog "prog".
2840 */
2841 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002842buflist_match(
2843 regmatch_T *rmp,
2844 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002845 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
2847 char_u *match;
2848
Bram Moolenaarc667da52019-11-30 20:52:27 +01002849 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002850 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002852 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 return match;
2855}
2856
2857/*
2858 * Try matching the regexp in "prog" with file name "name".
2859 * Return "name" when there is a match, NULL when not.
2860 */
2861 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002862fname_match(
2863 regmatch_T *rmp,
2864 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002865 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 char_u *match = NULL;
2868 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 if (name != NULL)
2871 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002872 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002873 rmp->rm_ic = p_fic || ignore_case;
2874 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 match = name;
2876 else
2877 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002878 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002880 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 match = name;
2882 vim_free(p);
2883 }
2884 }
2885
2886 return match;
2887}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
2889/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002890 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 */
2892 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002895 char_u key[VIM_SIZEOF_INT * 2 + 1];
2896 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 if (nr == 0)
2899 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002900 sprintf((char *)key, "%x", nr);
2901 hi = hash_find(&buf_hashtab, key);
2902
2903 if (!HASHITEM_EMPTY(hi))
2904 return (buf_T *)(hi->hi_key
2905 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 return NULL;
2907}
2908
2909/*
2910 * Get name of file 'n' in the buffer list.
2911 * When the file has no name an empty string is returned.
2912 * home_replace() is used to shorten the file name (used for marks).
2913 * Returns a pointer to allocated memory, of NULL when failed.
2914 */
2915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002916buflist_nr2name(
2917 int n,
2918 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002919 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
2921 buf_T *buf;
2922
2923 buf = buflist_findnr(n);
2924 if (buf == NULL)
2925 return NULL;
2926 return home_replace_save(helptail ? buf : NULL,
2927 fullname ? buf->b_ffname : buf->b_fname);
2928}
2929
2930/*
2931 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2932 * When "copy_options" is TRUE save the local window option values.
2933 * When "lnum" is 0 only do the options.
2934 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002936buflist_setfpos(
2937 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002938 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939 linenr_T lnum,
2940 colnr_T col,
2941 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 wininfo_T *wip;
2944
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002945 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (wip->wi_win == win)
2947 break;
2948 if (wip == NULL)
2949 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002950 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002951 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (wip == NULL)
2953 return;
2954 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002955 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 lnum = 1;
2957 }
2958 else
2959 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002960 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (wip->wi_prev)
2962 wip->wi_prev->wi_next = wip->wi_next;
2963 else
2964 buf->b_wininfo = wip->wi_next;
2965 if (wip->wi_next)
2966 wip->wi_next->wi_prev = wip->wi_prev;
2967 if (copy_options && wip->wi_optset)
2968 {
2969 clear_winopt(&wip->wi_opt);
2970#ifdef FEAT_FOLDING
2971 deleteFoldRecurse(&wip->wi_folds);
2972#endif
2973 }
2974 }
2975 if (lnum != 0)
2976 {
2977 wip->wi_fpos.lnum = lnum;
2978 wip->wi_fpos.col = col;
2979 }
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002980 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002982 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2984#ifdef FEAT_FOLDING
2985 wip->wi_fold_manual = win->w_fold_manual;
2986 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2987#endif
2988 wip->wi_optset = TRUE;
2989 }
2990
Bram Moolenaarc667da52019-11-30 20:52:27 +01002991 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 wip->wi_next = buf->b_wininfo;
2993 buf->b_wininfo = wip;
2994 wip->wi_prev = NULL;
2995 if (wip->wi_next)
2996 wip->wi_next->wi_prev = wip;
2997
2998 return;
2999}
3000
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003001#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003002/*
3003 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3004 * page. That's because a diff is local to a tab page.
3005 */
3006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003008{
3009 win_T *wp;
3010
3011 if (wip->wi_opt.wo_diff)
3012 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003013 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003014 // return FALSE when it's a window in the current tab page, thus
3015 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003016 if (wip->wi_win == wp)
3017 return FALSE;
3018 return TRUE;
3019 }
3020 return FALSE;
3021}
3022#endif
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024/*
3025 * Find info for the current window in buffer "buf".
3026 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003027 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003028 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3029 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 * Returns NULL when there isn't any info.
3031 */
3032 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003033find_wininfo(
3034 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003035 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003036 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 wininfo_T *wip;
3039
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003040 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003041 if (wip->wi_win == curwin
3042#ifdef FEAT_DIFF
3043 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3044#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003045
3046 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003048
Bram Moolenaarc667da52019-11-30 20:52:27 +01003049 // If no wininfo for curwin, use the first in the list (that doesn't have
3050 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003051 // If "need_options" is TRUE skip entries that don't have options set,
3052 // unless the window is editing "buf", so we can copy from the window
3053 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003054 if (wip == NULL)
3055 {
3056#ifdef FEAT_DIFF
3057 if (skip_diff_buffer)
3058 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003059 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003060 if (!wininfo_other_tab_diff(wip)
3061 && (!need_options || wip->wi_optset
3062 || (wip->wi_win != NULL
3063 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003064 break;
3065 }
3066 else
3067#endif
3068 wip = buf->b_wininfo;
3069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 return wip;
3071}
3072
3073/*
3074 * Reset the local window options to the values last used in this window.
3075 * If the buffer wasn't used in this window before, use the values from
3076 * the most recently used window. If the values were never set, use the
3077 * global values for the window.
3078 */
3079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 wininfo_T *wip;
3083
3084 clear_winopt(&curwin->w_onebuf_opt);
3085#ifdef FEAT_FOLDING
3086 clearFolding(curwin);
3087#endif
3088
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003089 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003090 if (wip != NULL && wip->wi_win != NULL
3091 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003093 // The buffer is currently displayed in the window: use the actual
3094 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003095 win_T *wp = wip->wi_win;
3096
3097 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3098#ifdef FEAT_FOLDING
3099 curwin->w_fold_manual = wp->w_fold_manual;
3100 curwin->w_foldinvalid = TRUE;
3101 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3102#endif
3103 }
3104 else if (wip != NULL && wip->wi_optset)
3105 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003106 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3108#ifdef FEAT_FOLDING
3109 curwin->w_fold_manual = wip->wi_fold_manual;
3110 curwin->w_foldinvalid = TRUE;
3111 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3112#endif
3113 }
3114 else
3115 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3116
3117#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003118 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (p_fdls >= 0)
3120 curwin->w_p_fdl = p_fdls;
3121#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003122 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125/*
3126 * Find the position (lnum and col) for the buffer 'buf' for the current
3127 * window.
3128 * Returns a pointer to no_position if no position is found.
3129 */
3130 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003131buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003134 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003136 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (wip != NULL)
3138 return &(wip->wi_fpos);
3139 else
3140 return &no_position;
3141}
3142
3143/*
3144 * Find the lnum for the buffer 'buf' for the current window.
3145 */
3146 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003147buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 return buflist_findfpos(buf)->lnum;
3150}
3151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003153 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaar52410572019-10-27 05:12:45 +01003158 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int len;
3160 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003161 int ro_char;
3162 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003163#ifdef FEAT_TERMINAL
3164 int job_running;
3165 int job_none_open;
3166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaar52410572019-10-27 05:12:45 +01003168#ifdef FEAT_VIMINFO
3169 garray_T buflist;
3170 buf_T **buflist_data = NULL, **p;
3171
3172 if (vim_strchr(eap->arg, 't'))
3173 {
3174 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003175 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003176 {
3177 if (ga_grow(&buflist, 1) == OK)
3178 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3179 }
3180
3181 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3182 sizeof(buf_T *), buf_compare);
3183
Bram Moolenaar3b991522019-11-06 23:26:20 +01003184 buflist_data = (buf_T **)buflist.ga_data;
3185 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003186 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003187 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003188
Bram Moolenaar3b991522019-11-06 23:26:20 +01003189 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003190 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3191 : buf->b_next)
3192#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003196#ifdef FEAT_TERMINAL
3197 job_running = term_job_running(buf->b_term);
3198 job_none_open = job_running && term_none_open(buf->b_term);
3199#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003200 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003201 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3202 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3203 || (vim_strchr(eap->arg, '+')
3204 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3205 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003206 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003207 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003208 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3209#ifdef FEAT_TERMINAL
3210 || (vim_strchr(eap->arg, 'R')
3211 && (!job_running || (job_running && job_none_open)))
3212 || (vim_strchr(eap->arg, '?')
3213 && (!job_running || (job_running && !job_none_open)))
3214 || (vim_strchr(eap->arg, 'F')
3215 && (job_running || buf->b_term == NULL))
3216#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003217 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3218 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3219 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3220 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3221 || (vim_strchr(eap->arg, '#')
3222 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003225 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
3227 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003228 if (message_filtered(NameBuff))
3229 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003231 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3232 : (bufIsChanged(buf) ? '+' : ' ');
3233#ifdef FEAT_TERMINAL
3234 if (term_job_running(buf->b_term))
3235 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003236 if (term_none_open(buf->b_term))
3237 ro_char = '?';
3238 else
3239 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003240 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3241 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003242 }
3243 else if (buf->b_term != NULL)
3244 ro_char = 'F';
3245 else
3246#endif
3247 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3248
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003249 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003250 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 buf->b_fnum,
3252 buf->b_p_bl ? ' ' : 'u',
3253 buf == curbuf ? '%' :
3254 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3255 buf->b_ml.ml_mfp == NULL ? ' ' :
3256 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003257 ro_char,
3258 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003259 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003260 if (len > IOSIZE - 20)
3261 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
Bram Moolenaarc667da52019-11-30 20:52:27 +01003263 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 i = 40 - vim_strsize(IObuff);
3265 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003267 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003268#ifdef FEAT_VIMINFO
3269 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3270 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3271 else
3272#endif
3273 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3274 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003275 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003277 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 ui_breakcheck();
3279 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003280
3281#ifdef FEAT_VIMINFO
3282 if (buflist_data)
3283 ga_clear(&buflist);
3284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287/*
3288 * Get file name and line number for file 'fnum'.
3289 * Used by DoOneCmd() for translating '%' and '#'.
3290 * Used by insert_reg() and cmdline_paste() for '#' register.
3291 * Return FAIL if not found, OK for success.
3292 */
3293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003294buflist_name_nr(
3295 int fnum,
3296 char_u **fname,
3297 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 buf_T *buf;
3300
3301 buf = buflist_findnr(fnum);
3302 if (buf == NULL || buf->b_fname == NULL)
3303 return FAIL;
3304
3305 *fname = buf->b_fname;
3306 *lnum = buflist_findlnum(buf);
3307
3308 return OK;
3309}
3310
3311/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003312 * Set the file name for "buf"' to "ffname_arg", short file name to
3313 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 * The file name with the full path is also remembered, for when :cd is used.
3315 * Returns FAIL for failure (file name already in use by other buffer)
3316 * OK otherwise.
3317 */
3318 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003319setfname(
3320 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003321 char_u *ffname_arg,
3322 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003323 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003325 char_u *ffname = ffname_arg;
3326 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003327 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003329 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
3331
3332 if (ffname == NULL || *ffname == NUL)
3333 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003334 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003335 if (buf->b_sfname != buf->b_ffname)
3336 VIM_CLEAR(buf->b_sfname);
3337 else
3338 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003339 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#ifdef UNIX
3341 st.st_dev = (dev_T)-1;
3342#endif
3343 }
3344 else
3345 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003346 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3347 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 return FAIL;
3349
3350 /*
3351 * if the file name is already used in another buffer:
3352 * - if the buffer is loaded, fail
3353 * - if the buffer is not loaded, delete it from the list
3354 */
3355#ifdef UNIX
3356 if (mch_stat((char *)ffname, &st) < 0)
3357 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003358#endif
3359 if (!(buf->b_flags & BF_DUMMY))
3360#ifdef UNIX
3361 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003363 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#endif
3365 if (obuf != NULL && obuf != buf)
3366 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003367 if (obuf->b_ml.ml_mfp != NULL) // it's loaded, fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
3369 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003370 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 vim_free(ffname);
3372 return FAIL;
3373 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003374 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003375 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 sfname = vim_strsave(sfname);
3378 if (ffname == NULL || sfname == NULL)
3379 {
3380 vim_free(sfname);
3381 vim_free(ffname);
3382 return FAIL;
3383 }
3384#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003385 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003387 if (buf->b_sfname != buf->b_ffname)
3388 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 buf->b_ffname = ffname;
3391 buf->b_sfname = sfname;
3392 }
3393 buf->b_fname = buf->b_sfname;
3394#ifdef UNIX
3395 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003396 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else
3398 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003399 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 buf->b_dev = st.st_dev;
3401 buf->b_ino = st.st_ino;
3402 }
3403#endif
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 buf_name_changed(buf);
3408 return OK;
3409}
3410
3411/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003412 * Crude way of changing the name of a buffer. Use with care!
3413 * The name should be relative to the current directory.
3414 */
3415 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003416buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003417{
3418 buf_T *buf;
3419
3420 buf = buflist_findnr(fnum);
3421 if (buf != NULL)
3422 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003423 if (buf->b_sfname != buf->b_ffname)
3424 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003425 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003426 buf->b_ffname = vim_strsave(name);
3427 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 // Allocate ffname and expand into full path. Also resolves .lnk
3429 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003430 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003431 buf->b_fname = buf->b_sfname;
3432 }
3433}
3434
3435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * Take care of what needs to be done when the name of buffer "buf" has
3437 * changed.
3438 */
3439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003440buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 /*
3443 * If the file name changed, also change the name of the swapfile
3444 */
3445 if (buf->b_ml.ml_mfp != NULL)
3446 ml_setname(buf);
3447
3448 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003449 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003451 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003453 status_redraw_all(); // status lines need to be redrawn
3454 fmarks_check_names(buf); // check named file marks
3455 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456}
3457
3458/*
3459 * set alternate file name for current window
3460 *
3461 * Used by do_one_cmd(), do_write() and do_ecmd().
3462 * Return the buffer.
3463 */
3464 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003465setaltfname(
3466 char_u *ffname,
3467 char_u *sfname,
3468 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 buf_T *buf;
3471
Bram Moolenaarc667da52019-11-30 20:52:27 +01003472 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003474 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 curwin->w_alt_fnum = buf->b_fnum;
3476 return buf;
3477}
3478
3479/*
3480 * Get alternate file name for current window.
3481 * Return NULL if there isn't any, and give error message if requested.
3482 */
3483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003485 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 char_u *fname;
3488 linenr_T dummy;
3489
3490 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3491 {
3492 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003493 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 return NULL;
3495 }
3496 return fname;
3497}
3498
3499/*
3500 * Add a file name to the buflist and return its number.
3501 * Uses same flags as buflist_new(), except BLN_DUMMY.
3502 *
3503 * used by qf_init(), main() and doarglist()
3504 */
3505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 buf_T *buf;
3509
3510 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3511 if (buf != NULL)
3512 return buf->b_fnum;
3513 return 0;
3514}
3515
3516#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3517/*
3518 * Adjust slashes in file names. Called after 'shellslash' was set.
3519 */
3520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 buf_T *bp;
3524
Bram Moolenaar29323592016-07-24 22:04:11 +02003525 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 if (bp->b_ffname != NULL)
3528 slash_adjust(bp->b_ffname);
3529 if (bp->b_sfname != NULL)
3530 slash_adjust(bp->b_sfname);
3531 }
3532}
3533#endif
3534
3535/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003536 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 * Also save the local window option values.
3538 */
3539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003542 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543}
3544
3545/*
3546 * Return TRUE if 'ffname' is not the same file as current file.
3547 * Fname must have a full path (expanded by mch_FullName()).
3548 */
3549 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003550otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 return otherfile_buf(curbuf, ffname
3553#ifdef UNIX
3554 , NULL
3555#endif
3556 );
3557}
3558
3559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003560otherfile_buf(
3561 buf_T *buf,
3562 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003564 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003568 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3570 return TRUE;
3571 if (fnamecmp(ffname, buf->b_ffname) == 0)
3572 return FALSE;
3573#ifdef UNIX
3574 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003575 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaarc667da52019-11-30 20:52:27 +01003577 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (stp == NULL)
3579 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003580 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 st.st_dev = (dev_T)-1;
3582 stp = &st;
3583 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003584 // Use dev/ino to check if the files are the same, even when the names
3585 // are different (possible with links). Still need to compare the
3586 // name above, for when the file doesn't exist yet.
3587 // Problem: The dev/ino changes when a file is deleted (and created
3588 // again) and remains the same when renamed/moved. We don't want to
3589 // mch_stat() each buffer each time, that would be too slow. Get the
3590 // dev/ino again when they appear to match, but not when they appear
3591 // to be different: Could skip a buffer when it's actually the same
3592 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (buf_same_ino(buf, stp))
3594 {
3595 buf_setino(buf);
3596 if (buf_same_ino(buf, stp))
3597 return FALSE;
3598 }
3599 }
3600#endif
3601 return TRUE;
3602}
3603
3604#if defined(UNIX) || defined(PROTO)
3605/*
3606 * Set inode and device number for a buffer.
3607 * Must always be called when b_fname is changed!.
3608 */
3609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003610buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003612 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3615 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003616 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 buf->b_dev = st.st_dev;
3618 buf->b_ino = st.st_ino;
3619 }
3620 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003621 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622}
3623
3624/*
3625 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3626 */
3627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003628buf_same_ino(
3629 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003630 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003632 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 && stp->st_dev == buf->b_dev
3634 && stp->st_ino == buf->b_ino);
3635}
3636#endif
3637
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003638/*
3639 * Print info about the current buffer.
3640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003643 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644 int shorthelp,
3645 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 char_u *name;
3648 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003649 char *p;
3650 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003651 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003653 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (buffer == NULL)
3655 return;
3656
Bram Moolenaarc667da52019-11-30 20:52:27 +01003657 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003659 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 p = buffer + STRLEN(buffer);
3661 }
3662 else
3663 p = buffer;
3664
3665 *p++ = '"';
3666 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003667 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 else
3669 {
3670 if (!fullname && curbuf->b_fname != NULL)
3671 name = curbuf->b_fname;
3672 else
3673 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003674 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 (int)(IOSIZE - (p - buffer)), TRUE);
3676 }
3677
Bram Moolenaar32526b32019-01-19 17:43:09 +01003678 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 curbufIsChanged() ? (shortmess(SHM_MOD)
3680 ? " [+]" : _(" [Modified]")) : " ",
3681 (curbuf->b_flags & BF_NOTEDITED)
3682#ifdef FEAT_QUICKFIX
3683 && !bt_dontwrite(curbuf)
3684#endif
3685 ? _("[Not edited]") : "",
3686 (curbuf->b_flags & BF_NEW)
3687#ifdef FEAT_QUICKFIX
3688 && !bt_dontwrite(curbuf)
3689#endif
Bram Moolenaar722e5052020-06-12 22:31:00 +02003690 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003692 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 : _("[readonly]")) : "",
3694 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3695 || curbuf->b_p_ro) ?
3696 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003697 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3698 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (curwin->w_cursor.lnum > 1000000L)
3700 n = (int)(((long)curwin->w_cursor.lnum) /
3701 ((long)curbuf->b_ml.ml_line_count / 100L));
3702 else
3703 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3704 (long)curbuf->b_ml.ml_line_count);
3705 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003706 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#ifdef FEAT_CMDL_INFO
3708 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003709 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003710 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003711 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3712 curbuf->b_ml.ml_line_count),
3713 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#endif
3715 else
3716 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003717 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 _("line %ld of %ld --%d%%-- col "),
3719 (long)curwin->w_cursor.lnum,
3720 (long)curbuf->b_ml.ml_line_count,
3721 n);
3722 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003723 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003724 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3726 }
3727
Bram Moolenaar32526b32019-01-19 17:43:09 +01003728 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3729 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731 if (dont_truncate)
3732 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003733 // Temporarily set msg_scroll to avoid the message being truncated.
3734 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 msg_start();
3736 n = msg_scroll;
3737 msg_scroll = TRUE;
3738 msg(buffer);
3739 msg_scroll = n;
3740 }
3741 else
3742 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003743 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003745 // Need to repeat the message after redrawing when:
3746 // - When restart_edit is set (otherwise there will be a delay
3747 // before redrawing).
3748 // - When the screen was scrolled but there is no wait-return
3749 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003750 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752
3753 vim_free(buffer);
3754}
3755
3756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003757col_print(
3758 char_u *buf,
3759 size_t buflen,
3760 int col,
3761 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003764 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003766 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767}
3768
3769#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770static char_u *lasttitle = NULL;
3771static char_u *lasticon = NULL;
3772
Bram Moolenaar84a93082018-06-16 22:58:15 +02003773/*
3774 * Put the file name in the title bar and icon of the window.
3775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003780 char_u *title_str = NULL;
3781 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 int maxlen = 0;
3783 int len;
3784 int mustset;
3785 char_u buf[IOSIZE];
3786 int off;
3787
3788 if (!redrawing())
3789 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003790 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 need_maketitle = TRUE;
3792 return;
3793 }
3794
3795 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003796 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003797 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 if (p_title)
3800 {
3801 if (p_titlelen > 0)
3802 {
3803 maxlen = p_titlelen * Columns / 100;
3804 if (maxlen < 10)
3805 maxlen = 10;
3806 }
3807
Bram Moolenaar84a93082018-06-16 22:58:15 +02003808 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (*p_titlestring != NUL)
3810 {
3811#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003812 if (stl_syntax & STL_IN_TITLE)
3813 {
3814 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003815 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003816
3817# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003818 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003819# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003820 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003821 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003822 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003823 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003824 set_string_option_direct((char_u *)"titlestring", -1,
3825 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 else
3828#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003829 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 else
3832 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003833 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
Bram Moolenaar2c666692012-09-05 13:30:40 +02003835#define SPACE_FOR_FNAME (IOSIZE - 100)
3836#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003837#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003839 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003840#ifdef FEAT_TERMINAL
3841 else if (curbuf->b_term != NULL)
3842 {
3843 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3844 SPACE_FOR_FNAME);
3845 }
3846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 else
3848 {
3849 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003850 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 }
3853
Bram Moolenaar21554412017-07-24 21:44:43 +02003854#ifdef FEAT_TERMINAL
3855 if (curbuf->b_term == NULL)
3856#endif
3857 switch (bufIsChanged(curbuf)
3858 + (curbuf->b_p_ro * 2)
3859 + (!curbuf->b_p_ma * 4))
3860 {
3861 case 1: STRCAT(buf, " +"); break;
3862 case 2: STRCAT(buf, " ="); break;
3863 case 3: STRCAT(buf, " =+"); break;
3864 case 4:
3865 case 6: STRCAT(buf, " -"); break;
3866 case 5:
3867 case 7: STRCAT(buf, " -+"); break;
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
Bram Moolenaar21554412017-07-24 21:44:43 +02003870 if (curbuf->b_fname != NULL
3871#ifdef FEAT_TERMINAL
3872 && curbuf->b_term == NULL
3873#endif
3874 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003876 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 off = (int)STRLEN(buf);
3878 buf[off++] = ' ';
3879 buf[off++] = '(';
3880 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003881 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003883 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (isalpha(buf[off]) && buf[off + 1] == ':')
3885 off += 2;
3886#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003887 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003888 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003890 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003891 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003892 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003893 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003897
Bram Moolenaarc667da52019-11-30 20:52:27 +01003898 // Translate unprintable chars and concatenate. Keep some
3899 // room for the server name. When there is no room (very long
3900 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003901 if (off < SPACE_FOR_DIR)
3902 {
3903 p = transstr(buf + off);
3904 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3905 vim_free(p);
3906 }
3907 else
3908 {
3909 vim_strncpy(buf + off, (char_u *)"...",
3910 (size_t)(SPACE_FOR_ARGNR - off));
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 STRCAT(buf, ")");
3913 }
3914
Bram Moolenaar2c666692012-09-05 13:30:40 +02003915 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917#if defined(FEAT_CLIENTSERVER)
3918 if (serverName != NULL)
3919 {
3920 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003921 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 else
3924#endif
3925 STRCAT(buf, " - VIM");
3926
3927 if (maxlen > 0)
3928 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003929 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003930 if (vim_strsize(buf) > maxlen)
3931 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933 }
3934 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003935 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 if (p_icon)
3938 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003939 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (*p_iconstring != NUL)
3941 {
3942#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003943 if (stl_syntax & STL_IN_ICON)
3944 {
3945 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003946 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003947
3948# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003949 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003950# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003951 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003952 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003953 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003954 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003955 set_string_option_direct((char_u *)"iconstring", -1,
3956 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 else
3959#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003960 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 else
3963 {
3964 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003965 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003966 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003967 p = gettail(curbuf->b_ffname);
3968 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003969 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003970 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (len > 100)
3972 {
3973 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003975 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003976 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003978 STRCPY(icon_str, p);
3979 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
3982
Bram Moolenaar84a93082018-06-16 22:58:15 +02003983 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985 if (mustset)
3986 resettitle();
3987}
3988
3989/*
3990 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3991 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003992 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 */
3994 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003995value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
3997 if ((str == NULL) != (*last == NULL)
3998 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3999 {
4000 vim_free(*last);
4001 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004004 mch_restore_title(
4005 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004010 return TRUE;
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 return FALSE;
4014}
4015
4016/*
4017 * Put current window title back (used after calling a shell)
4018 */
4019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004020resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021{
4022 mch_settitle(lasttitle, lasticon);
4023}
Bram Moolenaarea408852005-06-25 22:49:46 +00004024
4025# if defined(EXITFREE) || defined(PROTO)
4026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004027free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004028{
4029 vim_free(lasttitle);
4030 vim_free(lasticon);
4031}
4032# endif
4033
Bram Moolenaarc667da52019-11-30 20:52:27 +01004034#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004036#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004037
4038/*
4039 * Used for building in the status line.
4040 */
4041typedef struct
4042{
4043 char_u *stl_start;
4044 int stl_minwid;
4045 int stl_maxwid;
4046 enum {
4047 Normal,
4048 Empty,
4049 Group,
4050 Middle,
4051 Highlight,
4052 TabPage,
4053 Trunc
4054 } stl_type;
4055} stl_item_T;
4056
4057static size_t stl_items_len = 20; // Initial value, grows as needed.
4058static stl_item_T *stl_items = NULL;
4059static int *stl_groupitem = NULL;
4060static stl_hlrec_T *stl_hltab = NULL;
4061static stl_hlrec_T *stl_tabtab = NULL;
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004064 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 * Return length of string in screen cells.
4066 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004067 * Normally works for window "wp", except when working for 'tabline' then it
4068 * is "curwin".
4069 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * Items are drawn interspersed with the text that surrounds it
4071 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4072 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4073 *
4074 * If maxwidth is not zero, the string will be filled at any middle marker
4075 * or truncated if too long, fillchar is used for all whitespace.
4076 */
4077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004078build_stl_str_hl(
4079 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004080 char_u *out, // buffer to write into != NameBuff
4081 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004082 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004083 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004084 int fillchar,
4085 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004086 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4087 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
Bram Moolenaar10772302019-01-20 18:25:54 +01004089 linenr_T lnum;
4090 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u *p;
4092 char_u *s;
4093 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004094 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004096 win_T *save_curwin;
4097 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004098 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#endif
4100 int empty_line;
4101 colnr_T virtcol;
4102 long l;
4103 long n;
4104 int prevchar_isflag;
4105 int prevchar_isitem;
4106 int itemisflag;
4107 int fillable;
4108 char_u *str;
4109 long num;
4110 int width;
4111 int itemcnt;
4112 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004113 int group_end_userhl;
4114 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 int groupdepth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 int minwid;
4117 int maxwid;
4118 int zeropad;
4119 char_u base;
4120 char_u opt;
4121#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004122 char_u buf_tmp[TMPLEN];
4123 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004124 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004125 stl_hlrec_T *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004126 int save_must_redraw = must_redraw;
4127 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004128
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004129 if (stl_items == NULL)
4130 {
4131 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4132 stl_groupitem = ALLOC_MULT(int, stl_items_len);
4133 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4134 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4135 }
4136
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004137#ifdef FEAT_EVAL
4138 /*
4139 * When the format starts with "%!" then evaluate it as an expression and
4140 * use the result as the actual format string.
4141 */
4142 if (fmt[0] == '%' && fmt[1] == '!')
4143 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004144 typval_T tv;
4145
4146 tv.v_type = VAR_NUMBER;
4147 tv.vval.v_number = wp->w_id;
4148 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4149
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004150 usefmt = eval_to_string_safe(fmt + 2, use_sandbox);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004151 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004152 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004153
4154 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004155 }
4156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 if (fillchar == 0)
4159 fillchar = ' ';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004160 // Can't handle a multi-byte fill character yet.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004161 else if (mb_char2len(fillchar) > 1)
4162 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004163
Bram Moolenaar10772302019-01-20 18:25:54 +01004164 // The cursor in windows other than the current one isn't always
4165 // up-to-date, esp. because of autocommands and timers.
4166 lnum = wp->w_cursor.lnum;
4167 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4168 {
4169 lnum = wp->w_buffer->b_ml.ml_line_count;
4170 wp->w_cursor.lnum = lnum;
4171 }
4172
4173 // Get line & check if empty (cursorpos will show "0-1"). Note that
4174 // p will become invalid when getting another buffer line.
4175 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004176 empty_line = (*p == NUL);
4177
Bram Moolenaar10772302019-01-20 18:25:54 +01004178 // Get the byte value now, in case we need it below. This is more efficient
4179 // than making a copy of the line.
4180 len = STRLEN(p);
4181 if (wp->w_cursor.col > (colnr_T)len)
4182 {
4183 // Line may have changed since checking the cursor column, or the lnum
4184 // was adjusted above.
4185 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004186 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004187 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004188 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004189 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004190 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 groupdepth = 0;
4193 p = out;
4194 curitem = 0;
4195 prevchar_isflag = TRUE;
4196 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004197 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004199 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004200 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004201 size_t new_len = stl_items_len * 3 / 2;
4202 stl_item_T *new_items;
4203 int *new_groupitem;
4204 stl_hlrec_T *new_hlrec;
4205
4206 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4207 if (new_items == NULL)
4208 break;
4209 stl_items = new_items;
4210 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4211 if (new_groupitem == NULL)
4212 break;
4213 stl_groupitem = new_groupitem;
4214 new_hlrec = vim_realloc(stl_hltab, sizeof(stl_hlrec_T) * new_len);
4215 if (new_hlrec == NULL)
4216 break;
4217 stl_hltab = new_hlrec;
4218 new_hlrec = vim_realloc(stl_tabtab, sizeof(stl_hlrec_T) * new_len);
4219 if (new_hlrec == NULL)
4220 break;
4221 stl_tabtab = new_hlrec;
4222 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004223 }
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 if (*s != NUL && *s != '%')
4226 prevchar_isflag = prevchar_isitem = FALSE;
4227
4228 /*
4229 * Handle up to the next '%' or the end.
4230 */
4231 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4232 *p++ = *s++;
4233 if (*s == NUL || p + 1 >= out + outlen)
4234 break;
4235
4236 /*
4237 * Handle one '%' item.
4238 */
4239 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004240 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 if (*s == '%')
4243 {
4244 if (p + 1 >= out + outlen)
4245 break;
4246 *p++ = *s++;
4247 prevchar_isflag = prevchar_isitem = FALSE;
4248 continue;
4249 }
4250 if (*s == STL_MIDDLEMARK)
4251 {
4252 s++;
4253 if (groupdepth > 0)
4254 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004255 stl_items[curitem].stl_type = Middle;
4256 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 continue;
4258 }
4259 if (*s == STL_TRUNCMARK)
4260 {
4261 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004262 stl_items[curitem].stl_type = Trunc;
4263 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 continue;
4265 }
4266 if (*s == ')')
4267 {
4268 s++;
4269 if (groupdepth < 1)
4270 continue;
4271 groupdepth--;
4272
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004273 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 *p = NUL;
4275 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004276 if (curitem > stl_groupitem[groupdepth] + 1
4277 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004279 // remove group if all items are empty and highlight group
4280 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004281 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004282 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004283 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004284 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004285 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004286 group_start_userhl = group_end_userhl =
4287 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004288 break;
4289 }
4290 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004291 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004292 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004293 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004295 if (stl_items[n].stl_type == Highlight)
4296 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004297 }
4298 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004300 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 p = t;
4302 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004303 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004304 {
4305 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004306 if (stl_items[n].stl_type == Highlight)
4307 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004308 // adjust the start position of TabPage to the next
4309 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004310 if (stl_items[n].stl_type == TabPage)
4311 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004315 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004317 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (has_mbyte)
4319 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004320 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004322 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004325 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 }
4328 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004329 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4330 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
4332 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004333 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004335
4336 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004337 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaarc667da52019-11-30 20:52:27 +01004340 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004341 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004343 stl_items[l].stl_start -= n;
4344 if (stl_items[l].stl_start < t)
4345 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004348 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004350 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004351 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (n < 0)
4353 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004354 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 n = 0 - n;
4356 while (l++ < n && p + 1 < out + outlen)
4357 *p++ = fillchar;
4358 }
4359 else
4360 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004361 // fill by inserting characters
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004362 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 l = n - l;
4364 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004365 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004367 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4368 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 for ( ; l > 0; l--)
4370 *t++ = fillchar;
4371 }
4372 }
4373 continue;
4374 }
4375 minwid = 0;
4376 maxwid = 9999;
4377 zeropad = FALSE;
4378 l = 1;
4379 if (*s == '0')
4380 {
4381 s++;
4382 zeropad = TRUE;
4383 }
4384 if (*s == '-')
4385 {
4386 s++;
4387 l = -1;
4388 }
4389 if (VIM_ISDIGIT(*s))
4390 {
4391 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004392 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 minwid = 0;
4394 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004395 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004397 stl_items[curitem].stl_type = Highlight;
4398 stl_items[curitem].stl_start = p;
4399 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 s++;
4401 curitem++;
4402 continue;
4403 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004404 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4405 {
4406 if (*s == STL_TABCLOSENR)
4407 {
4408 if (minwid == 0)
4409 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004410 // %X ends the close label, go back to the previously
4411 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004412 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004413 if (stl_items[n].stl_type == TabPage
4414 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004415 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004416 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004417 break;
4418 }
4419 }
4420 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004421 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004422 minwid = - minwid;
4423 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 stl_items[curitem].stl_type = TabPage;
4425 stl_items[curitem].stl_start = p;
4426 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004427 s++;
4428 curitem++;
4429 continue;
4430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (*s == '.')
4432 {
4433 s++;
4434 if (VIM_ISDIGIT(*s))
4435 {
4436 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004437 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 maxwid = 50;
4439 }
4440 }
4441 minwid = (minwid > 50 ? 50 : minwid) * l;
4442 if (*s == '(')
4443 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004444 stl_groupitem[groupdepth++] = curitem;
4445 stl_items[curitem].stl_type = Group;
4446 stl_items[curitem].stl_start = p;
4447 stl_items[curitem].stl_minwid = minwid;
4448 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 s++;
4450 curitem++;
4451 continue;
4452 }
4453 if (vim_strchr(STL_ALL, *s) == NULL)
4454 {
4455 s++;
4456 continue;
4457 }
4458 opt = *s++;
4459
Bram Moolenaarc667da52019-11-30 20:52:27 +01004460 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 base = 'D';
4462 itemisflag = FALSE;
4463 fillable = TRUE;
4464 num = -1;
4465 str = NULL;
4466 switch (opt)
4467 {
4468 case STL_FILEPATH:
4469 case STL_FULLPATH:
4470 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004471 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004473 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 else
4475 {
4476 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004477 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4479 }
4480 trans_characters(NameBuff, MAXPATHL);
4481 if (opt != STL_FILENAME)
4482 str = NameBuff;
4483 else
4484 str = gettail(NameBuff);
4485 break;
4486
Bram Moolenaarc667da52019-11-30 20:52:27 +01004487 case STL_VIM_EXPR: // '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 itemisflag = TRUE;
4489 t = p;
4490 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4491 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004492 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
4494 s++;
4495 *p = 0;
4496 p = t;
4497
4498#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004499 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4500 "%d", curbuf->b_fnum);
4501 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4502 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4503 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004505 save_curbuf = curbuf;
4506 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004507 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 curwin = wp;
4509 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004510 // Visual mode is only valid in the current window.
4511 if (curwin != save_curwin)
4512 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004514 str = eval_to_string_safe(p, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004516 curwin = save_curwin;
4517 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004518 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004519 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004520 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 if (str != NULL && *str != 0)
4523 {
4524 if (*skipdigits(str) == NUL)
4525 {
4526 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004527 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 itemisflag = FALSE;
4529 }
4530 }
4531#endif
4532 break;
4533
4534 case STL_LINE:
4535 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4536 ? 0L : (long)(wp->w_cursor.lnum);
4537 break;
4538
4539 case STL_NUMLINES:
4540 num = wp->w_buffer->b_ml.ml_line_count;
4541 break;
4542
4543 case STL_COLUMN:
4544 num = !(State & INSERT) && empty_line
4545 ? 0 : (int)wp->w_cursor.col + 1;
4546 break;
4547
4548 case STL_VIRTCOL:
4549 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004550 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 virtcol = wp->w_virtcol;
4552 if (wp->w_p_list && lcs_tab1 == NUL)
4553 {
4554 wp->w_p_list = FALSE;
4555 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4556 wp->w_p_list = TRUE;
4557 }
4558 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004559 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (opt == STL_VIRTCOL_ALT
4561 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4562 ? 0 : (int)wp->w_cursor.col + 1)))
4563 break;
4564 num = (long)virtcol;
4565 break;
4566
4567 case STL_PERCENTAGE:
4568 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4569 (long)wp->w_buffer->b_ml.ml_line_count);
4570 break;
4571
4572 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004573 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004574 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 break;
4576
4577 case STL_ARGLISTSTAT:
4578 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004579 buf_tmp[0] = 0;
4580 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4581 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 break;
4583
4584 case STL_KEYMAP:
4585 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004586 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4587 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 break;
4589 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004590#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004591 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#else
4593 num = 0;
4594#endif
4595 break;
4596
4597 case STL_BUFNO:
4598 num = wp->w_buffer->b_fnum;
4599 break;
4600
4601 case STL_OFFSET_X:
4602 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004603 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 case STL_OFFSET:
4605#ifdef FEAT_BYTEOFF
4606 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4607 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4608 0L : l + 1 + (!(State & INSERT) && empty_line ?
4609 0 : (int)wp->w_cursor.col);
4610#endif
4611 break;
4612
4613 case STL_BYTEVAL_X:
4614 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004615 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004617 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (num == NL)
4619 num = 0;
4620 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4621 num = NL;
4622 break;
4623
4624 case STL_ROFLAG:
4625 case STL_ROFLAG_ALT:
4626 itemisflag = TRUE;
4627 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004628 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630
4631 case STL_HELPFLAG:
4632 case STL_HELPFLAG_ALT:
4633 itemisflag = TRUE;
4634 if (wp->w_buffer->b_help)
4635 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004636 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 case STL_FILETYPE:
4640 if (*wp->w_buffer->b_p_ft != NUL
4641 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4642 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004643 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004644 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004645 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647 break;
4648
4649 case STL_FILETYPE_ALT:
4650 itemisflag = TRUE;
4651 if (*wp->w_buffer->b_p_ft != NUL
4652 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4653 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004654 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004655 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004656 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004658 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaar4033c552017-09-16 20:54:51 +02004662#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 case STL_PREVIEWFLAG:
4664 case STL_PREVIEWFLAG_ALT:
4665 itemisflag = TRUE;
4666 if (wp->w_p_pvw)
4667 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4668 : _("[Preview]"));
4669 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004670
4671 case STL_QUICKFIX:
4672 if (bt_quickfix(wp->w_buffer))
4673 str = (char_u *)(wp->w_llist_ref
4674 ? _(msg_loclist)
4675 : _(msg_qflist));
4676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#endif
4678
4679 case STL_MODIFIED:
4680 case STL_MODIFIED_ALT:
4681 itemisflag = TRUE;
4682 switch ((opt == STL_MODIFIED_ALT)
4683 + bufIsChanged(wp->w_buffer) * 2
4684 + (!wp->w_buffer->b_p_ma) * 4)
4685 {
4686 case 2: str = (char_u *)"[+]"; break;
4687 case 3: str = (char_u *)",+"; break;
4688 case 4: str = (char_u *)"[-]"; break;
4689 case 5: str = (char_u *)",-"; break;
4690 case 6: str = (char_u *)"[+-]"; break;
4691 case 7: str = (char_u *)",+-"; break;
4692 }
4693 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004694
4695 case STL_HIGHLIGHT:
4696 t = s;
4697 while (*s != '#' && *s != NUL)
4698 ++s;
4699 if (*s == '#')
4700 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004701 stl_items[curitem].stl_type = Highlight;
4702 stl_items[curitem].stl_start = p;
4703 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004704 curitem++;
4705 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004706 if (*s != NUL)
4707 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004708 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004711 stl_items[curitem].stl_start = p;
4712 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (str != NULL && *str)
4714 {
4715 t = str;
4716 if (itemisflag)
4717 {
4718 if ((t[0] && t[1])
4719 && ((!prevchar_isitem && *t == ',')
4720 || (prevchar_isflag && *t == ' ')))
4721 t++;
4722 prevchar_isflag = TRUE;
4723 }
4724 l = vim_strsize(t);
4725 if (l > 0)
4726 prevchar_isitem = TRUE;
4727 if (l > maxwid)
4728 {
4729 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (has_mbyte)
4731 {
4732 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004733 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 l -= byte2cells(*t++);
4737 if (p + 1 >= out + outlen)
4738 break;
4739 *p++ = '<';
4740 }
4741 if (minwid > 0)
4742 {
4743 for (; l < minwid && p + 1 < out + outlen; l++)
4744 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004745 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4747 *p++ = ' ';
4748 else
4749 *p++ = fillchar;
4750 }
4751 minwid = 0;
4752 }
4753 else
4754 minwid *= -1;
4755 while (*t && p + 1 < out + outlen)
4756 {
4757 *p++ = *t++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004758 // Change a space by fillchar, unless fillchar is '-' and a
4759 // digit follows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 if (fillable && p[-1] == ' '
4761 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4762 p[-1] = fillchar;
4763 }
4764 for (; l < minwid && p + 1 < out + outlen; l++)
4765 *p++ = fillchar;
4766 }
4767 else if (num >= 0)
4768 {
4769 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4770 char_u nstr[20];
4771
4772 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004773 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 prevchar_isitem = TRUE;
4775 t = nstr;
4776 if (opt == STL_VIRTCOL_ALT)
4777 {
4778 *t++ = '-';
4779 minwid--;
4780 }
4781 *t++ = '%';
4782 if (zeropad)
4783 *t++ = '0';
4784 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004785 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 *t = 0;
4787
4788 for (n = num, l = 1; n >= nbase; n /= nbase)
4789 l++;
4790 if (opt == STL_VIRTCOL_ALT)
4791 l++;
4792 if (l > maxwid)
4793 {
4794 l += 2;
4795 n = l - maxwid;
4796 while (l-- > maxwid)
4797 num /= nbase;
4798 *t++ = '>';
4799 *t++ = '%';
4800 *t = t[-3];
4801 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004802 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4803 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004806 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4807 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 p += STRLEN(p);
4809 }
4810 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004811 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 if (opt == STL_VIM_EXPR)
4814 vim_free(str);
4815
4816 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004817 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 curitem++;
4819 }
4820 *p = NUL;
4821 itemcnt = curitem;
4822
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004823#ifdef FEAT_EVAL
4824 if (usefmt != fmt)
4825 vim_free(usefmt);
4826#endif
4827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 width = vim_strsize(out);
4829 if (maxwidth > 0 && width > maxwidth)
4830 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004831 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 l = 0;
4833 if (itemcnt == 0)
4834 s = out;
4835 else
4836 {
4837 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004838 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004840 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004841 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 break;
4843 }
4844 if (l == itemcnt)
4845 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004846 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004847 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 l = 0;
4849 }
4850 }
4851
4852 if (width - vim_strsize(s) >= maxwidth)
4853 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004854 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (has_mbyte)
4856 {
4857 s = out;
4858 width = 0;
4859 for (;;)
4860 {
4861 width += ptr2cells(s);
4862 if (width >= maxwidth)
4863 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004864 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004866 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 while (++width < maxwidth)
4868 *s++ = fillchar;
4869 }
4870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 s = out + maxwidth - 1;
4872 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004873 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 break;
4875 itemcnt = l;
4876 *s++ = '>';
4877 *s = 0;
4878 }
4879 else
4880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (has_mbyte)
4882 {
4883 n = 0;
4884 while (width >= maxwidth)
4885 {
4886 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004887 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 }
4890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 n = width - maxwidth + 1;
4892 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004893 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 *s = '<';
4895
Bram Moolenaarc667da52019-11-30 20:52:27 +01004896 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 while (++width < maxwidth)
4898 {
4899 s = s + STRLEN(s);
4900 *s++ = fillchar;
4901 *s = NUL;
4902 }
4903
Bram Moolenaarc667da52019-11-30 20:52:27 +01004904 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 for (; l < itemcnt; l++)
4906 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004907 if (stl_items[l].stl_start - n >= s)
4908 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004910 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 }
4913 width = maxwidth;
4914 }
4915 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4916 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004917 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004919 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 break;
4921 if (l < itemcnt)
4922 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004923 p = stl_items[l].stl_start + maxwidth - width;
4924 STRMOVE(p, stl_items[l].stl_start);
4925 for (s = stl_items[l].stl_start; s < p; s++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 *s = fillchar;
4927 for (l++; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004928 stl_items[l].stl_start += maxwidth - width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 width = maxwidth;
4930 }
4931 }
4932
Bram Moolenaarc667da52019-11-30 20:52:27 +01004933 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004934 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004936 *hltab = stl_hltab;
4937 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 for (l = 0; l < itemcnt; l++)
4939 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004940 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004942 sp->start = stl_items[l].stl_start;
4943 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004944 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004947 sp->start = NULL;
4948 sp->userhl = 0;
4949 }
4950
Bram Moolenaarc667da52019-11-30 20:52:27 +01004951 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004952 if (tabtab != NULL)
4953 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004954 *tabtab = stl_tabtab;
4955 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004956 for (l = 0; l < itemcnt; l++)
4957 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004958 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004959 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004960 sp->start = stl_items[l].stl_start;
4961 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004962 sp++;
4963 }
4964 }
4965 sp->start = NULL;
4966 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968
Bram Moolenaarc667da52019-11-30 20:52:27 +01004969 // When inside update_screen we do not want redrawing a stausline, ruler,
4970 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004971 if (updating_screen)
4972 {
4973 must_redraw = save_must_redraw;
4974 curwin->w_redr_type = save_redr_type;
4975 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 return width;
4978}
Bram Moolenaarc667da52019-11-30 20:52:27 +01004979#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004981#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4982 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004984 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4985 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
4987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004988get_rel_pos(
4989 win_T *wp,
4990 char_u *buf,
4991 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaarc667da52019-11-30 20:52:27 +01004993 long above; // number of lines above window
4994 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
Bram Moolenaarc667da52019-11-30 20:52:27 +01004996 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01004997 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 above = wp->w_topline - 1;
4999#ifdef FEAT_DIFF
5000 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005001 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005002 above = 0; // All buffer lines are displayed and there is an
5003 // indication of filler lines, that can be considered
5004 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#endif
5006 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5007 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005008 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5009 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005011 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005013 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 ? (int)(above / ((above + below) / 100L))
5015 : (int)(above * 100L / (above + below)));
5016}
5017#endif
5018
5019/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005020 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 * Return TRUE if it was appended.
5022 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005024append_arg_number(
5025 win_T *wp,
5026 char_u *buf,
5027 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005028 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030 char_u *p;
5031
Bram Moolenaarc667da52019-11-30 20:52:27 +01005032 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 return FALSE;
5034
Bram Moolenaarc667da52019-11-30 20:52:27 +01005035 p = buf + STRLEN(buf); // go to the end of the buffer
5036 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 return FALSE;
5038 *p++ = ' ';
5039 *p++ = '(';
5040 if (add_file)
5041 {
5042 STRCPY(p, "file ");
5043 p += 5;
5044 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005045 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5046 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5048 return TRUE;
5049}
5050
5051/*
5052 * If fname is not a full path, make it a full path.
5053 * Returns pointer to allocated memory (NULL for failure).
5054 */
5055 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005056fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 /*
5059 * Force expanding the path always for Unix, because symbolic links may
5060 * mess up the full path name, even though it starts with a '/'.
5061 * Also expand when there is ".." in the file name, try to remove it,
5062 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005063 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 * For MS-Windows also expand names like "longna~1" to "longname".
5065 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005066#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 return FullName_save(fname, TRUE);
5068#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005069 if (!vim_isAbsName(fname)
5070 || strstr((char *)fname, "..") != NULL
5071 || strstr((char *)fname, "//") != NULL
5072# ifdef BACKSLASH_IN_FILENAME
5073 || strstr((char *)fname, "\\\\") != NULL
5074# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005075# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 )
5079 return FullName_save(fname, FALSE);
5080
5081 fname = vim_strsave(fname);
5082
Bram Moolenaar9b942202007-10-03 12:31:33 +00005083# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005084 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005085 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 return fname;
5089#endif
5090}
5091
5092/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005093 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5094 * "*ffname" becomes a pointer to allocated memory (or NULL).
5095 * When resolving a link both "*sfname" and "*ffname" will point to the same
5096 * allocated memory.
5097 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005098 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005101fname_expand(
5102 buf_T *buf UNUSED,
5103 char_u **ffname,
5104 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005106 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005108 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005110 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112#ifdef FEAT_SHORTCUT
5113 if (!buf->b_p_bin)
5114 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005115 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005117 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005118 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005119 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 vim_free(*ffname);
5122 *ffname = rfname;
5123 *sfname = rfname;
5124 }
5125 }
5126#endif
5127}
5128
5129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 * Open a window for a number of buffers.
5131 */
5132 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005133ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
5135 buf_T *buf;
5136 win_T *wp, *wpnext;
5137 int split_ret = OK;
5138 int p_ea_save;
5139 int open_wins = 0;
5140 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005141 int count; // Maximum number of windows to open.
5142 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005143 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005144 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaarc667da52019-11-30 20:52:27 +01005146 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 count = 9999;
5148 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005149 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5151 all = FALSE;
5152 else
5153 all = TRUE;
5154
5155 setpcmark();
5156
5157#ifdef FEAT_GUI
5158 need_mouse_correct = TRUE;
5159#endif
5160
5161 /*
5162 * Close superfluous windows (two windows for the same buffer).
5163 * Also close windows that are not full-width.
5164 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005165 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005166 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005167 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005169 tpnext = curtab->tp_next;
5170 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005172 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005173 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1004402020-10-24 20:49:43 +02005174 || ((cmdmod.cmod_split & WSP_VERT)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005176 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005177 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005178 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005179 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005180 {
5181 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005182 wpnext = firstwin; // just in case an autocommand does
5183 // something strange with windows
5184 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005185 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005186 }
5187 else
5188 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190
Bram Moolenaarc667da52019-11-30 20:52:27 +01005191 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 if (had_tab == 0 || tpnext == NULL)
5193 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005194 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196
5197 /*
5198 * Go through the buffer list. When a buffer doesn't have a window yet,
5199 * open one. Otherwise move the window to the right position.
5200 * Watch out for autocommands that delete buffers or windows!
5201 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005202 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5207 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005208 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5210 continue;
5211
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005212 if (had_tab != 0)
5213 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005214 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005215 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005216 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005217 else
5218 wp = NULL;
5219 }
5220 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005221 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005222 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005223 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005224 if (wp->w_buffer == buf)
5225 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005226 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005227 if (wp != NULL)
5228 win_move_after(wp, curwin);
5229 }
5230
5231 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005233 bufref_T bufref;
5234
5235 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005236
Bram Moolenaarc667da52019-11-30 20:52:27 +01005237 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005239 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5241 ++open_wins;
5242 p_ea = p_ea_save;
5243 if (split_ret == FAIL)
5244 continue;
5245
Bram Moolenaarc667da52019-11-30 20:52:27 +01005246 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005249 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005251 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 break;
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (swap_exists_action == SEA_QUIT)
5256 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005257#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005258 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005259
Bram Moolenaarc667da52019-11-30 20:52:27 +01005260 // Reset the error/interrupt/exception state here so that
5261 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005262 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005263#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005264
Bram Moolenaarc667da52019-11-30 20:52:27 +01005265 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 win_close(curwin, TRUE);
5267 --open_wins;
5268 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005269 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005270
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005271#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005272 // Restore the error/interrupt/exception state if not
5273 // discarded by a new aborting error, interrupt, or uncaught
5274 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005275 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
5278 else
5279 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281
5282 ui_breakcheck();
5283 if (got_int)
5284 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005285 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 break;
5287 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005288#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005289 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005290 if (aborting())
5291 break;
5292#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005293 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005294 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005295 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005298 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Close superfluous windows.
5303 */
5304 for (wp = lastwin; open_wins > count; )
5305 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005306 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (!win_valid(wp))
5309 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005310 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 wp = lastwin;
5312 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005313 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005315 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 --open_wins;
5317 wp = lastwin;
5318 }
5319 else
5320 {
5321 wp = wp->w_prev;
5322 if (wp == NULL)
5323 break;
5324 }
5325 }
5326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005329static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331/*
5332 * do_modelines() - process mode lines for the current file
5333 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005334 * "flags" can be:
5335 * OPT_WINONLY only set options local to window
5336 * OPT_NOWIN don't set options local to window
5337 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 * Returns immediately if the "ml" option isn't set.
5339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005343 linenr_T lnum;
5344 int nmlines;
5345 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5348 return;
5349
Bram Moolenaarc667da52019-11-30 20:52:27 +01005350 // Disallow recursive entry here. Can happen when executing a modeline
5351 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 if (entered)
5353 return;
5354
5355 ++entered;
5356 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5357 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005358 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 nmlines = 0;
5360
5361 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5362 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005363 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 nmlines = 0;
5365 --entered;
5366}
5367
Bram Moolenaarc667da52019-11-30 20:52:27 +01005368#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370/*
5371 * chk_modeline() - check a single line for a mode string
5372 * Return FAIL if an error encountered.
5373 */
5374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005375chk_modeline(
5376 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005377 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 char_u *s;
5380 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005381 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 int prev;
5383 int vers;
5384 int end;
5385 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005386 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005387
Bram Moolenaare31ee862020-01-07 20:59:34 +01005388 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390 prev = -1;
5391 for (s = ml_get(lnum); *s != NUL; ++s)
5392 {
5393 if (prev == -1 || vim_isspace(prev))
5394 {
5395 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5396 || STRNCMP(s, "vi:", (size_t)3) == 0)
5397 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005398 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005399 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5402 e = s + 4;
5403 else
5404 e = s + 3;
5405 vers = getdigits(&e);
5406 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005407 && (s[0] != 'V'
5408 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 && (s[3] == ':'
5410 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5411 || (VIM_VERSION_100 < vers && s[3] == '<')
5412 || (VIM_VERSION_100 > vers && s[3] == '>')
5413 || (VIM_VERSION_100 == vers && s[3] == '=')))
5414 break;
5415 }
5416 }
5417 prev = *s;
5418 }
5419
5420 if (*s)
5421 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005422 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 ++s;
5424 while (s[-1] != ':');
5425
Bram Moolenaarc667da52019-11-30 20:52:27 +01005426 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 if (linecopy == NULL)
5428 return FAIL;
5429
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005430 // prepare for emsg()
5431 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005432 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
5434 end = FALSE;
5435 while (end == FALSE)
5436 {
5437 s = skipwhite(s);
5438 if (*s == NUL)
5439 break;
5440
5441 /*
5442 * Find end of set command: ':' or end of line.
5443 * Skip over "\:", replacing it with ":".
5444 */
5445 for (e = s; *e != ':' && *e != NUL; ++e)
5446 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005447 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 if (*e == NUL)
5449 end = TRUE;
5450
5451 /*
5452 * If there is a "set" command, require a terminating ':' and
5453 * ignore the stuff after the ':'.
5454 * "vi:set opt opt opt: foo" -- foo not interpreted
5455 * "vi:opt opt opt: foo" -- foo interpreted
5456 * Accept "se" for compatibility with Elvis.
5457 */
5458 if (STRNCMP(s, "set ", (size_t)4) == 0
5459 || STRNCMP(s, "se ", (size_t)3) == 0)
5460 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005461 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 break;
5463 end = TRUE;
5464 s = vim_strchr(s, ' ') + 1;
5465 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005466 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
Bram Moolenaarc667da52019-11-30 20:52:27 +01005468 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005470 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005471
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005472 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005473 current_sctx.sc_version = 1;
5474#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005475 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005476 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005477 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005479
Bram Moolenaar5958f952018-11-20 04:25:21 +01005480 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005481 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005482
Bram Moolenaara3227e22006-03-08 21:32:40 +00005483 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005484
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005485 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005486 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005487 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 break;
5489 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005490 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492
Bram Moolenaare31ee862020-01-07 20:59:34 +01005493 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005494 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 vim_free(linecopy);
5496 }
5497 return retval;
5498}
5499
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005500/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005501 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5502 */
5503 int
5504bt_normal(buf_T *buf)
5505{
5506 return buf != NULL && buf->b_p_bt[0] == NUL;
5507}
5508
Bram Moolenaar113e1072019-01-20 15:30:40 +01005509#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005510/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005511 * Return TRUE if "buf" is the quickfix buffer.
5512 */
5513 int
5514bt_quickfix(buf_T *buf)
5515{
5516 return buf != NULL && buf->b_p_bt[0] == 'q';
5517}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005518#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005519
Bram Moolenaar113e1072019-01-20 15:30:40 +01005520#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005521/*
5522 * Return TRUE if "buf" is a terminal buffer.
5523 */
5524 int
5525bt_terminal(buf_T *buf)
5526{
5527 return buf != NULL && buf->b_p_bt[0] == 't';
5528}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005529#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005530
5531/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005532 * Return TRUE if "buf" is a help buffer.
5533 */
5534 int
5535bt_help(buf_T *buf)
5536{
5537 return buf != NULL && buf->b_help;
5538}
5539
5540/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005541 * Return TRUE if "buf" is a prompt buffer.
5542 */
5543 int
5544bt_prompt(buf_T *buf)
5545{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005546 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5547}
5548
5549/*
5550 * Return TRUE if "buf" is a buffer for a popup window.
5551 */
5552 int
5553bt_popup(buf_T *buf)
5554{
5555 return buf != NULL && buf->b_p_bt != NULL
5556 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005557}
5558
5559/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005560 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5561 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005562 */
5563 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005564bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005565{
5566 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5567 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005568 || buf->b_p_bt[0] == 't'
5569 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005570}
5571
5572/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005573 * Return TRUE if "buf" has 'buftype' set to "nofile".
5574 */
5575 int
5576bt_nofile(buf_T *buf)
5577{
5578 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5579}
5580
5581/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005582 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5583 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005584 */
5585 int
5586bt_dontwrite(buf_T *buf)
5587{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005588 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005589 || buf->b_p_bt[0] == 't'
5590 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005591}
5592
Bram Moolenaar113e1072019-01-20 15:30:40 +01005593#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005594 int
5595bt_dontwrite_msg(buf_T *buf)
5596{
5597 if (bt_dontwrite(buf))
5598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005599 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005600 return TRUE;
5601 }
5602 return FALSE;
5603}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005604#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005605
5606/*
5607 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5608 * and 'bufhidden'.
5609 */
5610 int
5611buf_hide(buf_T *buf)
5612{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005613 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005614 switch (buf->b_p_bh[0])
5615 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005616 case 'u': // "unload"
5617 case 'w': // "wipe"
5618 case 'd': return FALSE; // "delete"
5619 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005620 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005621 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624/*
5625 * Return special buffer name.
5626 * Returns NULL when the buffer has a normal file name.
5627 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005631#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005633 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005634 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005635 * Differentiate between the quickfix and location list buffers using
5636 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005637 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005638 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005639 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005640 else
5641 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005644
Bram Moolenaarc667da52019-11-30 20:52:27 +01005645 // There is no _file_ when 'buftype' is "nofile", b_sfname
5646 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005647 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005649#ifdef FEAT_TERMINAL
5650 if (buf->b_term != NULL)
5651 return term_get_status_text(buf->b_term);
5652#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005653 if (buf->b_fname != NULL)
5654 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005655#ifdef FEAT_JOB_CHANNEL
5656 if (bt_prompt(buf))
5657 return (char_u *)_("[Prompt]");
5658#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005659#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005660 if (bt_popup(buf))
5661 return (char_u *)_("[Popup]");
5662#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005663 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005665
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005667 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 return NULL;
5669}
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005672 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5673 */
5674 char_u *
5675buf_get_fname(buf_T *buf)
5676{
5677 if (buf->b_fname == NULL)
5678 return (char_u *)_("[No Name]");
5679 return buf->b_fname;
5680}
5681
5682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5684 */
5685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005686set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 if (on != curbuf->b_p_bl)
5689 {
5690 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 if (on)
5692 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5693 else
5694 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696}
5697
5698/*
5699 * Read the file for "buf" again and check if the contents changed.
5700 * Return TRUE if it changed or this could not be checked.
5701 */
5702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005703buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705 buf_T *newbuf;
5706 int differ = TRUE;
5707 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 exarg_T ea;
5710
Bram Moolenaarc667da52019-11-30 20:52:27 +01005711 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5713 if (newbuf == NULL)
5714 return TRUE;
5715
Bram Moolenaarc667da52019-11-30 20:52:27 +01005716 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (prep_exarg(&ea, buf) == FAIL)
5718 {
5719 wipe_buffer(newbuf, FALSE);
5720 return TRUE;
5721 }
5722
Bram Moolenaarc667da52019-11-30 20:52:27 +01005723 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar4770d092006-01-12 23:22:24 +00005726 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 && readfile(buf->b_ffname, buf->b_fname,
5728 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5729 &ea, READ_NEW | READ_DUMMY) == OK)
5730 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005731 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5733 {
5734 differ = FALSE;
5735 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5736 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5737 {
5738 differ = TRUE;
5739 break;
5740 }
5741 }
5742 }
5743 vim_free(ea.cmd);
5744
Bram Moolenaarc667da52019-11-30 20:52:27 +01005745 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaarc667da52019-11-30 20:52:27 +01005748 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 wipe_buffer(newbuf, FALSE);
5750
5751 return differ;
5752}
5753
5754/*
5755 * Wipe out a buffer and decrement the last buffer number if it was used for
5756 * this buffer. Call this to wipe out a temp buffer that does not contain any
5757 * marks.
5758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005760wipe_buffer(
5761 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005762 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 if (buf->b_fnum == top_file_num - 1)
5765 --top_file_num;
5766
Bram Moolenaarc667da52019-11-30 20:52:27 +01005767 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005768 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005769
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005770 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005773 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774}