blob: 90ca596783c6689caa716570b54ce999f492c094 [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);
Yee Cheng Chin15b314f2022-10-09 18:53:32 +010052static void no_write_message_buf(buf_T *buf);
LemonBoy893eeeb2024-07-10 20:20:48 +020053static int do_buffer_ext(int action, int start, int dir, int count, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55#ifdef UNIX
56# define dev_T dev_t
57#else
58# define dev_T unsigned
59#endif
60
Bram Moolenaar00d253e2020-04-06 22:13:01 +020061#define FOR_ALL_BUFS_FROM_LAST(buf) \
62 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
63
Bram Moolenaar4033c552017-09-16 20:54:51 +020064#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020065static char *msg_loclist = N_("[Location List]");
66static char *msg_qflist = N_("[Quickfix List]");
67#endif
68
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020069// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070static int buf_free_count = 0;
71
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020072static int top_file_num = 1; // highest file number
73static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
74
75/*
76 * Return the highest possible buffer number.
77 */
78 int
79get_highest_fnum(void)
80{
81 return top_file_num - 1;
82}
83
Bram Moolenaarc024b462019-06-08 18:07:21 +020084/*
85 * Read data from buffer for retrying.
86 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020087 static int
88read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010089 int read_stdin, // read file from stdin, otherwise fifo
90 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
91 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020092{
93 int retval = OK;
94 linenr_T line_count;
95
Bram Moolenaarc5935a82021-10-19 20:48:52 +010096 // Read from the buffer which the text is already filled in and append at
97 // the end. This makes it possible to retry when 'fileformat' or
98 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020099 line_count = curbuf->b_ml.ml_line_count;
100 retval = readfile(
101 read_stdin ? NULL : curbuf->b_ffname,
102 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100103 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200104 flags | READ_BUFFER);
105 if (retval == OK)
106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100107 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200108 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200109 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200110 }
111 else
112 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100113 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200115 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200116 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100117 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200118 curwin->w_cursor.lnum = 1;
119 curwin->w_cursor.col = 0;
120
121 if (read_stdin)
122 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100123 // Set or reset 'modified' before executing autocommands, so that
124 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100125 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100127 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200128 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100130 if (retval == OK)
131 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100133 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100134 curbuf, &retval);
135#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100136 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100138 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200139 }
140 return retval;
141}
142
Dominique Pelle748b3082022-01-08 12:41:16 +0000143#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200145 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
146 */
147 void
148buffer_ensure_loaded(buf_T *buf)
149{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000150 if (buf->b_ml.ml_mfp != NULL)
151 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200152
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000153 aco_save_T aco;
154
155 // Make sure the buffer is in a window. If not then skip it.
156 aucmd_prepbuf(&aco, buf);
157 if (curbuf == buf)
158 {
159 if (swap_exists_action != SEA_READONLY)
160 swap_exists_action = SEA_NONE;
161 open_buffer(FALSE, NULL, 0);
162 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200163 }
164}
Dominique Pelle748b3082022-01-08 12:41:16 +0000165#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200166
167/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200168 * Open current buffer, that is: open the memfile and read the file into
169 * memory.
170 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 */
172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100173open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100174 int read_stdin, // read file from stdin
175 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100176 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100178 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200180 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100181#ifdef FEAT_SYN_HL
182 long old_tw = curbuf->b_p_tw;
183#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200184 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100186 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
187 // When re-entering the same buffer, it should not change, because the
188 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (readonlymode && curbuf->b_ffname != NULL
190 && (curbuf->b_flags & BF_NEVERLOADED))
191 curbuf->b_p_ro = TRUE;
192
Bram Moolenaar4770d092006-01-12 23:22:24 +0000193 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100195 // There MUST be a memfile, otherwise we can't do anything
196 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100197 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200198 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 if (curbuf->b_ml.ml_mfp != NULL)
200 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100201 // If there is no memfile at all, exit.
202 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 if (curbuf == NULL)
204 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000205 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200206
207 // Don't try to do any saving, with "curbuf" NULL almost nothing
208 // will work.
209 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 getout(2);
211 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200212
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000213 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100215#ifdef FEAT_SYN_HL
216 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +0200217 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 return FAIL;
220 }
221
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100222 // Do not sync this buffer yet, may first want to read the file.
223 if (curbuf->b_ml.ml_mfp != NULL)
224 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
225
Bram Moolenaarc667da52019-11-30 20:52:27 +0100226 // The autocommands in readfile() may change the buffer, but only AFTER
227 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200228 set_bufref(&old_curbuf, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +0200229 curbuf->b_modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
Bram Moolenaarc667da52019-11-30 20:52:27 +0100231 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100232 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100234 // A buffer without an actual file should not use the buffer name to read a
235 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100236 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100237 flags |= READ_NOFILE;
238
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100239 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 if (curbuf->b_ffname != NULL
241#ifdef FEAT_NETBEANS_INTG
242 && netbeansReadFile
243#endif
244 )
245 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100246 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200247#ifdef UNIX
248 int save_bin = curbuf->b_p_bin;
249 int perm;
250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_NETBEANS_INTG
252 int oldFire = netbeansFireChanges;
253
254 netbeansFireChanges = 0;
255#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200256#ifdef UNIX
257 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200258 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200259 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200260# ifdef OPEN_CHR_FILES
261 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
262# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200263 ))
264 read_fifo = TRUE;
265 if (read_fifo)
266 curbuf->b_p_bin = TRUE;
267#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100268 if (shortmess(SHM_FILEINFO))
269 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200271 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200272 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
273#ifdef UNIX
274 if (read_fifo)
275 {
276 curbuf->b_p_bin = save_bin;
277 if (retval == OK)
Christian Brabandtf1c31342025-02-23 09:36:56 +0100278 // don't add READ_FIFO here, otherwise we won't be able to
279 // detect the encoding
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200280 retval = read_buffer(FALSE, eap, flags);
281 }
282#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100283 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#ifdef FEAT_NETBEANS_INTG
285 netbeansFireChanges = oldFire;
286#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100287 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200288 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 fix_help_buffer();
290 }
291 else if (read_stdin)
292 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200293 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100295 // First read the text in binary mode into the buffer.
296 // Then read from that same buffer and append at the end. This makes
297 // it possible to retry when 'fileformat' or 'fileencoding' was
298 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 curbuf->b_p_bin = TRUE;
300 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200301 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
302 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 curbuf->b_p_bin = save_bin;
304 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200305 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100308 // Can now sync this buffer in ml_sync_all().
309 if (curbuf->b_ml.ml_mfp != NULL
310 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
311 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
312
Bram Moolenaarc667da52019-11-30 20:52:27 +0100313 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100317 parse_cino(curbuf);
318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100320 // Set/reset the Changed flag first, autocmds may change the buffer.
321 // Apply the automatic commands, before processing the modelines.
322 // So the modelines have priority over autocommands.
323 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100324 // When reading stdin, the buffer contents always needs writing, so set
325 // the changed flag. Unless in readonly mode: "ls | gview -".
326 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000327 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200328 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100329#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000332 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200335 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100336 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaarc667da52019-11-30 20:52:27 +0100338 // Set last_changedtick to avoid triggering a TextChanged autocommand right
339 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100340 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100341 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100342 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100343
Bram Moolenaarc667da52019-11-30 20:52:27 +0100344 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#ifdef FEAT_EVAL
346 if (aborting())
347#else
348 if (got_int)
349#endif
350 curbuf->b_flags |= BF_READERR;
351
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000352#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100353 // Need to update automatic folding. Do this before the autocommands,
354 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000355 foldUpdateAll(curwin);
356#endif
357
Bram Moolenaarc667da52019-11-30 20:52:27 +0100358 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 if (!(curwin->w_valid & VALID_TOPLINE))
360 {
361 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100362#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100366#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100368#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#endif
371
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000372 if (retval != OK)
373 return retval;
374
375 // The autocommands may have changed the current buffer. Apply the
376 // modelines to the correct buffer, if it still exists and is loaded.
377 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000379 aco_save_T aco;
380
381 // Go to the buffer that was opened, make sure it is in a window.
382 // If not then skip it.
383 aucmd_prepbuf(&aco, old_curbuf.br_buf);
384 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000386 do_modelines(0);
387 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000389 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100390#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000391 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
392 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100393#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000394 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
395 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000398 // restore curwin/curbuf and a few other things
399 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 return retval;
404}
405
406/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200407 * Store "buf" in "bufref" and set the free count.
408 */
409 void
410set_bufref(bufref_T *bufref, buf_T *buf)
411{
412 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200413 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200414 bufref->br_buf_free_count = buf_free_count;
415}
416
417/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200418 * Return TRUE if "bufref->br_buf" points to the same buffer as when
419 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200420 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200421 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
422 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200423 */
424 int
425bufref_valid(bufref_T *bufref)
426{
427 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200428 ? TRUE : buf_valid(bufref->br_buf)
429 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200430}
431
432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200434 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 */
436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100437buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 buf_T *bp;
440
Bram Moolenaarc667da52019-11-30 20:52:27 +0100441 // Assume that we more often have a recent buffer, start with the last
442 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200443 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (bp == buf)
445 return TRUE;
446 return FALSE;
447}
448
449/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200450 * A hash table used to quickly lookup a buffer by its number.
451 */
452static hashtab_T buf_hashtab;
453
454 static void
455buf_hashtab_add(buf_T *buf)
456{
457 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000458 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000459 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200460}
461
462 static void
463buf_hashtab_remove(buf_T *buf)
464{
465 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
466
467 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000468 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200469}
470
Bram Moolenaar94f01952018-09-01 15:30:03 +0200471/*
472 * Return TRUE when buffer "buf" can be unloaded.
473 * Give an error message and return FALSE when the buffer is locked or the
474 * screen is being redrawn and the buffer is in a window.
475 */
476 static int
477can_unload_buffer(buf_T *buf)
478{
479 int can_unload = !buf->b_locked;
480
481 if (can_unload && updating_screen)
482 {
483 win_T *wp;
484
485 FOR_ALL_WINDOWS(wp)
486 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200487 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200488 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200489 break;
490 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200491 }
492 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100493 {
494 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
495
496 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
497 fname != NULL ? fname : (char_u *)"[No Name]");
498 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200499 return can_unload;
500}
Bram Moolenaara997b452018-04-17 23:24:06 +0200501
Christian Brabandt51b62382024-10-06 17:31:10 +0200502 int
503buf_locked(buf_T *buf)
504{
505 return buf->b_locked || buf->b_locked_split;
506}
507
Bram Moolenaar480778b2016-07-14 22:09:39 +0200508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * Close the link to a buffer.
510 * "action" is used when there is no longer a window for the buffer.
511 * It can be:
512 * 0 buffer becomes hidden
513 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100514 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200516 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * When doing all but the first one on the current buffer, the caller should
518 * get a new buffer very soon!
519 *
520 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100521 *
522 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
523 * cause there to be only one window with this buffer. e.g. when ":quit" is
524 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100525 *
526 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100527 *
528 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100530 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100531close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100532 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100533 buf_T *buf,
534 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100535 int abort_if_last,
536 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100539 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200540 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100541 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200542 win_T *the_curwin = curwin;
543 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200545 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
546 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaarcee52202020-03-11 14:19:58 +0100548 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100549
550 // Force unloading or deleting when 'bufhidden' says so.
551 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
552 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100553 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200554 {
555 del_buf = TRUE;
556 unload_buf = TRUE;
557 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100558 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200559 {
560 del_buf = TRUE;
561 unload_buf = TRUE;
562 wipe_buf = TRUE;
563 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100564 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200565 unload_buf = TRUE;
566
Bram Moolenaar94053a52017-08-01 21:44:33 +0200567#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100568 // depending on how we get here b_nwindows may already be zero
569 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200570 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100571 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200572 if (term_job_running(buf->b_term))
573 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200574 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200575 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200576 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100577 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200578
Bram Moolenaarc667da52019-11-30 20:52:27 +0100579 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200580 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100581
582 // A terminal buffer is wiped out when job has finished.
583 del_buf = TRUE;
584 unload_buf = TRUE;
585 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200586 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200587 else
588 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100589 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200590 del_buf = FALSE;
591 unload_buf = FALSE;
592 }
593 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100594 else if (buf->b_p_bh[0] == 'h' && !del_buf)
595 {
596 // Hide a terminal buffer.
597 unload_buf = FALSE;
598 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200599 else
600 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100601 if (del_buf || unload_buf)
602 {
603 // A terminal buffer is wiped out if the job has finished.
604 // We only do this when there's an intention to unload the
605 // buffer. This way, :hide and other similar commands won't
606 // wipe the buffer.
607 del_buf = TRUE;
608 unload_buf = TRUE;
609 wipe_buf = TRUE;
610 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200611 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100612 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200613 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
Bram Moolenaarc667da52019-11-30 20:52:27 +0100616 // Disallow deleting the buffer when it is locked (already being closed or
617 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200618 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100619 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200620
Bram Moolenaarc667da52019-11-30 20:52:27 +0100621 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200622 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100624 // Set b_last_cursor when closing the last window for the buffer.
625 // Remember the last cursor position and window options of the buffer.
626 // This used to be only for the current window, but then options like
627 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (buf->b_nwindows == 1)
629 set_last_cursor(win);
630 buflist_setfpos(buf, win,
631 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
632 win->w_cursor.col, TRUE);
633 }
634
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200635 set_bufref(&bufref, buf);
636
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 if (buf->b_nwindows == 1)
639 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200640 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100641 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200642 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
643 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200644 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100645 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100646 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200647aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000648 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100649 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100650 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200651 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100652 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200653 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100654 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200655 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaarc667da52019-11-30 20:52:27 +0100657 // When the buffer becomes hidden, but is not unloaded, trigger
658 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (!unload_buf)
660 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200661 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100662 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200663 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
664 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200665 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100666 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200667 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200668 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100669 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200670 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100671 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200672 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100674#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100675 // autocmds may abort script processing
676 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100677 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200680
Bram Moolenaarc667da52019-11-30 20:52:27 +0100681 // If the buffer was in curwin and the window has changed, go back to that
682 // window, if it still exists. This avoids that ":edit x" triggering a
683 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200684 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
685 {
686 block_autocmds();
687 goto_tabpage_win(the_curtab, the_curwin);
688 unblock_autocmds();
689 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (buf->b_nwindows > 0)
695 --buf->b_nwindows;
696
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100697#ifdef FEAT_DIFF
698 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100699 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100700#endif
701
Bram Moolenaarc667da52019-11-30 20:52:27 +0100702 // Return when a window is displaying the buffer or when it's not
703 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100705 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
Bram Moolenaarc667da52019-11-30 20:52:27 +0100707 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (buf->b_ffname == NULL)
709 del_buf = TRUE;
710
Bram Moolenaarc667da52019-11-30 20:52:27 +0100711 // When closing the current buffer stop Visual mode before freeing
712 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200713 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200714#if defined(EXITFREE)
715 && !entered_free_all_mem
716#endif
717 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200718 end_visual_mode();
719
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100720 // Free all things allocated for this buffer.
721 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
722 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100723 // Remember if we are closing the current buffer. Restore the number of
724 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 is_curbuf = (buf == curbuf);
726 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100728 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100729 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100730 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaarc667da52019-11-30 20:52:27 +0100732 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200733 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100734 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100735#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100736 // autocmds may abort script processing
737 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100738 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100741 // It's possible that autocommands change curbuf to the one being deleted.
742 // This might cause the previous curbuf to be deleted unexpectedly. But
743 // in some cases it's OK to delete the curbuf, because a new one is
744 // obtained anyway. Therefore only return if curbuf changed to the
745 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100747 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200748
Bram Moolenaar4033c552017-09-16 20:54:51 +0200749 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100750 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200751
Bram Moolenaarc667da52019-11-30 20:52:27 +0100752 // Autocommands may have opened or closed windows for this buffer.
753 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200754 if (buf->b_nwindows > 0)
755 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 /*
758 * Remove the buffer from the list.
759 */
760 if (wipe_buf)
761 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200762 tabpage_T *tp;
763 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200764
Bram Moolenaar347538f2022-03-26 16:28:06 +0000765 // Do not wipe out the buffer if it is used in a window.
766 if (buf->b_nwindows > 0)
767 return FALSE;
768
zeertzjq2e7d89b2024-07-10 19:36:36 +0200769 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200770 mark_forget_file(wp, buf->b_fnum);
771
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200772 if (action == DOBUF_WIPE_REUSE)
773 {
774 // we can re-use this buffer number, store it
775 if (buf_reuse.ga_itemsize == 0)
776 ga_init2(&buf_reuse, sizeof(int), 50);
777 if (ga_grow(&buf_reuse, 1) == OK)
778 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
779 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200780 if (buf->b_sfname != buf->b_ffname)
781 VIM_CLEAR(buf->b_sfname);
782 else
783 buf->b_sfname = NULL;
784 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (buf->b_prev == NULL)
786 firstbuf = buf->b_next;
787 else
788 buf->b_prev->b_next = buf->b_next;
789 if (buf->b_next == NULL)
790 lastbuf = buf->b_prev;
791 else
792 buf->b_next->b_prev = buf->b_prev;
793 free_buffer(buf);
794 }
795 else
796 {
797 if (del_buf)
798 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100799 // Free all internal variables and reset option values, to make
800 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 free_buffer_stuff(buf, TRUE);
802
Bram Moolenaarc667da52019-11-30 20:52:27 +0100803 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
805
Bram Moolenaarc667da52019-11-30 20:52:27 +0100806 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 buf->b_p_initialized = FALSE;
808 }
809 buf_clear_file(buf);
810 if (del_buf)
811 buf->b_p_bl = FALSE;
812 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100813 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100814 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817/*
818 * Make buffer not contain a file.
819 */
820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200824 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100826 buf->b_p_eof = FALSE;
827 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 buf->b_p_eol = TRUE;
829 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000831 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100833 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_NETBEANS_INTG
835 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#endif
837}
838
839/*
840 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200841 * the file. Careful: get here with "curwin" NULL when exiting.
842 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100843 * BFA_DEL buffer is going to be deleted
844 * BFA_WIPE buffer is going to be wiped out
845 * BFA_KEEP_UNDO do not free undo information
846 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100849buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200852 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200853 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200854 win_T *the_curwin = curwin;
855 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaarc667da52019-11-30 20:52:27 +0100857 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200858 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100859 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200860 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200861 if (buf->b_ml.ml_mfp != NULL)
862 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200863 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
864 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200865 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100866 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200867 return;
868 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200869 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200871 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
872 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200873 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100874 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 return;
876 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200877 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200879 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
880 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200881 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100882 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 return;
884 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200885 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100886 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200887
Bram Moolenaarc667da52019-11-30 20:52:27 +0100888 // If the buffer was in curwin and the window has changed, go back to that
889 // window, if it still exists. This avoids that ":edit x" triggering a
890 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200891 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
892 {
893 block_autocmds();
894 goto_tabpage_win(the_curtab, the_curwin);
895 unblock_autocmds();
896 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200897
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100898#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100899 // autocmds may abort script processing
900 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100904 // It's possible that autocommands change curbuf to the one being deleted.
905 // This might cause curbuf to be deleted unexpectedly. But in some cases
906 // it's OK to delete the curbuf, because a new one is obtained anyway.
907 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (buf == curbuf && !is_curbuf)
909 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100911 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200913#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100914 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200915 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100916 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200917#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000918
919#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100920 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000921 {
922 win_T *win;
923 tabpage_T *tp;
924
925 FOR_ALL_TAB_WINDOWS(tp, win)
926 if (win->w_buffer == buf)
927 clearFolding(win);
928 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000929#endif
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#ifdef FEAT_TCL
932 tcl_buffer_free(buf);
933#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100934 ml_close(buf, TRUE); // close and delete the memline/memfile
935 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200936 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100937 // free the memory allocated for undo
938 // and reset all undo information
939 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100941 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100943#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100944 clear_buf_prop_types(buf);
945#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100946 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Free a buffer structure and the things it contains related to the buffer
951 * itself (not the file, that must have been done already).
952 */
953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100954free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200956 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200958#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100959 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000960 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200961 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200962 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200963#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200964#ifdef FEAT_LUA
965 lua_buffer_free(buf);
966#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000967#ifdef FEAT_MZSCHEME
968 mzscheme_buffer_free(buf);
969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#ifdef FEAT_PERL
971 perl_buf_free(buf);
972#endif
973#ifdef FEAT_PYTHON
974 python_buffer_free(buf);
975#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976#ifdef FEAT_PYTHON3
977 python3_buffer_free(buf);
978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef FEAT_RUBY
980 ruby_buffer_free(buf);
981#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200982#ifdef FEAT_JOB_CHANNEL
983 channel_buffer_free(buf);
984#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200985#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200986 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200987#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200988#ifdef FEAT_JOB_CHANNEL
989 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200990 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200991 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200992#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200993
994 buf_hashtab_remove(buf);
995
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200996 aubuflocal_remove(buf);
997
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200998 if (autocmd_busy)
999 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001000 // Do not free the buffer structure while autocommands are executing,
1001 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001002 buf->b_next = au_pending_free_buf;
1003 au_pending_free_buf = buf;
1004 }
1005 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001006 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001007 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001008 if (curbuf == buf)
1009 curbuf = NULL; // make clear it's not to be used
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011}
1012
1013/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001014 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001015 */
1016 static void
1017init_changedtick(buf_T *buf)
1018{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001019 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001020
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001021 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1022 di->di_tv.v_type = VAR_NUMBER;
1023 di->di_tv.v_lock = VAR_FIXED;
1024 di->di_tv.vval.v_number = 0;
1025
Bram Moolenaar92769c32017-02-25 15:41:37 +01001026#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001027 STRCPY(buf->b_ct_di.di_key, "changedtick");
1028 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001029#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001030}
1031
1032/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001033 * Free the b_wininfo list for buffer "buf".
1034 */
1035 static void
1036clear_wininfo(buf_T *buf)
1037{
1038 wininfo_T *wip;
1039
1040 while (buf->b_wininfo != NULL)
1041 {
1042 wip = buf->b_wininfo;
1043 buf->b_wininfo = wip->wi_next;
1044 free_wininfo(wip);
1045 }
1046}
1047
1048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1050 */
1051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001052free_buffer_stuff(
1053 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001054 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
1056 if (free_options)
1057 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001058 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001060#ifdef FEAT_SPELL
1061 ga_clear(&buf->b_s.b_langp);
1062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001065 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001066 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001067
Bram Moolenaarc667da52019-11-30 20:52:27 +01001068 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001069 hash_init(&buf->b_vars->dv_hashtab);
1070 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001071 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001072 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001075 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001077 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001079#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001080 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001081#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001082#ifdef FEAT_PROP_POPUP
1083 ga_clear_strings(&buf->b_textprop_text);
1084#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001085 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1086 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001087 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088}
1089
1090/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001091 * Free one wininfo_T.
1092 */
1093 void
1094free_wininfo(wininfo_T *wip)
1095{
1096 if (wip->wi_optset)
1097 {
1098 clear_winopt(&wip->wi_opt);
1099#ifdef FEAT_FOLDING
1100 deleteFoldRecurse(&wip->wi_folds);
1101#endif
1102 }
1103 vim_free(wip);
1104}
1105
1106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 * Go to another buffer. Handles the result of the ATTENTION dialog.
1108 */
1109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001110goto_buffer(
1111 exarg_T *eap,
1112 int start,
1113 int dir,
1114 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001116 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001117 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001118 int skip_help_buf;
1119
1120 switch (eap->cmdidx)
1121 {
1122 case CMD_bnext:
1123 case CMD_sbnext:
1124 case CMD_bNext:
1125 case CMD_bprevious:
1126 case CMD_sbNext:
1127 case CMD_sbprevious:
1128 skip_help_buf = TRUE;
1129 break;
1130 default:
1131 skip_help_buf = FALSE;
1132 break;
1133 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001134
1135 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar188639d2022-04-04 16:57:21 +01001137 if (swap_exists_action == SEA_NONE)
1138 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001139 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1140 (eap->forceit ? DOBUF_FORCEIT : 0) |
1141 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1143 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001144#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001145 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001146
Bram Moolenaarc667da52019-11-30 20:52:27 +01001147 // Reset the error/interrupt/exception state here so that
1148 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001149 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001150#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001151
Bram Moolenaarc667da52019-11-30 20:52:27 +01001152 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001154 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001155 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001156
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001157#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001158 // Restore the error/interrupt/exception state if not discarded by a
1159 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001164 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/*
1168 * Handle the situation of swap_exists_action being set.
1169 * It is allowed for "old_curbuf" to be NULL or invalid.
1170 */
1171 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001172handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001174#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001175 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001176#endif
1177#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001178 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001179#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001180 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (swap_exists_action == SEA_QUIT)
1183 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001184#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001185 // Reset the error/interrupt/exception state here so that
1186 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001187 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001188#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001189
Bram Moolenaarc667da52019-11-30 20:52:27 +01001190 // User selected Quit at ATTENTION prompt. Go back to previous
1191 // buffer. If that buffer is gone or the same as the current one,
1192 // open a new, empty buffer.
1193 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001194 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001195 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001196 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1197 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001198 {
1199 // Block autocommands here because curwin->w_buffer is NULL.
1200 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001201 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001202 unblock_autocmds();
1203 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001204 else
1205 buf = old_curbuf->br_buf;
1206 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001207 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001208 int old_msg_silent = msg_silent;
1209
1210 if (shortmess(SHM_FILEINFO))
1211 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001212 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001213 // restore msg_silent, so that the command line will be shown
1214 msg_silent = old_msg_silent;
1215
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001216#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001217 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001218 check_colorcolumn(NULL, curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001219#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001220 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001221 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001222
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001223#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001224 // Restore the error/interrupt/exception state if not discarded by a
1225 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001226 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229 else if (swap_exists_action == SEA_RECOVER)
1230 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001231#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001232 // Reset the error/interrupt/exception state here so that
1233 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001234 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001235#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001236
Bram Moolenaarc667da52019-11-30 20:52:27 +01001237 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001239 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001240 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001242 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001243
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001244#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001245 // Restore the error/interrupt/exception state if not discarded by a
1246 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001247 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 swap_exists_action = SEA_NONE;
1251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254 * Make the current buffer empty.
1255 * Used when it is wiped out and it's the last buffer.
1256 */
1257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001258empty_curbuf(
1259 int close_others,
1260 int forceit,
1261 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001262{
1263 int retval;
1264 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001265 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001266
1267 if (action == DOBUF_UNLOAD)
1268 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001269 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001270 return FAIL;
1271 }
1272
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001273 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001274 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001275 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001276 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001277
1278 setpcmark();
1279 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1280 forceit ? ECMD_FORCEIT : 0, curwin);
1281
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001282 // do_ecmd() may create a new buffer, then we have to delete
1283 // the old one. But do_ecmd() may have done that already, check
1284 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001285 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001286 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001287 if (!close_others)
1288 need_fileinfo = FALSE;
1289 return retval;
1290}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292/*
1293 * Implementation of the commands for the buffer list.
1294 *
1295 * action == DOBUF_GOTO go to specified buffer
1296 * action == DOBUF_SPLIT split window and go to specified buffer
1297 * action == DOBUF_UNLOAD unload specified buffer(s)
1298 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1299 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001300 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 *
1302 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1303 * start == DOBUF_FIRST go to "count" buffer from first buffer
1304 * start == DOBUF_LAST go to "count" buffer from last buffer
1305 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1306 *
1307 * Return FAIL or OK.
1308 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001309 static int
1310do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001311 int action,
1312 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001313 int dir, // FORWARD or BACKWARD
1314 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001315 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 buf_T *buf;
1318 buf_T *bp;
1319 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001320 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 switch (start)
1323 {
1324 case DOBUF_FIRST: buf = firstbuf; break;
1325 case DOBUF_LAST: buf = lastbuf; break;
1326 default: buf = curbuf; break;
1327 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001328 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 while (count-- > 0)
1331 {
1332 do
1333 {
1334 buf = buf->b_next;
1335 if (buf == NULL)
1336 buf = firstbuf;
1337 }
1338 while (buf != curbuf && !bufIsChanged(buf));
1339 }
1340 if (!bufIsChanged(buf))
1341 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001342 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return FAIL;
1344 }
1345 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001346 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 while (buf != NULL && buf->b_fnum != count)
1349 buf = buf->b_next;
1350 }
1351 else
1352 {
1353 bp = NULL;
1354 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1355 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001356 // remember the buffer where we start, we come back there when all
1357 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (bp == NULL)
1359 bp = buf;
1360 if (dir == FORWARD)
1361 {
1362 buf = buf->b_next;
1363 if (buf == NULL)
1364 buf = firstbuf;
1365 }
1366 else
1367 {
1368 buf = buf->b_prev;
1369 if (buf == NULL)
1370 buf = lastbuf;
1371 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001372 // Don't count unlisted buffers.
1373 // Avoid non-help buffers if the starting point was a non-help buffer and
1374 // vice-versa.
1375 if (unload || (buf->b_p_bl
1376 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 {
1378 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001379 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 if (bp == buf)
1382 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001383 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001384 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 return FAIL;
1386 }
1387 }
1388 }
1389
Bram Moolenaarc667da52019-11-30 20:52:27 +01001390 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
1392 if (start == DOBUF_FIRST)
1393 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001394 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001396 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001399 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001401 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 return FAIL;
1403 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001404#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001405 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001406 return OK;
1407#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001408 if (
1409 action == DOBUF_GOTO
1410 && buf != curbuf
1411 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1412 // disallow navigating to another buffer when 'winfixbuf' is applied
1413 return FAIL;
1414
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001415 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1416 && (buf->b_flags & BF_DUMMY))
1417 {
1418 // disallow navigating to the dummy buffer
1419 semsg(_(e_buffer_nr_does_not_exist), count);
1420 return FAIL;
1421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
1423#ifdef FEAT_GUI
1424 need_mouse_correct = TRUE;
1425#endif
1426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001428 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 */
1430 if (unload)
1431 {
1432 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001433 bufref_T bufref;
1434
Bram Moolenaar94f01952018-09-01 15:30:03 +02001435 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001436 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001437
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001438 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaarc667da52019-11-30 20:52:27 +01001440 // When unloading or deleting a buffer that's already unloaded and
1441 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001442 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1443 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return FAIL;
1445
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001446 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001448#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001449 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001451# ifdef FEAT_TERMINAL
1452 if (term_job_running(buf->b_term))
1453 {
1454 if (term_confirm_stop(buf) == FAIL)
1455 return FAIL;
1456 }
1457 else
1458# endif
1459 {
1460 dialog_changed(buf, FALSE);
1461 if (!bufref_valid(&bufref))
1462 // Autocommand deleted buffer, oops! It's not changed
1463 // now.
1464 return FAIL;
1465 // If it's still changed fail silently, the dialog already
1466 // mentioned why it fails.
1467 if (bufIsChanged(buf))
1468 return FAIL;
1469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001471 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001474 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return FAIL;
1476 }
1477 }
1478
Bram Moolenaarc667da52019-11-30 20:52:27 +01001479 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001480 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001481 end_visual_mode();
1482
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001483 // If deleting the last (listed) buffer, make it empty.
1484 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001485 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if (bp->b_p_bl && bp != buf)
1487 break;
1488 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001489 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001491 // If the deleted buffer is the current one, close the current window
1492 // (unless it's the only window). Repeat this so long as we end up in
1493 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001494 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001495 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001496 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001497 {
1498 if (win_close(curwin, FALSE) == FAIL)
1499 break;
1500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001502 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (buf != curbuf)
1504 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001505 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001506 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001507 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 return OK;
1509 }
1510
1511 /*
1512 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001513 * There should be another, otherwise it would have been handled
1514 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001515 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 * Then prefer the buffer we most recently visited.
1517 * Else try to find one that is loaded, after the current buffer,
1518 * then before the current buffer.
1519 * Finally use any buffer.
1520 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001521 buf = NULL; // selected buffer
1522 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001523 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1524 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001525 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 int jumpidx;
1528
1529 jumpidx = curwin->w_jumplistidx - 1;
1530 if (jumpidx < 0)
1531 jumpidx = curwin->w_jumplistlen - 1;
1532
1533 forward = jumpidx;
1534 while (jumpidx != curwin->w_jumplistidx)
1535 {
1536 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1537 if (buf != NULL)
1538 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001539 // Skip current and unlisted bufs. Also skip a quickfix
1540 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001541 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001542 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 else if (buf->b_ml.ml_mfp == NULL)
1544 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001545 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (bp == NULL)
1547 bp = buf;
1548 buf = NULL;
1549 }
1550 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001551 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001553 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1555 break;
1556 if (--jumpidx < 0)
1557 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001558 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 break;
1560 }
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaarc667da52019-11-30 20:52:27 +01001563 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 forward = TRUE;
1566 buf = curbuf->b_next;
1567 for (;;)
1568 {
1569 if (buf == NULL)
1570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001571 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 break;
1573 buf = curbuf->b_prev;
1574 forward = FALSE;
1575 continue;
1576 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001577 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001578 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001579 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001581 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001583 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 bp = buf;
1585 }
1586 if (forward)
1587 buf = buf->b_next;
1588 else
1589 buf = buf->b_prev;
1590 }
1591 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001592 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001594 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001596 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001597 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 break;
1599 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001600 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 if (curbuf->b_next != NULL)
1603 buf = curbuf->b_next;
1604 else
1605 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001606 if (bt_quickfix(buf))
1607 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610
Bram Moolenaara02471e2014-01-10 16:43:14 +01001611 if (buf == NULL)
1612 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001613 // Autocommands must have wiped out all other buffers. Only option
1614 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001615 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001616 }
1617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001619 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001621 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001623 // If 'switchbuf' is set jump to the window containing "buf".
1624 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001625 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 return FAIL;
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaarc667da52019-11-30 20:52:27 +01001631 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (buf == curbuf)
1633 return OK;
1634
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001635 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001636 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001639 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001641# ifdef FEAT_TERMINAL
1642 if (term_job_running(curbuf->b_term))
1643 {
1644 if (term_confirm_stop(curbuf) == FAIL)
1645 return FAIL;
1646 // Manually kill the terminal here because this command will
1647 // hide it otherwise.
1648 free_terminal(curbuf);
1649 }
1650 else
1651# endif
1652 {
1653 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001654
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001655 set_bufref(&bufref, buf);
1656 dialog_changed(curbuf, FALSE);
1657 if (!bufref_valid(&bufref))
1658 // Autocommand deleted buffer, oops!
1659 return FAIL;
1660
1661 if (bufIsChanged(curbuf))
1662 {
1663 no_write_message();
1664 return FAIL;
1665 }
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669#endif
1670 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001671 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 return FAIL;
1673 }
1674 }
1675
Bram Moolenaarc667da52019-11-30 20:52:27 +01001676 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 set_curbuf(buf, action);
1678
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001680 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001683 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 return FAIL;
1685#endif
1686
1687 return OK;
1688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001690 int
1691do_buffer(
1692 int action,
1693 int start,
1694 int dir, // FORWARD or BACKWARD
1695 int count, // buffer number or number of buffers
1696 int forceit) // TRUE when using !
1697{
1698 return do_buffer_ext(action, start, dir, count,
1699 forceit ? DOBUF_FORCEIT : 0);
1700}
1701
1702/*
1703 * do_bufdel() - delete or unload buffer(s)
1704 *
1705 * addr_count == 0: ":bdel" - delete current buffer
1706 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1707 * buffer "end_bnr", then any other arguments.
1708 * addr_count == 2: ":N,N bdel" - delete buffers in range
1709 *
1710 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1711 * DOBUF_DEL (":bdel")
1712 *
1713 * Returns error message or NULL
1714 */
1715 char *
1716do_bufdel(
1717 int command,
1718 char_u *arg, // pointer to extra arguments
1719 int addr_count,
1720 int start_bnr, // first buffer number in a range
1721 int end_bnr, // buffer nr or last buffer nr in a range
1722 int forceit)
1723{
1724 int do_current = 0; // delete current buffer?
1725 int deleted = 0; // number of buffers deleted
1726 char *errormsg = NULL; // return value
1727 int bnr; // buffer number
1728 char_u *p;
1729
1730 if (addr_count == 0)
1731 {
1732 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1733 }
1734 else
1735 {
1736 if (addr_count == 2)
1737 {
1738 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001739 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001740 bnr = start_bnr;
1741 }
1742 else // addr_count == 1
1743 bnr = end_bnr;
1744
1745 for ( ;!got_int; ui_breakcheck())
1746 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001747 // Delete the current buffer last, otherwise when the
1748 // current buffer is deleted, the next buffer becomes
1749 // the current one and will be loaded, which may then
1750 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001751 if (bnr == curbuf->b_fnum)
1752 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001753 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001754 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1755 ++deleted;
1756
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001757 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001758 if (addr_count == 2)
1759 {
1760 if (++bnr > end_bnr)
1761 break;
1762 }
1763 else // addr_count == 1
1764 {
1765 arg = skipwhite(arg);
1766 if (*arg == NUL)
1767 break;
1768 if (!VIM_ISDIGIT(*arg))
1769 {
1770 p = skiptowhite_esc(arg);
1771 bnr = buflist_findpat(arg, p,
1772 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1773 FALSE, FALSE);
1774 if (bnr < 0) // failed
1775 break;
1776 arg = p;
1777 }
1778 else
1779 bnr = getdigits(&arg);
1780 }
1781 }
1782 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1783 FORWARD, do_current, forceit) == OK)
1784 ++deleted;
1785
1786 if (deleted == 0)
1787 {
1788 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001789 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001790 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001791 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001792 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001793 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001794 errormsg = (char *)IObuff;
1795 }
1796 else if (deleted >= p_report)
1797 {
1798 if (command == DOBUF_UNLOAD)
1799 smsg(NGETTEXT("%d buffer unloaded",
1800 "%d buffers unloaded", deleted), deleted);
1801 else if (command == DOBUF_DEL)
1802 smsg(NGETTEXT("%d buffer deleted",
1803 "%d buffers deleted", deleted), deleted);
1804 else
1805 smsg(NGETTEXT("%d buffer wiped out",
1806 "%d buffers wiped out", deleted), deleted);
1807 }
1808 }
1809
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001810 return errormsg;
1811}
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813/*
1814 * Set current buffer to "buf". Executes autocommands and closes current
1815 * buffer. "action" tells how to close the current buffer:
1816 * DOBUF_GOTO free or hide it
1817 * DOBUF_SPLIT nothing
1818 * DOBUF_UNLOAD unload it
1819 * DOBUF_DEL delete it
1820 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001821 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 buf_T *prevbuf;
1827 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001828 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001829#ifdef FEAT_SYN_HL
1830 long old_tw = curbuf->b_p_tw;
1831#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001832 bufref_T newbufref;
1833 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001834 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001835 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001838 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001839 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1840 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
Bram Moolenaarc667da52019-11-30 20:52:27 +01001842 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaarc667da52019-11-30 20:52:27 +01001845 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001847 set_bufref(&prevbufref, prevbuf);
1848 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001850 // Autocommands may delete the current buffer and/or the buffer we want to
1851 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001852 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001853 || (bufref_valid(&prevbufref)
1854 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001855#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001856 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001858 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001860#ifdef FEAT_SYN_HL
1861 if (prevbuf == curwin->w_buffer)
1862 reset_synblock(curwin);
1863#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001864 // autocommands may have opened a new window
1865 // with prevbuf, grr
1866 if (unload ||
1867 (last_winid != get_last_winid() &&
1868 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001869 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001870#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001871 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001873 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001875 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001876 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001877
Bram Moolenaare615db02022-01-20 21:00:54 +00001878 // Do not sync when in Insert mode and the buffer is open in
1879 // another window, might be a timer doing something in another
1880 // window.
1881 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001882 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001883 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1885 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001886 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001887 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1888 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001889 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001890 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001891 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001894 // An autocommand may have deleted "buf", already entered it (e.g., when
1895 // it did ":bunload") or aborted the script processing.
1896 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001897 valid = buf_valid(buf);
1898 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001899#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001900 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001902 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001903 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001904 // autocommands changed curbuf and we will move to another
1905 // buffer soon, so decrement curbuf->b_nwindows
1906 if (curbuf != NULL && prevbuf != curbuf)
1907 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001908 // If the buffer is not valid but curwin->w_buffer is NULL we must
1909 // enter some buffer. Using the last one is hopefully OK.
1910 if (!valid)
1911 enter_buffer(lastbuf);
1912 else
1913 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001914#ifdef FEAT_SYN_HL
1915 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001916 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001917#endif
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919}
1920
1921/*
1922 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001923 * Old curbuf must have been abandoned already! This also means "curbuf" may
1924 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001929 // when closing the current buffer stop Visual mode
1930 if (VIsual_active
1931#if defined(EXITFREE)
1932 && !entered_free_all_mem
1933#endif
1934 )
1935 end_visual_mode();
1936
Bram Moolenaar010ee962019-09-25 20:37:36 +02001937 // Get the buffer in the current window.
1938 curwin->w_buffer = buf;
1939 curbuf = buf;
1940 ++curbuf->b_nwindows;
1941
1942 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1944 if (!buf->b_help)
1945 get_winopts(buf);
1946#ifdef FEAT_FOLDING
1947 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001948 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001950 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#endif
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001954 if (curwin->w_p_diff)
1955 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956#endif
1957
Bram Moolenaara971b822011-09-14 14:43:25 +02001958#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001959 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001960#endif
1961
Bram Moolenaarc667da52019-11-30 20:52:27 +01001962 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 curwin->w_cursor.lnum = 1;
1964 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001967 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
Bram Moolenaarc667da52019-11-30 20:52:27 +01001969 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001970 curwin->w_valid = 0;
1971
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001972 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1973 curbuf->b_last_cursor.col, TRUE);
1974
Bram Moolenaarc667da52019-11-30 20:52:27 +01001975 // Make sure the buffer is loaded.
1976 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001977 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001978 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001979 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001980 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001981 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02001982 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001983
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001984 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 else
1987 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001988 if (!msg_silent && !shortmess(SHM_FILEINFO))
1989 need_fileinfo = TRUE; // display file info after redraw
1990
1991 // check if file changed
1992 (void)buf_check_timestamp(curbuf, FALSE);
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001995#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1999 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001
Bram Moolenaarc667da52019-11-30 20:52:27 +01002002 // If autocommands did not change the cursor position, restore cursor lnum
2003 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (curwin->w_cursor.lnum == 1 && inindent(0))
2005 buflist_getfpos();
2006
Bram Moolenaarc667da52019-11-30 20:52:27 +01002007 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002009 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002010 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002011 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
Bram Moolenaar009b2592004-10-24 19:18:58 +00002013#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002014 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002015 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002016#endif
2017
Bram Moolenaarc667da52019-11-30 20:52:27 +01002018 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002019 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021#ifdef FEAT_KEYMAP
2022 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002023 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002025#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002026 // May need to set the spell language. Can only do this after the buffer
2027 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002028 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002029 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002030#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002031#ifdef FEAT_VIMINFO
2032 curbuf->b_last_used = vim_time();
2033#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002034
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002035 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036}
2037
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002038#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2039/*
2040 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002041 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002042 */
2043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002044do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002045{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002046 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002047 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002048 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002049 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002050 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002051 last_chdir_reason = "autochdir";
2052 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002053}
2054#endif
2055
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002056 static void
2057no_write_message_buf(buf_T *buf UNUSED)
2058{
2059#ifdef FEAT_TERMINAL
2060 if (term_job_running(buf->b_term))
2061 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2062 else
2063#endif
2064 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2065 buf->b_fnum);
2066}
2067
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002068 void
2069no_write_message(void)
2070{
2071#ifdef FEAT_TERMINAL
2072 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002073 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002074 else
2075#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002076 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002077}
2078
2079 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002080no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002081{
2082#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002083 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002084 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002085 else
2086#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002087 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002088}
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090/*
2091 * functions for dealing with the buffer list
2092 */
2093
2094/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002095 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2096 * only one window. That means it can be re-used.
2097 */
2098 int
2099curbuf_reusable(void)
2100{
2101 return (curbuf != NULL
2102 && curbuf->b_ffname == NULL
2103 && curbuf->b_nwindows <= 1
2104 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002105 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002106 && !curbufIsChanged());
2107}
2108
2109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 * Add a file name to the buffer list. Return a pointer to the buffer.
2111 * If the same file name already exists return a pointer to that buffer.
2112 * If it does not exist, or if fname == NULL, a new entry is created.
2113 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2114 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2115 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002116 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002117 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2118 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002119 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 * This is the ONLY way to create a new buffer.
2121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002123buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002124 char_u *ffname_arg, // full path of fname or relative
2125 char_u *sfname_arg, // short fname or NULL
2126 linenr_T lnum, // preferred cursor line
2127 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002129 char_u *ffname = ffname_arg;
2130 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 buf_T *buf;
2132#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002133 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134#endif
2135
Bram Moolenaar480778b2016-07-14 22:09:39 +02002136 if (top_file_num == 1)
2137 hash_init(&buf_hashtab);
2138
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002139 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002142 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 */
2144#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002145 // On Unix we can use inode numbers when the file exists. Works better
2146 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2148 st.st_dev = (dev_T)-1;
2149#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002150 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151#ifdef UNIX
2152 buflist_findname_stat(ffname, &st)
2153#else
2154 buflist_findname(ffname)
2155#endif
2156 ) != NULL)
2157 {
2158 vim_free(ffname);
2159 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002160 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2161 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002162
2163 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002164 // copy the options now, if 'cpo' doesn't have 's' and not done
2165 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002166 buf_copy_options(buf, 0);
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2169 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002170 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002171
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002173 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002175 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002176 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002177 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002178 return NULL;
2179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181 return buf;
2182 }
2183
2184 /*
2185 * If the current buffer has no name and no contents, use the current
2186 * buffer. Otherwise: Need to allocate a new buffer structure.
2187 *
2188 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002189 * (A spell file buffer is allocated in spell.c, but that's not a normal
2190 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 */
2192 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002193 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002196 // It's like this buffer is deleted. Watch out for autocommands that
2197 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002198 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2199 if (buf != curbuf) // autocommands deleted the buffer!
2200 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002201#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002202 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002203 {
2204 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 if (buf != curbuf || curbuf == NULL)
2210 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002211 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (buf == NULL)
2213 {
2214 vim_free(ffname);
2215 return NULL;
2216 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002217#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002218 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002219 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002220 if (buf->b_vars == NULL)
2221 {
2222 vim_free(ffname);
2223 vim_free(buf);
2224 return NULL;
2225 }
2226 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2227#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002228 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230
2231 if (ffname != NULL)
2232 {
2233 buf->b_ffname = ffname;
2234 buf->b_sfname = vim_strsave(sfname);
2235 }
2236
2237 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002238 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2241 || buf->b_wininfo == NULL)
2242 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002243 if (buf->b_sfname != buf->b_ffname)
2244 VIM_CLEAR(buf->b_sfname);
2245 else
2246 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002247 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (buf != curbuf)
2249 free_buffer(buf);
2250 return NULL;
2251 }
2252
2253 if (buf == curbuf)
2254 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002255 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002256
Bram Moolenaarc667da52019-11-30 20:52:27 +01002257 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002258 buf->b_p_initialized = FALSE;
2259 buf_copy_options(buf, BCO_ENTER);
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002262 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 curbuf->b_kmap_state |= KEYMAP_INIT;
2264#endif
2265 }
2266 else
2267 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002268 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002270 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 buf->b_prev = NULL;
2273 firstbuf = buf;
2274 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002275 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 lastbuf->b_next = buf;
2278 buf->b_prev = lastbuf;
2279 }
2280 lastbuf = buf;
2281
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002282 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2283 {
2284 // Recycle a previously used buffer number. Used for buffers which
2285 // are normally hidden, e.g. in a popup window. Avoids that the
2286 // buffer number grows rapidly.
2287 --buf_reuse.ga_len;
2288 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002289
2290 // Move buffer to the right place in the buffer list.
2291 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2292 {
2293 buf_T *prev = buf->b_prev;
2294
2295 prev->b_next = buf->b_next;
2296 if (prev->b_next != NULL)
2297 prev->b_next->b_prev = prev;
2298 buf->b_next = prev;
2299 buf->b_prev = prev->b_prev;
2300 if (buf->b_prev != NULL)
2301 buf->b_prev->b_next = buf;
2302 prev->b_prev = buf;
2303 if (lastbuf == buf)
2304 lastbuf = prev;
2305 if (firstbuf == prev)
2306 firstbuf = buf;
2307 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002308 }
2309 else
2310 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002311 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002313 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002314 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002317 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 top_file_num = 1;
2320 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002321 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002323 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 buf_copy_options(buf, BCO_ALWAYS);
2325 }
2326
2327 buf->b_wininfo->wi_fpos.lnum = lnum;
2328 buf->b_wininfo->wi_win = curwin;
2329
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002330#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002331 hash_init(&buf->b_s.b_keywtab);
2332 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#endif
2334
2335 buf->b_fname = buf->b_sfname;
2336#ifdef UNIX
2337 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002338 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else
2340 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002341 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 buf->b_dev = st.st_dev;
2343 buf->b_ino = st.st_ino;
2344 }
2345#endif
2346 buf->b_u_synced = TRUE;
2347 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002348 if (flags & BLN_DUMMY)
2349 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002351 clrallmarks(buf); // clear marks
2352 fmarks_check_names(buf); // check file marks for this file
2353 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (!(flags & BLN_DUMMY))
2355 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002356 bufref_T bufref;
2357
Bram Moolenaarc667da52019-11-30 20:52:27 +01002358 // Tricky: these autocommands may change the buffer list. They could
2359 // also split the window with re-using the one empty buffer. This may
2360 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002361 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002362 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002363 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002364 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002366 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002367 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002368 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002369 return NULL;
2370 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002371#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002372 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377 return buf;
2378}
2379
2380/*
2381 * Free the memory for the options of a buffer.
2382 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2383 * 'fileencoding'.
2384 */
2385 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002386free_buf_options(
2387 buf_T *buf,
2388 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390 if (free_p_ff)
2391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 clear_string_option(&buf->b_p_bh);
2395 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397#ifdef FEAT_FIND_ID
2398 clear_string_option(&buf->b_p_def);
2399 clear_string_option(&buf->b_p_inc);
2400# ifdef FEAT_EVAL
2401 clear_string_option(&buf->b_p_inex);
2402# endif
2403#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002404#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 clear_string_option(&buf->b_p_inde);
2406 clear_string_option(&buf->b_p_indk);
2407#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002408#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2409 clear_string_option(&buf->b_p_bexpr);
2410#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002411#if defined(FEAT_CRYPT)
2412 clear_string_option(&buf->b_p_cm);
2413#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002414 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002415#if defined(FEAT_EVAL)
2416 clear_string_option(&buf->b_p_fex);
2417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002419# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002420 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2421 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002422 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 clear_string_option(&buf->b_p_key);
2425#endif
2426 clear_string_option(&buf->b_p_kp);
2427 clear_string_option(&buf->b_p_mps);
2428 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002429 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002431#ifdef FEAT_VARTABS
2432 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002433 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002434 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002435 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002436 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438#ifdef FEAT_KEYMAP
2439 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002440 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 ga_clear(&buf->b_kmap_ga);
2442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444#ifdef FEAT_FOLDING
2445 clear_string_option(&buf->b_p_cms);
2446#endif
2447 clear_string_option(&buf->b_p_nf);
2448#ifdef FEAT_SYN_HL
2449 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002450 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002451#endif
2452#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002453 clear_string_option(&buf->b_s.b_p_spc);
2454 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002455 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002456 buf->b_s.b_cap_prog = NULL;
2457 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002458 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 clear_string_option(&buf->b_p_cink);
2463 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002464 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002465 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002467 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002469#ifdef FEAT_COMPL_FUNC
2470 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002471 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002472 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002473 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002474 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002475 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477#ifdef FEAT_QUICKFIX
2478 clear_string_option(&buf->b_p_gp);
2479 clear_string_option(&buf->b_p_mp);
2480 clear_string_option(&buf->b_p_efm);
2481#endif
2482 clear_string_option(&buf->b_p_ep);
2483 clear_string_option(&buf->b_p_path);
2484 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002485 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002486#ifdef FEAT_EVAL
2487 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002488 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002489 clear_string_option(&buf->b_p_ffu);
2490 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 clear_string_option(&buf->b_p_dict);
2493 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002494 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002496 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002497 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002498 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002499 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
2502/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002503 * Get alternate file "n".
2504 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2505 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 * if (options & GETF_SETMARK) call setpcmark()
2507 * if (options & GETF_ALT) we are jumping to an alternate file.
2508 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2509 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002510 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 */
2512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002513buflist_getfile(
2514 int n,
2515 linenr_T lnum,
2516 int options,
2517 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 pos_T *fpos;
2522 colnr_T col;
2523
2524 buf = buflist_findnr(n);
2525 if (buf == NULL)
2526 {
2527 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002528 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002530 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 return FAIL;
2532 }
2533
Bram Moolenaarc667da52019-11-30 20:52:27 +01002534 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (buf == curbuf)
2536 return OK;
2537
Bram Moolenaar71223e22022-05-30 15:23:09 +01002538 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002539 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaarc667da52019-11-30 20:52:27 +01002541 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (lnum == 0)
2543 {
2544 fpos = buflist_findfpos(buf);
2545 lnum = fpos->lnum;
2546 col = fpos->col;
2547 }
2548 else
2549 col = 0;
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (options & GETF_SWITCH)
2552 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002553 // If 'switchbuf' is set jump to the window containing "buf".
2554 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002555
Bram Moolenaarc667da52019-11-30 20:52:27 +01002556 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2557 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002558 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002559 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002561 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002562 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002563 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2564 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002566 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002571 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002572 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2573 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002575 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (!p_sol && col != 0)
2577 {
2578 curwin->w_cursor.col = col;
2579 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 curwin->w_set_curswant = TRUE;
2582 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002583 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002585
2586 if (RedrawingDisabled > 0)
2587 --RedrawingDisabled;
2588 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
2590
2591/*
2592 * go to the last know line number for the current buffer
2593 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002595buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597 pos_T *fpos;
2598
2599 fpos = buflist_findfpos(curbuf);
2600
2601 curwin->w_cursor.lnum = fpos->lnum;
2602 check_cursor_lnum();
2603
2604 if (p_sol)
2605 curwin->w_cursor.col = 0;
2606 else
2607 {
2608 curwin->w_cursor.col = fpos->col;
2609 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 curwin->w_set_curswant = TRUE;
2612 }
2613}
2614
Bram Moolenaar81695252004-12-29 20:58:21 +00002615/*
2616 * Find file in buffer list by name (it has to be for the current window).
2617 * Returns NULL if not found.
2618 */
2619 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002620buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002621{
2622 char_u *ffname;
2623 buf_T *buf = NULL;
2624
Bram Moolenaarc667da52019-11-30 20:52:27 +01002625 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002626 ffname = FullName_save(fname,
2627#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002628 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002629#else
2630 FALSE
2631#endif
2632 );
2633 if (ffname != NULL)
2634 {
2635 buf = buflist_findname(ffname);
2636 vim_free(ffname);
2637 }
2638 return buf;
2639}
Bram Moolenaar81695252004-12-29 20:58:21 +00002640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641/*
2642 * Find file in buffer list by name (it has to be for the current window).
2643 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002644 * Skips dummy buffers.
2645 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 */
2647 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002648buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002651 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 if (mch_stat((char *)ffname, &st) < 0)
2654 st.st_dev = (dev_T)-1;
2655 return buflist_findname_stat(ffname, &st);
2656}
2657
2658/*
2659 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2660 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002661 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 */
2663 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664buflist_findname_stat(
2665 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002666 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668#endif
2669 buf_T *buf;
2670
Bram Moolenaarc667da52019-11-30 20:52:27 +01002671 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002672 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002673 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#ifdef UNIX
2675 , stp
2676#endif
2677 ))
2678 return buf;
2679 return NULL;
2680}
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682/*
2683 * Find file in buffer list by a regexp pattern.
2684 * Return fnum of the found buffer.
2685 * Return < 0 for error.
2686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002688buflist_findpat(
2689 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002690 char_u *pattern_end, // pointer to first char after pattern
2691 int unlisted, // find unlisted buffers
2692 int diffmode UNUSED, // find diff-mode buffers only
2693 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 int match = -1;
2697 int find_listed;
2698 char_u *pat;
2699 char_u *patend;
2700 int attempt;
2701 char_u *p;
2702 int toggledollar;
2703
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002704 // "%" is current file, "%%" or "#" is alternate file
2705 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2706 || (in_vim9script() && pattern_end == pattern + 2
2707 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002709 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002711 else
2712 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713#ifdef FEAT_DIFF
2714 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2715 match = -1;
2716#endif
2717 }
2718
2719 /*
2720 * Try four ways of matching a listed buffer:
2721 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002722 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 * attempt == 2: with '$' at end (only match at end)
2724 * attempt == 3: with '^' at start and '$' at end (only full match)
2725 * Repeat this for finding an unlisted buffer if there was no matching
2726 * listed buffer.
2727 */
2728 else
2729 {
2730 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2731 if (pat == NULL)
2732 return -1;
2733 patend = pat + STRLEN(pat) - 1;
2734 toggledollar = (patend > pat && *patend == '$');
2735
Bram Moolenaarc667da52019-11-30 20:52:27 +01002736 // First try finding a listed buffer. If not found and "unlisted"
2737 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 find_listed = TRUE;
2739 for (;;)
2740 {
2741 for (attempt = 0; attempt <= 3; ++attempt)
2742 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002743 regmatch_T regmatch;
2744
Bram Moolenaarc667da52019-11-30 20:52:27 +01002745 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002747 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002749 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002751 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002753 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002754 {
2755 if (regmatch.regprog == NULL)
2756 {
2757 // invalid pattern, possibly after switching engine
2758 vim_free(pat);
2759 return -1;
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 if (buf->b_p_bl == find_listed
2762#ifdef FEAT_DIFF
2763 && (!diffmode || diff_mode_buf(buf))
2764#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002765 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002767 if (curtab_only)
2768 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002769 // Ignore the match if the buffer is not open in
2770 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002771 win_T *wp;
2772
Bram Moolenaar29323592016-07-24 22:04:11 +02002773 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002774 if (wp->w_buffer == buf)
2775 break;
2776 if (wp == NULL)
2777 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002778 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002779 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
2781 match = -2;
2782 break;
2783 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002784 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002788 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002789 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 break;
2791 }
2792
Bram Moolenaarc667da52019-11-30 20:52:27 +01002793 // Only search for unlisted buffers if there was no match with
2794 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (!unlisted || !find_listed || match != -1)
2796 break;
2797 find_listed = FALSE;
2798 }
2799
2800 vim_free(pat);
2801 }
2802
2803 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002804 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002806 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 return match;
2808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
Bram Moolenaar52410572019-10-27 05:12:45 +01002810#ifdef FEAT_VIMINFO
2811typedef struct {
2812 buf_T *buf;
2813 char_u *match;
2814} bufmatch_T;
2815#endif
2816
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
2818 * Find all buffer names that match.
2819 * For command line expansion of ":buf" and ":sbuf".
2820 * Return OK if matches found, FAIL otherwise.
2821 */
2822 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002823ExpandBufnames(
2824 char_u *pat,
2825 int *num_file,
2826 char_u ***file,
2827 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002829 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 buf_T *buf;
2831 int round;
2832 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002833 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002834#ifdef FEAT_VIMINFO
2835 bufmatch_T *matches = NULL;
2836#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002837 int fuzzy;
2838 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002839 regmatch_T regmatch;
2840 int score = 0;
2841 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
Bram Moolenaarc667da52019-11-30 20:52:27 +01002843 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *file = NULL;
2845
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002846#ifdef FEAT_DIFF
2847 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2848 return FAIL;
2849#endif
2850
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002851 fuzzy = cmdline_fuzzy_complete(pat);
2852
2853 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2854 // expression matching)
2855 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002857 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002858 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002859 int len = (int)STRLEN(pat);
2860 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002861 if (patc == NULL)
2862 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002863 STRNCPY(patc, pat + 1, len - 1);
2864 patc[len - 1] = NUL;
2865 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002866 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002867 else if (*pat == '^')
2868 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002869 else
2870 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002871 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002872 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002873
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002874 // round == 1: Count the matches.
2875 // round == 2: Build the array to keep the matches.
2876 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002877 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002878 count = 0;
2879 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002880 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002881 if (!buf->b_p_bl) // skip unlisted buffers
2882 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002883#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002884 if (options & BUF_DIFF_FILTER)
2885 // Skip buffers not suitable for
2886 // :diffget or :diffput completion.
2887 if (buf == curbuf || !diff_mode_buf(buf))
2888 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002889#endif
2890
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002891 if (!fuzzy)
2892 {
2893 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002894 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002895 // invalid pattern, possibly after recompiling
2896 if (to_free)
2897 vim_free(patc);
2898 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002899 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002900 p = buflist_match(&regmatch, buf, p_wic);
2901 }
2902 else
2903 {
2904 p = NULL;
2905 // first try matching with the short file name
2906 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2907 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002908 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002909 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002910 // next try matching with the full path file name
2911 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2912 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002915
2916 if (p == NULL)
2917 continue;
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (round == 1)
2920 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002921 ++count;
2922 continue;
2923 }
2924
2925 if (options & WILD_HOME_REPLACE)
2926 p = home_replace_save(buf, p);
2927 else
2928 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002929 if (p == NULL)
2930 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002931
2932 if (!fuzzy)
2933 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002934#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002935 if (matches != NULL)
2936 {
2937 matches[count].buf = buf;
2938 matches[count].match = p;
2939 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002940 }
2941 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002942#endif
2943 (*file)[count++] = p;
2944 }
2945 else
2946 {
2947 fuzmatch[count].idx = count;
2948 fuzmatch[count].str = p;
2949 fuzmatch[count].score = score;
2950 count++;
2951 }
2952 }
2953 if (count == 0) // no match found, break here
2954 break;
2955 if (round == 1)
2956 {
2957 if (!fuzzy)
2958 {
2959 *file = ALLOC_MULT(char_u *, count);
2960 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002961 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002962 vim_regfree(regmatch.regprog);
2963 if (to_free)
2964 vim_free(patc);
2965 return FAIL;
2966 }
2967#ifdef FEAT_VIMINFO
2968 if (options & WILD_BUFLASTUSED)
2969 matches = ALLOC_MULT(bufmatch_T, count);
2970#endif
2971 }
2972 else
2973 {
2974 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2975 if (fuzmatch == NULL)
2976 {
2977 *num_file = 0;
2978 *file = NULL;
2979 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002985 if (!fuzzy)
2986 {
2987 vim_regfree(regmatch.regprog);
2988 if (to_free)
2989 vim_free(patc);
2990 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002991
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002992 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002993 {
826814741_6dff3c9c2024-12-10 17:15:14 +01002994#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002995 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002996 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002997 int i;
2998 if (count > 1)
2999 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3000 // if the current buffer is first in the list, place it at the end
3001 if (matches[0].buf == curbuf)
3002 {
3003 for (i = 1; i < count; i++)
3004 (*file)[i-1] = matches[i].match;
3005 (*file)[count-1] = matches[0].match;
3006 }
3007 else
3008 {
3009 for (i = 0; i < count; i++)
3010 (*file)[i] = matches[i].match;
3011 }
3012 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003013 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003014#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003015 }
3016 else
3017 {
3018 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3019 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003020 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 *num_file = count;
3023 return (count == 0 ? FAIL : OK);
3024}
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026/*
3027 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003028 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 */
3030 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003031buflist_match(
3032 regmatch_T *rmp,
3033 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003034 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 char_u *match;
3037
Bram Moolenaarc667da52019-11-30 20:52:27 +01003038 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003039 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003040 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003041 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 return match;
3044}
3045
3046/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003047 * Try matching the regexp in "rmp->regprog" with file name "name".
3048 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 * Return "name" when there is a match, NULL when not.
3050 */
3051 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003052fname_match(
3053 regmatch_T *rmp,
3054 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003055 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
3057 char_u *match = NULL;
3058 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003060 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003061 if (name == NULL || rmp->regprog == NULL)
3062 return NULL;
3063
3064 // Ignore case when 'fileignorecase' or the argument is set.
3065 rmp->rm_ic = p_fic || ignore_case;
3066 if (vim_regexec(rmp, name, (colnr_T)0))
3067 match = name;
3068 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003070 // Replace $(HOME) with '~' and try matching again.
3071 p = home_replace_save(NULL, name);
3072 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003074 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076
3077 return match;
3078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003081 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 */
3083 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003084buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003086 char_u key[VIM_SIZEOF_INT * 2 + 1];
3087 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 if (nr == 0)
3090 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003091 sprintf((char *)key, "%x", nr);
3092 hi = hash_find(&buf_hashtab, key);
3093
3094 if (!HASHITEM_EMPTY(hi))
3095 return (buf_T *)(hi->hi_key
3096 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 return NULL;
3098}
3099
3100/*
3101 * Get name of file 'n' in the buffer list.
3102 * When the file has no name an empty string is returned.
3103 * home_replace() is used to shorten the file name (used for marks).
3104 * Returns a pointer to allocated memory, of NULL when failed.
3105 */
3106 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107buflist_nr2name(
3108 int n,
3109 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003110 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 buf_T *buf;
3113
3114 buf = buflist_findnr(n);
3115 if (buf == NULL)
3116 return NULL;
3117 return home_replace_save(helptail ? buf : NULL,
3118 fullname ? buf->b_ffname : buf->b_fname);
3119}
3120
3121/*
3122 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3123 * When "copy_options" is TRUE save the local window option values.
3124 * When "lnum" is 0 only do the options.
3125 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003127buflist_setfpos(
3128 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003129 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003130 linenr_T lnum,
3131 colnr_T col,
3132 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 wininfo_T *wip;
3135
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003136 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (wip->wi_win == win)
3138 break;
3139 if (wip == NULL)
3140 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003141 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003142 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (wip == NULL)
3144 return;
3145 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003146 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 lnum = 1;
3148 }
3149 else
3150 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003151 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (wip->wi_prev)
3153 wip->wi_prev->wi_next = wip->wi_next;
3154 else
3155 buf->b_wininfo = wip->wi_next;
3156 if (wip->wi_next)
3157 wip->wi_next->wi_prev = wip->wi_prev;
3158 if (copy_options && wip->wi_optset)
3159 {
3160 clear_winopt(&wip->wi_opt);
3161#ifdef FEAT_FOLDING
3162 deleteFoldRecurse(&wip->wi_folds);
3163#endif
3164 }
3165 }
3166 if (lnum != 0)
3167 {
3168 wip->wi_fpos.lnum = lnum;
3169 wip->wi_fpos.col = col;
3170 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003171 if (win != NULL)
3172 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003173 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003175 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3177#ifdef FEAT_FOLDING
3178 wip->wi_fold_manual = win->w_fold_manual;
3179 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3180#endif
3181 wip->wi_optset = TRUE;
3182 }
3183
Bram Moolenaarc667da52019-11-30 20:52:27 +01003184 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 wip->wi_next = buf->b_wininfo;
3186 buf->b_wininfo = wip;
3187 wip->wi_prev = NULL;
3188 if (wip->wi_next)
3189 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190}
3191
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003192#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003193/*
3194 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3195 * page. That's because a diff is local to a tab page.
3196 */
3197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003199{
3200 win_T *wp;
3201
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003202 if (!wip->wi_opt.wo_diff)
3203 return FALSE;
3204
3205 FOR_ALL_WINDOWS(wp)
3206 // return FALSE when it's a window in the current tab page, thus
3207 // the buffer was in diff mode here
3208 if (wip->wi_win == wp)
3209 return FALSE;
3210 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003211}
3212#endif
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214/*
3215 * Find info for the current window in buffer "buf".
3216 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003217 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003218 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3219 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 * Returns NULL when there isn't any info.
3221 */
3222 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223find_wininfo(
3224 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003225 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 wininfo_T *wip;
3229
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003230 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003231 if (wip->wi_win == curwin
3232#ifdef FEAT_DIFF
3233 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3234#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003235
3236 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003238
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003239 if (wip != NULL)
3240 return wip;
3241
Bram Moolenaarc667da52019-11-30 20:52:27 +01003242 // If no wininfo for curwin, use the first in the list (that doesn't have
3243 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003244 // If "need_options" is TRUE skip entries that don't have options set,
3245 // unless the window is editing "buf", so we can copy from the window
3246 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003247#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003248 if (skip_diff_buffer)
3249 {
3250 FOR_ALL_BUF_WININFO(buf, wip)
3251 if (!wininfo_other_tab_diff(wip)
3252 && (!need_options || wip->wi_optset
3253 || (wip->wi_win != NULL
3254 && wip->wi_win->w_buffer == buf)))
3255 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003256 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003257 else
3258#endif
3259 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 return wip;
3261}
3262
3263/*
3264 * Reset the local window options to the values last used in this window.
3265 * If the buffer wasn't used in this window before, use the values from
3266 * the most recently used window. If the values were never set, use the
3267 * global values for the window.
3268 */
3269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 wininfo_T *wip;
3273
3274 clear_winopt(&curwin->w_onebuf_opt);
3275#ifdef FEAT_FOLDING
3276 clearFolding(curwin);
3277#endif
3278
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003279 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003280 if (wip != NULL && wip->wi_win != NULL
3281 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003283 // The buffer is currently displayed in the window: use the actual
3284 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003285 win_T *wp = wip->wi_win;
3286
3287 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3288#ifdef FEAT_FOLDING
3289 curwin->w_fold_manual = wp->w_fold_manual;
3290 curwin->w_foldinvalid = TRUE;
3291 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3292#endif
3293 }
3294 else if (wip != NULL && wip->wi_optset)
3295 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003296 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3298#ifdef FEAT_FOLDING
3299 curwin->w_fold_manual = wip->wi_fold_manual;
3300 curwin->w_foldinvalid = TRUE;
3301 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3302#endif
3303 }
3304 else
3305 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003306 if (wip != NULL)
3307 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308
3309#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003310 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (p_fdls >= 0)
3312 curwin->w_p_fdl = p_fdls;
3313#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003314 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315}
3316
3317/*
3318 * Find the position (lnum and col) for the buffer 'buf' for the current
3319 * window.
3320 * Returns a pointer to no_position if no position is found.
3321 */
3322 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003323buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003326 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003328 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (wip != NULL)
3330 return &(wip->wi_fpos);
3331 else
3332 return &no_position;
3333}
3334
3335/*
3336 * Find the lnum for the buffer 'buf' for the current window.
3337 */
3338 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 return buflist_findfpos(buf)->lnum;
3342}
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003345 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003348buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
Bram Moolenaar52410572019-10-27 05:12:45 +01003350 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 int len;
3352 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003353 int ro_char;
3354 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003355#ifdef FEAT_TERMINAL
3356 int job_running;
3357 int job_none_open;
3358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar52410572019-10-27 05:12:45 +01003360#ifdef FEAT_VIMINFO
3361 garray_T buflist;
3362 buf_T **buflist_data = NULL, **p;
3363
3364 if (vim_strchr(eap->arg, 't'))
3365 {
3366 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003367 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003368 {
3369 if (ga_grow(&buflist, 1) == OK)
3370 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3371 }
3372
3373 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3374 sizeof(buf_T *), buf_compare);
3375
Bram Moolenaar3b991522019-11-06 23:26:20 +01003376 buflist_data = (buf_T **)buflist.ga_data;
3377 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003378 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003379 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003380
Bram Moolenaar3b991522019-11-06 23:26:20 +01003381 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003382 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3383 : buf->b_next)
3384#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003388#ifdef FEAT_TERMINAL
3389 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003390 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003391#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003392 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003393 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3394 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3395 || (vim_strchr(eap->arg, '+')
3396 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3397 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003398 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003399 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003400 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3401#ifdef FEAT_TERMINAL
3402 || (vim_strchr(eap->arg, 'R')
3403 && (!job_running || (job_running && job_none_open)))
3404 || (vim_strchr(eap->arg, '?')
3405 && (!job_running || (job_running && !job_none_open)))
3406 || (vim_strchr(eap->arg, 'F')
3407 && (job_running || buf->b_term == NULL))
3408#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003409 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3410 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3411 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3412 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3413 || (vim_strchr(eap->arg, '#')
3414 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003417 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else
3419 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003420 if (message_filtered(NameBuff))
3421 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003423 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3424 : (bufIsChanged(buf) ? '+' : ' ');
3425#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003426 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003427 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003428 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003429 ro_char = '?';
3430 else
3431 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003432 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3433 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003434 }
3435 else if (buf->b_term != NULL)
3436 ro_char = 'F';
3437 else
3438#endif
3439 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3440
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003441 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003442 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 buf->b_fnum,
3444 buf->b_p_bl ? ' ' : 'u',
3445 buf == curbuf ? '%' :
3446 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3447 buf->b_ml.ml_mfp == NULL ? ' ' :
3448 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003449 ro_char,
3450 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003451 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003452 if (len > IOSIZE - 20)
3453 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaarc667da52019-11-30 20:52:27 +01003455 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 i = 40 - vim_strsize(IObuff);
3457 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003459 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003460#ifdef FEAT_VIMINFO
3461 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3462 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3463 else
3464#endif
3465 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3466 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003467 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003469 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 ui_breakcheck();
3471 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003472
3473#ifdef FEAT_VIMINFO
3474 if (buflist_data)
3475 ga_clear(&buflist);
3476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479/*
3480 * Get file name and line number for file 'fnum'.
3481 * Used by DoOneCmd() for translating '%' and '#'.
3482 * Used by insert_reg() and cmdline_paste() for '#' register.
3483 * Return FAIL if not found, OK for success.
3484 */
3485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003486buflist_name_nr(
3487 int fnum,
3488 char_u **fname,
3489 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 buf_T *buf;
3492
3493 buf = buflist_findnr(fnum);
3494 if (buf == NULL || buf->b_fname == NULL)
3495 return FAIL;
3496
3497 *fname = buf->b_fname;
3498 *lnum = buflist_findlnum(buf);
3499
3500 return OK;
3501}
3502
3503/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003504 * Set the file name for "buf"' to "ffname_arg", short file name to
3505 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 * The file name with the full path is also remembered, for when :cd is used.
3507 * Returns FAIL for failure (file name already in use by other buffer)
3508 * OK otherwise.
3509 */
3510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003511setfname(
3512 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003513 char_u *ffname_arg,
3514 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003515 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003517 char_u *ffname = ffname_arg;
3518 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003519 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003521 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523
3524 if (ffname == NULL || *ffname == NUL)
3525 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003526 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003527 if (buf->b_sfname != buf->b_ffname)
3528 VIM_CLEAR(buf->b_sfname);
3529 else
3530 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003531 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#ifdef UNIX
3533 st.st_dev = (dev_T)-1;
3534#endif
3535 }
3536 else
3537 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003538 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3539 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return FAIL;
3541
3542 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003543 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 * - if the buffer is loaded, fail
3545 * - if the buffer is not loaded, delete it from the list
3546 */
3547#ifdef UNIX
3548 if (mch_stat((char *)ffname, &st) < 0)
3549 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003550#endif
3551 if (!(buf->b_flags & BF_DUMMY))
3552#ifdef UNIX
3553 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003555 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
3557 if (obuf != NULL && obuf != buf)
3558 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003559 win_T *win;
3560 tabpage_T *tab;
3561 int in_use = FALSE;
3562
3563 // during startup a window may use a buffer that is not loaded yet
3564 FOR_ALL_TAB_WINDOWS(tab, win)
3565 if (win->w_buffer == obuf)
3566 in_use = TRUE;
3567
3568 // it's loaded or used in a window, fail
3569 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003572 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 vim_free(ffname);
3574 return FAIL;
3575 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003576 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003577 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 sfname = vim_strsave(sfname);
3580 if (ffname == NULL || sfname == NULL)
3581 {
3582 vim_free(sfname);
3583 vim_free(ffname);
3584 return FAIL;
3585 }
3586#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003587 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003589 if (buf->b_sfname != buf->b_ffname)
3590 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 buf->b_ffname = ffname;
3593 buf->b_sfname = sfname;
3594 }
3595 buf->b_fname = buf->b_sfname;
3596#ifdef UNIX
3597 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003598 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
3600 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003601 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 buf->b_dev = st.st_dev;
3603 buf->b_ino = st.st_ino;
3604 }
3605#endif
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 buf_name_changed(buf);
3610 return OK;
3611}
3612
3613/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003614 * Crude way of changing the name of a buffer. Use with care!
3615 * The name should be relative to the current directory.
3616 */
3617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003618buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003619{
3620 buf_T *buf;
3621
3622 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003623 if (buf == NULL)
3624 return;
3625
3626 if (buf->b_sfname != buf->b_ffname)
3627 vim_free(buf->b_sfname);
3628 vim_free(buf->b_ffname);
3629 buf->b_ffname = vim_strsave(name);
3630 buf->b_sfname = NULL;
3631 // Allocate ffname and expand into full path. Also resolves .lnk
3632 // files on Win32.
3633 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3634 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003635}
3636
3637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 * Take care of what needs to be done when the name of buffer "buf" has
3639 * changed.
3640 */
3641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 /*
3645 * If the file name changed, also change the name of the swapfile
3646 */
3647 if (buf->b_ml.ml_mfp != NULL)
3648 ml_setname(buf);
3649
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003650#ifdef FEAT_TERMINAL
3651 if (buf->b_term != NULL)
3652 term_clear_status_text(buf->b_term);
3653#endif
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003656 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003657 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003658 status_redraw_all(); // status lines need to be redrawn
3659 fmarks_check_names(buf); // check named file marks
3660 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661}
3662
3663/*
3664 * set alternate file name for current window
3665 *
3666 * Used by do_one_cmd(), do_write() and do_ecmd().
3667 * Return the buffer.
3668 */
3669 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670setaltfname(
3671 char_u *ffname,
3672 char_u *sfname,
3673 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 buf_T *buf;
3676
Bram Moolenaarc667da52019-11-30 20:52:27 +01003677 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003679 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 curwin->w_alt_fnum = buf->b_fnum;
3681 return buf;
3682}
3683
3684/*
3685 * Get alternate file name for current window.
3686 * Return NULL if there isn't any, and give error message if requested.
3687 */
3688 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003689getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003690 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692 char_u *fname;
3693 linenr_T dummy;
3694
3695 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3696 {
3697 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003698 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 return NULL;
3700 }
3701 return fname;
3702}
3703
3704/*
3705 * Add a file name to the buflist and return its number.
3706 * Uses same flags as buflist_new(), except BLN_DUMMY.
3707 *
3708 * used by qf_init(), main() and doarglist()
3709 */
3710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
3713 buf_T *buf;
3714
3715 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3716 if (buf != NULL)
3717 return buf->b_fnum;
3718 return 0;
3719}
3720
3721#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3722/*
3723 * Adjust slashes in file names. Called after 'shellslash' was set.
3724 */
3725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003726buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
3728 buf_T *bp;
3729
Bram Moolenaar29323592016-07-24 22:04:11 +02003730 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 if (bp->b_ffname != NULL)
3733 slash_adjust(bp->b_ffname);
3734 if (bp->b_sfname != NULL)
3735 slash_adjust(bp->b_sfname);
3736 }
3737}
3738#endif
3739
3740/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003741 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 * Also save the local window option values.
3743 */
3744 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003747 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748}
3749
3750/*
3751 * Return TRUE if 'ffname' is not the same file as current file.
3752 * Fname must have a full path (expanded by mch_FullName()).
3753 */
3754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003755otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 return otherfile_buf(curbuf, ffname
3758#ifdef UNIX
3759 , NULL
3760#endif
3761 );
3762}
3763
3764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003765otherfile_buf(
3766 buf_T *buf,
3767 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003769 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003771 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003773 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3775 return TRUE;
3776 if (fnamecmp(ffname, buf->b_ffname) == 0)
3777 return FALSE;
3778#ifdef UNIX
3779 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003780 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
Bram Moolenaarc667da52019-11-30 20:52:27 +01003782 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 if (stp == NULL)
3784 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003785 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 st.st_dev = (dev_T)-1;
3787 stp = &st;
3788 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003789 // Use dev/ino to check if the files are the same, even when the names
3790 // are different (possible with links). Still need to compare the
3791 // name above, for when the file doesn't exist yet.
3792 // Problem: The dev/ino changes when a file is deleted (and created
3793 // again) and remains the same when renamed/moved. We don't want to
3794 // mch_stat() each buffer each time, that would be too slow. Get the
3795 // dev/ino again when they appear to match, but not when they appear
3796 // to be different: Could skip a buffer when it's actually the same
3797 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (buf_same_ino(buf, stp))
3799 {
3800 buf_setino(buf);
3801 if (buf_same_ino(buf, stp))
3802 return FALSE;
3803 }
3804 }
3805#endif
3806 return TRUE;
3807}
3808
3809#if defined(UNIX) || defined(PROTO)
3810/*
3811 * Set inode and device number for a buffer.
3812 * Must always be called when b_fname is changed!.
3813 */
3814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003817 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3820 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003821 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 buf->b_dev = st.st_dev;
3823 buf->b_ino = st.st_ino;
3824 }
3825 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003826 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827}
3828
3829/*
3830 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3831 */
3832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003833buf_same_ino(
3834 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003835 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003837 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 && stp->st_dev == buf->b_dev
3839 && stp->st_ino == buf->b_ino);
3840}
3841#endif
3842
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003843/*
3844 * Print info about the current buffer.
3845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003847fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003848 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849 int shorthelp,
3850 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 char_u *name;
3853 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003854 char *p;
3855 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003856 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003858 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (buffer == NULL)
3860 return;
3861
Bram Moolenaarc667da52019-11-30 20:52:27 +01003862 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003864 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 p = buffer + STRLEN(buffer);
3866 }
3867 else
3868 p = buffer;
3869
3870 *p++ = '"';
3871 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003872 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 else
3874 {
3875 if (!fullname && curbuf->b_fname != NULL)
3876 name = curbuf->b_fname;
3877 else
3878 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003879 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 (int)(IOSIZE - (p - buffer)), TRUE);
3881 }
3882
Bram Moolenaar32526b32019-01-19 17:43:09 +01003883 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 curbufIsChanged() ? (shortmess(SHM_MOD)
3885 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003886 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003888 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003889 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003891 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 : _("[readonly]")) : "",
3893 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3894 || curbuf->b_p_ro) ?
3895 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003896 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3897 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (curwin->w_cursor.lnum > 1000000L)
3899 n = (int)(((long)curwin->w_cursor.lnum) /
3900 ((long)curbuf->b_ml.ml_line_count / 100L));
3901 else
3902 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3903 (long)curbuf->b_ml.ml_line_count);
3904 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003905 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003907 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003908 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003909 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3910 curbuf->b_ml.ml_line_count),
3911 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else
3913 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003914 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 _("line %ld of %ld --%d%%-- col "),
3916 (long)curwin->w_cursor.lnum,
3917 (long)curbuf->b_ml.ml_line_count,
3918 n);
3919 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003920 len = STRLEN(buffer);
John Marriotta21240b2025-01-08 20:10:59 +01003921 (void)col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3923 }
3924
Bram Moolenaar32526b32019-01-19 17:43:09 +01003925 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3926 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (dont_truncate)
3929 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003930 // Temporarily set msg_scroll to avoid the message being truncated.
3931 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 msg_start();
3933 n = msg_scroll;
3934 msg_scroll = TRUE;
3935 msg(buffer);
3936 msg_scroll = n;
3937 }
3938 else
3939 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003940 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003942 // Need to repeat the message after redrawing when:
3943 // - When restart_edit is set (otherwise there will be a delay
3944 // before redrawing).
3945 // - When the screen was scrolled but there is no wait-return
3946 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003947 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949
3950 vim_free(buffer);
3951}
3952
John Marriotta21240b2025-01-08 20:10:59 +01003953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003954col_print(
3955 char_u *buf,
3956 size_t buflen,
3957 int col,
3958 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 if (col == vcol)
John Marriotta21240b2025-01-08 20:10:59 +01003961 return vim_snprintf((char *)buf, buflen, "%d", col);
3962
3963 return vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964}
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966static char_u *lasttitle = NULL;
3967static char_u *lasticon = NULL;
3968
Bram Moolenaar84a93082018-06-16 22:58:15 +02003969/*
3970 * Put the file name in the title bar and icon of the window.
3971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003976 char_u *title_str = NULL;
3977 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int maxlen = 0;
3979 int len;
3980 int mustset;
3981 char_u buf[IOSIZE];
3982 int off;
3983
3984 if (!redrawing())
3985 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003986 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 need_maketitle = TRUE;
3988 return;
3989 }
3990
3991 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003992 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003993 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995 if (p_title)
3996 {
3997 if (p_titlelen > 0)
3998 {
3999 maxlen = p_titlelen * Columns / 100;
4000 if (maxlen < 10)
4001 maxlen = 10;
4002 }
4003
Bram Moolenaar84a93082018-06-16 22:58:15 +02004004 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (*p_titlestring != NUL)
4006 {
4007#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004008 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004009 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4010 (char_u *)"titlestring", 0,
4011 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 else
4013#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004014 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016 else
4017 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004018 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
Bram Moolenaar2c666692012-09-05 13:30:40 +02004020#define SPACE_FOR_FNAME (IOSIZE - 100)
4021#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004022#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02004024 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02004025#ifdef FEAT_TERMINAL
4026 else if (curbuf->b_term != NULL)
4027 {
4028 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
4029 SPACE_FOR_FNAME);
4030 }
4031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 else
4033 {
4034 p = transstr(gettail(curbuf->b_fname));
John Marriott7fb90812025-04-02 20:32:35 +02004035 if (p != NULL)
4036 {
4037 vim_strncpy(buf, p, SPACE_FOR_FNAME);
4038 vim_free(p);
4039 }
zeertzjqf16d8b22025-04-03 21:21:15 +02004040 else
4041 STRCPY(buf, "");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043
Bram Moolenaar21554412017-07-24 21:44:43 +02004044#ifdef FEAT_TERMINAL
4045 if (curbuf->b_term == NULL)
4046#endif
4047 switch (bufIsChanged(curbuf)
4048 + (curbuf->b_p_ro * 2)
4049 + (!curbuf->b_p_ma * 4))
4050 {
4051 case 1: STRCAT(buf, " +"); break;
4052 case 2: STRCAT(buf, " ="); break;
4053 case 3: STRCAT(buf, " =+"); break;
4054 case 4:
4055 case 6: STRCAT(buf, " -"); break;
4056 case 5:
4057 case 7: STRCAT(buf, " -+"); break;
4058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
Bram Moolenaar21554412017-07-24 21:44:43 +02004060 if (curbuf->b_fname != NULL
4061#ifdef FEAT_TERMINAL
4062 && curbuf->b_term == NULL
4063#endif
4064 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004066 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 off = (int)STRLEN(buf);
4068 buf[off++] = ' ';
4069 buf[off++] = '(';
4070 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004071 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004073 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004074 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 off += 2;
4076#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004077 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004078 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004080 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004081 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004082 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004083 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004087
Bram Moolenaarc667da52019-11-30 20:52:27 +01004088 // Translate unprintable chars and concatenate. Keep some
4089 // room for the server name. When there is no room (very long
4090 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004091 if (off < SPACE_FOR_DIR)
4092 {
4093 p = transstr(buf + off);
John Marriott7fb90812025-04-02 20:32:35 +02004094 if (p != NULL)
4095 {
4096 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4097 vim_free(p);
4098 }
Bram Moolenaar2c666692012-09-05 13:30:40 +02004099 }
4100 else
4101 {
4102 vim_strncpy(buf + off, (char_u *)"...",
4103 (size_t)(SPACE_FOR_ARGNR - off));
4104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 STRCAT(buf, ")");
4106 }
4107
Bram Moolenaar2c666692012-09-05 13:30:40 +02004108 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110#if defined(FEAT_CLIENTSERVER)
4111 if (serverName != NULL)
4112 {
4113 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004114 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 else
4117#endif
4118 STRCAT(buf, " - VIM");
4119
4120 if (maxlen > 0)
4121 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004122 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004123 if (vim_strsize(buf) > maxlen)
4124 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 }
4127 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004128 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 if (p_icon)
4131 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004132 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (*p_iconstring != NUL)
4134 {
4135#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004136 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004137 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4138 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
4140#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004141 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else
4144 {
4145 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004146 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004147 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004148 p = gettail(curbuf->b_ffname);
4149 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004150 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004151 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (len > 100)
4153 {
4154 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004156 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004157 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004159 STRCPY(icon_str, p);
4160 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 }
4163
Bram Moolenaar84a93082018-06-16 22:58:15 +02004164 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 if (mustset)
4167 resettitle();
4168}
4169
4170/*
4171 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4172 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004173 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 */
4175 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004176value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 if ((str == NULL) != (*last == NULL)
4179 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4180 {
4181 vim_free(*last);
4182 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004185 mch_restore_title(
4186 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004191 return TRUE;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 return FALSE;
4195}
4196
4197/*
4198 * Put current window title back (used after calling a shell)
4199 */
4200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
4203 mch_settitle(lasttitle, lasticon);
4204}
Bram Moolenaarea408852005-06-25 22:49:46 +00004205
4206# if defined(EXITFREE) || defined(PROTO)
4207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004208free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004209{
4210 vim_free(lasttitle);
4211 vim_free(lasticon);
4212}
4213# endif
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004216#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004217
4218/*
4219 * Used for building in the status line.
4220 */
4221typedef struct
4222{
4223 char_u *stl_start;
4224 int stl_minwid;
4225 int stl_maxwid;
4226 enum {
4227 Normal,
4228 Empty,
4229 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004230 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004231 Highlight,
4232 TabPage,
4233 Trunc
4234 } stl_type;
4235} stl_item_T;
4236
4237static size_t stl_items_len = 20; // Initial value, grows as needed.
4238static stl_item_T *stl_items = NULL;
4239static int *stl_groupitem = NULL;
4240static stl_hlrec_T *stl_hltab = NULL;
4241static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004242static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004245 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 * Return length of string in screen cells.
4247 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004248 * Normally works for window "wp", except when working for 'tabline' then it
4249 * is "curwin".
4250 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004252 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4254 *
4255 * If maxwidth is not zero, the string will be filled at any middle marker
4256 * or truncated if too long, fillchar is used for all whitespace.
4257 */
4258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004259build_stl_str_hl(
4260 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004261 char_u *out, // buffer to write into != NameBuff
4262 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004263 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004264 char_u *opt_name, // option name corresponding to "fmt"
4265 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004266 int fillchar,
4267 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004268 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4269 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
Bram Moolenaar10772302019-01-20 18:25:54 +01004271 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004272 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 char_u *p;
4274 char_u *s;
4275 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004276 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004278 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004279 win_T *save_curwin;
4280 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004281 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#endif
4283 int empty_line;
4284 colnr_T virtcol;
4285 long l;
4286 long n;
4287 int prevchar_isflag;
4288 int prevchar_isitem;
4289 int itemisflag;
4290 int fillable;
4291 char_u *str;
4292 long num;
4293 int width;
4294 int itemcnt;
4295 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004296 int group_end_userhl;
4297 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004299#ifdef FEAT_EVAL
4300 int evaldepth;
4301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 int minwid;
4303 int maxwid;
4304 int zeropad;
4305 char_u base;
4306 char_u opt;
4307#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004308 char_u buf_tmp[TMPLEN];
4309 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004310 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004311 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004312 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004313 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004314 // TODO: find out why using called_emsg_before makes tests fail, does it
4315 // matter?
4316 // int called_emsg_before = called_emsg;
4317 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004318
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004319 // When inside update_screen() we do not want redrawing a statusline,
4320 // ruler, title, etc. to trigger another redraw, it may cause an endless
4321 // loop.
4322 if (updating_screen)
4323 redraw_not_allowed = TRUE;
4324
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004325 if (stl_items == NULL)
4326 {
4327 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4328 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004329
4330 // Allocate one more, because the last element is used to indicate the
4331 // end of the list.
4332 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4333 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004334
4335 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004336 }
4337
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004338#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004339 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4340 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4341
4342 // When the format starts with "%!" then evaluate it as an expression and
4343 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004344 if (fmt[0] == '%' && fmt[1] == '!')
4345 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004346 typval_T tv;
4347
4348 tv.v_type = VAR_NUMBER;
4349 tv.vval.v_number = wp->w_id;
4350 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4351
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004352 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004353 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004354 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004355
4356 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004357 }
4358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 if (fillchar == 0)
4361 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004362
Bram Moolenaar10772302019-01-20 18:25:54 +01004363 // The cursor in windows other than the current one isn't always
4364 // up-to-date, esp. because of autocommands and timers.
4365 lnum = wp->w_cursor.lnum;
4366 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4367 {
4368 lnum = wp->w_buffer->b_ml.ml_line_count;
4369 wp->w_cursor.lnum = lnum;
4370 }
4371
4372 // Get line & check if empty (cursorpos will show "0-1"). Note that
4373 // p will become invalid when getting another buffer line.
4374 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004375 empty_line = (*p == NUL);
4376
Bram Moolenaar10772302019-01-20 18:25:54 +01004377 // Get the byte value now, in case we need it below. This is more efficient
4378 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004379 len = ml_get_buf_len(wp->w_buffer, lnum);
4380 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004381 {
4382 // Line may have changed since checking the cursor column, or the lnum
4383 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004384 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004385 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004386 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004387 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004388 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004389 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004392#ifdef FEAT_EVAL
4393 evaldepth = 0;
4394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 p = out;
4396 curitem = 0;
4397 prevchar_isflag = TRUE;
4398 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004399 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004401 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004402 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004403 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004404
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004405 stl_item_T *new_items =
4406 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004407 if (new_items == NULL)
4408 break;
4409 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004410
4411 int *new_groupitem =
4412 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004413 if (new_groupitem == NULL)
4414 break;
4415 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004416
4417 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004418 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004419 if (new_hlrec == NULL)
4420 break;
4421 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004422 new_hlrec = vim_realloc(stl_tabtab,
4423 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 if (new_hlrec == NULL)
4425 break;
4426 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004427
4428 int *new_separator_locs = vim_realloc(stl_separator_locations,
4429 sizeof(int) * new_len);
4430 if (new_separator_locs == NULL)
4431 break;
4432 stl_separator_locations = new_separator_locs;;
4433
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004434 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004435 }
4436
zeertzjq122dea72022-07-27 15:48:45 +01004437 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 prevchar_isflag = prevchar_isitem = FALSE;
4439
4440 /*
4441 * Handle up to the next '%' or the end.
4442 */
4443 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4444 *p++ = *s++;
4445 if (*s == NUL || p + 1 >= out + outlen)
4446 break;
4447
4448 /*
4449 * Handle one '%' item.
4450 */
4451 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004452 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (*s == '%')
4455 {
4456 if (p + 1 >= out + outlen)
4457 break;
4458 *p++ = *s++;
4459 prevchar_isflag = prevchar_isitem = FALSE;
4460 continue;
4461 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004462 // STL_SEPARATE: Separation between items, filled with white space.
4463 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 s++;
4466 if (groupdepth > 0)
4467 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004468 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004469 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 continue;
4471 }
4472 if (*s == STL_TRUNCMARK)
4473 {
4474 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004475 stl_items[curitem].stl_type = Trunc;
4476 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 continue;
4478 }
4479 if (*s == ')')
4480 {
4481 s++;
4482 if (groupdepth < 1)
4483 continue;
4484 groupdepth--;
4485
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004486 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 *p = NUL;
4488 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004489 if (curitem > stl_groupitem[groupdepth] + 1
4490 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004492 // remove group if all items are empty and highlight group
4493 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004494 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004495 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004496 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004497 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004498 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004499 group_start_userhl = group_end_userhl =
4500 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004501 break;
4502 }
4503 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004504 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004505 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004506 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004508 if (stl_items[n].stl_type == Highlight)
4509 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004510 }
4511 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004513 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 p = t;
4515 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004516 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004517 {
4518 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004519 if (stl_items[n].stl_type == Highlight)
4520 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004521 // adjust the start position of TabPage to the next
4522 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004523 if (stl_items[n].stl_type == TabPage)
4524 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004528 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004530 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (has_mbyte)
4532 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004533 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004535 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
4537 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004538 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 }
4541 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004542 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4543 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004546 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004548
4549 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004550 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004551 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaarc667da52019-11-30 20:52:27 +01004553 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004554 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
LemonBoy57ff5262022-05-09 21:03:47 +01004556 // Minus one for the leading '<' added above.
4557 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004558 if (stl_items[l].stl_start < t)
4559 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004562 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004564 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004565 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (n < 0)
4567 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004568 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 n = 0 - n;
4570 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004571 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
4573 else
4574 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004575 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004576 l = (n - l) * MB_CHAR2LEN(fillchar);
4577 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004579 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004581 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4582 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004584 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 }
4587 continue;
4588 }
4589 minwid = 0;
4590 maxwid = 9999;
4591 zeropad = FALSE;
4592 l = 1;
4593 if (*s == '0')
4594 {
4595 s++;
4596 zeropad = TRUE;
4597 }
4598 if (*s == '-')
4599 {
4600 s++;
4601 l = -1;
4602 }
4603 if (VIM_ISDIGIT(*s))
4604 {
4605 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004606 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 minwid = 0;
4608 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004609 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004611 stl_items[curitem].stl_type = Highlight;
4612 stl_items[curitem].stl_start = p;
4613 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 s++;
4615 curitem++;
4616 continue;
4617 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004618 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4619 {
4620 if (*s == STL_TABCLOSENR)
4621 {
4622 if (minwid == 0)
4623 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004624 // %X ends the close label, go back to the previously
4625 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004626 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004627 if (stl_items[n].stl_type == TabPage
4628 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004629 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004630 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004631 break;
4632 }
4633 }
4634 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004635 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004636 minwid = - minwid;
4637 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004638 stl_items[curitem].stl_type = TabPage;
4639 stl_items[curitem].stl_start = p;
4640 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004641 s++;
4642 curitem++;
4643 continue;
4644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (*s == '.')
4646 {
4647 s++;
4648 if (VIM_ISDIGIT(*s))
4649 {
4650 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004651 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 maxwid = 50;
4653 }
4654 }
4655 minwid = (minwid > 50 ? 50 : minwid) * l;
4656 if (*s == '(')
4657 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004658 stl_groupitem[groupdepth++] = curitem;
4659 stl_items[curitem].stl_type = Group;
4660 stl_items[curitem].stl_start = p;
4661 stl_items[curitem].stl_minwid = minwid;
4662 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 s++;
4664 curitem++;
4665 continue;
4666 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004667#ifdef FEAT_EVAL
4668 // Denotes end of expanded %{} block
4669 if (*s == '}' && evaldepth > 0)
4670 {
4671 s++;
4672 evaldepth--;
4673 continue;
4674 }
4675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (vim_strchr(STL_ALL, *s) == NULL)
4677 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004678 if (*s == NUL) // can happen with "%0"
4679 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 s++;
4681 continue;
4682 }
4683 opt = *s++;
4684
Bram Moolenaarc667da52019-11-30 20:52:27 +01004685 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 base = 'D';
4687 itemisflag = FALSE;
4688 fillable = TRUE;
4689 num = -1;
4690 str = NULL;
4691 switch (opt)
4692 {
4693 case STL_FILEPATH:
4694 case STL_FULLPATH:
4695 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004696 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004698 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 else
4700 {
4701 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004702 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4704 }
4705 trans_characters(NameBuff, MAXPATHL);
4706 if (opt != STL_FILENAME)
4707 str = NameBuff;
4708 else
4709 str = gettail(NameBuff);
4710 break;
4711
Bram Moolenaarc667da52019-11-30 20:52:27 +01004712 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004713 {
4714#ifdef FEAT_EVAL
4715 char_u *block_start = s - 1;
4716#endif
4717 int reevaluate = (*s == '%');
4718
4719 if (reevaluate)
4720 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 itemisflag = TRUE;
4722 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004723 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4724 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004726 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 break;
4728 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004729 if (reevaluate)
4730 p[-1] = 0; // remove the % at the end of %{% expr %}
4731 else
4732 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004735 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4736 "%d", curbuf->b_fnum);
4737 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4738 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4739 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004741 save_curbuf = curbuf;
4742 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004743 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 curwin = wp;
4745 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004746 // Visual mode is only valid in the current window.
4747 if (curwin != save_curwin)
4748 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004750 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004752 curwin = save_curwin;
4753 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004754 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004755 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004756 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
4758 if (str != NULL && *str != 0)
4759 {
4760 if (*skipdigits(str) == NUL)
4761 {
4762 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004763 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 itemisflag = FALSE;
4765 }
4766 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004767
4768 // If the output of the expression needs to be evaluated
4769 // replace the %{} block with the result of evaluation
4770 if (reevaluate && str != NULL && *str != 0
4771 && strchr((const char *)str, '%') != NULL
4772 && evaldepth < MAX_STL_EVAL_DEPTH)
4773 {
4774 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4775 size_t str_length = strlen((const char *)str);
4776 size_t fmt_length = strlen((const char *)s);
4777 size_t new_fmt_len = parsed_usefmt
4778 + str_length + fmt_length + 3;
4779 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004780
John Marriott7fb90812025-04-02 20:32:35 +02004781 if (new_fmt != NULL)
4782 {
4783 char_u *new_fmt_p = new_fmt;
shadmansaleh30e3de22021-05-15 17:23:28 +02004784
John Marriott7fb90812025-04-02 20:32:35 +02004785 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4786 + parsed_usefmt;
4787 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4788 + str_length;
4789 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4790 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4791 + fmt_length;
4792 *new_fmt_p = 0;
4793 new_fmt_p = NULL;
4794
4795 if (usefmt != fmt)
4796 vim_free(usefmt);
4797 VIM_CLEAR(str);
4798 usefmt = new_fmt;
4799 s = usefmt + parsed_usefmt;
4800 evaldepth++;
4801 continue;
4802 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#endif
4805 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 case STL_LINE:
4808 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4809 ? 0L : (long)(wp->w_cursor.lnum);
4810 break;
4811
4812 case STL_NUMLINES:
4813 num = wp->w_buffer->b_ml.ml_line_count;
4814 break;
4815
4816 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004817 num = (State & MODE_INSERT) == 0 && empty_line
4818 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 break;
4820
4821 case STL_VIRTCOL:
4822 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004823 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004824 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004826 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4827 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 break;
4829 num = (long)virtcol;
4830 break;
4831
4832 case STL_PERCENTAGE:
4833 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4834 (long)wp->w_buffer->b_ml.ml_line_count);
4835 break;
4836
4837 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004838 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004839 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 break;
4841
Luuk van Baalba936f62022-12-15 13:15:39 +00004842 case STL_SHOWCMD:
4843 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4844 str = showcmd_buf;
4845 break;
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 case STL_ARGLISTSTAT:
4848 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004849 buf_tmp[0] = 0;
4850 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4851 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 break;
4853
4854 case STL_KEYMAP:
4855 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004856 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004857 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 break;
4859 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004860#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004861 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#else
4863 num = 0;
4864#endif
4865 break;
4866
4867 case STL_BUFNO:
4868 num = wp->w_buffer->b_fnum;
4869 break;
4870
4871 case STL_OFFSET_X:
4872 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004873 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 case STL_OFFSET:
4875#ifdef FEAT_BYTEOFF
4876 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004877 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4878 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4879 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880#endif
4881 break;
4882
4883 case STL_BYTEVAL_X:
4884 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004885 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004887 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (num == NL)
4889 num = 0;
4890 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4891 num = NL;
4892 break;
4893
4894 case STL_ROFLAG:
4895 case STL_ROFLAG_ALT:
4896 itemisflag = TRUE;
4897 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004898 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 break;
4900
4901 case STL_HELPFLAG:
4902 case STL_HELPFLAG_ALT:
4903 itemisflag = TRUE;
4904 if (wp->w_buffer->b_help)
4905 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004906 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 break;
4908
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 case STL_FILETYPE:
4910 if (*wp->w_buffer->b_p_ft != NUL
4911 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4912 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004913 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004914 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004915 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 break;
4918
4919 case STL_FILETYPE_ALT:
4920 itemisflag = TRUE;
4921 if (*wp->w_buffer->b_p_ft != NUL
4922 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4923 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004924 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004925 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004926 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004928 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931
Bram Moolenaar4033c552017-09-16 20:54:51 +02004932#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 case STL_PREVIEWFLAG:
4934 case STL_PREVIEWFLAG_ALT:
4935 itemisflag = TRUE;
4936 if (wp->w_p_pvw)
4937 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4938 : _("[Preview]"));
4939 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004940
4941 case STL_QUICKFIX:
4942 if (bt_quickfix(wp->w_buffer))
4943 str = (char_u *)(wp->w_llist_ref
4944 ? _(msg_loclist)
4945 : _(msg_qflist));
4946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#endif
4948
4949 case STL_MODIFIED:
4950 case STL_MODIFIED_ALT:
4951 itemisflag = TRUE;
4952 switch ((opt == STL_MODIFIED_ALT)
4953 + bufIsChanged(wp->w_buffer) * 2
4954 + (!wp->w_buffer->b_p_ma) * 4)
4955 {
4956 case 2: str = (char_u *)"[+]"; break;
4957 case 3: str = (char_u *)",+"; break;
4958 case 4: str = (char_u *)"[-]"; break;
4959 case 5: str = (char_u *)",-"; break;
4960 case 6: str = (char_u *)"[+-]"; break;
4961 case 7: str = (char_u *)",+-"; break;
4962 }
4963 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004964
4965 case STL_HIGHLIGHT:
4966 t = s;
4967 while (*s != '#' && *s != NUL)
4968 ++s;
4969 if (*s == '#')
4970 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004971 stl_items[curitem].stl_type = Highlight;
4972 stl_items[curitem].stl_start = p;
4973 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004974 curitem++;
4975 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004976 if (*s != NUL)
4977 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004978 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004981 stl_items[curitem].stl_start = p;
4982 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (str != NULL && *str)
4984 {
4985 t = str;
4986 if (itemisflag)
4987 {
4988 if ((t[0] && t[1])
4989 && ((!prevchar_isitem && *t == ',')
4990 || (prevchar_isflag && *t == ' ')))
4991 t++;
4992 prevchar_isflag = TRUE;
4993 }
4994 l = vim_strsize(t);
4995 if (l > 0)
4996 prevchar_isitem = TRUE;
4997 if (l > maxwid)
4998 {
4999 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (has_mbyte)
5001 {
5002 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005003 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 l -= byte2cells(*t++);
5007 if (p + 1 >= out + outlen)
5008 break;
5009 *p++ = '<';
5010 }
5011 if (minwid > 0)
5012 {
5013 for (; l < minwid && p + 1 < out + outlen; l++)
5014 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005015 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5017 *p++ = ' ';
5018 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005019 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 minwid = 0;
5022 }
5023 else
5024 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005025 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005027 // Change a space by fillchar, unless fillchar is '-' and a
5028 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005029 if (fillable && *t == ' '
5030 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5031 MB_CHAR2BYTES(fillchar, p);
5032 else
5033 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005036 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 else if (num >= 0)
5039 {
5040 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5041 char_u nstr[20];
5042
5043 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005044 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 prevchar_isitem = TRUE;
5046 t = nstr;
5047 if (opt == STL_VIRTCOL_ALT)
5048 {
5049 *t++ = '-';
5050 minwid--;
5051 }
5052 *t++ = '%';
5053 if (zeropad)
5054 *t++ = '0';
5055 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005056 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 *t = 0;
5058
5059 for (n = num, l = 1; n >= nbase; n /= nbase)
5060 l++;
5061 if (opt == STL_VIRTCOL_ALT)
5062 l++;
5063 if (l > maxwid)
5064 {
5065 l += 2;
5066 n = l - maxwid;
5067 while (l-- > maxwid)
5068 num /= nbase;
5069 *t++ = '>';
5070 *t++ = '%';
5071 *t = t[-3];
5072 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005073 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5074 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005077 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5078 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 p += STRLEN(p);
5080 }
5081 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005082 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005084 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5085 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5086 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (opt == STL_VIM_EXPR)
5088 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 curitem++;
5090 }
5091 *p = NUL;
5092 itemcnt = curitem;
5093
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005094#ifdef FEAT_EVAL
5095 if (usefmt != fmt)
5096 vim_free(usefmt);
5097#endif
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 width = vim_strsize(out);
5100 if (maxwidth > 0 && width > maxwidth)
5101 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005102 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 l = 0;
5104 if (itemcnt == 0)
5105 s = out;
5106 else
5107 {
5108 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005109 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005111 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005112 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
5114 }
5115 if (l == itemcnt)
5116 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005117 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005118 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 l = 0;
5120 }
5121 }
5122
5123 if (width - vim_strsize(s) >= maxwidth)
5124 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005125 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (has_mbyte)
5127 {
5128 s = out;
5129 width = 0;
5130 for (;;)
5131 {
5132 width += ptr2cells(s);
5133 if (width >= maxwidth)
5134 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005135 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005137 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005139 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 s = out + maxwidth - 1;
5143 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005144 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146 itemcnt = l;
5147 *s++ = '>';
5148 *s = 0;
5149 }
5150 else
5151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (has_mbyte)
5153 {
5154 n = 0;
5155 while (width >= maxwidth)
5156 {
5157 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005158 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 }
5161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 n = width - maxwidth + 1;
5163 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005164 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 *s = '<';
5166
Bram Moolenaarc667da52019-11-30 20:52:27 +01005167 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 for (; l < itemcnt; l++)
5169 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005170 if (stl_items[l].stl_start - n >= s)
5171 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005173 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
zeertzjqd392a742023-07-01 20:24:40 +01005175
5176 // Fill up for half a double-wide character.
5177 while (++width < maxwidth)
5178 {
5179 s = s + STRLEN(s);
5180 MB_CHAR2BYTES(fillchar, s);
5181 *s = NUL;
5182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 width = maxwidth;
5185 }
5186 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5187 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005188 // Find how many separators there are, which we will use when
5189 // figuring out how many groups there are.
5190 int num_separators = 0;
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005194 if (stl_items[l].stl_type == Separate)
5195 {
5196 // Create an array of the start location for each separator
5197 // mark.
5198 stl_separator_locations[num_separators] = l;
5199 num_separators++;
5200 }
5201 }
5202
5203 // If we have separated groups, then we deal with it now
5204 if (num_separators)
5205 {
5206 int standard_spaces;
5207 int final_spaces;
5208
5209 standard_spaces = (maxwidth - width) / num_separators;
5210 final_spaces = (maxwidth - width) -
5211 standard_spaces * (num_separators - 1);
5212 for (l = 0; l < num_separators; l++)
5213 {
5214 int dislocation = (l == (num_separators - 1)) ?
5215 final_spaces : standard_spaces;
5216 dislocation *= MB_CHAR2LEN(fillchar);
5217 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5218 char_u *seploc = start + dislocation;
5219 STRMOVE(seploc, start);
5220 for (s = start; s < seploc;)
5221 MB_CHAR2BYTES(fillchar, s);
5222
5223 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5224 stl_items[i].stl_start += dislocation;
5225 }
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 width = maxwidth;
5228 }
5229 }
5230
Bram Moolenaarc667da52019-11-30 20:52:27 +01005231 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005232 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005234 *hltab = stl_hltab;
5235 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 for (l = 0; l < itemcnt; l++)
5237 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005238 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005240 sp->start = stl_items[l].stl_start;
5241 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005242 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005245 sp->start = NULL;
5246 sp->userhl = 0;
5247 }
5248
Bram Moolenaarc667da52019-11-30 20:52:27 +01005249 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005250 if (tabtab != NULL)
5251 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005252 *tabtab = stl_tabtab;
5253 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005254 for (l = 0; l < itemcnt; l++)
5255 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005256 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005257 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005258 sp->start = stl_items[l].stl_start;
5259 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005260 sp++;
5261 }
5262 }
5263 sp->start = NULL;
5264 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005267 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005268
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005269 // A user function may reset KeyTyped, restore it.
5270 KeyTyped = save_KeyTyped;
5271
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005272 // Check for an error. If there is one the display will be messed up and
5273 // might loop redrawing. Avoid that by making the corresponding option
5274 // empty.
5275 // TODO: find out why using called_emsg_before makes tests fail, does it
5276 // matter?
5277 // if (called_emsg > called_emsg_before)
5278 if (did_emsg > did_emsg_before)
5279 set_string_option_direct(opt_name, -1, (char_u *)"",
5280 OPT_FREE | opt_scope, SID_ERROR);
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 return width;
5283}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005284#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005287 * Get relative cursor position in window into "buf[buflen]", in the localized
5288 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
John Marriotta21240b2025-01-08 20:10:59 +01005290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291get_rel_pos(
5292 win_T *wp,
5293 char_u *buf,
5294 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005296 long above; // number of lines above window
5297 long below; // number of lines below window
John Marriotta21240b2025-01-08 20:10:59 +01005298 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaarc667da52019-11-30 20:52:27 +01005300 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005301 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 above = wp->w_topline - 1;
5303#ifdef FEAT_DIFF
5304 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005305 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005306 above = 0; // All buffer lines are displayed and there is an
5307 // indication of filler lines, that can be considered
5308 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309#endif
5310 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5311 if (below <= 0)
John Marriotta21240b2025-01-08 20:10:59 +01005312 len = vim_snprintf((char *)buf, buflen, "%s", (above == 0) ? _("All") : _("Bot"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 else if (above <= 0)
John Marriotta21240b2025-01-08 20:10:59 +01005314 len = vim_snprintf((char *)buf, buflen, "%s", _("Top"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005316 {
5317 int perc = (above > 1000000L)
John Marriotta21240b2025-01-08 20:10:59 +01005318 ? (int)(above / ((above + below) / 100L))
5319 : (int)(above * 100L / (above + below));
Emir SARI971cd2b2023-04-29 12:09:53 +01005320
Emir SARI971cd2b2023-04-29 12:09:53 +01005321 // localized percentage value
John Marriotta21240b2025-01-08 20:10:59 +01005322 len = vim_snprintf((char *)buf, buflen, _("%s%d%%"), (perc < 10) ? " " : "", perc);
Emir SARI971cd2b2023-04-29 12:09:53 +01005323 }
John Marriotta21240b2025-01-08 20:10:59 +01005324 if (len < 0)
5325 {
5326 buf[0] = NUL;
5327 len = 0;
5328 }
5329 else if (len > buflen - 1)
5330 len = buflen - 1;
5331
5332 return len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005336 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 * Return TRUE if it was appended.
5338 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005340append_arg_number(
5341 win_T *wp,
5342 char_u *buf,
5343 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005344 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005346 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 return FALSE;
5348
Bram Moolenaara8490a42023-05-23 18:00:58 +01005349 char *msg;
5350 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005352 case 0: msg = _(" (%d of %d)"); break;
5353 case 1: msg = _(" ((%d) of %d)"); break;
5354 case 2: msg = _(" (file %d of %d)"); break;
5355 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005357
5358 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5359 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5360 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 return TRUE;
5362}
5363
5364/*
5365 * If fname is not a full path, make it a full path.
5366 * Returns pointer to allocated memory (NULL for failure).
5367 */
5368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005369fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 /*
5372 * Force expanding the path always for Unix, because symbolic links may
5373 * mess up the full path name, even though it starts with a '/'.
5374 * Also expand when there is ".." in the file name, try to remove it,
5375 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005376 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * For MS-Windows also expand names like "longna~1" to "longname".
5378 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005379#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 return FullName_save(fname, TRUE);
5381#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005382 if (!vim_isAbsName(fname)
5383 || strstr((char *)fname, "..") != NULL
5384 || strstr((char *)fname, "//") != NULL
5385# ifdef BACKSLASH_IN_FILENAME
5386 || strstr((char *)fname, "\\\\") != NULL
5387# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005388# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 )
5392 return FullName_save(fname, FALSE);
5393
5394 fname = vim_strsave(fname);
5395
Bram Moolenaar9b942202007-10-03 12:31:33 +00005396# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005397 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005398 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401 return fname;
5402#endif
5403}
5404
5405/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005406 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5407 * "*ffname" becomes a pointer to allocated memory (or NULL).
5408 * When resolving a link both "*sfname" and "*ffname" will point to the same
5409 * allocated memory.
5410 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005411 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005414fname_expand(
5415 buf_T *buf UNUSED,
5416 char_u **ffname,
5417 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005419 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005421 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005423 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425#ifdef FEAT_SHORTCUT
5426 if (!buf->b_p_bin)
5427 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005428 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005430 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005431 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005432 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
5434 vim_free(*ffname);
5435 *ffname = rfname;
5436 *sfname = rfname;
5437 }
5438 }
5439#endif
5440}
5441
5442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * Open a window for a number of buffers.
5444 */
5445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005446ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 buf_T *buf;
5449 win_T *wp, *wpnext;
5450 int split_ret = OK;
5451 int p_ea_save;
5452 int open_wins = 0;
5453 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005454 int count; // Maximum number of windows to open.
5455 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005456 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005457 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
Bram Moolenaarc667da52019-11-30 20:52:27 +01005459 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 count = 9999;
5461 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005462 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5464 all = FALSE;
5465 else
5466 all = TRUE;
5467
Pavel Mayorove1121b12023-02-20 14:35:20 +00005468 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5469 // switching to another buffer.
5470 reset_VIsual_and_resel();
5471
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 setpcmark();
5473
5474#ifdef FEAT_GUI
5475 need_mouse_correct = TRUE;
5476#endif
5477
5478 /*
5479 * Close superfluous windows (two windows for the same buffer).
5480 * Also close windows that are not full-width.
5481 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005482 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005483 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005484 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005486 tpnext = curtab->tp_next;
5487 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005489 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005490 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005491 || ((cmdmod.cmod_split & WSP_VERT)
5492 ? wp->w_height + wp->w_status_height < Rows - p_ch
5493 - tabline_height()
5494 : wp->w_width != Columns)
5495 || (had_tab > 0 && wp != firstwin))
5496 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005497 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005498 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005499 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005500 if (win_close(wp, FALSE) == FAIL)
5501 break;
5502 // Just in case an autocommand does something strange with
5503 // windows: start all over...
5504 wpnext = firstwin;
5505 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005506 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005507 }
5508 else
5509 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005511
Bram Moolenaarc667da52019-11-30 20:52:27 +01005512 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005513 if (had_tab == 0 || tpnext == NULL)
5514 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005515 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517
5518 /*
5519 * Go through the buffer list. When a buffer doesn't have a window yet,
5520 * open one. Otherwise move the window to the right position.
5521 * Watch out for autocommands that delete buffers or windows!
5522 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005523 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5528 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005529 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5531 continue;
5532
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005533 if (had_tab != 0)
5534 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005535 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005536 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005537 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005538 else
5539 wp = NULL;
5540 }
5541 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005542 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005543 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005544 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005545 if (wp->w_buffer == buf)
5546 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005547 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005548 if (wp != NULL)
5549 win_move_after(wp, curwin);
5550 }
5551
5552 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005554 bufref_T bufref;
5555
5556 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005557
Bram Moolenaarc667da52019-11-30 20:52:27 +01005558 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005560 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5562 ++open_wins;
5563 p_ea = p_ea_save;
5564 if (split_ret == FAIL)
5565 continue;
5566
Bram Moolenaarc667da52019-11-30 20:52:27 +01005567 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005570 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005572 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 break;
5575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (swap_exists_action == SEA_QUIT)
5577 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005578#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005579 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005580
Bram Moolenaarc667da52019-11-30 20:52:27 +01005581 // Reset the error/interrupt/exception state here so that
5582 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005583 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005584#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005585
Bram Moolenaarc667da52019-11-30 20:52:27 +01005586 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 win_close(curwin, TRUE);
5588 --open_wins;
5589 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005590 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005591
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005592#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005593 // Restore the error/interrupt/exception state if not
5594 // discarded by a new aborting error, interrupt, or uncaught
5595 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005596 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 else
5600 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602
5603 ui_breakcheck();
5604 if (got_int)
5605 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005606 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005609#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005610 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005611 if (aborting())
5612 break;
5613#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005614 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005615 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005616 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005619 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622 /*
5623 * Close superfluous windows.
5624 */
5625 for (wp = lastwin; open_wins > count; )
5626 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005627 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (!win_valid(wp))
5630 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005631 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 wp = lastwin;
5633 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005634 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005636 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 --open_wins;
5638 wp = lastwin;
5639 }
5640 else
5641 {
5642 wp = wp->w_prev;
5643 if (wp == NULL)
5644 break;
5645 }
5646 }
5647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005650static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652/*
5653 * do_modelines() - process mode lines for the current file
5654 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005655 * "flags" can be:
5656 * OPT_WINONLY only set options local to window
5657 * OPT_NOWIN don't set options local to window
5658 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 * Returns immediately if the "ml" option isn't set.
5660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005662do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005664 linenr_T lnum;
5665 int nmlines;
5666 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5669 return;
5670
Bram Moolenaarc667da52019-11-30 20:52:27 +01005671 // Disallow recursive entry here. Can happen when executing a modeline
5672 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (entered)
5674 return;
5675
5676 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005677 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005679 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 nmlines = 0;
5681
Hu Jialun9dcd3492021-08-28 20:42:50 +02005682 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005684 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 nmlines = 0;
5686 --entered;
5687}
5688
Bram Moolenaarc667da52019-11-30 20:52:27 +01005689#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691/*
5692 * chk_modeline() - check a single line for a mode string
5693 * Return FAIL if an error encountered.
5694 */
5695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005696chk_modeline(
5697 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005698 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 char_u *s;
5701 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005702 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 int prev;
5704 int vers;
5705 int end;
5706 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005707 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005708 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
5710 prev = -1;
5711 for (s = ml_get(lnum); *s != NUL; ++s)
5712 {
5713 if (prev == -1 || vim_isspace(prev))
5714 {
5715 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5716 || STRNCMP(s, "vi:", (size_t)3) == 0)
5717 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005718 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005719 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5722 e = s + 4;
5723 else
5724 e = s + 3;
5725 vers = getdigits(&e);
5726 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005727 && (s[0] != 'V'
5728 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005730 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 || (VIM_VERSION_100 < vers && s[3] == '<')
5732 || (VIM_VERSION_100 > vers && s[3] == '>')
5733 || (VIM_VERSION_100 == vers && s[3] == '=')))
5734 break;
5735 }
5736 }
5737 prev = *s;
5738 }
5739
5740 if (*s)
5741 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005742 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 ++s;
5744 while (s[-1] != ':');
5745
Bram Moolenaarc667da52019-11-30 20:52:27 +01005746 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (linecopy == NULL)
5748 return FAIL;
5749
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005750 // prepare for emsg()
5751 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005752 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 end = FALSE;
5755 while (end == FALSE)
5756 {
5757 s = skipwhite(s);
5758 if (*s == NUL)
5759 break;
5760
5761 /*
5762 * Find end of set command: ':' or end of line.
5763 * Skip over "\:", replacing it with ":".
5764 */
5765 for (e = s; *e != ':' && *e != NUL; ++e)
5766 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005767 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (*e == NUL)
5769 end = TRUE;
5770
5771 /*
5772 * If there is a "set" command, require a terminating ':' and
5773 * ignore the stuff after the ':'.
5774 * "vi:set opt opt opt: foo" -- foo not interpreted
5775 * "vi:opt opt opt: foo" -- foo interpreted
5776 * Accept "se" for compatibility with Elvis.
5777 */
5778 if (STRNCMP(s, "set ", (size_t)4) == 0
5779 || STRNCMP(s, "se ", (size_t)3) == 0)
5780 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005781 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 break;
5783 end = TRUE;
5784 s = vim_strchr(s, ' ') + 1;
5785 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005786 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaarc667da52019-11-30 20:52:27 +01005788 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005790 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005791
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005792 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005793 current_sctx.sc_version = 1;
5794#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005795 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005796 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005797 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005799
Bram Moolenaar5958f952018-11-20 04:25:21 +01005800 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005801 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005802
Bram Moolenaara3227e22006-03-08 21:32:40 +00005803 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005804
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005805 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005806 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005807 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 break;
5809 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005810 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
5812
ichizok7e5fe382023-04-15 13:17:50 +01005813 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005814 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 vim_free(linecopy);
5816 }
5817 return retval;
5818}
5819
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005820/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005821 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5822 */
5823 int
5824bt_normal(buf_T *buf)
5825{
5826 return buf != NULL && buf->b_p_bt[0] == NUL;
5827}
5828
5829/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005830 * Return TRUE if "buf" is the quickfix buffer.
5831 */
5832 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005833bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005834{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005835#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005836 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005837#else
5838 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005839#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005840}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005841
5842/*
5843 * Return TRUE if "buf" is a terminal buffer.
5844 */
5845 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005846bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005847{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005848#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005849 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005850#else
5851 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005852#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005853}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005854
5855/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005856 * Return TRUE if "buf" is a help buffer.
5857 */
5858 int
5859bt_help(buf_T *buf)
5860{
5861 return buf != NULL && buf->b_help;
5862}
5863
5864/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005865 * Return TRUE if "buf" is a prompt buffer.
5866 */
5867 int
5868bt_prompt(buf_T *buf)
5869{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005870 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5871}
5872
Dominique Pelle748b3082022-01-08 12:41:16 +00005873#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005874/*
5875 * Return TRUE if "buf" is a buffer for a popup window.
5876 */
5877 int
5878bt_popup(buf_T *buf)
5879{
5880 return buf != NULL && buf->b_p_bt != NULL
5881 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005882}
Dominique Pelle748b3082022-01-08 12:41:16 +00005883#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005884
5885/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005886 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005887 * buffer. This means the buffer name may not be a file name, at least not for
5888 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005889 */
5890 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005891bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005892{
5893 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5894 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005895 || buf->b_p_bt[0] == 't'
5896 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005897}
5898
Bram Moolenaarc3126192022-08-26 12:58:17 +01005899/*
5900 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5901 * buffer. This means the buffer is not to be read from a file.
5902 */
5903 static int
5904bt_nofileread(buf_T *buf)
5905{
5906 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5907 || buf->b_p_bt[0] == 't'
5908 || buf->b_p_bt[0] == 'q'
5909 || buf->b_p_bt[0] == 'p');
5910}
5911
Dominique Pelle748b3082022-01-08 12:41:16 +00005912#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005913/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005914 * Return TRUE if "buf" has 'buftype' set to "nofile".
5915 */
5916 int
5917bt_nofile(buf_T *buf)
5918{
5919 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5920}
Dominique Pelle748b3082022-01-08 12:41:16 +00005921#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005922
5923/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005924 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5925 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005926 */
5927 int
5928bt_dontwrite(buf_T *buf)
5929{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005930 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005931 || buf->b_p_bt[0] == 't'
5932 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005933}
5934
5935 int
5936bt_dontwrite_msg(buf_T *buf)
5937{
5938 if (bt_dontwrite(buf))
5939 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005940 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005941 return TRUE;
5942 }
5943 return FALSE;
5944}
5945
5946/*
5947 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5948 * and 'bufhidden'.
5949 */
5950 int
5951buf_hide(buf_T *buf)
5952{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005953 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005954 switch (buf->b_p_bh[0])
5955 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005956 case 'u': // "unload"
5957 case 'w': // "wipe"
5958 case 'd': return FALSE; // "delete"
5959 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005960 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005961 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
5964/*
5965 * Return special buffer name.
5966 * Returns NULL when the buffer has a normal file name.
5967 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005968 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005971#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005973 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005974 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005975 * Differentiate between the quickfix and location list buffers using
5976 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005977 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005978 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005979 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005980 else
5981 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005984
Bram Moolenaarc667da52019-11-30 20:52:27 +01005985 // There is no _file_ when 'buftype' is "nofile", b_sfname
5986 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005987 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005989#ifdef FEAT_TERMINAL
5990 if (buf->b_term != NULL)
5991 return term_get_status_text(buf->b_term);
5992#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005993 if (buf->b_fname != NULL)
5994 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005995 if (buf == cmdwin_buf)
5996 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005997#ifdef FEAT_JOB_CHANNEL
5998 if (bt_prompt(buf))
5999 return (char_u *)_("[Prompt]");
6000#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006001#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006002 if (bt_popup(buf))
6003 return (char_u *)_("[Popup]");
6004#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006005 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006009 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 return NULL;
6011}
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006014 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6015 */
6016 char_u *
6017buf_get_fname(buf_T *buf)
6018{
6019 if (buf->b_fname == NULL)
6020 return (char_u *)_("[No Name]");
6021 return buf->b_fname;
6022}
6023
6024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6026 */
6027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006030 if (on == curbuf->b_p_bl)
6031 return;
6032
6033 curbuf->b_p_bl = on;
6034 if (on)
6035 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6036 else
6037 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038}
6039
6040/*
6041 * Read the file for "buf" again and check if the contents changed.
6042 * Return TRUE if it changed or this could not be checked.
6043 */
6044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 buf_T *newbuf;
6048 int differ = TRUE;
6049 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 exarg_T ea;
6052
Bram Moolenaarc667da52019-11-30 20:52:27 +01006053 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6055 if (newbuf == NULL)
6056 return TRUE;
6057
Bram Moolenaarc667da52019-11-30 20:52:27 +01006058 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (prep_exarg(&ea, buf) == FAIL)
6060 {
6061 wipe_buffer(newbuf, FALSE);
6062 return TRUE;
6063 }
6064
Bram Moolenaare76062c2022-11-28 18:51:43 +00006065 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006067 if (curbuf != newbuf)
6068 {
6069 // Failed to find a window for "newbuf".
6070 wipe_buffer(newbuf, FALSE);
6071 return TRUE;
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006074 // We don't want to trigger autocommands now, they may have nasty
6075 // side-effects like wiping buffers
6076 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006077 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 && readfile(buf->b_ffname, buf->b_fname,
6079 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6080 &ea, READ_NEW | READ_DUMMY) == OK)
6081 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006082 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6084 {
6085 differ = FALSE;
6086 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6087 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6088 {
6089 differ = TRUE;
6090 break;
6091 }
6092 }
6093 }
6094 vim_free(ea.cmd);
6095
Bram Moolenaarc667da52019-11-30 20:52:27 +01006096 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
Bram Moolenaarc667da52019-11-30 20:52:27 +01006099 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 wipe_buffer(newbuf, FALSE);
6101
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006102 unblock_autocmds();
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 return differ;
6105}
6106
6107/*
6108 * Wipe out a buffer and decrement the last buffer number if it was used for
6109 * this buffer. Call this to wipe out a temp buffer that does not contain any
6110 * marks.
6111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006113wipe_buffer(
6114 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006115 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 if (buf->b_fnum == top_file_num - 1)
6118 --top_file_num;
6119
Bram Moolenaarc667da52019-11-30 20:52:27 +01006120 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006121 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006122
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006123 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006126 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127}