blob: be7f1fa8d4167445a4530228b87d575bda9f9a02 [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
shadmansaleh30e3de22021-05-15 17:23:28 +020030
31#ifdef FEAT_EVAL
32// Determines how deeply nested %{} blocks will be evaluated in statusline.
33# define MAX_STL_EVAL_DEPTH 100
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void enter_buffer(buf_T *buf);
37static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020041static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
42static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
43static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +020047static int value_changed(char_u *str, char_u **last);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010048static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
49static void free_buffer(buf_T *);
50static void free_buffer_stuff(buf_T *buf, int free_options);
Bram Moolenaarc3126192022-08-26 12:58:17 +010051static int bt_nofileread(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef UNIX
54# define dev_T dev_t
55#else
56# define dev_T unsigned
57#endif
58
Bram Moolenaar00d253e2020-04-06 22:13:01 +020059#define FOR_ALL_BUFS_FROM_LAST(buf) \
60 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
61
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
66
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020067// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020068static int buf_free_count = 0;
69
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020070static int top_file_num = 1; // highest file number
71static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
72
73/*
74 * Return the highest possible buffer number.
75 */
76 int
77get_highest_fnum(void)
78{
79 return top_file_num - 1;
80}
81
Bram Moolenaarc024b462019-06-08 18:07:21 +020082/*
83 * Read data from buffer for retrying.
84 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020085 static int
86read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010087 int read_stdin, // read file from stdin, otherwise fifo
88 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
89 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020090{
91 int retval = OK;
92 linenr_T line_count;
93
Bram Moolenaarc5935a82021-10-19 20:48:52 +010094 // Read from the buffer which the text is already filled in and append at
95 // the end. This makes it possible to retry when 'fileformat' or
96 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020097 line_count = curbuf->b_ml.ml_line_count;
98 retval = readfile(
99 read_stdin ? NULL : curbuf->b_ffname,
100 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100101 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200102 flags | READ_BUFFER);
103 if (retval == OK)
104 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100105 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200106 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200107 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200108 }
109 else
110 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100111 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200112 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200113 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100115 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200116 curwin->w_cursor.lnum = 1;
117 curwin->w_cursor.col = 0;
118
119 if (read_stdin)
120 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100121 // Set or reset 'modified' before executing autocommands, so that
122 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100123 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100125 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200126 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 if (retval == OK)
129 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100130#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100131 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132 curbuf, &retval);
133#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100134 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200135#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100136 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137 }
138 return retval;
139}
140
Dominique Pelle748b3082022-01-08 12:41:16 +0000141#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200143 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
144 */
145 void
146buffer_ensure_loaded(buf_T *buf)
147{
148 if (buf->b_ml.ml_mfp == NULL)
149 {
150 aco_save_T aco;
151
152 aucmd_prepbuf(&aco, buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +0100153 if (swap_exists_action != SEA_READONLY)
154 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200155 open_buffer(FALSE, NULL, 0);
156 aucmd_restbuf(&aco);
157 }
158}
Dominique Pelle748b3082022-01-08 12:41:16 +0000159#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200160
161/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200162 * Open current buffer, that is: open the memfile and read the file into
163 * memory.
164 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 */
166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100167open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100168 int read_stdin, // read file from stdin
169 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100170 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100172 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200174 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100175#ifdef FEAT_SYN_HL
176 long old_tw = curbuf->b_p_tw;
177#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200178 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100180 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
181 // When re-entering the same buffer, it should not change, because the
182 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 if (readonlymode && curbuf->b_ffname != NULL
184 && (curbuf->b_flags & BF_NEVERLOADED))
185 curbuf->b_p_ro = TRUE;
186
Bram Moolenaar4770d092006-01-12 23:22:24 +0000187 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100189 // There MUST be a memfile, otherwise we can't do anything
190 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100191 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200192 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (curbuf->b_ml.ml_mfp != NULL)
194 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100195 // If there is no memfile at all, exit.
196 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 if (curbuf == NULL)
198 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000199 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200200
201 // Don't try to do any saving, with "curbuf" NULL almost nothing
202 // will work.
203 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 getout(2);
205 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200206
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000207 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100209#ifdef FEAT_SYN_HL
210 if (old_tw != curbuf->b_p_tw)
211 check_colorcolumn(curwin);
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 return FAIL;
214 }
215
Bram Moolenaarc667da52019-11-30 20:52:27 +0100216 // The autocommands in readfile() may change the buffer, but only AFTER
217 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200218 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
Bram Moolenaarc667da52019-11-30 20:52:27 +0100221 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100222 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100224 // A buffer without an actual file should not use the buffer name to read a
225 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100226 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100227 flags |= READ_NOFILE;
228
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100229 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (curbuf->b_ffname != NULL
231#ifdef FEAT_NETBEANS_INTG
232 && netbeansReadFile
233#endif
234 )
235 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100236 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200237#ifdef UNIX
238 int save_bin = curbuf->b_p_bin;
239 int perm;
240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241#ifdef FEAT_NETBEANS_INTG
242 int oldFire = netbeansFireChanges;
243
244 netbeansFireChanges = 0;
245#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200246#ifdef UNIX
247 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200248 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200249 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200250# ifdef OPEN_CHR_FILES
251 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
252# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200253 ))
254 read_fifo = TRUE;
255 if (read_fifo)
256 curbuf->b_p_bin = TRUE;
257#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100258 if (shortmess(SHM_FILEINFO))
259 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200261 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200262 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
263#ifdef UNIX
264 if (read_fifo)
265 {
266 curbuf->b_p_bin = save_bin;
267 if (retval == OK)
268 retval = read_buffer(FALSE, eap, flags);
269 }
270#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100271 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#ifdef FEAT_NETBEANS_INTG
273 netbeansFireChanges = oldFire;
274#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100275 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200276 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 fix_help_buffer();
278 }
279 else if (read_stdin)
280 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200281 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100283 // First read the text in binary mode into the buffer.
284 // Then read from that same buffer and append at the end. This makes
285 // it possible to retry when 'fileformat' or 'fileencoding' was
286 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 curbuf->b_p_bin = TRUE;
288 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200289 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
290 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 curbuf->b_p_bin = save_bin;
292 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200293 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 }
295
Bram Moolenaarc667da52019-11-30 20:52:27 +0100296 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100300 parse_cino(curbuf);
301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100303 // Set/reset the Changed flag first, autocmds may change the buffer.
304 // Apply the automatic commands, before processing the modelines.
305 // So the modelines have priority over autocommands.
306 //
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);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100324 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100325 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100326
Bram Moolenaarc667da52019-11-30 20:52:27 +0100327 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_EVAL
329 if (aborting())
330#else
331 if (got_int)
332#endif
333 curbuf->b_flags |= BF_READERR;
334
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000335#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100336 // Need to update automatic folding. Do this before the autocommands,
337 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000338 foldUpdateAll(curwin);
339#endif
340
Bram Moolenaarc667da52019-11-30 20:52:27 +0100341 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 if (!(curwin->w_valid & VALID_TOPLINE))
343 {
344 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100349#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100351#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#endif
354
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100355 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100357 // The autocommands may have changed the current buffer. Apply the
358 // modelines to the correct buffer, if it still exists and is loaded.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200359 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {
361 aco_save_T aco;
362
Bram Moolenaarc667da52019-11-30 20:52:27 +0100363 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200364 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000365 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
367
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100368 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100369#ifdef FEAT_EVAL
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100370 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE,
371 curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100372#else
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100373 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 Moolenaarf1474d82021-12-31 19:59:55 +0000437 emsg(_(e_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 Moolenaaref976322022-09-28 11:48:30 +0100471 {
472 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
473
474 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
475 fname != NULL ? fname : (char_u *)"[No Name]");
476 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200477 return can_unload;
478}
Bram Moolenaara997b452018-04-17 23:24:06 +0200479
Bram Moolenaar480778b2016-07-14 22:09:39 +0200480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 * Close the link to a buffer.
482 * "action" is used when there is no longer a window for the buffer.
483 * It can be:
484 * 0 buffer becomes hidden
485 * DOBUF_UNLOAD buffer is unloaded
486 * DOBUF_DELETE buffer is unloaded and removed from buffer list
487 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200488 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 * When doing all but the first one on the current buffer, the caller should
490 * get a new buffer very soon!
491 *
492 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100493 *
494 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
495 * cause there to be only one window with this buffer. e.g. when ":quit" is
496 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100497 *
498 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100499 *
500 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100503close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100504 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100505 buf_T *buf,
506 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100507 int abort_if_last,
508 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100511 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200512 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100513 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200514 win_T *the_curwin = curwin;
515 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200517 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
518 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
Bram Moolenaarcee52202020-03-11 14:19:58 +0100520 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100521
522 // Force unloading or deleting when 'bufhidden' says so.
523 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
524 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100525 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200526 {
527 del_buf = TRUE;
528 unload_buf = TRUE;
529 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100530 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200531 {
532 del_buf = TRUE;
533 unload_buf = TRUE;
534 wipe_buf = TRUE;
535 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100536 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200537 unload_buf = TRUE;
538
Bram Moolenaar94053a52017-08-01 21:44:33 +0200539#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200540 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200541 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100542 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 if (term_job_running(buf->b_term))
544 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200545 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200546 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200547 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100548 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200549
Bram Moolenaarc667da52019-11-30 20:52:27 +0100550 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200551 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200552 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200553 else
554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100555 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200556 del_buf = FALSE;
557 unload_buf = FALSE;
558 }
559 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100560 else if (buf->b_p_bh[0] == 'h' && !del_buf)
561 {
562 // Hide a terminal buffer.
563 unload_buf = FALSE;
564 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200565 else
566 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100567 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200568 del_buf = TRUE;
569 unload_buf = TRUE;
570 wipe_buf = TRUE;
571 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100572 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200573 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
Bram Moolenaarc667da52019-11-30 20:52:27 +0100576 // Disallow deleting the buffer when it is locked (already being closed or
577 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200578 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100579 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200580
Bram Moolenaarc667da52019-11-30 20:52:27 +0100581 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200582 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100584 // Set b_last_cursor when closing the last window for the buffer.
585 // Remember the last cursor position and window options of the buffer.
586 // This used to be only for the current window, but then options like
587 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (buf->b_nwindows == 1)
589 set_last_cursor(win);
590 buflist_setfpos(buf, win,
591 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
592 win->w_cursor.col, TRUE);
593 }
594
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200595 set_bufref(&bufref, buf);
596
Bram Moolenaarc667da52019-11-30 20:52:27 +0100597 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (buf->b_nwindows == 1)
599 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200600 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100601 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200602 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
603 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200604 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100605 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100606 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200607aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000608 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100609 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100610 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200611 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100612 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200613 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200615 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaarc667da52019-11-30 20:52:27 +0100617 // When the buffer becomes hidden, but is not unloaded, trigger
618 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (!unload_buf)
620 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200621 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100622 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200623 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
624 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200625 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100626 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200627 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200628 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100629 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200630 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100631 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200632 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100634#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100635 // autocmds may abort script processing
636 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100637 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200640
Bram Moolenaarc667da52019-11-30 20:52:27 +0100641 // If the buffer was in curwin and the window has changed, go back to that
642 // window, if it still exists. This avoids that ":edit x" triggering a
643 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200644 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
645 {
646 block_autocmds();
647 goto_tabpage_win(the_curtab, the_curwin);
648 unblock_autocmds();
649 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaarc667da52019-11-30 20:52:27 +0100653 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (buf->b_nwindows > 0)
655 --buf->b_nwindows;
656
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100657#ifdef FEAT_DIFF
658 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100659 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100660#endif
661
Bram Moolenaarc667da52019-11-30 20:52:27 +0100662 // Return when a window is displaying the buffer or when it's not
663 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100665 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (buf->b_ffname == NULL)
669 del_buf = TRUE;
670
Bram Moolenaarc667da52019-11-30 20:52:27 +0100671 // When closing the current buffer stop Visual mode before freeing
672 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200673 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200674#if defined(EXITFREE)
675 && !entered_free_all_mem
676#endif
677 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200678 end_visual_mode();
679
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100680 // Free all things allocated for this buffer.
681 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
682 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100683 // Remember if we are closing the current buffer. Restore the number of
684 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 is_curbuf = (buf == curbuf);
686 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100688 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100689 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100690 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
Bram Moolenaarc667da52019-11-30 20:52:27 +0100692 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200693 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100694 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100695#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100696 // autocmds may abort script processing
697 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100698 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100701 // It's possible that autocommands change curbuf to the one being deleted.
702 // This might cause the previous curbuf to be deleted unexpectedly. But
703 // in some cases it's OK to delete the curbuf, because a new one is
704 // obtained anyway. Therefore only return if curbuf changed to the
705 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 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 Moolenaar347538f2022-03-26 16:28:06 +0000722 // Do not wipe out the buffer if it is used in a window.
723 if (buf->b_nwindows > 0)
724 return FALSE;
725
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200726 if (action == DOBUF_WIPE_REUSE)
727 {
728 // we can re-use this buffer number, store it
729 if (buf_reuse.ga_itemsize == 0)
730 ga_init2(&buf_reuse, sizeof(int), 50);
731 if (ga_grow(&buf_reuse, 1) == OK)
732 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
733 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200734 if (buf->b_sfname != buf->b_ffname)
735 VIM_CLEAR(buf->b_sfname);
736 else
737 buf->b_sfname = NULL;
738 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (buf->b_prev == NULL)
740 firstbuf = buf->b_next;
741 else
742 buf->b_prev->b_next = buf->b_next;
743 if (buf->b_next == NULL)
744 lastbuf = buf->b_prev;
745 else
746 buf->b_next->b_prev = buf->b_prev;
747 free_buffer(buf);
748 }
749 else
750 {
751 if (del_buf)
752 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100753 // Free all internal variables and reset option values, to make
754 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 free_buffer_stuff(buf, TRUE);
756
Bram Moolenaarc667da52019-11-30 20:52:27 +0100757 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
759
Bram Moolenaarc667da52019-11-30 20:52:27 +0100760 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 buf->b_p_initialized = FALSE;
762 }
763 buf_clear_file(buf);
764 if (del_buf)
765 buf->b_p_bl = FALSE;
766 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100767 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100768 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769}
770
771/*
772 * Make buffer not contain a file.
773 */
774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100775buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776{
777 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200778 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 buf->b_p_eol = TRUE;
781 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000783 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100785 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786#ifdef FEAT_NETBEANS_INTG
787 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789}
790
791/*
792 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200793 * the file. Careful: get here with "curwin" NULL when exiting.
794 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100795 * BFA_DEL buffer is going to be deleted
796 * BFA_WIPE buffer is going to be wiped out
797 * BFA_KEEP_UNDO do not free undo information
798 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100801buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200804 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200805 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200806 win_T *the_curwin = curwin;
807 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
Bram Moolenaarc667da52019-11-30 20:52:27 +0100809 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200810 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100811 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200812 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200813 if (buf->b_ml.ml_mfp != NULL)
814 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200815 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
816 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200817 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100818 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200819 return;
820 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200821 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200823 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
824 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200825 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100826 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 return;
828 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200829 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200831 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
832 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200833 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100834 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 return;
836 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200837 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100838 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200839
Bram Moolenaarc667da52019-11-30 20:52:27 +0100840 // If the buffer was in curwin and the window has changed, go back to that
841 // window, if it still exists. This avoids that ":edit x" triggering a
842 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200843 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
844 {
845 block_autocmds();
846 goto_tabpage_win(the_curtab, the_curwin);
847 unblock_autocmds();
848 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200849
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100850#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100851 // autocmds may abort script processing
852 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100856 // It's possible that autocommands change curbuf to the one being deleted.
857 // This might cause curbuf to be deleted unexpectedly. But in some cases
858 // it's OK to delete the curbuf, because a new one is obtained anyway.
859 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (buf == curbuf && !is_curbuf)
861 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100863 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200865#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100866 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200867 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100868 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200869#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000870
871#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100872 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000873 {
874 win_T *win;
875 tabpage_T *tp;
876
877 FOR_ALL_TAB_WINDOWS(tp, win)
878 if (win->w_buffer == buf)
879 clearFolding(win);
880 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000881#endif
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_TCL
884 tcl_buffer_free(buf);
885#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100886 ml_close(buf, TRUE); // close and delete the memline/memfile
887 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200888 if ((flags & BFA_KEEP_UNDO) == 0)
889 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100890 u_blockfree(buf); // free the memory allocated for undo
891 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100894 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100896#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100897 clear_buf_prop_types(buf);
898#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100899 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900}
901
902/*
903 * Free a buffer structure and the things it contains related to the buffer
904 * itself (not the file, that must have been done already).
905 */
906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100907free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200909 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200911#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100912 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200913 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200914 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200915 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200916#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200917#ifdef FEAT_LUA
918 lua_buffer_free(buf);
919#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000920#ifdef FEAT_MZSCHEME
921 mzscheme_buffer_free(buf);
922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_PERL
924 perl_buf_free(buf);
925#endif
926#ifdef FEAT_PYTHON
927 python_buffer_free(buf);
928#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200929#ifdef FEAT_PYTHON3
930 python3_buffer_free(buf);
931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#ifdef FEAT_RUBY
933 ruby_buffer_free(buf);
934#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200935#ifdef FEAT_JOB_CHANNEL
936 channel_buffer_free(buf);
937#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200938#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200939 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200940#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200941#ifdef FEAT_JOB_CHANNEL
942 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200943 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200944 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200945#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200946
947 buf_hashtab_remove(buf);
948
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200949 aubuflocal_remove(buf);
950
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200951 if (autocmd_busy)
952 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100953 // Do not free the buffer structure while autocommands are executing,
954 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200955 buf->b_next = au_pending_free_buf;
956 au_pending_free_buf = buf;
957 }
958 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100959 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200960 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100961 if (curbuf == buf)
962 curbuf = NULL; // make clear it's not to be used
963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100967 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100968 */
969 static void
970init_changedtick(buf_T *buf)
971{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100972 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100973
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100974 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
975 di->di_tv.v_type = VAR_NUMBER;
976 di->di_tv.v_lock = VAR_FIXED;
977 di->di_tv.vval.v_number = 0;
978
Bram Moolenaar92769c32017-02-25 15:41:37 +0100979#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100980 STRCPY(buf->b_ct_di.di_key, "changedtick");
981 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100982#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100983}
984
985/*
Bram Moolenaarc3126192022-08-26 12:58:17 +0100986 * Free the b_wininfo list for buffer "buf".
987 */
988 static void
989clear_wininfo(buf_T *buf)
990{
991 wininfo_T *wip;
992
993 while (buf->b_wininfo != NULL)
994 {
995 wip = buf->b_wininfo;
996 buf->b_wininfo = wip->wi_next;
997 free_wininfo(wip);
998 }
999}
1000
1001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1003 */
1004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005free_buffer_stuff(
1006 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001007 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
1009 if (free_options)
1010 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001011 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001013#ifdef FEAT_SPELL
1014 ga_clear(&buf->b_s.b_langp);
1015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001018 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001019 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001020
Bram Moolenaarc667da52019-11-30 20:52:27 +01001021 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001022 hash_init(&buf->b_vars->dv_hashtab);
1023 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001024 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001025 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001028 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001030 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001032#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001033 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001034#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001035#ifdef FEAT_PROP_POPUP
1036 ga_clear_strings(&buf->b_textprop_text);
1037#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001038 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1039 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001040 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041}
1042
1043/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001044 * Free one wininfo_T.
1045 */
1046 void
1047free_wininfo(wininfo_T *wip)
1048{
1049 if (wip->wi_optset)
1050 {
1051 clear_winopt(&wip->wi_opt);
1052#ifdef FEAT_FOLDING
1053 deleteFoldRecurse(&wip->wi_folds);
1054#endif
1055 }
1056 vim_free(wip);
1057}
1058
1059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 * Go to another buffer. Handles the result of the ATTENTION dialog.
1061 */
1062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063goto_buffer(
1064 exarg_T *eap,
1065 int start,
1066 int dir,
1067 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001069 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001070 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001071
1072 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar188639d2022-04-04 16:57:21 +01001074 if (swap_exists_action == SEA_NONE)
1075 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1077 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1079 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001080#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001081 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001082
Bram Moolenaarc667da52019-11-30 20:52:27 +01001083 // Reset the error/interrupt/exception state here so that
1084 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001085 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001086#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001087
Bram Moolenaarc667da52019-11-30 20:52:27 +01001088 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001090 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001091 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001092
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001093#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001094 // Restore the error/interrupt/exception state if not discarded by a
1095 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001096 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001100 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001101}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * Handle the situation of swap_exists_action being set.
1105 * It is allowed for "old_curbuf" to be NULL or invalid.
1106 */
1107 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001110#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001111 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001112#endif
1113#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001114 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001115#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001116 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (swap_exists_action == SEA_QUIT)
1119 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001120#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001121 // Reset the error/interrupt/exception state here so that
1122 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001123 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001124#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001125
Bram Moolenaarc667da52019-11-30 20:52:27 +01001126 // User selected Quit at ATTENTION prompt. Go back to previous
1127 // buffer. If that buffer is gone or the same as the current one,
1128 // open a new, empty buffer.
1129 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001130 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001131 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001132 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1133 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001134 {
1135 // Block autocommands here because curwin->w_buffer is NULL.
1136 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001137 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001138 unblock_autocmds();
1139 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001140 else
1141 buf = old_curbuf->br_buf;
1142 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001143 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001144 int old_msg_silent = msg_silent;
1145
1146 if (shortmess(SHM_FILEINFO))
1147 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001148 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001149 // restore msg_silent, so that the command line will be shown
1150 msg_silent = old_msg_silent;
1151
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001152#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001153 if (old_tw != curbuf->b_p_tw)
1154 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001155#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001156 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001157 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001159#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001160 // Restore the error/interrupt/exception state if not discarded by a
1161 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001162 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 else if (swap_exists_action == SEA_RECOVER)
1166 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001167#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001168 // Reset the error/interrupt/exception state here so that
1169 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001170 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001171#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001172
Bram Moolenaarc667da52019-11-30 20:52:27 +01001173 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001175 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001176 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001178 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001179
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001180#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001181 // Restore the error/interrupt/exception state if not discarded by a
1182 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001183 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
1186 swap_exists_action = SEA_NONE;
1187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001190 * Make the current buffer empty.
1191 * Used when it is wiped out and it's the last buffer.
1192 */
1193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194empty_curbuf(
1195 int close_others,
1196 int forceit,
1197 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001198{
1199 int retval;
1200 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001201 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001202
1203 if (action == DOBUF_UNLOAD)
1204 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001205 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001206 return FAIL;
1207 }
1208
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001209 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001210 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001211 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001212 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001213
1214 setpcmark();
1215 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1216 forceit ? ECMD_FORCEIT : 0, curwin);
1217
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001218 // do_ecmd() may create a new buffer, then we have to delete
1219 // the old one. But do_ecmd() may have done that already, check
1220 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001221 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001222 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001223 if (!close_others)
1224 need_fileinfo = FALSE;
1225 return retval;
1226}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228/*
1229 * Implementation of the commands for the buffer list.
1230 *
1231 * action == DOBUF_GOTO go to specified buffer
1232 * action == DOBUF_SPLIT split window and go to specified buffer
1233 * action == DOBUF_UNLOAD unload specified buffer(s)
1234 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1235 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001236 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 *
1238 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1239 * start == DOBUF_FIRST go to "count" buffer from first buffer
1240 * start == DOBUF_LAST go to "count" buffer from last buffer
1241 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1242 *
1243 * Return FAIL or OK.
1244 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001245 static int
1246do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001247 int action,
1248 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001249 int dir, // FORWARD or BACKWARD
1250 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001251 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 buf_T *buf;
1254 buf_T *bp;
1255 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001256 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258 switch (start)
1259 {
1260 case DOBUF_FIRST: buf = firstbuf; break;
1261 case DOBUF_LAST: buf = lastbuf; break;
1262 default: buf = curbuf; break;
1263 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001264 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
1266 while (count-- > 0)
1267 {
1268 do
1269 {
1270 buf = buf->b_next;
1271 if (buf == NULL)
1272 buf = firstbuf;
1273 }
1274 while (buf != curbuf && !bufIsChanged(buf));
1275 }
1276 if (!bufIsChanged(buf))
1277 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001278 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 return FAIL;
1280 }
1281 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001282 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 while (buf != NULL && buf->b_fnum != count)
1285 buf = buf->b_next;
1286 }
1287 else
1288 {
1289 bp = NULL;
1290 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1291 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001292 // remember the buffer where we start, we come back there when all
1293 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (bp == NULL)
1295 bp = buf;
1296 if (dir == FORWARD)
1297 {
1298 buf = buf->b_next;
1299 if (buf == NULL)
1300 buf = firstbuf;
1301 }
1302 else
1303 {
1304 buf = buf->b_prev;
1305 if (buf == NULL)
1306 buf = lastbuf;
1307 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001308 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (unload || buf->b_p_bl)
1310 {
1311 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001312 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 if (bp == buf)
1315 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001316 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001317 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 return FAIL;
1319 }
1320 }
1321 }
1322
Bram Moolenaarc667da52019-11-30 20:52:27 +01001323 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {
1325 if (start == DOBUF_FIRST)
1326 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001327 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001329 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001332 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001334 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 return FAIL;
1336 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001337#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001338 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001339 return OK;
1340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342#ifdef FEAT_GUI
1343 need_mouse_correct = TRUE;
1344#endif
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001347 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 if (unload)
1350 {
1351 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001352 bufref_T bufref;
1353
Bram Moolenaar94f01952018-09-01 15:30:03 +02001354 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001355 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001356
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001357 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaarc667da52019-11-30 20:52:27 +01001359 // When unloading or deleting a buffer that's already unloaded and
1360 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001361 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1362 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 return FAIL;
1364
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001365 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001367#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001368 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
1370 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001371 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001372 // Autocommand deleted buffer, oops! It's not changed
1373 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001375 // If it's still changed fail silently, the dialog already
1376 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001377 if (bufIsChanged(buf))
1378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001380 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001383 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 buf->b_fnum);
1385 return FAIL;
1386 }
1387 }
1388
Bram Moolenaarc667da52019-11-30 20:52:27 +01001389 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001390 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001391 end_visual_mode();
1392
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001393 // If deleting the last (listed) buffer, make it empty.
1394 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001395 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (bp->b_p_bl && bp != buf)
1397 break;
1398 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001399 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001401 // If the deleted buffer is the current one, close the current window
1402 // (unless it's the only window). Repeat this so long as we end up in
1403 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001404 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001405 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001406 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001407 {
1408 if (win_close(curwin, FALSE) == FAIL)
1409 break;
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001412 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (buf != curbuf)
1414 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001415 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001416 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001417 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 return OK;
1419 }
1420
1421 /*
1422 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001423 * There should be another, otherwise it would have been handled
1424 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001425 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 * Then prefer the buffer we most recently visited.
1427 * Else try to find one that is loaded, after the current buffer,
1428 * then before the current buffer.
1429 * Finally use any buffer.
1430 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001431 buf = NULL; // selected buffer
1432 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001433 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1434 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001435 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
1437 int jumpidx;
1438
1439 jumpidx = curwin->w_jumplistidx - 1;
1440 if (jumpidx < 0)
1441 jumpidx = curwin->w_jumplistlen - 1;
1442
1443 forward = jumpidx;
1444 while (jumpidx != curwin->w_jumplistidx)
1445 {
1446 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1447 if (buf != NULL)
1448 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001449 // Skip current and unlisted bufs. Also skip a quickfix
1450 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001451 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001452 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 else if (buf->b_ml.ml_mfp == NULL)
1454 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001455 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (bp == NULL)
1457 bp = buf;
1458 buf = NULL;
1459 }
1460 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001461 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001463 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1465 break;
1466 if (--jumpidx < 0)
1467 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001468 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 break;
1470 }
1471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaarc667da52019-11-30 20:52:27 +01001473 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 forward = TRUE;
1476 buf = curbuf->b_next;
1477 for (;;)
1478 {
1479 if (buf == NULL)
1480 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001481 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 break;
1483 buf = curbuf->b_prev;
1484 forward = FALSE;
1485 continue;
1486 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001487 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001488 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001489 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001491 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001493 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 bp = buf;
1495 }
1496 if (forward)
1497 buf = buf->b_next;
1498 else
1499 buf = buf->b_prev;
1500 }
1501 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001502 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001504 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001506 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001507 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 break;
1509 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001510 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 if (curbuf->b_next != NULL)
1513 buf = curbuf->b_next;
1514 else
1515 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001516 if (bt_quickfix(buf))
1517 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519 }
1520
Bram Moolenaara02471e2014-01-10 16:43:14 +01001521 if (buf == NULL)
1522 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001523 // Autocommands must have wiped out all other buffers. Only option
1524 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001525 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001526 }
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001529 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001531 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 // If 'switchbuf' contains "useopen": jump to first window containing
1534 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001535 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001536 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001537 // If 'switchbuf' contains "usetab": jump to first window in any tab
1538 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001539 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 return OK;
1541 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return FAIL;
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
Bram Moolenaarc667da52019-11-30 20:52:27 +01001545 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (buf == curbuf)
1547 return OK;
1548
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001549 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001550 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001553 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001555 bufref_T bufref;
1556
1557 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001559 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001560 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
1563 if (bufIsChanged(curbuf))
1564#endif
1565 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001566 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return FAIL;
1568 }
1569 }
1570
Bram Moolenaarc667da52019-11-30 20:52:27 +01001571 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 set_curbuf(buf, action);
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001577#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001578 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return FAIL;
1580#endif
1581
1582 return OK;
1583}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001585 int
1586do_buffer(
1587 int action,
1588 int start,
1589 int dir, // FORWARD or BACKWARD
1590 int count, // buffer number or number of buffers
1591 int forceit) // TRUE when using !
1592{
1593 return do_buffer_ext(action, start, dir, count,
1594 forceit ? DOBUF_FORCEIT : 0);
1595}
1596
1597/*
1598 * do_bufdel() - delete or unload buffer(s)
1599 *
1600 * addr_count == 0: ":bdel" - delete current buffer
1601 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1602 * buffer "end_bnr", then any other arguments.
1603 * addr_count == 2: ":N,N bdel" - delete buffers in range
1604 *
1605 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1606 * DOBUF_DEL (":bdel")
1607 *
1608 * Returns error message or NULL
1609 */
1610 char *
1611do_bufdel(
1612 int command,
1613 char_u *arg, // pointer to extra arguments
1614 int addr_count,
1615 int start_bnr, // first buffer number in a range
1616 int end_bnr, // buffer nr or last buffer nr in a range
1617 int forceit)
1618{
1619 int do_current = 0; // delete current buffer?
1620 int deleted = 0; // number of buffers deleted
1621 char *errormsg = NULL; // return value
1622 int bnr; // buffer number
1623 char_u *p;
1624
1625 if (addr_count == 0)
1626 {
1627 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1628 }
1629 else
1630 {
1631 if (addr_count == 2)
1632 {
1633 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001634 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001635 bnr = start_bnr;
1636 }
1637 else // addr_count == 1
1638 bnr = end_bnr;
1639
1640 for ( ;!got_int; ui_breakcheck())
1641 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001642 // Delete the current buffer last, otherwise when the
1643 // current buffer is deleted, the next buffer becomes
1644 // the current one and will be loaded, which may then
1645 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001646 if (bnr == curbuf->b_fnum)
1647 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001648 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001649 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1650 ++deleted;
1651
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001652 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001653 if (addr_count == 2)
1654 {
1655 if (++bnr > end_bnr)
1656 break;
1657 }
1658 else // addr_count == 1
1659 {
1660 arg = skipwhite(arg);
1661 if (*arg == NUL)
1662 break;
1663 if (!VIM_ISDIGIT(*arg))
1664 {
1665 p = skiptowhite_esc(arg);
1666 bnr = buflist_findpat(arg, p,
1667 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1668 FALSE, FALSE);
1669 if (bnr < 0) // failed
1670 break;
1671 arg = p;
1672 }
1673 else
1674 bnr = getdigits(&arg);
1675 }
1676 }
1677 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1678 FORWARD, do_current, forceit) == OK)
1679 ++deleted;
1680
1681 if (deleted == 0)
1682 {
1683 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001684 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001685 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001686 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001687 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001688 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001689 errormsg = (char *)IObuff;
1690 }
1691 else if (deleted >= p_report)
1692 {
1693 if (command == DOBUF_UNLOAD)
1694 smsg(NGETTEXT("%d buffer unloaded",
1695 "%d buffers unloaded", deleted), deleted);
1696 else if (command == DOBUF_DEL)
1697 smsg(NGETTEXT("%d buffer deleted",
1698 "%d buffers deleted", deleted), deleted);
1699 else
1700 smsg(NGETTEXT("%d buffer wiped out",
1701 "%d buffers wiped out", deleted), deleted);
1702 }
1703 }
1704
1705
1706 return errormsg;
1707}
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
1710 * Set current buffer to "buf". Executes autocommands and closes current
1711 * buffer. "action" tells how to close the current buffer:
1712 * DOBUF_GOTO free or hide it
1713 * DOBUF_SPLIT nothing
1714 * DOBUF_UNLOAD unload it
1715 * DOBUF_DEL delete it
1716 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001717 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
1719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 buf_T *prevbuf;
1723 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001724 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001725#ifdef FEAT_SYN_HL
1726 long old_tw = curbuf->b_p_tw;
1727#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001728 bufref_T newbufref;
1729 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001730 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001733 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001734 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1735 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
Bram Moolenaarc667da52019-11-30 20:52:27 +01001737 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaarc667da52019-11-30 20:52:27 +01001740 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001742 set_bufref(&prevbufref, prevbuf);
1743 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001745 // Autocommands may delete the current buffer and/or the buffer we want to
1746 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001747 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001748 || (bufref_valid(&prevbufref)
1749 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001750#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001751 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001753 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001755#ifdef FEAT_SYN_HL
1756 if (prevbuf == curwin->w_buffer)
1757 reset_synblock(curwin);
1758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001760 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001762 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001764 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001766 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001767 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001768
Bram Moolenaare615db02022-01-20 21:00:54 +00001769 // Do not sync when in Insert mode and the buffer is open in
1770 // another window, might be a timer doing something in another
1771 // window.
1772 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001773 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001774 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1776 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001777 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001778 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1779 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001780 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001781 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001782 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001785 // An autocommand may have deleted "buf", already entered it (e.g., when
1786 // it did ":bunload") or aborted the script processing.
1787 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001788 valid = buf_valid(buf);
1789 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001790#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001791 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001793 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001794 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001795 // If the buffer is not valid but curwin->w_buffer is NULL we must
1796 // enter some buffer. Using the last one is hopefully OK.
1797 if (!valid)
1798 enter_buffer(lastbuf);
1799 else
1800 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001801#ifdef FEAT_SYN_HL
1802 if (old_tw != curbuf->b_p_tw)
1803 check_colorcolumn(curwin);
1804#endif
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806}
1807
1808/*
1809 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001810 * Old curbuf must have been abandoned already! This also means "curbuf" may
1811 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001816 // when closing the current buffer stop Visual mode
1817 if (VIsual_active
1818#if defined(EXITFREE)
1819 && !entered_free_all_mem
1820#endif
1821 )
1822 end_visual_mode();
1823
Bram Moolenaar010ee962019-09-25 20:37:36 +02001824 // Get the buffer in the current window.
1825 curwin->w_buffer = buf;
1826 curbuf = buf;
1827 ++curbuf->b_nwindows;
1828
1829 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1831 if (!buf->b_help)
1832 get_winopts(buf);
1833#ifdef FEAT_FOLDING
1834 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001835 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001837 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#endif
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001841 if (curwin->w_p_diff)
1842 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843#endif
1844
Bram Moolenaara971b822011-09-14 14:43:25 +02001845#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001846 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001847#endif
1848
Bram Moolenaarc667da52019-11-30 20:52:27 +01001849 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 curwin->w_cursor.lnum = 1;
1851 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001854 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaarc667da52019-11-30 20:52:27 +01001856 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001857 curwin->w_valid = 0;
1858
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001859 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1860 curbuf->b_last_cursor.col, TRUE);
1861
Bram Moolenaarc667da52019-11-30 20:52:27 +01001862 // Make sure the buffer is loaded.
1863 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001864 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001865 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001866 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001867 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001868 if (*curbuf->b_p_ft == NUL)
1869 did_filetype = FALSE;
1870
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001871 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 else
1874 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001875 if (!msg_silent && !shortmess(SHM_FILEINFO))
1876 need_fileinfo = TRUE; // display file info after redraw
1877
1878 // check if file changed
1879 (void)buf_check_timestamp(curbuf, FALSE);
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001882#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1886 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888
Bram Moolenaarc667da52019-11-30 20:52:27 +01001889 // If autocommands did not change the cursor position, restore cursor lnum
1890 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (curwin->w_cursor.lnum == 1 && inindent(0))
1892 buflist_getfpos();
1893
Bram Moolenaarc667da52019-11-30 20:52:27 +01001894 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001896 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001897 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001898 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar009b2592004-10-24 19:18:58 +00001900#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001901 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001902 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001903#endif
1904
Bram Moolenaarc667da52019-11-30 20:52:27 +01001905 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001906 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
1908#ifdef FEAT_KEYMAP
1909 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001910 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001912#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001913 // May need to set the spell language. Can only do this after the buffer
1914 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001915 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1916 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001917#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001918#ifdef FEAT_VIMINFO
1919 curbuf->b_last_used = vim_time();
1920#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001921
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001922 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923}
1924
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001925#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1926/*
1927 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001928 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001929 */
1930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001931do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001932{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001933 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001934 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001935 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001936 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001937 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001938 last_chdir_reason = "autochdir";
1939 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001940}
1941#endif
1942
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001943 void
1944no_write_message(void)
1945{
1946#ifdef FEAT_TERMINAL
1947 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001948 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001949 else
1950#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001951 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001952}
1953
1954 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001955no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001956{
1957#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001958 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001959 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001960 else
1961#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001962 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001963}
1964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965/*
1966 * functions for dealing with the buffer list
1967 */
1968
1969/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001970 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1971 * only one window. That means it can be re-used.
1972 */
1973 int
1974curbuf_reusable(void)
1975{
1976 return (curbuf != NULL
1977 && curbuf->b_ffname == NULL
1978 && curbuf->b_nwindows <= 1
1979 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02001980 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001981 && !curbufIsChanged());
1982}
1983
1984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 * Add a file name to the buffer list. Return a pointer to the buffer.
1986 * If the same file name already exists return a pointer to that buffer.
1987 * If it does not exist, or if fname == NULL, a new entry is created.
1988 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1989 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1990 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001991 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001992 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1993 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001994 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 * This is the ONLY way to create a new buffer.
1996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001998buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001999 char_u *ffname_arg, // full path of fname or relative
2000 char_u *sfname_arg, // short fname or NULL
2001 linenr_T lnum, // preferred cursor line
2002 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002004 char_u *ffname = ffname_arg;
2005 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 buf_T *buf;
2007#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002008 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#endif
2010
Bram Moolenaar480778b2016-07-14 22:09:39 +02002011 if (top_file_num == 1)
2012 hash_init(&buf_hashtab);
2013
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002014 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002017 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 */
2019#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002020 // On Unix we can use inode numbers when the file exists. Works better
2021 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2023 st.st_dev = (dev_T)-1;
2024#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002025 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026#ifdef UNIX
2027 buflist_findname_stat(ffname, &st)
2028#else
2029 buflist_findname(ffname)
2030#endif
2031 ) != NULL)
2032 {
2033 vim_free(ffname);
2034 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002035 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2036 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002037
2038 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002039 // copy the options now, if 'cpo' doesn't have 's' and not done
2040 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002041 buf_copy_options(buf, 0);
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2044 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002045 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002048 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002050 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002051 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002052 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002053 return NULL;
2054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 return buf;
2057 }
2058
2059 /*
2060 * If the current buffer has no name and no contents, use the current
2061 * buffer. Otherwise: Need to allocate a new buffer structure.
2062 *
2063 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002064 * (A spell file buffer is allocated in spell.c, but that's not a normal
2065 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 */
2067 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002068 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
2070 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002071 // It's like this buffer is deleted. Watch out for autocommands that
2072 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002073 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2074 if (buf != curbuf) // autocommands deleted the buffer!
2075 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002076#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002077 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002078 {
2079 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084 if (buf != curbuf || curbuf == NULL)
2085 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002086 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (buf == NULL)
2088 {
2089 vim_free(ffname);
2090 return NULL;
2091 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002092#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002093 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002094 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002095 if (buf->b_vars == NULL)
2096 {
2097 vim_free(ffname);
2098 vim_free(buf);
2099 return NULL;
2100 }
2101 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2102#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002103 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105
2106 if (ffname != NULL)
2107 {
2108 buf->b_ffname = ffname;
2109 buf->b_sfname = vim_strsave(sfname);
2110 }
2111
2112 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002113 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2116 || buf->b_wininfo == NULL)
2117 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002118 if (buf->b_sfname != buf->b_ffname)
2119 VIM_CLEAR(buf->b_sfname);
2120 else
2121 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002122 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (buf != curbuf)
2124 free_buffer(buf);
2125 return NULL;
2126 }
2127
2128 if (buf == curbuf)
2129 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002130 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002131
Bram Moolenaarc667da52019-11-30 20:52:27 +01002132 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002133 buf->b_p_initialized = FALSE;
2134 buf_copy_options(buf, BCO_ENTER);
2135
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002137 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 curbuf->b_kmap_state |= KEYMAP_INIT;
2139#endif
2140 }
2141 else
2142 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002143 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002145 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 buf->b_prev = NULL;
2148 firstbuf = buf;
2149 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002150 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 lastbuf->b_next = buf;
2153 buf->b_prev = lastbuf;
2154 }
2155 lastbuf = buf;
2156
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002157 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2158 {
2159 // Recycle a previously used buffer number. Used for buffers which
2160 // are normally hidden, e.g. in a popup window. Avoids that the
2161 // buffer number grows rapidly.
2162 --buf_reuse.ga_len;
2163 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002164
2165 // Move buffer to the right place in the buffer list.
2166 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2167 {
2168 buf_T *prev = buf->b_prev;
2169
2170 prev->b_next = buf->b_next;
2171 if (prev->b_next != NULL)
2172 prev->b_next->b_prev = prev;
2173 buf->b_next = prev;
2174 buf->b_prev = prev->b_prev;
2175 if (buf->b_prev != NULL)
2176 buf->b_prev->b_next = buf;
2177 prev->b_prev = buf;
2178 if (lastbuf == buf)
2179 lastbuf = prev;
2180 if (firstbuf == prev)
2181 firstbuf = buf;
2182 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002183 }
2184 else
2185 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002186 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002188 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002189 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
2191 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002192 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194 top_file_num = 1;
2195 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002196 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002198 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 buf_copy_options(buf, BCO_ALWAYS);
2200 }
2201
2202 buf->b_wininfo->wi_fpos.lnum = lnum;
2203 buf->b_wininfo->wi_win = curwin;
2204
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002205#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002206 hash_init(&buf->b_s.b_keywtab);
2207 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#endif
2209
2210 buf->b_fname = buf->b_sfname;
2211#ifdef UNIX
2212 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002213 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else
2215 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002216 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 buf->b_dev = st.st_dev;
2218 buf->b_ino = st.st_ino;
2219 }
2220#endif
2221 buf->b_u_synced = TRUE;
2222 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002223 if (flags & BLN_DUMMY)
2224 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002226 clrallmarks(buf); // clear marks
2227 fmarks_check_names(buf); // check file marks for this file
2228 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 if (!(flags & BLN_DUMMY))
2230 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002231 bufref_T bufref;
2232
Bram Moolenaarc667da52019-11-30 20:52:27 +01002233 // Tricky: these autocommands may change the buffer list. They could
2234 // also split the window with re-using the one empty buffer. This may
2235 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002236 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002237 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002238 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002241 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002242 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002243 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002244 return NULL;
2245 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002246#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002247 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
2252 return buf;
2253}
2254
2255/*
2256 * Free the memory for the options of a buffer.
2257 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2258 * 'fileencoding'.
2259 */
2260 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002261free_buf_options(
2262 buf_T *buf,
2263 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (free_p_ff)
2266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 clear_string_option(&buf->b_p_bh);
2270 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272#ifdef FEAT_FIND_ID
2273 clear_string_option(&buf->b_p_def);
2274 clear_string_option(&buf->b_p_inc);
2275# ifdef FEAT_EVAL
2276 clear_string_option(&buf->b_p_inex);
2277# endif
2278#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002279#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 clear_string_option(&buf->b_p_inde);
2281 clear_string_option(&buf->b_p_indk);
2282#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002283#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2284 clear_string_option(&buf->b_p_bexpr);
2285#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002286#if defined(FEAT_CRYPT)
2287 clear_string_option(&buf->b_p_cm);
2288#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002289 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002290#if defined(FEAT_EVAL)
2291 clear_string_option(&buf->b_p_fex);
2292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002294# ifdef FEAT_SODIUM
K.Takata1a8825d2022-01-19 13:32:57 +00002295 if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) &&
2296 (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2297 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 clear_string_option(&buf->b_p_key);
2300#endif
2301 clear_string_option(&buf->b_p_kp);
2302 clear_string_option(&buf->b_p_mps);
2303 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002304 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002306#ifdef FEAT_VARTABS
2307 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002308 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002309 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002310 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002311 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002312 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#ifdef FEAT_KEYMAP
2315 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002316 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 ga_clear(&buf->b_kmap_ga);
2318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#ifdef FEAT_FOLDING
2321 clear_string_option(&buf->b_p_cms);
2322#endif
2323 clear_string_option(&buf->b_p_nf);
2324#ifdef FEAT_SYN_HL
2325 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002326 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002327#endif
2328#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002329 clear_string_option(&buf->b_s.b_p_spc);
2330 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002331 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002332 buf->b_s.b_cap_prog = NULL;
2333 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002334 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 clear_string_option(&buf->b_p_cink);
2339 clear_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01002340 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002343#ifdef FEAT_COMPL_FUNC
2344 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002345 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002346 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002347 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002348 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002349 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#ifdef FEAT_QUICKFIX
2352 clear_string_option(&buf->b_p_gp);
2353 clear_string_option(&buf->b_p_mp);
2354 clear_string_option(&buf->b_p_efm);
2355#endif
2356 clear_string_option(&buf->b_p_ep);
2357 clear_string_option(&buf->b_p_path);
2358 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002359 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002360#ifdef FEAT_EVAL
2361 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002362 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 clear_string_option(&buf->b_p_dict);
2365 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002366 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002368 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002369 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002370 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002371 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372}
2373
2374/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002375 * Get alternate file "n".
2376 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2377 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 * if (options & GETF_SETMARK) call setpcmark()
2379 * if (options & GETF_ALT) we are jumping to an alternate file.
2380 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2381 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002382 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
2384 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002385buflist_getfile(
2386 int n,
2387 linenr_T lnum,
2388 int options,
2389 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 pos_T *fpos;
2394 colnr_T col;
2395
2396 buf = buflist_findnr(n);
2397 if (buf == NULL)
2398 {
2399 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002400 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002402 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 return FAIL;
2404 }
2405
Bram Moolenaarc667da52019-11-30 20:52:27 +01002406 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (buf == curbuf)
2408 return OK;
2409
Bram Moolenaar71223e22022-05-30 15:23:09 +01002410 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaarc667da52019-11-30 20:52:27 +01002413 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (lnum == 0)
2415 {
2416 fpos = buflist_findfpos(buf);
2417 lnum = fpos->lnum;
2418 col = fpos->col;
2419 }
2420 else
2421 col = 0;
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (options & GETF_SWITCH)
2424 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002425 // If 'switchbuf' contains "useopen": jump to first window containing
2426 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002427 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002429
Bram Moolenaarc667da52019-11-30 20:52:27 +01002430 // If 'switchbuf' contains "usetab": jump to first window in any tab
2431 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002432 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002433 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002434
Bram Moolenaarc667da52019-11-30 20:52:27 +01002435 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2436 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002437 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002438 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002440 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002441 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002442 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2443 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002445 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002450 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2451 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 --RedrawingDisabled;
2454
Bram Moolenaarc667da52019-11-30 20:52:27 +01002455 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (!p_sol && col != 0)
2457 {
2458 curwin->w_cursor.col = col;
2459 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 curwin->w_set_curswant = TRUE;
2462 }
2463 return OK;
2464 }
2465 --RedrawingDisabled;
2466 return FAIL;
2467}
2468
2469/*
2470 * go to the last know line number for the current buffer
2471 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002473buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 pos_T *fpos;
2476
2477 fpos = buflist_findfpos(curbuf);
2478
2479 curwin->w_cursor.lnum = fpos->lnum;
2480 check_cursor_lnum();
2481
2482 if (p_sol)
2483 curwin->w_cursor.col = 0;
2484 else
2485 {
2486 curwin->w_cursor.col = fpos->col;
2487 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 curwin->w_set_curswant = TRUE;
2490 }
2491}
2492
Bram Moolenaar81695252004-12-29 20:58:21 +00002493#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2494/*
2495 * Find file in buffer list by name (it has to be for the current window).
2496 * Returns NULL if not found.
2497 */
2498 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002500{
2501 char_u *ffname;
2502 buf_T *buf = NULL;
2503
Bram Moolenaarc667da52019-11-30 20:52:27 +01002504 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002505 ffname = FullName_save(fname,
2506#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002507 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002508#else
2509 FALSE
2510#endif
2511 );
2512 if (ffname != NULL)
2513 {
2514 buf = buflist_findname(ffname);
2515 vim_free(ffname);
2516 }
2517 return buf;
2518}
2519#endif
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521/*
2522 * Find file in buffer list by name (it has to be for the current window).
2523 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002524 * Skips dummy buffers.
2525 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 */
2527 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002528buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002531 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 if (mch_stat((char *)ffname, &st) < 0)
2534 st.st_dev = (dev_T)-1;
2535 return buflist_findname_stat(ffname, &st);
2536}
2537
2538/*
2539 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2540 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002541 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
2543 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002544buflist_findname_stat(
2545 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002546 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548#endif
2549 buf_T *buf;
2550
Bram Moolenaarc667da52019-11-30 20:52:27 +01002551 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002552 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002553 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554#ifdef UNIX
2555 , stp
2556#endif
2557 ))
2558 return buf;
2559 return NULL;
2560}
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562/*
2563 * Find file in buffer list by a regexp pattern.
2564 * Return fnum of the found buffer.
2565 * Return < 0 for error.
2566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568buflist_findpat(
2569 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002570 char_u *pattern_end, // pointer to first char after pattern
2571 int unlisted, // find unlisted buffers
2572 int diffmode UNUSED, // find diff-mode buffers only
2573 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int match = -1;
2577 int find_listed;
2578 char_u *pat;
2579 char_u *patend;
2580 int attempt;
2581 char_u *p;
2582 int toggledollar;
2583
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002584 // "%" is current file, "%%" or "#" is alternate file
2585 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2586 || (in_vim9script() && pattern_end == pattern + 2
2587 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002589 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002591 else
2592 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#ifdef FEAT_DIFF
2594 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2595 match = -1;
2596#endif
2597 }
2598
2599 /*
2600 * Try four ways of matching a listed buffer:
2601 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002602 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 * attempt == 2: with '$' at end (only match at end)
2604 * attempt == 3: with '^' at start and '$' at end (only full match)
2605 * Repeat this for finding an unlisted buffer if there was no matching
2606 * listed buffer.
2607 */
2608 else
2609 {
2610 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2611 if (pat == NULL)
2612 return -1;
2613 patend = pat + STRLEN(pat) - 1;
2614 toggledollar = (patend > pat && *patend == '$');
2615
Bram Moolenaarc667da52019-11-30 20:52:27 +01002616 // First try finding a listed buffer. If not found and "unlisted"
2617 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 find_listed = TRUE;
2619 for (;;)
2620 {
2621 for (attempt = 0; attempt <= 3; ++attempt)
2622 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002623 regmatch_T regmatch;
2624
Bram Moolenaarc667da52019-11-30 20:52:27 +01002625 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002627 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002629 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002631 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002633 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002634 {
2635 if (regmatch.regprog == NULL)
2636 {
2637 // invalid pattern, possibly after switching engine
2638 vim_free(pat);
2639 return -1;
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (buf->b_p_bl == find_listed
2642#ifdef FEAT_DIFF
2643 && (!diffmode || diff_mode_buf(buf))
2644#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002645 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002647 if (curtab_only)
2648 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002649 // Ignore the match if the buffer is not open in
2650 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002651 win_T *wp;
2652
Bram Moolenaar29323592016-07-24 22:04:11 +02002653 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002654 if (wp->w_buffer == buf)
2655 break;
2656 if (wp == NULL)
2657 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002658 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002659 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 match = -2;
2662 break;
2663 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002664 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002668 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002669 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 break;
2671 }
2672
Bram Moolenaarc667da52019-11-30 20:52:27 +01002673 // Only search for unlisted buffers if there was no match with
2674 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (!unlisted || !find_listed || match != -1)
2676 break;
2677 find_listed = FALSE;
2678 }
2679
2680 vim_free(pat);
2681 }
2682
2683 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002684 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002686 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return match;
2688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaar52410572019-10-27 05:12:45 +01002690#ifdef FEAT_VIMINFO
2691typedef struct {
2692 buf_T *buf;
2693 char_u *match;
2694} bufmatch_T;
2695#endif
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Find all buffer names that match.
2699 * For command line expansion of ":buf" and ":sbuf".
2700 * Return OK if matches found, FAIL otherwise.
2701 */
2702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002703ExpandBufnames(
2704 char_u *pat,
2705 int *num_file,
2706 char_u ***file,
2707 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 int count = 0;
2710 buf_T *buf;
2711 int round;
2712 char_u *p;
2713 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002714 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002715#ifdef FEAT_VIMINFO
2716 bufmatch_T *matches = NULL;
2717#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002718 int fuzzy;
2719 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaarc667da52019-11-30 20:52:27 +01002721 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 *file = NULL;
2723
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002724#ifdef FEAT_DIFF
2725 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2726 return FAIL;
2727#endif
2728
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002729 fuzzy = cmdline_fuzzy_complete(pat);
2730
2731 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2732 // expression matching)
2733 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002735 if (*pat == '^')
2736 {
2737 patc = alloc(STRLEN(pat) + 11);
2738 if (patc == NULL)
2739 return FAIL;
2740 STRCPY(patc, "\\(^\\|[\\/]\\)");
2741 STRCPY(patc + 11, pat + 1);
2742 }
2743 else
2744 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002745 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002746
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002747 // attempt == 0: try match with '\<', match at start of word
2748 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002749 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002750 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002751 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002752 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002753
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002754 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002755 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002756 if (attempt > 0 && patc == pat)
2757 break; // there was no anchor, no need to try again
2758 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002761 // round == 1: Count the matches.
2762 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 for (round = 1; round <= 2; ++round)
2764 {
2765 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002766 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002768 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002770#ifdef FEAT_DIFF
2771 if (options & BUF_DIFF_FILTER)
2772 // Skip buffers not suitable for
2773 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002774 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002775 continue;
2776#endif
2777
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002778 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002779 {
2780 if (regmatch.regprog == NULL)
2781 {
2782 // invalid pattern, possibly after recompiling
2783 if (patc != pat)
2784 vim_free(patc);
2785 return FAIL;
2786 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002787 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002788 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002789 else
2790 {
2791 p = NULL;
2792 // first try matching with the short file name
2793 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2794 p = buf->b_sfname;
2795 if (p == NULL)
2796 {
2797 // next try matching with the full path file name
2798 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2799 p = buf->b_ffname;
2800 }
2801 }
2802
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002803 if (p == NULL)
2804 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002805
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002806 if (round == 1)
2807 {
2808 ++count;
2809 continue;
2810 }
2811
2812 if (options & WILD_HOME_REPLACE)
2813 p = home_replace_save(buf, p);
2814 else
2815 p = vim_strsave(p);
2816
2817 if (!fuzzy)
2818 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002820 if (matches != NULL)
2821 {
2822 matches[count].buf = buf;
2823 matches[count].match = p;
2824 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002826 else
2827#endif
2828 (*file)[count++] = p;
2829 }
2830 else
2831 {
2832 fuzmatch[count].idx = count;
2833 fuzmatch[count].str = p;
2834 fuzmatch[count].score = score;
2835 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002838 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 break;
2840 if (round == 1)
2841 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002842 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002844 *file = ALLOC_MULT(char_u *, count);
2845 if (*file == NULL)
2846 {
2847 vim_regfree(regmatch.regprog);
2848 if (patc != pat)
2849 vim_free(patc);
2850 return FAIL;
2851 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002852#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002853 if (options & WILD_BUFLASTUSED)
2854 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002855#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002856 }
2857 else
2858 {
2859 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2860 if (fuzmatch == NULL)
2861 {
2862 *num_file = 0;
2863 *file = NULL;
2864 return FAIL;
2865 }
2866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002869
2870 if (!fuzzy)
2871 {
2872 vim_regfree(regmatch.regprog);
2873 if (count) // match(es) found, break here
2874 break;
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002878 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002879 vim_free(patc);
2880
Bram Moolenaar52410572019-10-27 05:12:45 +01002881#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002882 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002883 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002884 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002885 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002886 int i;
2887 if (count > 1)
2888 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2889 // if the current buffer is first in the list, place it at the end
2890 if (matches[0].buf == curbuf)
2891 {
2892 for (i = 1; i < count; i++)
2893 (*file)[i-1] = matches[i].match;
2894 (*file)[count-1] = matches[0].match;
2895 }
2896 else
2897 {
2898 for (i = 0; i < count; i++)
2899 (*file)[i] = matches[i].match;
2900 }
2901 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002902 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002903 }
2904 else
2905 {
2906 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2907 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002908 }
2909#endif
2910
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 *num_file = count;
2912 return (count == 0 ? FAIL : OK);
2913}
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915/*
2916 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002917 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
2919 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920buflist_match(
2921 regmatch_T *rmp,
2922 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002923 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
2925 char_u *match;
2926
Bram Moolenaarc667da52019-11-30 20:52:27 +01002927 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002928 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002929 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002930 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 return match;
2933}
2934
2935/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002936 * Try matching the regexp in "rmp->regprog" with file name "name".
2937 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 * Return "name" when there is a match, NULL when not.
2939 */
2940 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941fname_match(
2942 regmatch_T *rmp,
2943 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002944 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 char_u *match = NULL;
2947 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002949 // extra check for valid arguments
2950 if (name != NULL && rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002952 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002953 rmp->rm_ic = p_fic || ignore_case;
2954 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 match = name;
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +01002956 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002958 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002960 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 match = name;
2962 vim_free(p);
2963 }
2964 }
2965
2966 return match;
2967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002970 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
2972 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002973buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002975 char_u key[VIM_SIZEOF_INT * 2 + 1];
2976 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
2978 if (nr == 0)
2979 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002980 sprintf((char *)key, "%x", nr);
2981 hi = hash_find(&buf_hashtab, key);
2982
2983 if (!HASHITEM_EMPTY(hi))
2984 return (buf_T *)(hi->hi_key
2985 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 return NULL;
2987}
2988
2989/*
2990 * Get name of file 'n' in the buffer list.
2991 * When the file has no name an empty string is returned.
2992 * home_replace() is used to shorten the file name (used for marks).
2993 * Returns a pointer to allocated memory, of NULL when failed.
2994 */
2995 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002996buflist_nr2name(
2997 int n,
2998 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002999 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 buf_T *buf;
3002
3003 buf = buflist_findnr(n);
3004 if (buf == NULL)
3005 return NULL;
3006 return home_replace_save(helptail ? buf : NULL,
3007 fullname ? buf->b_ffname : buf->b_fname);
3008}
3009
3010/*
3011 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3012 * When "copy_options" is TRUE save the local window option values.
3013 * When "lnum" is 0 only do the options.
3014 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003016buflist_setfpos(
3017 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003018 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019 linenr_T lnum,
3020 colnr_T col,
3021 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 wininfo_T *wip;
3024
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003025 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (wip->wi_win == win)
3027 break;
3028 if (wip == NULL)
3029 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003030 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003031 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (wip == NULL)
3033 return;
3034 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003035 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 lnum = 1;
3037 }
3038 else
3039 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003040 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (wip->wi_prev)
3042 wip->wi_prev->wi_next = wip->wi_next;
3043 else
3044 buf->b_wininfo = wip->wi_next;
3045 if (wip->wi_next)
3046 wip->wi_next->wi_prev = wip->wi_prev;
3047 if (copy_options && wip->wi_optset)
3048 {
3049 clear_winopt(&wip->wi_opt);
3050#ifdef FEAT_FOLDING
3051 deleteFoldRecurse(&wip->wi_folds);
3052#endif
3053 }
3054 }
3055 if (lnum != 0)
3056 {
3057 wip->wi_fpos.lnum = lnum;
3058 wip->wi_fpos.col = col;
3059 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003060 if (win != NULL)
3061 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003062 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003064 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3066#ifdef FEAT_FOLDING
3067 wip->wi_fold_manual = win->w_fold_manual;
3068 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3069#endif
3070 wip->wi_optset = TRUE;
3071 }
3072
Bram Moolenaarc667da52019-11-30 20:52:27 +01003073 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 wip->wi_next = buf->b_wininfo;
3075 buf->b_wininfo = wip;
3076 wip->wi_prev = NULL;
3077 if (wip->wi_next)
3078 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079}
3080
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003081#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003082/*
3083 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3084 * page. That's because a diff is local to a tab page.
3085 */
3086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003088{
3089 win_T *wp;
3090
3091 if (wip->wi_opt.wo_diff)
3092 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003093 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003094 // return FALSE when it's a window in the current tab page, thus
3095 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003096 if (wip->wi_win == wp)
3097 return FALSE;
3098 return TRUE;
3099 }
3100 return FALSE;
3101}
3102#endif
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
3105 * Find info for the current window in buffer "buf".
3106 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003107 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003108 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3109 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 * Returns NULL when there isn't any info.
3111 */
3112 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113find_wininfo(
3114 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003115 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 wininfo_T *wip;
3119
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003120 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003121 if (wip->wi_win == curwin
3122#ifdef FEAT_DIFF
3123 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3124#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003125
3126 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003128
Bram Moolenaarc667da52019-11-30 20:52:27 +01003129 // If no wininfo for curwin, use the first in the list (that doesn't have
3130 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003131 // If "need_options" is TRUE skip entries that don't have options set,
3132 // unless the window is editing "buf", so we can copy from the window
3133 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003134 if (wip == NULL)
3135 {
3136#ifdef FEAT_DIFF
3137 if (skip_diff_buffer)
3138 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003139 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003140 if (!wininfo_other_tab_diff(wip)
3141 && (!need_options || wip->wi_optset
3142 || (wip->wi_win != NULL
3143 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003144 break;
3145 }
3146 else
3147#endif
3148 wip = buf->b_wininfo;
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return wip;
3151}
3152
3153/*
3154 * Reset the local window options to the values last used in this window.
3155 * If the buffer wasn't used in this window before, use the values from
3156 * the most recently used window. If the values were never set, use the
3157 * global values for the window.
3158 */
3159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003160get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 wininfo_T *wip;
3163
3164 clear_winopt(&curwin->w_onebuf_opt);
3165#ifdef FEAT_FOLDING
3166 clearFolding(curwin);
3167#endif
3168
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003169 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003170 if (wip != NULL && wip->wi_win != NULL
3171 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003173 // The buffer is currently displayed in the window: use the actual
3174 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003175 win_T *wp = wip->wi_win;
3176
3177 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3178#ifdef FEAT_FOLDING
3179 curwin->w_fold_manual = wp->w_fold_manual;
3180 curwin->w_foldinvalid = TRUE;
3181 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3182#endif
3183 }
3184 else if (wip != NULL && wip->wi_optset)
3185 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003186 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3188#ifdef FEAT_FOLDING
3189 curwin->w_fold_manual = wip->wi_fold_manual;
3190 curwin->w_foldinvalid = TRUE;
3191 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3192#endif
3193 }
3194 else
3195 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003196 if (wip != NULL)
3197 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003200 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (p_fdls >= 0)
3202 curwin->w_p_fdl = p_fdls;
3203#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003204 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
3208 * Find the position (lnum and col) for the buffer 'buf' for the current
3209 * window.
3210 * Returns a pointer to no_position if no position is found.
3211 */
3212 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003216 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003218 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (wip != NULL)
3220 return &(wip->wi_fpos);
3221 else
3222 return &no_position;
3223}
3224
3225/*
3226 * Find the lnum for the buffer 'buf' for the current window.
3227 */
3228 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003229buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 return buflist_findfpos(buf)->lnum;
3232}
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003235 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
Bram Moolenaar52410572019-10-27 05:12:45 +01003240 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 int len;
3242 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003243 int ro_char;
3244 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003245#ifdef FEAT_TERMINAL
3246 int job_running;
3247 int job_none_open;
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaar52410572019-10-27 05:12:45 +01003250#ifdef FEAT_VIMINFO
3251 garray_T buflist;
3252 buf_T **buflist_data = NULL, **p;
3253
3254 if (vim_strchr(eap->arg, 't'))
3255 {
3256 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003257 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003258 {
3259 if (ga_grow(&buflist, 1) == OK)
3260 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3261 }
3262
3263 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3264 sizeof(buf_T *), buf_compare);
3265
Bram Moolenaar3b991522019-11-06 23:26:20 +01003266 buflist_data = (buf_T **)buflist.ga_data;
3267 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003268 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003269 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003270
Bram Moolenaar3b991522019-11-06 23:26:20 +01003271 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003272 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3273 : buf->b_next)
3274#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003278#ifdef FEAT_TERMINAL
3279 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003280 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003281#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003282 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003283 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3284 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3285 || (vim_strchr(eap->arg, '+')
3286 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3287 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003288 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003289 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003290 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3291#ifdef FEAT_TERMINAL
3292 || (vim_strchr(eap->arg, 'R')
3293 && (!job_running || (job_running && job_none_open)))
3294 || (vim_strchr(eap->arg, '?')
3295 && (!job_running || (job_running && !job_none_open)))
3296 || (vim_strchr(eap->arg, 'F')
3297 && (job_running || buf->b_term == NULL))
3298#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003299 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3300 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3301 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3302 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3303 || (vim_strchr(eap->arg, '#')
3304 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003307 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else
3309 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003310 if (message_filtered(NameBuff))
3311 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003313 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3314 : (bufIsChanged(buf) ? '+' : ' ');
3315#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003316 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003317 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003318 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003319 ro_char = '?';
3320 else
3321 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003322 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3323 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003324 }
3325 else if (buf->b_term != NULL)
3326 ro_char = 'F';
3327 else
3328#endif
3329 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3330
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003331 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003332 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 buf->b_fnum,
3334 buf->b_p_bl ? ' ' : 'u',
3335 buf == curbuf ? '%' :
3336 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3337 buf->b_ml.ml_mfp == NULL ? ' ' :
3338 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003339 ro_char,
3340 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003341 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003342 if (len > IOSIZE - 20)
3343 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaarc667da52019-11-30 20:52:27 +01003345 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 i = 40 - vim_strsize(IObuff);
3347 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003349 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003350#ifdef FEAT_VIMINFO
3351 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3352 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3353 else
3354#endif
3355 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3356 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003357 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003359 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 ui_breakcheck();
3361 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003362
3363#ifdef FEAT_VIMINFO
3364 if (buflist_data)
3365 ga_clear(&buflist);
3366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369/*
3370 * Get file name and line number for file 'fnum'.
3371 * Used by DoOneCmd() for translating '%' and '#'.
3372 * Used by insert_reg() and cmdline_paste() for '#' register.
3373 * Return FAIL if not found, OK for success.
3374 */
3375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003376buflist_name_nr(
3377 int fnum,
3378 char_u **fname,
3379 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
3381 buf_T *buf;
3382
3383 buf = buflist_findnr(fnum);
3384 if (buf == NULL || buf->b_fname == NULL)
3385 return FAIL;
3386
3387 *fname = buf->b_fname;
3388 *lnum = buflist_findlnum(buf);
3389
3390 return OK;
3391}
3392
3393/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003394 * Set the file name for "buf"' to "ffname_arg", short file name to
3395 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * The file name with the full path is also remembered, for when :cd is used.
3397 * Returns FAIL for failure (file name already in use by other buffer)
3398 * OK otherwise.
3399 */
3400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003401setfname(
3402 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003403 char_u *ffname_arg,
3404 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003405 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003407 char_u *ffname = ffname_arg;
3408 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003409 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003411 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
3413
3414 if (ffname == NULL || *ffname == NUL)
3415 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003416 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003417 if (buf->b_sfname != buf->b_ffname)
3418 VIM_CLEAR(buf->b_sfname);
3419 else
3420 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003421 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422#ifdef UNIX
3423 st.st_dev = (dev_T)-1;
3424#endif
3425 }
3426 else
3427 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3429 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return FAIL;
3431
3432 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003433 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * - if the buffer is loaded, fail
3435 * - if the buffer is not loaded, delete it from the list
3436 */
3437#ifdef UNIX
3438 if (mch_stat((char *)ffname, &st) < 0)
3439 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003440#endif
3441 if (!(buf->b_flags & BF_DUMMY))
3442#ifdef UNIX
3443 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003445 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
3447 if (obuf != NULL && obuf != buf)
3448 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003449 win_T *win;
3450 tabpage_T *tab;
3451 int in_use = FALSE;
3452
3453 // during startup a window may use a buffer that is not loaded yet
3454 FOR_ALL_TAB_WINDOWS(tab, win)
3455 if (win->w_buffer == obuf)
3456 in_use = TRUE;
3457
3458 // it's loaded or used in a window, fail
3459 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003462 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 vim_free(ffname);
3464 return FAIL;
3465 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003466 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003467 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 sfname = vim_strsave(sfname);
3470 if (ffname == NULL || sfname == NULL)
3471 {
3472 vim_free(sfname);
3473 vim_free(ffname);
3474 return FAIL;
3475 }
3476#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003477 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003479 if (buf->b_sfname != buf->b_ffname)
3480 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 buf->b_ffname = ffname;
3483 buf->b_sfname = sfname;
3484 }
3485 buf->b_fname = buf->b_sfname;
3486#ifdef UNIX
3487 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003488 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 else
3490 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003491 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 buf->b_dev = st.st_dev;
3493 buf->b_ino = st.st_ino;
3494 }
3495#endif
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 buf_name_changed(buf);
3500 return OK;
3501}
3502
3503/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003504 * Crude way of changing the name of a buffer. Use with care!
3505 * The name should be relative to the current directory.
3506 */
3507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003509{
3510 buf_T *buf;
3511
3512 buf = buflist_findnr(fnum);
3513 if (buf != NULL)
3514 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003515 if (buf->b_sfname != buf->b_ffname)
3516 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003517 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003518 buf->b_ffname = vim_strsave(name);
3519 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003520 // Allocate ffname and expand into full path. Also resolves .lnk
3521 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003522 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003523 buf->b_fname = buf->b_sfname;
3524 }
3525}
3526
3527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * Take care of what needs to be done when the name of buffer "buf" has
3529 * changed.
3530 */
3531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003532buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 /*
3535 * If the file name changed, also change the name of the swapfile
3536 */
3537 if (buf->b_ml.ml_mfp != NULL)
3538 ml_setname(buf);
3539
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003540#ifdef FEAT_TERMINAL
3541 if (buf->b_term != NULL)
3542 term_clear_status_text(buf->b_term);
3543#endif
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003546 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003547 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003548 status_redraw_all(); // status lines need to be redrawn
3549 fmarks_check_names(buf); // check named file marks
3550 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
3552
3553/*
3554 * set alternate file name for current window
3555 *
3556 * Used by do_one_cmd(), do_write() and do_ecmd().
3557 * Return the buffer.
3558 */
3559 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003560setaltfname(
3561 char_u *ffname,
3562 char_u *sfname,
3563 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
3565 buf_T *buf;
3566
Bram Moolenaarc667da52019-11-30 20:52:27 +01003567 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003569 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 curwin->w_alt_fnum = buf->b_fnum;
3571 return buf;
3572}
3573
3574/*
3575 * Get alternate file name for current window.
3576 * Return NULL if there isn't any, and give error message if requested.
3577 */
3578 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003580 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 char_u *fname;
3583 linenr_T dummy;
3584
3585 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3586 {
3587 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003588 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 return NULL;
3590 }
3591 return fname;
3592}
3593
3594/*
3595 * Add a file name to the buflist and return its number.
3596 * Uses same flags as buflist_new(), except BLN_DUMMY.
3597 *
3598 * used by qf_init(), main() and doarglist()
3599 */
3600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 buf_T *buf;
3604
3605 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3606 if (buf != NULL)
3607 return buf->b_fnum;
3608 return 0;
3609}
3610
3611#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3612/*
3613 * Adjust slashes in file names. Called after 'shellslash' was set.
3614 */
3615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 buf_T *bp;
3619
Bram Moolenaar29323592016-07-24 22:04:11 +02003620 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 if (bp->b_ffname != NULL)
3623 slash_adjust(bp->b_ffname);
3624 if (bp->b_sfname != NULL)
3625 slash_adjust(bp->b_sfname);
3626 }
3627}
3628#endif
3629
3630/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003631 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Also save the local window option values.
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003637 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638}
3639
3640/*
3641 * Return TRUE if 'ffname' is not the same file as current file.
3642 * Fname must have a full path (expanded by mch_FullName()).
3643 */
3644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 return otherfile_buf(curbuf, ffname
3648#ifdef UNIX
3649 , NULL
3650#endif
3651 );
3652}
3653
3654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655otherfile_buf(
3656 buf_T *buf,
3657 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003659 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003663 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3665 return TRUE;
3666 if (fnamecmp(ffname, buf->b_ffname) == 0)
3667 return FALSE;
3668#ifdef UNIX
3669 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003670 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaarc667da52019-11-30 20:52:27 +01003672 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (stp == NULL)
3674 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003675 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 st.st_dev = (dev_T)-1;
3677 stp = &st;
3678 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003679 // Use dev/ino to check if the files are the same, even when the names
3680 // are different (possible with links). Still need to compare the
3681 // name above, for when the file doesn't exist yet.
3682 // Problem: The dev/ino changes when a file is deleted (and created
3683 // again) and remains the same when renamed/moved. We don't want to
3684 // mch_stat() each buffer each time, that would be too slow. Get the
3685 // dev/ino again when they appear to match, but not when they appear
3686 // to be different: Could skip a buffer when it's actually the same
3687 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (buf_same_ino(buf, stp))
3689 {
3690 buf_setino(buf);
3691 if (buf_same_ino(buf, stp))
3692 return FALSE;
3693 }
3694 }
3695#endif
3696 return TRUE;
3697}
3698
3699#if defined(UNIX) || defined(PROTO)
3700/*
3701 * Set inode and device number for a buffer.
3702 * Must always be called when b_fname is changed!.
3703 */
3704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003707 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3710 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003711 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 buf->b_dev = st.st_dev;
3713 buf->b_ino = st.st_ino;
3714 }
3715 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003716 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717}
3718
3719/*
3720 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3721 */
3722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723buf_same_ino(
3724 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003725 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003727 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 && stp->st_dev == buf->b_dev
3729 && stp->st_ino == buf->b_ino);
3730}
3731#endif
3732
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003733/*
3734 * Print info about the current buffer.
3735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003738 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739 int shorthelp,
3740 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 char_u *name;
3743 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003744 char *p;
3745 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003746 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003748 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (buffer == NULL)
3750 return;
3751
Bram Moolenaarc667da52019-11-30 20:52:27 +01003752 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003754 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 p = buffer + STRLEN(buffer);
3756 }
3757 else
3758 p = buffer;
3759
3760 *p++ = '"';
3761 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003762 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 else
3764 {
3765 if (!fullname && curbuf->b_fname != NULL)
3766 name = curbuf->b_fname;
3767 else
3768 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003769 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 (int)(IOSIZE - (p - buffer)), TRUE);
3771 }
3772
Bram Moolenaar32526b32019-01-19 17:43:09 +01003773 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 curbufIsChanged() ? (shortmess(SHM_MOD)
3775 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003776 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003778 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003779 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003781 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 : _("[readonly]")) : "",
3783 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3784 || curbuf->b_p_ro) ?
3785 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003786 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3787 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (curwin->w_cursor.lnum > 1000000L)
3789 n = (int)(((long)curwin->w_cursor.lnum) /
3790 ((long)curbuf->b_ml.ml_line_count / 100L));
3791 else
3792 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3793 (long)curbuf->b_ml.ml_line_count);
3794 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003795 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef FEAT_CMDL_INFO
3797 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003798 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003799 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003800 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3801 curbuf->b_ml.ml_line_count),
3802 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#endif
3804 else
3805 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003806 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 _("line %ld of %ld --%d%%-- col "),
3808 (long)curwin->w_cursor.lnum,
3809 (long)curbuf->b_ml.ml_line_count,
3810 n);
3811 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003812 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003813 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3815 }
3816
Bram Moolenaar32526b32019-01-19 17:43:09 +01003817 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3818 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 if (dont_truncate)
3821 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003822 // Temporarily set msg_scroll to avoid the message being truncated.
3823 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 msg_start();
3825 n = msg_scroll;
3826 msg_scroll = TRUE;
3827 msg(buffer);
3828 msg_scroll = n;
3829 }
3830 else
3831 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003832 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003834 // Need to repeat the message after redrawing when:
3835 // - When restart_edit is set (otherwise there will be a delay
3836 // before redrawing).
3837 // - When the screen was scrolled but there is no wait-return
3838 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003839 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
3842 vim_free(buffer);
3843}
3844
3845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003846col_print(
3847 char_u *buf,
3848 size_t buflen,
3849 int col,
3850 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003853 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003855 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856}
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858static char_u *lasttitle = NULL;
3859static char_u *lasticon = NULL;
3860
Bram Moolenaar84a93082018-06-16 22:58:15 +02003861/*
3862 * Put the file name in the title bar and icon of the window.
3863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003865maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003868 char_u *title_str = NULL;
3869 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int maxlen = 0;
3871 int len;
3872 int mustset;
3873 char_u buf[IOSIZE];
3874 int off;
3875
3876 if (!redrawing())
3877 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003878 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 need_maketitle = TRUE;
3880 return;
3881 }
3882
3883 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003884 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003885 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 if (p_title)
3888 {
3889 if (p_titlelen > 0)
3890 {
3891 maxlen = p_titlelen * Columns / 100;
3892 if (maxlen < 10)
3893 maxlen = 10;
3894 }
3895
Bram Moolenaar84a93082018-06-16 22:58:15 +02003896 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (*p_titlestring != NUL)
3898 {
3899#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003900 if (stl_syntax & STL_IN_TITLE)
3901 {
3902 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003903 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003904
3905# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003906 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003907# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003908 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003909 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003910 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003911 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003912 set_string_option_direct((char_u *)"titlestring", -1,
3913 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 else
3916#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003917 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 else
3920 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003921 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar2c666692012-09-05 13:30:40 +02003923#define SPACE_FOR_FNAME (IOSIZE - 100)
3924#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003925#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003927 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003928#ifdef FEAT_TERMINAL
3929 else if (curbuf->b_term != NULL)
3930 {
3931 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3932 SPACE_FOR_FNAME);
3933 }
3934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 else
3936 {
3937 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003938 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941
Bram Moolenaar21554412017-07-24 21:44:43 +02003942#ifdef FEAT_TERMINAL
3943 if (curbuf->b_term == NULL)
3944#endif
3945 switch (bufIsChanged(curbuf)
3946 + (curbuf->b_p_ro * 2)
3947 + (!curbuf->b_p_ma * 4))
3948 {
3949 case 1: STRCAT(buf, " +"); break;
3950 case 2: STRCAT(buf, " ="); break;
3951 case 3: STRCAT(buf, " =+"); break;
3952 case 4:
3953 case 6: STRCAT(buf, " -"); break;
3954 case 5:
3955 case 7: STRCAT(buf, " -+"); break;
3956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
Bram Moolenaar21554412017-07-24 21:44:43 +02003958 if (curbuf->b_fname != NULL
3959#ifdef FEAT_TERMINAL
3960 && curbuf->b_term == NULL
3961#endif
3962 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003964 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 off = (int)STRLEN(buf);
3966 buf[off++] = ' ';
3967 buf[off++] = '(';
3968 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003969 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003971 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (isalpha(buf[off]) && buf[off + 1] == ':')
3973 off += 2;
3974#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003975 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003976 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003978 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003979 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003980 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003981 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003985
Bram Moolenaarc667da52019-11-30 20:52:27 +01003986 // Translate unprintable chars and concatenate. Keep some
3987 // room for the server name. When there is no room (very long
3988 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003989 if (off < SPACE_FOR_DIR)
3990 {
3991 p = transstr(buf + off);
3992 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3993 vim_free(p);
3994 }
3995 else
3996 {
3997 vim_strncpy(buf + off, (char_u *)"...",
3998 (size_t)(SPACE_FOR_ARGNR - off));
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 STRCAT(buf, ")");
4001 }
4002
Bram Moolenaar2c666692012-09-05 13:30:40 +02004003 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005#if defined(FEAT_CLIENTSERVER)
4006 if (serverName != NULL)
4007 {
4008 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004009 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011 else
4012#endif
4013 STRCAT(buf, " - VIM");
4014
4015 if (maxlen > 0)
4016 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004017 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004018 if (vim_strsize(buf) > maxlen)
4019 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 }
4022 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004023 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 if (p_icon)
4026 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004027 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (*p_iconstring != NUL)
4029 {
4030#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004031 if (stl_syntax & STL_IN_ICON)
4032 {
4033 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01004034 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004035
4036# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00004037 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004038# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004039 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004040 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004041 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01004042 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00004043 set_string_option_direct((char_u *)"iconstring", -1,
4044 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00004045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 else
4047#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004048 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 else
4051 {
4052 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004053 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004054 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004055 p = gettail(curbuf->b_ffname);
4056 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004057 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004058 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (len > 100)
4060 {
4061 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004063 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004064 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004066 STRCPY(icon_str, p);
4067 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 }
4070
Bram Moolenaar84a93082018-06-16 22:58:15 +02004071 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 if (mustset)
4074 resettitle();
4075}
4076
4077/*
4078 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4079 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004080 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 */
4082 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004083value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
4085 if ((str == NULL) != (*last == NULL)
4086 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4087 {
4088 vim_free(*last);
4089 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004092 mch_restore_title(
4093 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004098 return TRUE;
4099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 return FALSE;
4102}
4103
4104/*
4105 * Put current window title back (used after calling a shell)
4106 */
4107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004108resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 mch_settitle(lasttitle, lasticon);
4111}
Bram Moolenaarea408852005-06-25 22:49:46 +00004112
4113# if defined(EXITFREE) || defined(PROTO)
4114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004115free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004116{
4117 vim_free(lasttitle);
4118 vim_free(lasticon);
4119}
4120# endif
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004123#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004124
4125/*
4126 * Used for building in the status line.
4127 */
4128typedef struct
4129{
4130 char_u *stl_start;
4131 int stl_minwid;
4132 int stl_maxwid;
4133 enum {
4134 Normal,
4135 Empty,
4136 Group,
4137 Middle,
4138 Highlight,
4139 TabPage,
4140 Trunc
4141 } stl_type;
4142} stl_item_T;
4143
4144static size_t stl_items_len = 20; // Initial value, grows as needed.
4145static stl_item_T *stl_items = NULL;
4146static int *stl_groupitem = NULL;
4147static stl_hlrec_T *stl_hltab = NULL;
4148static stl_hlrec_T *stl_tabtab = NULL;
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004151 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 * Return length of string in screen cells.
4153 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004154 * Normally works for window "wp", except when working for 'tabline' then it
4155 * is "curwin".
4156 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * Items are drawn interspersed with the text that surrounds it
4158 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4159 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4160 *
4161 * If maxwidth is not zero, the string will be filled at any middle marker
4162 * or truncated if too long, fillchar is used for all whitespace.
4163 */
4164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004165build_stl_str_hl(
4166 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004167 char_u *out, // buffer to write into != NameBuff
4168 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004170 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004171 int fillchar,
4172 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004173 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4174 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
Bram Moolenaar10772302019-01-20 18:25:54 +01004176 linenr_T lnum;
4177 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 char_u *p;
4179 char_u *s;
4180 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004181 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004183 win_T *save_curwin;
4184 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004185 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186#endif
4187 int empty_line;
4188 colnr_T virtcol;
4189 long l;
4190 long n;
4191 int prevchar_isflag;
4192 int prevchar_isitem;
4193 int itemisflag;
4194 int fillable;
4195 char_u *str;
4196 long num;
4197 int width;
4198 int itemcnt;
4199 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004200 int group_end_userhl;
4201 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004203#ifdef FEAT_EVAL
4204 int evaldepth;
4205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int minwid;
4207 int maxwid;
4208 int zeropad;
4209 char_u base;
4210 char_u opt;
4211#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004212 char_u buf_tmp[TMPLEN];
4213 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004214 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004215 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004216 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004217 int save_KeyTyped = KeyTyped;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004218
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004219 // When inside update_screen() we do not want redrawing a statusline,
4220 // ruler, title, etc. to trigger another redraw, it may cause an endless
4221 // loop.
4222 if (updating_screen)
4223 redraw_not_allowed = TRUE;
4224
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004225 if (stl_items == NULL)
4226 {
4227 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4228 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004229
4230 // Allocate one more, because the last element is used to indicate the
4231 // end of the list.
4232 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4233 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004234 }
4235
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004236#ifdef FEAT_EVAL
4237 /*
4238 * When the format starts with "%!" then evaluate it as an expression and
4239 * use the result as the actual format string.
4240 */
4241 if (fmt[0] == '%' && fmt[1] == '!')
4242 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004243 typval_T tv;
4244
4245 tv.v_type = VAR_NUMBER;
4246 tv.vval.v_number = wp->w_id;
4247 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4248
Bram Moolenaar9530b582022-01-22 13:39:08 +00004249 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004250 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004251 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004252
4253 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004254 }
4255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 if (fillchar == 0)
4258 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004259
Bram Moolenaar10772302019-01-20 18:25:54 +01004260 // The cursor in windows other than the current one isn't always
4261 // up-to-date, esp. because of autocommands and timers.
4262 lnum = wp->w_cursor.lnum;
4263 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4264 {
4265 lnum = wp->w_buffer->b_ml.ml_line_count;
4266 wp->w_cursor.lnum = lnum;
4267 }
4268
4269 // Get line & check if empty (cursorpos will show "0-1"). Note that
4270 // p will become invalid when getting another buffer line.
4271 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004272 empty_line = (*p == NUL);
4273
Bram Moolenaar10772302019-01-20 18:25:54 +01004274 // Get the byte value now, in case we need it below. This is more efficient
4275 // than making a copy of the line.
4276 len = STRLEN(p);
4277 if (wp->w_cursor.col > (colnr_T)len)
4278 {
4279 // Line may have changed since checking the cursor column, or the lnum
4280 // was adjusted above.
4281 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004282 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004283 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004284 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004285 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004286 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004289#ifdef FEAT_EVAL
4290 evaldepth = 0;
4291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 p = out;
4293 curitem = 0;
4294 prevchar_isflag = TRUE;
4295 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004296 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004298 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004299 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004300 size_t new_len = stl_items_len * 3 / 2;
4301 stl_item_T *new_items;
4302 int *new_groupitem;
4303 stl_hlrec_T *new_hlrec;
4304
4305 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4306 if (new_items == NULL)
4307 break;
4308 stl_items = new_items;
4309 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4310 if (new_groupitem == NULL)
4311 break;
4312 stl_groupitem = new_groupitem;
Brandon Richardsona493b652022-02-19 11:45:03 +00004313 new_hlrec = vim_realloc(stl_hltab,
4314 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004315 if (new_hlrec == NULL)
4316 break;
4317 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004318 new_hlrec = vim_realloc(stl_tabtab,
4319 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004320 if (new_hlrec == NULL)
4321 break;
4322 stl_tabtab = new_hlrec;
4323 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004324 }
4325
zeertzjq122dea72022-07-27 15:48:45 +01004326 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 prevchar_isflag = prevchar_isitem = FALSE;
4328
4329 /*
4330 * Handle up to the next '%' or the end.
4331 */
4332 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4333 *p++ = *s++;
4334 if (*s == NUL || p + 1 >= out + outlen)
4335 break;
4336
4337 /*
4338 * Handle one '%' item.
4339 */
4340 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004341 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (*s == '%')
4344 {
4345 if (p + 1 >= out + outlen)
4346 break;
4347 *p++ = *s++;
4348 prevchar_isflag = prevchar_isitem = FALSE;
4349 continue;
4350 }
4351 if (*s == STL_MIDDLEMARK)
4352 {
4353 s++;
4354 if (groupdepth > 0)
4355 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004356 stl_items[curitem].stl_type = Middle;
4357 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 continue;
4359 }
4360 if (*s == STL_TRUNCMARK)
4361 {
4362 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004363 stl_items[curitem].stl_type = Trunc;
4364 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 continue;
4366 }
4367 if (*s == ')')
4368 {
4369 s++;
4370 if (groupdepth < 1)
4371 continue;
4372 groupdepth--;
4373
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004374 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *p = NUL;
4376 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004377 if (curitem > stl_groupitem[groupdepth] + 1
4378 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004380 // remove group if all items are empty and highlight group
4381 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004382 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004383 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004384 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004385 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004386 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004387 group_start_userhl = group_end_userhl =
4388 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004389 break;
4390 }
4391 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004392 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004393 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004394 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004396 if (stl_items[n].stl_type == Highlight)
4397 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004398 }
4399 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004401 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 p = t;
4403 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004404 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004405 {
4406 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004407 if (stl_items[n].stl_type == Highlight)
4408 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004409 // adjust the start position of TabPage to the next
4410 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004411 if (stl_items[n].stl_type == TabPage)
4412 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004416 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004418 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (has_mbyte)
4420 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004421 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004423 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004426 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 }
4429 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004430 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4431 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004434 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004436
4437 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004438 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004439 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
Bram Moolenaarc667da52019-11-30 20:52:27 +01004441 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004442 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
LemonBoy57ff5262022-05-09 21:03:47 +01004444 // Minus one for the leading '<' added above.
4445 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004446 if (stl_items[l].stl_start < t)
4447 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004450 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004452 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004453 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (n < 0)
4455 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004456 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 n = 0 - n;
4458 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004459 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461 else
4462 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004463 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004464 l = (n - l) * MB_CHAR2LEN(fillchar);
4465 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004467 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004469 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4470 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004472 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 }
4475 continue;
4476 }
4477 minwid = 0;
4478 maxwid = 9999;
4479 zeropad = FALSE;
4480 l = 1;
4481 if (*s == '0')
4482 {
4483 s++;
4484 zeropad = TRUE;
4485 }
4486 if (*s == '-')
4487 {
4488 s++;
4489 l = -1;
4490 }
4491 if (VIM_ISDIGIT(*s))
4492 {
4493 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004494 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 minwid = 0;
4496 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004497 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004499 stl_items[curitem].stl_type = Highlight;
4500 stl_items[curitem].stl_start = p;
4501 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 s++;
4503 curitem++;
4504 continue;
4505 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004506 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4507 {
4508 if (*s == STL_TABCLOSENR)
4509 {
4510 if (minwid == 0)
4511 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004512 // %X ends the close label, go back to the previously
4513 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004514 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004515 if (stl_items[n].stl_type == TabPage
4516 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004517 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004518 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004519 break;
4520 }
4521 }
4522 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004523 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004524 minwid = - minwid;
4525 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004526 stl_items[curitem].stl_type = TabPage;
4527 stl_items[curitem].stl_start = p;
4528 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004529 s++;
4530 curitem++;
4531 continue;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (*s == '.')
4534 {
4535 s++;
4536 if (VIM_ISDIGIT(*s))
4537 {
4538 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004539 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 maxwid = 50;
4541 }
4542 }
4543 minwid = (minwid > 50 ? 50 : minwid) * l;
4544 if (*s == '(')
4545 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004546 stl_groupitem[groupdepth++] = curitem;
4547 stl_items[curitem].stl_type = Group;
4548 stl_items[curitem].stl_start = p;
4549 stl_items[curitem].stl_minwid = minwid;
4550 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 s++;
4552 curitem++;
4553 continue;
4554 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004555#ifdef FEAT_EVAL
4556 // Denotes end of expanded %{} block
4557 if (*s == '}' && evaldepth > 0)
4558 {
4559 s++;
4560 evaldepth--;
4561 continue;
4562 }
4563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (vim_strchr(STL_ALL, *s) == NULL)
4565 {
4566 s++;
4567 continue;
4568 }
4569 opt = *s++;
4570
Bram Moolenaarc667da52019-11-30 20:52:27 +01004571 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 base = 'D';
4573 itemisflag = FALSE;
4574 fillable = TRUE;
4575 num = -1;
4576 str = NULL;
4577 switch (opt)
4578 {
4579 case STL_FILEPATH:
4580 case STL_FULLPATH:
4581 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004582 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004584 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 else
4586 {
4587 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004588 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4590 }
4591 trans_characters(NameBuff, MAXPATHL);
4592 if (opt != STL_FILENAME)
4593 str = NameBuff;
4594 else
4595 str = gettail(NameBuff);
4596 break;
4597
Bram Moolenaarc667da52019-11-30 20:52:27 +01004598 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004599 {
4600#ifdef FEAT_EVAL
4601 char_u *block_start = s - 1;
4602#endif
4603 int reevaluate = (*s == '%');
4604
4605 if (reevaluate)
4606 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 itemisflag = TRUE;
4608 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004609 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4610 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004612 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004615 if (reevaluate)
4616 p[-1] = 0; // remove the % at the end of %{% expr %}
4617 else
4618 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004621 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4622 "%d", curbuf->b_fnum);
4623 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4624 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4625 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004627 save_curbuf = curbuf;
4628 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004629 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 curwin = wp;
4631 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004632 // Visual mode is only valid in the current window.
4633 if (curwin != save_curwin)
4634 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
Bram Moolenaar9530b582022-01-22 13:39:08 +00004636 str = eval_to_string_safe(p, use_sandbox, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004638 curwin = save_curwin;
4639 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004640 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004641 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004642 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 if (str != NULL && *str != 0)
4645 {
4646 if (*skipdigits(str) == NUL)
4647 {
4648 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004649 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 itemisflag = FALSE;
4651 }
4652 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004653
4654 // If the output of the expression needs to be evaluated
4655 // replace the %{} block with the result of evaluation
4656 if (reevaluate && str != NULL && *str != 0
4657 && strchr((const char *)str, '%') != NULL
4658 && evaldepth < MAX_STL_EVAL_DEPTH)
4659 {
4660 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4661 size_t str_length = strlen((const char *)str);
4662 size_t fmt_length = strlen((const char *)s);
4663 size_t new_fmt_len = parsed_usefmt
4664 + str_length + fmt_length + 3;
4665 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4666 char_u *new_fmt_p = new_fmt;
4667
4668 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4669 + parsed_usefmt;
4670 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4671 + str_length;
4672 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4673 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4674 + fmt_length;
4675 *new_fmt_p = 0;
4676 new_fmt_p = NULL;
4677
4678 if (usefmt != fmt)
4679 vim_free(usefmt);
4680 VIM_CLEAR(str);
4681 usefmt = new_fmt;
4682 s = usefmt + parsed_usefmt;
4683 evaldepth++;
4684 continue;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#endif
4687 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 case STL_LINE:
4690 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4691 ? 0L : (long)(wp->w_cursor.lnum);
4692 break;
4693
4694 case STL_NUMLINES:
4695 num = wp->w_buffer->b_ml.ml_line_count;
4696 break;
4697
4698 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004699 num = (State & MODE_INSERT) == 0 && empty_line
4700 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 break;
4702
4703 case STL_VIRTCOL:
4704 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004705 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004706 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004708 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4709 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 break;
4711 num = (long)virtcol;
4712 break;
4713
4714 case STL_PERCENTAGE:
4715 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4716 (long)wp->w_buffer->b_ml.ml_line_count);
4717 break;
4718
4719 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004720 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004721 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 break;
4723
4724 case STL_ARGLISTSTAT:
4725 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004726 buf_tmp[0] = 0;
4727 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4728 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 break;
4730
4731 case STL_KEYMAP:
4732 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004733 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4734 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 break;
4736 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004737#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004738 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739#else
4740 num = 0;
4741#endif
4742 break;
4743
4744 case STL_BUFNO:
4745 num = wp->w_buffer->b_fnum;
4746 break;
4747
4748 case STL_OFFSET_X:
4749 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004750 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 case STL_OFFSET:
4752#ifdef FEAT_BYTEOFF
4753 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004754 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4755 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4756 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#endif
4758 break;
4759
4760 case STL_BYTEVAL_X:
4761 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004762 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004764 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (num == NL)
4766 num = 0;
4767 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4768 num = NL;
4769 break;
4770
4771 case STL_ROFLAG:
4772 case STL_ROFLAG_ALT:
4773 itemisflag = TRUE;
4774 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004775 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 break;
4777
4778 case STL_HELPFLAG:
4779 case STL_HELPFLAG_ALT:
4780 itemisflag = TRUE;
4781 if (wp->w_buffer->b_help)
4782 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004783 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 break;
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 case STL_FILETYPE:
4787 if (*wp->w_buffer->b_p_ft != NUL
4788 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4789 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004790 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004791 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004792 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794 break;
4795
4796 case STL_FILETYPE_ALT:
4797 itemisflag = TRUE;
4798 if (*wp->w_buffer->b_p_ft != NUL
4799 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4800 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004801 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004802 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004803 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004805 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
Bram Moolenaar4033c552017-09-16 20:54:51 +02004809#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 case STL_PREVIEWFLAG:
4811 case STL_PREVIEWFLAG_ALT:
4812 itemisflag = TRUE;
4813 if (wp->w_p_pvw)
4814 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4815 : _("[Preview]"));
4816 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004817
4818 case STL_QUICKFIX:
4819 if (bt_quickfix(wp->w_buffer))
4820 str = (char_u *)(wp->w_llist_ref
4821 ? _(msg_loclist)
4822 : _(msg_qflist));
4823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#endif
4825
4826 case STL_MODIFIED:
4827 case STL_MODIFIED_ALT:
4828 itemisflag = TRUE;
4829 switch ((opt == STL_MODIFIED_ALT)
4830 + bufIsChanged(wp->w_buffer) * 2
4831 + (!wp->w_buffer->b_p_ma) * 4)
4832 {
4833 case 2: str = (char_u *)"[+]"; break;
4834 case 3: str = (char_u *)",+"; break;
4835 case 4: str = (char_u *)"[-]"; break;
4836 case 5: str = (char_u *)",-"; break;
4837 case 6: str = (char_u *)"[+-]"; break;
4838 case 7: str = (char_u *)",+-"; break;
4839 }
4840 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004841
4842 case STL_HIGHLIGHT:
4843 t = s;
4844 while (*s != '#' && *s != NUL)
4845 ++s;
4846 if (*s == '#')
4847 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004848 stl_items[curitem].stl_type = Highlight;
4849 stl_items[curitem].stl_start = p;
4850 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004851 curitem++;
4852 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004853 if (*s != NUL)
4854 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004855 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004858 stl_items[curitem].stl_start = p;
4859 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (str != NULL && *str)
4861 {
4862 t = str;
4863 if (itemisflag)
4864 {
4865 if ((t[0] && t[1])
4866 && ((!prevchar_isitem && *t == ',')
4867 || (prevchar_isflag && *t == ' ')))
4868 t++;
4869 prevchar_isflag = TRUE;
4870 }
4871 l = vim_strsize(t);
4872 if (l > 0)
4873 prevchar_isitem = TRUE;
4874 if (l > maxwid)
4875 {
4876 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (has_mbyte)
4878 {
4879 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004880 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 l -= byte2cells(*t++);
4884 if (p + 1 >= out + outlen)
4885 break;
4886 *p++ = '<';
4887 }
4888 if (minwid > 0)
4889 {
4890 for (; l < minwid && p + 1 < out + outlen; l++)
4891 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004892 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4894 *p++ = ' ';
4895 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004896 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 minwid = 0;
4899 }
4900 else
4901 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004902 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004904 // Change a space by fillchar, unless fillchar is '-' and a
4905 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004906 if (fillable && *t == ' '
4907 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4908 MB_CHAR2BYTES(fillchar, p);
4909 else
4910 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004913 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else if (num >= 0)
4916 {
4917 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4918 char_u nstr[20];
4919
4920 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004921 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 prevchar_isitem = TRUE;
4923 t = nstr;
4924 if (opt == STL_VIRTCOL_ALT)
4925 {
4926 *t++ = '-';
4927 minwid--;
4928 }
4929 *t++ = '%';
4930 if (zeropad)
4931 *t++ = '0';
4932 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004933 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 *t = 0;
4935
4936 for (n = num, l = 1; n >= nbase; n /= nbase)
4937 l++;
4938 if (opt == STL_VIRTCOL_ALT)
4939 l++;
4940 if (l > maxwid)
4941 {
4942 l += 2;
4943 n = l - maxwid;
4944 while (l-- > maxwid)
4945 num /= nbase;
4946 *t++ = '>';
4947 *t++ = '%';
4948 *t = t[-3];
4949 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004950 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4951 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004954 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4955 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 p += STRLEN(p);
4957 }
4958 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004959 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004961 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
4962 prevchar_isflag = FALSE; // Item not NULL, but not a flag
4963 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (opt == STL_VIM_EXPR)
4965 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 curitem++;
4967 }
4968 *p = NUL;
4969 itemcnt = curitem;
4970
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004971#ifdef FEAT_EVAL
4972 if (usefmt != fmt)
4973 vim_free(usefmt);
4974#endif
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 width = vim_strsize(out);
4977 if (maxwidth > 0 && width > maxwidth)
4978 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004979 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 l = 0;
4981 if (itemcnt == 0)
4982 s = out;
4983 else
4984 {
4985 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004986 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004988 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004989 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 break;
4991 }
4992 if (l == itemcnt)
4993 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004994 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004995 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 l = 0;
4997 }
4998 }
4999
5000 if (width - vim_strsize(s) >= maxwidth)
5001 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005002 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (has_mbyte)
5004 {
5005 s = out;
5006 width = 0;
5007 for (;;)
5008 {
5009 width += ptr2cells(s);
5010 if (width >= maxwidth)
5011 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005012 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005014 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005016 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 s = out + maxwidth - 1;
5020 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005021 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023 itemcnt = l;
5024 *s++ = '>';
5025 *s = 0;
5026 }
5027 else
5028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (has_mbyte)
5030 {
5031 n = 0;
5032 while (width >= maxwidth)
5033 {
5034 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005035 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 }
5038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 n = width - maxwidth + 1;
5040 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005041 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *s = '<';
5043
Bram Moolenaarc667da52019-11-30 20:52:27 +01005044 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 while (++width < maxwidth)
5046 {
5047 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005048 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 *s = NUL;
5050 }
5051
Bram Moolenaarc667da52019-11-30 20:52:27 +01005052 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 for (; l < itemcnt; l++)
5054 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005055 if (stl_items[l].stl_start - n >= s)
5056 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005058 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 }
5061 width = maxwidth;
5062 }
5063 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5064 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005065 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005067 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069 if (l < itemcnt)
5070 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01005071 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
5072 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005073 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005074 for (s = stl_items[l].stl_start; s < p;)
5075 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005077 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 width = maxwidth;
5079 }
5080 }
5081
Bram Moolenaarc667da52019-11-30 20:52:27 +01005082 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005083 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005085 *hltab = stl_hltab;
5086 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 for (l = 0; l < itemcnt; l++)
5088 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005089 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005091 sp->start = stl_items[l].stl_start;
5092 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005093 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005096 sp->start = NULL;
5097 sp->userhl = 0;
5098 }
5099
Bram Moolenaarc667da52019-11-30 20:52:27 +01005100 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005101 if (tabtab != NULL)
5102 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005103 *tabtab = stl_tabtab;
5104 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005105 for (l = 0; l < itemcnt; l++)
5106 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005107 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005108 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005109 sp->start = stl_items[l].stl_start;
5110 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005111 sp++;
5112 }
5113 }
5114 sp->start = NULL;
5115 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005118 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005119
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005120 // A user function may reset KeyTyped, restore it.
5121 KeyTyped = save_KeyTyped;
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 return width;
5124}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005125#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005127#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
5128 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005130 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5131 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 */
5133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005134get_rel_pos(
5135 win_T *wp,
5136 char_u *buf,
5137 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005139 long above; // number of lines above window
5140 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
Bram Moolenaarc667da52019-11-30 20:52:27 +01005142 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005143 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 above = wp->w_topline - 1;
5145#ifdef FEAT_DIFF
5146 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005147 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005148 above = 0; // All buffer lines are displayed and there is an
5149 // indication of filler lines, that can be considered
5150 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151#endif
5152 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5153 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005154 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5155 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005157 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005159 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 ? (int)(above / ((above + below) / 100L))
5161 : (int)(above * 100L / (above + below)));
5162}
5163#endif
5164
5165/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005166 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 * Return TRUE if it was appended.
5168 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005170append_arg_number(
5171 win_T *wp,
5172 char_u *buf,
5173 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005174 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 char_u *p;
5177
Bram Moolenaarc667da52019-11-30 20:52:27 +01005178 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 return FALSE;
5180
Bram Moolenaarc667da52019-11-30 20:52:27 +01005181 p = buf + STRLEN(buf); // go to the end of the buffer
5182 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 return FALSE;
5184 *p++ = ' ';
5185 *p++ = '(';
5186 if (add_file)
5187 {
5188 STRCPY(p, "file ");
5189 p += 5;
5190 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005191 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5192 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5194 return TRUE;
5195}
5196
5197/*
5198 * If fname is not a full path, make it a full path.
5199 * Returns pointer to allocated memory (NULL for failure).
5200 */
5201 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005202fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 /*
5205 * Force expanding the path always for Unix, because symbolic links may
5206 * mess up the full path name, even though it starts with a '/'.
5207 * Also expand when there is ".." in the file name, try to remove it,
5208 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005209 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 * For MS-Windows also expand names like "longna~1" to "longname".
5211 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005212#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 return FullName_save(fname, TRUE);
5214#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005215 if (!vim_isAbsName(fname)
5216 || strstr((char *)fname, "..") != NULL
5217 || strstr((char *)fname, "//") != NULL
5218# ifdef BACKSLASH_IN_FILENAME
5219 || strstr((char *)fname, "\\\\") != NULL
5220# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005221# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 )
5225 return FullName_save(fname, FALSE);
5226
5227 fname = vim_strsave(fname);
5228
Bram Moolenaar9b942202007-10-03 12:31:33 +00005229# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005230 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005231 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 return fname;
5235#endif
5236}
5237
5238/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005239 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5240 * "*ffname" becomes a pointer to allocated memory (or NULL).
5241 * When resolving a link both "*sfname" and "*ffname" will point to the same
5242 * allocated memory.
5243 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005244 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005247fname_expand(
5248 buf_T *buf UNUSED,
5249 char_u **ffname,
5250 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005252 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005254 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005256 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258#ifdef FEAT_SHORTCUT
5259 if (!buf->b_p_bin)
5260 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005261 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005263 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005264 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005265 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 vim_free(*ffname);
5268 *ffname = rfname;
5269 *sfname = rfname;
5270 }
5271 }
5272#endif
5273}
5274
5275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 * Open a window for a number of buffers.
5277 */
5278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005279ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 buf_T *buf;
5282 win_T *wp, *wpnext;
5283 int split_ret = OK;
5284 int p_ea_save;
5285 int open_wins = 0;
5286 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005287 int count; // Maximum number of windows to open.
5288 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005289 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005290 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaarc667da52019-11-30 20:52:27 +01005292 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 count = 9999;
5294 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005295 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5297 all = FALSE;
5298 else
5299 all = TRUE;
5300
5301 setpcmark();
5302
5303#ifdef FEAT_GUI
5304 need_mouse_correct = TRUE;
5305#endif
5306
5307 /*
5308 * Close superfluous windows (two windows for the same buffer).
5309 * Also close windows that are not full-width.
5310 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005311 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005312 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005313 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005315 tpnext = curtab->tp_next;
5316 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005318 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005319 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005320 || ((cmdmod.cmod_split & WSP_VERT)
5321 ? wp->w_height + wp->w_status_height < Rows - p_ch
5322 - tabline_height()
5323 : wp->w_width != Columns)
5324 || (had_tab > 0 && wp != firstwin))
5325 && !ONE_WINDOW
5326 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5327 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005328 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005329 if (win_close(wp, FALSE) == FAIL)
5330 break;
5331 // Just in case an autocommand does something strange with
5332 // windows: start all over...
5333 wpnext = firstwin;
5334 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005335 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005336 }
5337 else
5338 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005340
Bram Moolenaarc667da52019-11-30 20:52:27 +01005341 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005342 if (had_tab == 0 || tpnext == NULL)
5343 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005344 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346
5347 /*
5348 * Go through the buffer list. When a buffer doesn't have a window yet,
5349 * open one. Otherwise move the window to the right position.
5350 * Watch out for autocommands that delete buffers or windows!
5351 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005352 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5357 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005358 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5360 continue;
5361
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005362 if (had_tab != 0)
5363 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005364 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005365 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005366 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005367 else
5368 wp = NULL;
5369 }
5370 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005371 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005372 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005373 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005374 if (wp->w_buffer == buf)
5375 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005376 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005377 if (wp != NULL)
5378 win_move_after(wp, curwin);
5379 }
5380
5381 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005383 bufref_T bufref;
5384
5385 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005386
Bram Moolenaarc667da52019-11-30 20:52:27 +01005387 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005389 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5391 ++open_wins;
5392 p_ea = p_ea_save;
5393 if (split_ret == FAIL)
5394 continue;
5395
Bram Moolenaarc667da52019-11-30 20:52:27 +01005396 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005399 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005401 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 break;
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (swap_exists_action == SEA_QUIT)
5406 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005407#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005408 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005409
Bram Moolenaarc667da52019-11-30 20:52:27 +01005410 // Reset the error/interrupt/exception state here so that
5411 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005412 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005413#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005414
Bram Moolenaarc667da52019-11-30 20:52:27 +01005415 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 win_close(curwin, TRUE);
5417 --open_wins;
5418 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005419 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005420
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005421#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005422 // Restore the error/interrupt/exception state if not
5423 // discarded by a new aborting error, interrupt, or uncaught
5424 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005425 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428 else
5429 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
5432 ui_breakcheck();
5433 if (got_int)
5434 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005435 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 break;
5437 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005438#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005439 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005440 if (aborting())
5441 break;
5442#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005443 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005444 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005445 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005448 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 /*
5452 * Close superfluous windows.
5453 */
5454 for (wp = lastwin; open_wins > count; )
5455 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005456 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if (!win_valid(wp))
5459 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005460 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 wp = lastwin;
5462 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005463 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005465 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 --open_wins;
5467 wp = lastwin;
5468 }
5469 else
5470 {
5471 wp = wp->w_prev;
5472 if (wp == NULL)
5473 break;
5474 }
5475 }
5476}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005479static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481/*
5482 * do_modelines() - process mode lines for the current file
5483 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005484 * "flags" can be:
5485 * OPT_WINONLY only set options local to window
5486 * OPT_NOWIN don't set options local to window
5487 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 * Returns immediately if the "ml" option isn't set.
5489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005491do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005493 linenr_T lnum;
5494 int nmlines;
5495 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5498 return;
5499
Bram Moolenaarc667da52019-11-30 20:52:27 +01005500 // Disallow recursive entry here. Can happen when executing a modeline
5501 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (entered)
5503 return;
5504
5505 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005506 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005508 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 nmlines = 0;
5510
Hu Jialun9dcd3492021-08-28 20:42:50 +02005511 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005513 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 nmlines = 0;
5515 --entered;
5516}
5517
Bram Moolenaarc667da52019-11-30 20:52:27 +01005518#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520/*
5521 * chk_modeline() - check a single line for a mode string
5522 * Return FAIL if an error encountered.
5523 */
5524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005525chk_modeline(
5526 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005527 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 char_u *s;
5530 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005531 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 int prev;
5533 int vers;
5534 int end;
5535 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005536 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005537
Bram Moolenaare31ee862020-01-07 20:59:34 +01005538 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 prev = -1;
5541 for (s = ml_get(lnum); *s != NUL; ++s)
5542 {
5543 if (prev == -1 || vim_isspace(prev))
5544 {
5545 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5546 || STRNCMP(s, "vi:", (size_t)3) == 0)
5547 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005548 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005549 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
5551 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5552 e = s + 4;
5553 else
5554 e = s + 3;
5555 vers = getdigits(&e);
5556 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005557 && (s[0] != 'V'
5558 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 && (s[3] == ':'
5560 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5561 || (VIM_VERSION_100 < vers && s[3] == '<')
5562 || (VIM_VERSION_100 > vers && s[3] == '>')
5563 || (VIM_VERSION_100 == vers && s[3] == '=')))
5564 break;
5565 }
5566 }
5567 prev = *s;
5568 }
5569
5570 if (*s)
5571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005572 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 ++s;
5574 while (s[-1] != ':');
5575
Bram Moolenaarc667da52019-11-30 20:52:27 +01005576 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 if (linecopy == NULL)
5578 return FAIL;
5579
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005580 // prepare for emsg()
5581 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005582 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 end = FALSE;
5585 while (end == FALSE)
5586 {
5587 s = skipwhite(s);
5588 if (*s == NUL)
5589 break;
5590
5591 /*
5592 * Find end of set command: ':' or end of line.
5593 * Skip over "\:", replacing it with ":".
5594 */
5595 for (e = s; *e != ':' && *e != NUL; ++e)
5596 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005597 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (*e == NUL)
5599 end = TRUE;
5600
5601 /*
5602 * If there is a "set" command, require a terminating ':' and
5603 * ignore the stuff after the ':'.
5604 * "vi:set opt opt opt: foo" -- foo not interpreted
5605 * "vi:opt opt opt: foo" -- foo interpreted
5606 * Accept "se" for compatibility with Elvis.
5607 */
5608 if (STRNCMP(s, "set ", (size_t)4) == 0
5609 || STRNCMP(s, "se ", (size_t)3) == 0)
5610 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005611 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break;
5613 end = TRUE;
5614 s = vim_strchr(s, ' ') + 1;
5615 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005616 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaarc667da52019-11-30 20:52:27 +01005618 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005620 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005621
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005622 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005623 current_sctx.sc_version = 1;
5624#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005625 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005626 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005627 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005629
Bram Moolenaar5958f952018-11-20 04:25:21 +01005630 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005631 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005632
Bram Moolenaara3227e22006-03-08 21:32:40 +00005633 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005634
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005635 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005636 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005637 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 break;
5639 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005640 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642
Bram Moolenaare31ee862020-01-07 20:59:34 +01005643 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005644 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 vim_free(linecopy);
5646 }
5647 return retval;
5648}
5649
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005650/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005651 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5652 */
5653 int
5654bt_normal(buf_T *buf)
5655{
5656 return buf != NULL && buf->b_p_bt[0] == NUL;
5657}
5658
5659/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005660 * Return TRUE if "buf" is the quickfix buffer.
5661 */
5662 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005663bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005664{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005665#ifdef FEAT_QUICKFIX
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005666 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005667#else
5668 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005669#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005670}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005671
5672/*
5673 * Return TRUE if "buf" is a terminal buffer.
5674 */
5675 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005676bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005677{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005678#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005679 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005680#else
5681 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005682#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005683}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005684
5685/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005686 * Return TRUE if "buf" is a help buffer.
5687 */
5688 int
5689bt_help(buf_T *buf)
5690{
5691 return buf != NULL && buf->b_help;
5692}
5693
5694/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005695 * Return TRUE if "buf" is a prompt buffer.
5696 */
5697 int
5698bt_prompt(buf_T *buf)
5699{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005700 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5701}
5702
Dominique Pelle748b3082022-01-08 12:41:16 +00005703#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005704/*
5705 * Return TRUE if "buf" is a buffer for a popup window.
5706 */
5707 int
5708bt_popup(buf_T *buf)
5709{
5710 return buf != NULL && buf->b_p_bt != NULL
5711 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005712}
Dominique Pelle748b3082022-01-08 12:41:16 +00005713#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005714
5715/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005716 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005717 * buffer. This means the buffer name may not be a file name, at least not for
5718 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005719 */
5720 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005721bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005722{
5723 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5724 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005725 || buf->b_p_bt[0] == 't'
5726 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005727}
5728
Bram Moolenaarc3126192022-08-26 12:58:17 +01005729/*
5730 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5731 * buffer. This means the buffer is not to be read from a file.
5732 */
5733 static int
5734bt_nofileread(buf_T *buf)
5735{
5736 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5737 || buf->b_p_bt[0] == 't'
5738 || buf->b_p_bt[0] == 'q'
5739 || buf->b_p_bt[0] == 'p');
5740}
5741
Dominique Pelle748b3082022-01-08 12:41:16 +00005742#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005743/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005744 * Return TRUE if "buf" has 'buftype' set to "nofile".
5745 */
5746 int
5747bt_nofile(buf_T *buf)
5748{
5749 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5750}
Dominique Pelle748b3082022-01-08 12:41:16 +00005751#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005752
5753/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005754 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5755 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005756 */
5757 int
5758bt_dontwrite(buf_T *buf)
5759{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005760 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005761 || buf->b_p_bt[0] == 't'
5762 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005763}
5764
5765 int
5766bt_dontwrite_msg(buf_T *buf)
5767{
5768 if (bt_dontwrite(buf))
5769 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005770 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005771 return TRUE;
5772 }
5773 return FALSE;
5774}
5775
5776/*
5777 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5778 * and 'bufhidden'.
5779 */
5780 int
5781buf_hide(buf_T *buf)
5782{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005783 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005784 switch (buf->b_p_bh[0])
5785 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005786 case 'u': // "unload"
5787 case 'w': // "wipe"
5788 case 'd': return FALSE; // "delete"
5789 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005790 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005791 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794/*
5795 * Return special buffer name.
5796 * Returns NULL when the buffer has a normal file name.
5797 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005798 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005799buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005801#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005803 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005804 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005805 * Differentiate between the quickfix and location list buffers using
5806 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005807 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005808 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005809 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005810 else
5811 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005814
Bram Moolenaarc667da52019-11-30 20:52:27 +01005815 // There is no _file_ when 'buftype' is "nofile", b_sfname
5816 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005817 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005819#ifdef FEAT_TERMINAL
5820 if (buf->b_term != NULL)
5821 return term_get_status_text(buf->b_term);
5822#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005823 if (buf->b_fname != NULL)
5824 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005825#ifdef FEAT_JOB_CHANNEL
5826 if (bt_prompt(buf))
5827 return (char_u *)_("[Prompt]");
5828#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005829#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005830 if (bt_popup(buf))
5831 return (char_u *)_("[Popup]");
5832#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005833 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005837 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 return NULL;
5839}
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005842 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5843 */
5844 char_u *
5845buf_get_fname(buf_T *buf)
5846{
5847 if (buf->b_fname == NULL)
5848 return (char_u *)_("[No Name]");
5849 return buf->b_fname;
5850}
5851
5852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5854 */
5855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005856set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 if (on != curbuf->b_p_bl)
5859 {
5860 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (on)
5862 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5863 else
5864 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
5866}
5867
5868/*
5869 * Read the file for "buf" again and check if the contents changed.
5870 * Return TRUE if it changed or this could not be checked.
5871 */
5872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005873buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 buf_T *newbuf;
5876 int differ = TRUE;
5877 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 exarg_T ea;
5880
Bram Moolenaarc667da52019-11-30 20:52:27 +01005881 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5883 if (newbuf == NULL)
5884 return TRUE;
5885
Bram Moolenaarc667da52019-11-30 20:52:27 +01005886 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 if (prep_exarg(&ea, buf) == FAIL)
5888 {
5889 wipe_buffer(newbuf, FALSE);
5890 return TRUE;
5891 }
5892
Bram Moolenaarc667da52019-11-30 20:52:27 +01005893 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
Bram Moolenaar4770d092006-01-12 23:22:24 +00005896 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 && readfile(buf->b_ffname, buf->b_fname,
5898 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5899 &ea, READ_NEW | READ_DUMMY) == OK)
5900 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005901 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5903 {
5904 differ = FALSE;
5905 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5906 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5907 {
5908 differ = TRUE;
5909 break;
5910 }
5911 }
5912 }
5913 vim_free(ea.cmd);
5914
Bram Moolenaarc667da52019-11-30 20:52:27 +01005915 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaarc667da52019-11-30 20:52:27 +01005918 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 wipe_buffer(newbuf, FALSE);
5920
5921 return differ;
5922}
5923
5924/*
5925 * Wipe out a buffer and decrement the last buffer number if it was used for
5926 * this buffer. Call this to wipe out a temp buffer that does not contain any
5927 * marks.
5928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005930wipe_buffer(
5931 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005932 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933{
5934 if (buf->b_fnum == top_file_num - 1)
5935 --top_file_num;
5936
Bram Moolenaarc667da52019-11-30 20:52:27 +01005937 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005938 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005939
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005940 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005943 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944}