blob: 68388db088818c7686058ad6397a0e61c3000914 [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);
2929
2930 if (!fuzzy)
2931 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002932#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002933 if (matches != NULL)
2934 {
2935 matches[count].buf = buf;
2936 matches[count].match = p;
2937 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002938 }
2939 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002940#endif
2941 (*file)[count++] = p;
2942 }
2943 else
2944 {
2945 fuzmatch[count].idx = count;
2946 fuzmatch[count].str = p;
2947 fuzmatch[count].score = score;
2948 count++;
2949 }
2950 }
2951 if (count == 0) // no match found, break here
2952 break;
2953 if (round == 1)
2954 {
2955 if (!fuzzy)
2956 {
2957 *file = ALLOC_MULT(char_u *, count);
2958 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002959 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002960 vim_regfree(regmatch.regprog);
2961 if (to_free)
2962 vim_free(patc);
2963 return FAIL;
2964 }
2965#ifdef FEAT_VIMINFO
2966 if (options & WILD_BUFLASTUSED)
2967 matches = ALLOC_MULT(bufmatch_T, count);
2968#endif
2969 }
2970 else
2971 {
2972 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2973 if (fuzmatch == NULL)
2974 {
2975 *num_file = 0;
2976 *file = NULL;
2977 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002983 if (!fuzzy)
2984 {
2985 vim_regfree(regmatch.regprog);
2986 if (to_free)
2987 vim_free(patc);
2988 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002989
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002990 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002991 {
826814741_6dff3c9c2024-12-10 17:15:14 +01002992#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002993 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002994 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002995 int i;
2996 if (count > 1)
2997 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2998 // if the current buffer is first in the list, place it at the end
2999 if (matches[0].buf == curbuf)
3000 {
3001 for (i = 1; i < count; i++)
3002 (*file)[i-1] = matches[i].match;
3003 (*file)[count-1] = matches[0].match;
3004 }
3005 else
3006 {
3007 for (i = 0; i < count; i++)
3008 (*file)[i] = matches[i].match;
3009 }
3010 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003011 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003012#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003013 }
3014 else
3015 {
3016 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3017 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003018 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 *num_file = count;
3021 return (count == 0 ? FAIL : OK);
3022}
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024/*
3025 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003026 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 */
3028 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003029buflist_match(
3030 regmatch_T *rmp,
3031 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003032 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 char_u *match;
3035
Bram Moolenaarc667da52019-11-30 20:52:27 +01003036 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003037 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003038 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003039 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 return match;
3042}
3043
3044/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003045 * Try matching the regexp in "rmp->regprog" with file name "name".
3046 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 * Return "name" when there is a match, NULL when not.
3048 */
3049 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003050fname_match(
3051 regmatch_T *rmp,
3052 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003053 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 char_u *match = NULL;
3056 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003058 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003059 if (name == NULL || rmp->regprog == NULL)
3060 return NULL;
3061
3062 // Ignore case when 'fileignorecase' or the argument is set.
3063 rmp->rm_ic = p_fic || ignore_case;
3064 if (vim_regexec(rmp, name, (colnr_T)0))
3065 match = name;
3066 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003068 // Replace $(HOME) with '~' and try matching again.
3069 p = home_replace_save(NULL, name);
3070 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003072 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074
3075 return match;
3076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003079 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 */
3081 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003084 char_u key[VIM_SIZEOF_INT * 2 + 1];
3085 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 if (nr == 0)
3088 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003089 sprintf((char *)key, "%x", nr);
3090 hi = hash_find(&buf_hashtab, key);
3091
3092 if (!HASHITEM_EMPTY(hi))
3093 return (buf_T *)(hi->hi_key
3094 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 return NULL;
3096}
3097
3098/*
3099 * Get name of file 'n' in the buffer list.
3100 * When the file has no name an empty string is returned.
3101 * home_replace() is used to shorten the file name (used for marks).
3102 * Returns a pointer to allocated memory, of NULL when failed.
3103 */
3104 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105buflist_nr2name(
3106 int n,
3107 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003108 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 buf_T *buf;
3111
3112 buf = buflist_findnr(n);
3113 if (buf == NULL)
3114 return NULL;
3115 return home_replace_save(helptail ? buf : NULL,
3116 fullname ? buf->b_ffname : buf->b_fname);
3117}
3118
3119/*
3120 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3121 * When "copy_options" is TRUE save the local window option values.
3122 * When "lnum" is 0 only do the options.
3123 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003125buflist_setfpos(
3126 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003127 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128 linenr_T lnum,
3129 colnr_T col,
3130 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 wininfo_T *wip;
3133
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003134 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (wip->wi_win == win)
3136 break;
3137 if (wip == NULL)
3138 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003139 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003140 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (wip == NULL)
3142 return;
3143 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003144 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 lnum = 1;
3146 }
3147 else
3148 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003149 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (wip->wi_prev)
3151 wip->wi_prev->wi_next = wip->wi_next;
3152 else
3153 buf->b_wininfo = wip->wi_next;
3154 if (wip->wi_next)
3155 wip->wi_next->wi_prev = wip->wi_prev;
3156 if (copy_options && wip->wi_optset)
3157 {
3158 clear_winopt(&wip->wi_opt);
3159#ifdef FEAT_FOLDING
3160 deleteFoldRecurse(&wip->wi_folds);
3161#endif
3162 }
3163 }
3164 if (lnum != 0)
3165 {
3166 wip->wi_fpos.lnum = lnum;
3167 wip->wi_fpos.col = col;
3168 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003169 if (win != NULL)
3170 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003171 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003173 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3175#ifdef FEAT_FOLDING
3176 wip->wi_fold_manual = win->w_fold_manual;
3177 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3178#endif
3179 wip->wi_optset = TRUE;
3180 }
3181
Bram Moolenaarc667da52019-11-30 20:52:27 +01003182 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 wip->wi_next = buf->b_wininfo;
3184 buf->b_wininfo = wip;
3185 wip->wi_prev = NULL;
3186 if (wip->wi_next)
3187 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188}
3189
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003190#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003191/*
3192 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3193 * page. That's because a diff is local to a tab page.
3194 */
3195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003196wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003197{
3198 win_T *wp;
3199
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003200 if (!wip->wi_opt.wo_diff)
3201 return FALSE;
3202
3203 FOR_ALL_WINDOWS(wp)
3204 // return FALSE when it's a window in the current tab page, thus
3205 // the buffer was in diff mode here
3206 if (wip->wi_win == wp)
3207 return FALSE;
3208 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003209}
3210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212/*
3213 * Find info for the current window in buffer "buf".
3214 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003215 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003216 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3217 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * Returns NULL when there isn't any info.
3219 */
3220 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221find_wininfo(
3222 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003223 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 wininfo_T *wip;
3227
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003228 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003229 if (wip->wi_win == curwin
3230#ifdef FEAT_DIFF
3231 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3232#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003233
3234 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003236
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003237 if (wip != NULL)
3238 return wip;
3239
Bram Moolenaarc667da52019-11-30 20:52:27 +01003240 // If no wininfo for curwin, use the first in the list (that doesn't have
3241 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003242 // If "need_options" is TRUE skip entries that don't have options set,
3243 // unless the window is editing "buf", so we can copy from the window
3244 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003245#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003246 if (skip_diff_buffer)
3247 {
3248 FOR_ALL_BUF_WININFO(buf, wip)
3249 if (!wininfo_other_tab_diff(wip)
3250 && (!need_options || wip->wi_optset
3251 || (wip->wi_win != NULL
3252 && wip->wi_win->w_buffer == buf)))
3253 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003254 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003255 else
3256#endif
3257 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 return wip;
3259}
3260
3261/*
3262 * Reset the local window options to the values last used in this window.
3263 * If the buffer wasn't used in this window before, use the values from
3264 * the most recently used window. If the values were never set, use the
3265 * global values for the window.
3266 */
3267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 wininfo_T *wip;
3271
3272 clear_winopt(&curwin->w_onebuf_opt);
3273#ifdef FEAT_FOLDING
3274 clearFolding(curwin);
3275#endif
3276
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003277 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003278 if (wip != NULL && wip->wi_win != NULL
3279 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003281 // The buffer is currently displayed in the window: use the actual
3282 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003283 win_T *wp = wip->wi_win;
3284
3285 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3286#ifdef FEAT_FOLDING
3287 curwin->w_fold_manual = wp->w_fold_manual;
3288 curwin->w_foldinvalid = TRUE;
3289 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3290#endif
3291 }
3292 else if (wip != NULL && wip->wi_optset)
3293 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003294 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3296#ifdef FEAT_FOLDING
3297 curwin->w_fold_manual = wip->wi_fold_manual;
3298 curwin->w_foldinvalid = TRUE;
3299 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3300#endif
3301 }
3302 else
3303 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003304 if (wip != NULL)
3305 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003308 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (p_fdls >= 0)
3310 curwin->w_p_fdl = p_fdls;
3311#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003312 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314
3315/*
3316 * Find the position (lnum and col) for the buffer 'buf' for the current
3317 * window.
3318 * Returns a pointer to no_position if no position is found.
3319 */
3320 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003324 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003326 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (wip != NULL)
3328 return &(wip->wi_fpos);
3329 else
3330 return &no_position;
3331}
3332
3333/*
3334 * Find the lnum for the buffer 'buf' for the current window.
3335 */
3336 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 return buflist_findfpos(buf)->lnum;
3340}
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003343 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003346buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
Bram Moolenaar52410572019-10-27 05:12:45 +01003348 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 int len;
3350 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003351 int ro_char;
3352 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003353#ifdef FEAT_TERMINAL
3354 int job_running;
3355 int job_none_open;
3356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar52410572019-10-27 05:12:45 +01003358#ifdef FEAT_VIMINFO
3359 garray_T buflist;
3360 buf_T **buflist_data = NULL, **p;
3361
3362 if (vim_strchr(eap->arg, 't'))
3363 {
3364 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003365 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003366 {
3367 if (ga_grow(&buflist, 1) == OK)
3368 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3369 }
3370
3371 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3372 sizeof(buf_T *), buf_compare);
3373
Bram Moolenaar3b991522019-11-06 23:26:20 +01003374 buflist_data = (buf_T **)buflist.ga_data;
3375 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003376 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003377 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003378
Bram Moolenaar3b991522019-11-06 23:26:20 +01003379 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003380 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3381 : buf->b_next)
3382#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003386#ifdef FEAT_TERMINAL
3387 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003388 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003389#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003390 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003391 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3392 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3393 || (vim_strchr(eap->arg, '+')
3394 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3395 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003396 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003397 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003398 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3399#ifdef FEAT_TERMINAL
3400 || (vim_strchr(eap->arg, 'R')
3401 && (!job_running || (job_running && job_none_open)))
3402 || (vim_strchr(eap->arg, '?')
3403 && (!job_running || (job_running && !job_none_open)))
3404 || (vim_strchr(eap->arg, 'F')
3405 && (job_running || buf->b_term == NULL))
3406#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003407 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3408 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3409 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3410 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3411 || (vim_strchr(eap->arg, '#')
3412 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003415 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 else
3417 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003418 if (message_filtered(NameBuff))
3419 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003421 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3422 : (bufIsChanged(buf) ? '+' : ' ');
3423#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003424 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003425 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003426 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003427 ro_char = '?';
3428 else
3429 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003430 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3431 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003432 }
3433 else if (buf->b_term != NULL)
3434 ro_char = 'F';
3435 else
3436#endif
3437 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3438
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003439 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003440 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 buf->b_fnum,
3442 buf->b_p_bl ? ' ' : 'u',
3443 buf == curbuf ? '%' :
3444 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3445 buf->b_ml.ml_mfp == NULL ? ' ' :
3446 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003447 ro_char,
3448 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003449 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003450 if (len > IOSIZE - 20)
3451 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaarc667da52019-11-30 20:52:27 +01003453 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 i = 40 - vim_strsize(IObuff);
3455 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003457 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003458#ifdef FEAT_VIMINFO
3459 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3460 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3461 else
3462#endif
3463 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3464 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003465 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003467 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 ui_breakcheck();
3469 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003470
3471#ifdef FEAT_VIMINFO
3472 if (buflist_data)
3473 ga_clear(&buflist);
3474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477/*
3478 * Get file name and line number for file 'fnum'.
3479 * Used by DoOneCmd() for translating '%' and '#'.
3480 * Used by insert_reg() and cmdline_paste() for '#' register.
3481 * Return FAIL if not found, OK for success.
3482 */
3483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484buflist_name_nr(
3485 int fnum,
3486 char_u **fname,
3487 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 buf_T *buf;
3490
3491 buf = buflist_findnr(fnum);
3492 if (buf == NULL || buf->b_fname == NULL)
3493 return FAIL;
3494
3495 *fname = buf->b_fname;
3496 *lnum = buflist_findlnum(buf);
3497
3498 return OK;
3499}
3500
3501/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003502 * Set the file name for "buf"' to "ffname_arg", short file name to
3503 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 * The file name with the full path is also remembered, for when :cd is used.
3505 * Returns FAIL for failure (file name already in use by other buffer)
3506 * OK otherwise.
3507 */
3508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003509setfname(
3510 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003511 char_u *ffname_arg,
3512 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003513 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003515 char_u *ffname = ffname_arg;
3516 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003517 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003519 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#endif
3521
3522 if (ffname == NULL || *ffname == NUL)
3523 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003524 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003525 if (buf->b_sfname != buf->b_ffname)
3526 VIM_CLEAR(buf->b_sfname);
3527 else
3528 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003529 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530#ifdef UNIX
3531 st.st_dev = (dev_T)-1;
3532#endif
3533 }
3534 else
3535 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003536 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3537 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return FAIL;
3539
3540 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003541 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 * - if the buffer is loaded, fail
3543 * - if the buffer is not loaded, delete it from the list
3544 */
3545#ifdef UNIX
3546 if (mch_stat((char *)ffname, &st) < 0)
3547 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003548#endif
3549 if (!(buf->b_flags & BF_DUMMY))
3550#ifdef UNIX
3551 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003553 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#endif
3555 if (obuf != NULL && obuf != buf)
3556 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003557 win_T *win;
3558 tabpage_T *tab;
3559 int in_use = FALSE;
3560
3561 // during startup a window may use a buffer that is not loaded yet
3562 FOR_ALL_TAB_WINDOWS(tab, win)
3563 if (win->w_buffer == obuf)
3564 in_use = TRUE;
3565
3566 // it's loaded or used in a window, fail
3567 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003570 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 vim_free(ffname);
3572 return FAIL;
3573 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003574 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003575 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 sfname = vim_strsave(sfname);
3578 if (ffname == NULL || sfname == NULL)
3579 {
3580 vim_free(sfname);
3581 vim_free(ffname);
3582 return FAIL;
3583 }
3584#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003585 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003587 if (buf->b_sfname != buf->b_ffname)
3588 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 buf->b_ffname = ffname;
3591 buf->b_sfname = sfname;
3592 }
3593 buf->b_fname = buf->b_sfname;
3594#ifdef UNIX
3595 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003596 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 else
3598 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003599 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 buf->b_dev = st.st_dev;
3601 buf->b_ino = st.st_ino;
3602 }
3603#endif
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 buf_name_changed(buf);
3608 return OK;
3609}
3610
3611/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003612 * Crude way of changing the name of a buffer. Use with care!
3613 * The name should be relative to the current directory.
3614 */
3615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003617{
3618 buf_T *buf;
3619
3620 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003621 if (buf == NULL)
3622 return;
3623
3624 if (buf->b_sfname != buf->b_ffname)
3625 vim_free(buf->b_sfname);
3626 vim_free(buf->b_ffname);
3627 buf->b_ffname = vim_strsave(name);
3628 buf->b_sfname = NULL;
3629 // Allocate ffname and expand into full path. Also resolves .lnk
3630 // files on Win32.
3631 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3632 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003633}
3634
3635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * Take care of what needs to be done when the name of buffer "buf" has
3637 * changed.
3638 */
3639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 /*
3643 * If the file name changed, also change the name of the swapfile
3644 */
3645 if (buf->b_ml.ml_mfp != NULL)
3646 ml_setname(buf);
3647
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003648#ifdef FEAT_TERMINAL
3649 if (buf->b_term != NULL)
3650 term_clear_status_text(buf->b_term);
3651#endif
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003654 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003655 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003656 status_redraw_all(); // status lines need to be redrawn
3657 fmarks_check_names(buf); // check named file marks
3658 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659}
3660
3661/*
3662 * set alternate file name for current window
3663 *
3664 * Used by do_one_cmd(), do_write() and do_ecmd().
3665 * Return the buffer.
3666 */
3667 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003668setaltfname(
3669 char_u *ffname,
3670 char_u *sfname,
3671 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 buf_T *buf;
3674
Bram Moolenaarc667da52019-11-30 20:52:27 +01003675 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003677 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 curwin->w_alt_fnum = buf->b_fnum;
3679 return buf;
3680}
3681
3682/*
3683 * Get alternate file name for current window.
3684 * Return NULL if there isn't any, and give error message if requested.
3685 */
3686 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003687getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003688 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 char_u *fname;
3691 linenr_T dummy;
3692
3693 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3694 {
3695 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003696 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return NULL;
3698 }
3699 return fname;
3700}
3701
3702/*
3703 * Add a file name to the buflist and return its number.
3704 * Uses same flags as buflist_new(), except BLN_DUMMY.
3705 *
3706 * used by qf_init(), main() and doarglist()
3707 */
3708 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003709buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710{
3711 buf_T *buf;
3712
3713 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3714 if (buf != NULL)
3715 return buf->b_fnum;
3716 return 0;
3717}
3718
3719#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3720/*
3721 * Adjust slashes in file names. Called after 'shellslash' was set.
3722 */
3723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003724buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 buf_T *bp;
3727
Bram Moolenaar29323592016-07-24 22:04:11 +02003728 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
3730 if (bp->b_ffname != NULL)
3731 slash_adjust(bp->b_ffname);
3732 if (bp->b_sfname != NULL)
3733 slash_adjust(bp->b_sfname);
3734 }
3735}
3736#endif
3737
3738/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003739 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 * Also save the local window option values.
3741 */
3742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003743buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003745 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746}
3747
3748/*
3749 * Return TRUE if 'ffname' is not the same file as current file.
3750 * Fname must have a full path (expanded by mch_FullName()).
3751 */
3752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003753otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754{
3755 return otherfile_buf(curbuf, ffname
3756#ifdef UNIX
3757 , NULL
3758#endif
3759 );
3760}
3761
3762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763otherfile_buf(
3764 buf_T *buf,
3765 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003767 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003771 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3773 return TRUE;
3774 if (fnamecmp(ffname, buf->b_ffname) == 0)
3775 return FALSE;
3776#ifdef UNIX
3777 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003778 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
Bram Moolenaarc667da52019-11-30 20:52:27 +01003780 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (stp == NULL)
3782 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003783 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 st.st_dev = (dev_T)-1;
3785 stp = &st;
3786 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003787 // Use dev/ino to check if the files are the same, even when the names
3788 // are different (possible with links). Still need to compare the
3789 // name above, for when the file doesn't exist yet.
3790 // Problem: The dev/ino changes when a file is deleted (and created
3791 // again) and remains the same when renamed/moved. We don't want to
3792 // mch_stat() each buffer each time, that would be too slow. Get the
3793 // dev/ino again when they appear to match, but not when they appear
3794 // to be different: Could skip a buffer when it's actually the same
3795 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (buf_same_ino(buf, stp))
3797 {
3798 buf_setino(buf);
3799 if (buf_same_ino(buf, stp))
3800 return FALSE;
3801 }
3802 }
3803#endif
3804 return TRUE;
3805}
3806
3807#if defined(UNIX) || defined(PROTO)
3808/*
3809 * Set inode and device number for a buffer.
3810 * Must always be called when b_fname is changed!.
3811 */
3812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003815 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3818 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003819 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 buf->b_dev = st.st_dev;
3821 buf->b_ino = st.st_ino;
3822 }
3823 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003824 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825}
3826
3827/*
3828 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3829 */
3830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003831buf_same_ino(
3832 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003833 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003835 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 && stp->st_dev == buf->b_dev
3837 && stp->st_ino == buf->b_ino);
3838}
3839#endif
3840
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003841/*
3842 * Print info about the current buffer.
3843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003846 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003847 int shorthelp,
3848 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849{
3850 char_u *name;
3851 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003852 char *p;
3853 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003854 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003856 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (buffer == NULL)
3858 return;
3859
Bram Moolenaarc667da52019-11-30 20:52:27 +01003860 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003862 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 p = buffer + STRLEN(buffer);
3864 }
3865 else
3866 p = buffer;
3867
3868 *p++ = '"';
3869 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003870 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
3872 {
3873 if (!fullname && curbuf->b_fname != NULL)
3874 name = curbuf->b_fname;
3875 else
3876 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003877 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 (int)(IOSIZE - (p - buffer)), TRUE);
3879 }
3880
Bram Moolenaar32526b32019-01-19 17:43:09 +01003881 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 curbufIsChanged() ? (shortmess(SHM_MOD)
3883 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003884 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003886 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003887 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003889 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 : _("[readonly]")) : "",
3891 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3892 || curbuf->b_p_ro) ?
3893 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003894 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3895 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (curwin->w_cursor.lnum > 1000000L)
3897 n = (int)(((long)curwin->w_cursor.lnum) /
3898 ((long)curbuf->b_ml.ml_line_count / 100L));
3899 else
3900 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3901 (long)curbuf->b_ml.ml_line_count);
3902 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003903 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003905 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003906 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003907 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3908 curbuf->b_ml.ml_line_count),
3909 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 else
3911 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003912 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 _("line %ld of %ld --%d%%-- col "),
3914 (long)curwin->w_cursor.lnum,
3915 (long)curbuf->b_ml.ml_line_count,
3916 n);
3917 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003918 len = STRLEN(buffer);
John Marriotta21240b2025-01-08 20:10:59 +01003919 (void)col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3921 }
3922
Bram Moolenaar32526b32019-01-19 17:43:09 +01003923 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3924 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 if (dont_truncate)
3927 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003928 // Temporarily set msg_scroll to avoid the message being truncated.
3929 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 msg_start();
3931 n = msg_scroll;
3932 msg_scroll = TRUE;
3933 msg(buffer);
3934 msg_scroll = n;
3935 }
3936 else
3937 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003938 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003940 // Need to repeat the message after redrawing when:
3941 // - When restart_edit is set (otherwise there will be a delay
3942 // before redrawing).
3943 // - When the screen was scrolled but there is no wait-return
3944 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003945 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
3948 vim_free(buffer);
3949}
3950
John Marriotta21240b2025-01-08 20:10:59 +01003951 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003952col_print(
3953 char_u *buf,
3954 size_t buflen,
3955 int col,
3956 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 if (col == vcol)
John Marriotta21240b2025-01-08 20:10:59 +01003959 return vim_snprintf((char *)buf, buflen, "%d", col);
3960
3961 return vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962}
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964static char_u *lasttitle = NULL;
3965static char_u *lasticon = NULL;
3966
Bram Moolenaar84a93082018-06-16 22:58:15 +02003967/*
3968 * Put the file name in the title bar and icon of the window.
3969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003974 char_u *title_str = NULL;
3975 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 int maxlen = 0;
3977 int len;
3978 int mustset;
3979 char_u buf[IOSIZE];
3980 int off;
3981
3982 if (!redrawing())
3983 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003984 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 need_maketitle = TRUE;
3986 return;
3987 }
3988
3989 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003990 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003991 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 if (p_title)
3994 {
3995 if (p_titlelen > 0)
3996 {
3997 maxlen = p_titlelen * Columns / 100;
3998 if (maxlen < 10)
3999 maxlen = 10;
4000 }
4001
Bram Moolenaar84a93082018-06-16 22:58:15 +02004002 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (*p_titlestring != NUL)
4004 {
4005#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004006 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004007 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4008 (char_u *)"titlestring", 0,
4009 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 else
4011#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004012 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014 else
4015 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004016 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Bram Moolenaar2c666692012-09-05 13:30:40 +02004018#define SPACE_FOR_FNAME (IOSIZE - 100)
4019#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004020#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02004022 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02004023#ifdef FEAT_TERMINAL
4024 else if (curbuf->b_term != NULL)
4025 {
4026 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
4027 SPACE_FOR_FNAME);
4028 }
4029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 else
4031 {
4032 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02004033 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
4036
Bram Moolenaar21554412017-07-24 21:44:43 +02004037#ifdef FEAT_TERMINAL
4038 if (curbuf->b_term == NULL)
4039#endif
4040 switch (bufIsChanged(curbuf)
4041 + (curbuf->b_p_ro * 2)
4042 + (!curbuf->b_p_ma * 4))
4043 {
4044 case 1: STRCAT(buf, " +"); break;
4045 case 2: STRCAT(buf, " ="); break;
4046 case 3: STRCAT(buf, " =+"); break;
4047 case 4:
4048 case 6: STRCAT(buf, " -"); break;
4049 case 5:
4050 case 7: STRCAT(buf, " -+"); break;
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaar21554412017-07-24 21:44:43 +02004053 if (curbuf->b_fname != NULL
4054#ifdef FEAT_TERMINAL
4055 && curbuf->b_term == NULL
4056#endif
4057 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004059 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 off = (int)STRLEN(buf);
4061 buf[off++] = ' ';
4062 buf[off++] = '(';
4063 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004064 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004066 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004067 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 off += 2;
4069#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004070 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004071 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004073 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004074 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004075 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004076 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004080
Bram Moolenaarc667da52019-11-30 20:52:27 +01004081 // Translate unprintable chars and concatenate. Keep some
4082 // room for the server name. When there is no room (very long
4083 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004084 if (off < SPACE_FOR_DIR)
4085 {
4086 p = transstr(buf + off);
4087 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4088 vim_free(p);
4089 }
4090 else
4091 {
4092 vim_strncpy(buf + off, (char_u *)"...",
4093 (size_t)(SPACE_FOR_ARGNR - off));
4094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 STRCAT(buf, ")");
4096 }
4097
Bram Moolenaar2c666692012-09-05 13:30:40 +02004098 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100#if defined(FEAT_CLIENTSERVER)
4101 if (serverName != NULL)
4102 {
4103 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004104 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 else
4107#endif
4108 STRCAT(buf, " - VIM");
4109
4110 if (maxlen > 0)
4111 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004112 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004113 if (vim_strsize(buf) > maxlen)
4114 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
4117 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004118 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 if (p_icon)
4121 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004122 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (*p_iconstring != NUL)
4124 {
4125#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004126 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004127 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4128 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else
4130#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004131 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 else
4134 {
4135 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004136 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004137 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004138 p = gettail(curbuf->b_ffname);
4139 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004140 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004141 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 if (len > 100)
4143 {
4144 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004146 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004147 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004149 STRCPY(icon_str, p);
4150 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 }
4153
Bram Moolenaar84a93082018-06-16 22:58:15 +02004154 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 if (mustset)
4157 resettitle();
4158}
4159
4160/*
4161 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4162 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004163 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 */
4165 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004166value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 if ((str == NULL) != (*last == NULL)
4169 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4170 {
4171 vim_free(*last);
4172 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004175 mch_restore_title(
4176 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004181 return TRUE;
4182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 return FALSE;
4185}
4186
4187/*
4188 * Put current window title back (used after calling a shell)
4189 */
4190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004191resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 mch_settitle(lasttitle, lasticon);
4194}
Bram Moolenaarea408852005-06-25 22:49:46 +00004195
4196# if defined(EXITFREE) || defined(PROTO)
4197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004199{
4200 vim_free(lasttitle);
4201 vim_free(lasticon);
4202}
4203# endif
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004206#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004207
4208/*
4209 * Used for building in the status line.
4210 */
4211typedef struct
4212{
4213 char_u *stl_start;
4214 int stl_minwid;
4215 int stl_maxwid;
4216 enum {
4217 Normal,
4218 Empty,
4219 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004220 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004221 Highlight,
4222 TabPage,
4223 Trunc
4224 } stl_type;
4225} stl_item_T;
4226
4227static size_t stl_items_len = 20; // Initial value, grows as needed.
4228static stl_item_T *stl_items = NULL;
4229static int *stl_groupitem = NULL;
4230static stl_hlrec_T *stl_hltab = NULL;
4231static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004232static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004233
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004235 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 * Return length of string in screen cells.
4237 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004238 * Normally works for window "wp", except when working for 'tabline' then it
4239 * is "curwin".
4240 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004242 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4244 *
4245 * If maxwidth is not zero, the string will be filled at any middle marker
4246 * or truncated if too long, fillchar is used for all whitespace.
4247 */
4248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249build_stl_str_hl(
4250 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004251 char_u *out, // buffer to write into != NameBuff
4252 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004253 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004254 char_u *opt_name, // option name corresponding to "fmt"
4255 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004256 int fillchar,
4257 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004258 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4259 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260{
Bram Moolenaar10772302019-01-20 18:25:54 +01004261 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004262 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 char_u *p;
4264 char_u *s;
4265 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004266 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004268 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004269 win_T *save_curwin;
4270 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004271 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#endif
4273 int empty_line;
4274 colnr_T virtcol;
4275 long l;
4276 long n;
4277 int prevchar_isflag;
4278 int prevchar_isitem;
4279 int itemisflag;
4280 int fillable;
4281 char_u *str;
4282 long num;
4283 int width;
4284 int itemcnt;
4285 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004286 int group_end_userhl;
4287 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004289#ifdef FEAT_EVAL
4290 int evaldepth;
4291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int minwid;
4293 int maxwid;
4294 int zeropad;
4295 char_u base;
4296 char_u opt;
4297#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004298 char_u buf_tmp[TMPLEN];
4299 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004300 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004301 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004302 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004303 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004304 // TODO: find out why using called_emsg_before makes tests fail, does it
4305 // matter?
4306 // int called_emsg_before = called_emsg;
4307 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004308
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004309 // When inside update_screen() we do not want redrawing a statusline,
4310 // ruler, title, etc. to trigger another redraw, it may cause an endless
4311 // loop.
4312 if (updating_screen)
4313 redraw_not_allowed = TRUE;
4314
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004315 if (stl_items == NULL)
4316 {
4317 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4318 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004319
4320 // Allocate one more, because the last element is used to indicate the
4321 // end of the list.
4322 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4323 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004324
4325 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004326 }
4327
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004328#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004329 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4330 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4331
4332 // When the format starts with "%!" then evaluate it as an expression and
4333 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004334 if (fmt[0] == '%' && fmt[1] == '!')
4335 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004336 typval_T tv;
4337
4338 tv.v_type = VAR_NUMBER;
4339 tv.vval.v_number = wp->w_id;
4340 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4341
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004342 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004343 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004344 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004345
4346 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004347 }
4348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 if (fillchar == 0)
4351 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004352
Bram Moolenaar10772302019-01-20 18:25:54 +01004353 // The cursor in windows other than the current one isn't always
4354 // up-to-date, esp. because of autocommands and timers.
4355 lnum = wp->w_cursor.lnum;
4356 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4357 {
4358 lnum = wp->w_buffer->b_ml.ml_line_count;
4359 wp->w_cursor.lnum = lnum;
4360 }
4361
4362 // Get line & check if empty (cursorpos will show "0-1"). Note that
4363 // p will become invalid when getting another buffer line.
4364 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004365 empty_line = (*p == NUL);
4366
Bram Moolenaar10772302019-01-20 18:25:54 +01004367 // Get the byte value now, in case we need it below. This is more efficient
4368 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004369 len = ml_get_buf_len(wp->w_buffer, lnum);
4370 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004371 {
4372 // Line may have changed since checking the cursor column, or the lnum
4373 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004374 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004375 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004376 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004377 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004378 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004379 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004382#ifdef FEAT_EVAL
4383 evaldepth = 0;
4384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 p = out;
4386 curitem = 0;
4387 prevchar_isflag = TRUE;
4388 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004389 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004391 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004392 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004393 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004394
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004395 stl_item_T *new_items =
4396 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004397 if (new_items == NULL)
4398 break;
4399 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004400
4401 int *new_groupitem =
4402 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004403 if (new_groupitem == NULL)
4404 break;
4405 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004406
4407 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004408 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004409 if (new_hlrec == NULL)
4410 break;
4411 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004412 new_hlrec = vim_realloc(stl_tabtab,
4413 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004414 if (new_hlrec == NULL)
4415 break;
4416 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004417
4418 int *new_separator_locs = vim_realloc(stl_separator_locations,
4419 sizeof(int) * new_len);
4420 if (new_separator_locs == NULL)
4421 break;
4422 stl_separator_locations = new_separator_locs;;
4423
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004425 }
4426
zeertzjq122dea72022-07-27 15:48:45 +01004427 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 prevchar_isflag = prevchar_isitem = FALSE;
4429
4430 /*
4431 * Handle up to the next '%' or the end.
4432 */
4433 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4434 *p++ = *s++;
4435 if (*s == NUL || p + 1 >= out + outlen)
4436 break;
4437
4438 /*
4439 * Handle one '%' item.
4440 */
4441 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004442 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if (*s == '%')
4445 {
4446 if (p + 1 >= out + outlen)
4447 break;
4448 *p++ = *s++;
4449 prevchar_isflag = prevchar_isitem = FALSE;
4450 continue;
4451 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004452 // STL_SEPARATE: Separation between items, filled with white space.
4453 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 s++;
4456 if (groupdepth > 0)
4457 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004458 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004459 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 continue;
4461 }
4462 if (*s == STL_TRUNCMARK)
4463 {
4464 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004465 stl_items[curitem].stl_type = Trunc;
4466 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 continue;
4468 }
4469 if (*s == ')')
4470 {
4471 s++;
4472 if (groupdepth < 1)
4473 continue;
4474 groupdepth--;
4475
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004476 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 *p = NUL;
4478 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004479 if (curitem > stl_groupitem[groupdepth] + 1
4480 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004482 // remove group if all items are empty and highlight group
4483 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004484 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004485 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004486 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004487 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004488 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004489 group_start_userhl = group_end_userhl =
4490 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004491 break;
4492 }
4493 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004494 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004495 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004496 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004498 if (stl_items[n].stl_type == Highlight)
4499 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004500 }
4501 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004503 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 p = t;
4505 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004506 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004507 {
4508 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004509 if (stl_items[n].stl_type == Highlight)
4510 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004511 // adjust the start position of TabPage to the next
4512 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004513 if (stl_items[n].stl_type == TabPage)
4514 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004518 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004520 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (has_mbyte)
4522 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004523 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004525 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004528 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 }
4531 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004532 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4533 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004536 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004538
4539 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004540 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004541 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
Bram Moolenaarc667da52019-11-30 20:52:27 +01004543 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004544 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
LemonBoy57ff5262022-05-09 21:03:47 +01004546 // Minus one for the leading '<' added above.
4547 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004548 if (stl_items[l].stl_start < t)
4549 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004552 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004554 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004555 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (n < 0)
4557 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004558 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 n = 0 - n;
4560 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004561 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 else
4564 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004565 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004566 l = (n - l) * MB_CHAR2LEN(fillchar);
4567 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004569 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004571 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4572 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004574 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576 }
4577 continue;
4578 }
4579 minwid = 0;
4580 maxwid = 9999;
4581 zeropad = FALSE;
4582 l = 1;
4583 if (*s == '0')
4584 {
4585 s++;
4586 zeropad = TRUE;
4587 }
4588 if (*s == '-')
4589 {
4590 s++;
4591 l = -1;
4592 }
4593 if (VIM_ISDIGIT(*s))
4594 {
4595 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004596 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 minwid = 0;
4598 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004599 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004601 stl_items[curitem].stl_type = Highlight;
4602 stl_items[curitem].stl_start = p;
4603 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 s++;
4605 curitem++;
4606 continue;
4607 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004608 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4609 {
4610 if (*s == STL_TABCLOSENR)
4611 {
4612 if (minwid == 0)
4613 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004614 // %X ends the close label, go back to the previously
4615 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004616 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004617 if (stl_items[n].stl_type == TabPage
4618 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004619 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004620 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004621 break;
4622 }
4623 }
4624 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004625 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004626 minwid = - minwid;
4627 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004628 stl_items[curitem].stl_type = TabPage;
4629 stl_items[curitem].stl_start = p;
4630 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004631 s++;
4632 curitem++;
4633 continue;
4634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (*s == '.')
4636 {
4637 s++;
4638 if (VIM_ISDIGIT(*s))
4639 {
4640 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004641 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 maxwid = 50;
4643 }
4644 }
4645 minwid = (minwid > 50 ? 50 : minwid) * l;
4646 if (*s == '(')
4647 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004648 stl_groupitem[groupdepth++] = curitem;
4649 stl_items[curitem].stl_type = Group;
4650 stl_items[curitem].stl_start = p;
4651 stl_items[curitem].stl_minwid = minwid;
4652 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 s++;
4654 curitem++;
4655 continue;
4656 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004657#ifdef FEAT_EVAL
4658 // Denotes end of expanded %{} block
4659 if (*s == '}' && evaldepth > 0)
4660 {
4661 s++;
4662 evaldepth--;
4663 continue;
4664 }
4665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (vim_strchr(STL_ALL, *s) == NULL)
4667 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004668 if (*s == NUL) // can happen with "%0"
4669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 s++;
4671 continue;
4672 }
4673 opt = *s++;
4674
Bram Moolenaarc667da52019-11-30 20:52:27 +01004675 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 base = 'D';
4677 itemisflag = FALSE;
4678 fillable = TRUE;
4679 num = -1;
4680 str = NULL;
4681 switch (opt)
4682 {
4683 case STL_FILEPATH:
4684 case STL_FULLPATH:
4685 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004686 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004688 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 else
4690 {
4691 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004692 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4694 }
4695 trans_characters(NameBuff, MAXPATHL);
4696 if (opt != STL_FILENAME)
4697 str = NameBuff;
4698 else
4699 str = gettail(NameBuff);
4700 break;
4701
Bram Moolenaarc667da52019-11-30 20:52:27 +01004702 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004703 {
4704#ifdef FEAT_EVAL
4705 char_u *block_start = s - 1;
4706#endif
4707 int reevaluate = (*s == '%');
4708
4709 if (reevaluate)
4710 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 itemisflag = TRUE;
4712 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004713 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4714 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004716 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 break;
4718 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004719 if (reevaluate)
4720 p[-1] = 0; // remove the % at the end of %{% expr %}
4721 else
4722 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004725 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4726 "%d", curbuf->b_fnum);
4727 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4728 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4729 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004731 save_curbuf = curbuf;
4732 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004733 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 curwin = wp;
4735 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004736 // Visual mode is only valid in the current window.
4737 if (curwin != save_curwin)
4738 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004740 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004742 curwin = save_curwin;
4743 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004744 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004745 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004746 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 if (str != NULL && *str != 0)
4749 {
4750 if (*skipdigits(str) == NUL)
4751 {
4752 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004753 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 itemisflag = FALSE;
4755 }
4756 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004757
4758 // If the output of the expression needs to be evaluated
4759 // replace the %{} block with the result of evaluation
4760 if (reevaluate && str != NULL && *str != 0
4761 && strchr((const char *)str, '%') != NULL
4762 && evaldepth < MAX_STL_EVAL_DEPTH)
4763 {
4764 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4765 size_t str_length = strlen((const char *)str);
4766 size_t fmt_length = strlen((const char *)s);
4767 size_t new_fmt_len = parsed_usefmt
4768 + str_length + fmt_length + 3;
4769 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4770 char_u *new_fmt_p = new_fmt;
4771
4772 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4773 + parsed_usefmt;
4774 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4775 + str_length;
4776 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4777 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4778 + fmt_length;
4779 *new_fmt_p = 0;
4780 new_fmt_p = NULL;
4781
4782 if (usefmt != fmt)
4783 vim_free(usefmt);
4784 VIM_CLEAR(str);
4785 usefmt = new_fmt;
4786 s = usefmt + parsed_usefmt;
4787 evaldepth++;
4788 continue;
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#endif
4791 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 case STL_LINE:
4794 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4795 ? 0L : (long)(wp->w_cursor.lnum);
4796 break;
4797
4798 case STL_NUMLINES:
4799 num = wp->w_buffer->b_ml.ml_line_count;
4800 break;
4801
4802 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004803 num = (State & MODE_INSERT) == 0 && empty_line
4804 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 break;
4806
4807 case STL_VIRTCOL:
4808 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004809 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004810 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004812 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4813 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 break;
4815 num = (long)virtcol;
4816 break;
4817
4818 case STL_PERCENTAGE:
4819 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4820 (long)wp->w_buffer->b_ml.ml_line_count);
4821 break;
4822
4823 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004824 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004825 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 break;
4827
Luuk van Baalba936f62022-12-15 13:15:39 +00004828 case STL_SHOWCMD:
4829 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4830 str = showcmd_buf;
4831 break;
4832
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 case STL_ARGLISTSTAT:
4834 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004835 buf_tmp[0] = 0;
4836 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4837 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 break;
4839
4840 case STL_KEYMAP:
4841 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004842 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004843 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 break;
4845 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004846#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004847 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#else
4849 num = 0;
4850#endif
4851 break;
4852
4853 case STL_BUFNO:
4854 num = wp->w_buffer->b_fnum;
4855 break;
4856
4857 case STL_OFFSET_X:
4858 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004859 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 case STL_OFFSET:
4861#ifdef FEAT_BYTEOFF
4862 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004863 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4864 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4865 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#endif
4867 break;
4868
4869 case STL_BYTEVAL_X:
4870 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004871 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004873 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (num == NL)
4875 num = 0;
4876 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4877 num = NL;
4878 break;
4879
4880 case STL_ROFLAG:
4881 case STL_ROFLAG_ALT:
4882 itemisflag = TRUE;
4883 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004884 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 break;
4886
4887 case STL_HELPFLAG:
4888 case STL_HELPFLAG_ALT:
4889 itemisflag = TRUE;
4890 if (wp->w_buffer->b_help)
4891 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004892 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 break;
4894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 case STL_FILETYPE:
4896 if (*wp->w_buffer->b_p_ft != NUL
4897 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4898 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004899 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004900 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004901 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 break;
4904
4905 case STL_FILETYPE_ALT:
4906 itemisflag = TRUE;
4907 if (*wp->w_buffer->b_p_ft != NUL
4908 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4909 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004910 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004911 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004912 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004914 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
Bram Moolenaar4033c552017-09-16 20:54:51 +02004918#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 case STL_PREVIEWFLAG:
4920 case STL_PREVIEWFLAG_ALT:
4921 itemisflag = TRUE;
4922 if (wp->w_p_pvw)
4923 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4924 : _("[Preview]"));
4925 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004926
4927 case STL_QUICKFIX:
4928 if (bt_quickfix(wp->w_buffer))
4929 str = (char_u *)(wp->w_llist_ref
4930 ? _(msg_loclist)
4931 : _(msg_qflist));
4932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933#endif
4934
4935 case STL_MODIFIED:
4936 case STL_MODIFIED_ALT:
4937 itemisflag = TRUE;
4938 switch ((opt == STL_MODIFIED_ALT)
4939 + bufIsChanged(wp->w_buffer) * 2
4940 + (!wp->w_buffer->b_p_ma) * 4)
4941 {
4942 case 2: str = (char_u *)"[+]"; break;
4943 case 3: str = (char_u *)",+"; break;
4944 case 4: str = (char_u *)"[-]"; break;
4945 case 5: str = (char_u *)",-"; break;
4946 case 6: str = (char_u *)"[+-]"; break;
4947 case 7: str = (char_u *)",+-"; break;
4948 }
4949 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004950
4951 case STL_HIGHLIGHT:
4952 t = s;
4953 while (*s != '#' && *s != NUL)
4954 ++s;
4955 if (*s == '#')
4956 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004957 stl_items[curitem].stl_type = Highlight;
4958 stl_items[curitem].stl_start = p;
4959 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004960 curitem++;
4961 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004962 if (*s != NUL)
4963 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004964 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004967 stl_items[curitem].stl_start = p;
4968 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (str != NULL && *str)
4970 {
4971 t = str;
4972 if (itemisflag)
4973 {
4974 if ((t[0] && t[1])
4975 && ((!prevchar_isitem && *t == ',')
4976 || (prevchar_isflag && *t == ' ')))
4977 t++;
4978 prevchar_isflag = TRUE;
4979 }
4980 l = vim_strsize(t);
4981 if (l > 0)
4982 prevchar_isitem = TRUE;
4983 if (l > maxwid)
4984 {
4985 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (has_mbyte)
4987 {
4988 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004989 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 l -= byte2cells(*t++);
4993 if (p + 1 >= out + outlen)
4994 break;
4995 *p++ = '<';
4996 }
4997 if (minwid > 0)
4998 {
4999 for (; l < minwid && p + 1 < out + outlen; l++)
5000 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005001 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5003 *p++ = ' ';
5004 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005005 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007 minwid = 0;
5008 }
5009 else
5010 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005011 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005013 // Change a space by fillchar, unless fillchar is '-' and a
5014 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005015 if (fillable && *t == ' '
5016 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5017 MB_CHAR2BYTES(fillchar, p);
5018 else
5019 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005022 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 else if (num >= 0)
5025 {
5026 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5027 char_u nstr[20];
5028
5029 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005030 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 prevchar_isitem = TRUE;
5032 t = nstr;
5033 if (opt == STL_VIRTCOL_ALT)
5034 {
5035 *t++ = '-';
5036 minwid--;
5037 }
5038 *t++ = '%';
5039 if (zeropad)
5040 *t++ = '0';
5041 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005042 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 *t = 0;
5044
5045 for (n = num, l = 1; n >= nbase; n /= nbase)
5046 l++;
5047 if (opt == STL_VIRTCOL_ALT)
5048 l++;
5049 if (l > maxwid)
5050 {
5051 l += 2;
5052 n = l - maxwid;
5053 while (l-- > maxwid)
5054 num /= nbase;
5055 *t++ = '>';
5056 *t++ = '%';
5057 *t = t[-3];
5058 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005059 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5060 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005063 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5064 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 p += STRLEN(p);
5066 }
5067 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005068 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005070 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5071 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5072 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (opt == STL_VIM_EXPR)
5074 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 curitem++;
5076 }
5077 *p = NUL;
5078 itemcnt = curitem;
5079
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005080#ifdef FEAT_EVAL
5081 if (usefmt != fmt)
5082 vim_free(usefmt);
5083#endif
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 width = vim_strsize(out);
5086 if (maxwidth > 0 && width > maxwidth)
5087 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005088 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 l = 0;
5090 if (itemcnt == 0)
5091 s = out;
5092 else
5093 {
5094 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005095 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005097 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005098 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 break;
5100 }
5101 if (l == itemcnt)
5102 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005103 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005104 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 l = 0;
5106 }
5107 }
5108
5109 if (width - vim_strsize(s) >= maxwidth)
5110 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005111 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (has_mbyte)
5113 {
5114 s = out;
5115 width = 0;
5116 for (;;)
5117 {
5118 width += ptr2cells(s);
5119 if (width >= maxwidth)
5120 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005121 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005123 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005125 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 s = out + maxwidth - 1;
5129 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005130 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 break;
5132 itemcnt = l;
5133 *s++ = '>';
5134 *s = 0;
5135 }
5136 else
5137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (has_mbyte)
5139 {
5140 n = 0;
5141 while (width >= maxwidth)
5142 {
5143 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005144 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 }
5147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 n = width - maxwidth + 1;
5149 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005150 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 *s = '<';
5152
Bram Moolenaarc667da52019-11-30 20:52:27 +01005153 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 for (; l < itemcnt; l++)
5155 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005156 if (stl_items[l].stl_start - n >= s)
5157 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005159 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
zeertzjqd392a742023-07-01 20:24:40 +01005161
5162 // Fill up for half a double-wide character.
5163 while (++width < maxwidth)
5164 {
5165 s = s + STRLEN(s);
5166 MB_CHAR2BYTES(fillchar, s);
5167 *s = NUL;
5168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 width = maxwidth;
5171 }
5172 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5173 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005174 // Find how many separators there are, which we will use when
5175 // figuring out how many groups there are.
5176 int num_separators = 0;
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005180 if (stl_items[l].stl_type == Separate)
5181 {
5182 // Create an array of the start location for each separator
5183 // mark.
5184 stl_separator_locations[num_separators] = l;
5185 num_separators++;
5186 }
5187 }
5188
5189 // If we have separated groups, then we deal with it now
5190 if (num_separators)
5191 {
5192 int standard_spaces;
5193 int final_spaces;
5194
5195 standard_spaces = (maxwidth - width) / num_separators;
5196 final_spaces = (maxwidth - width) -
5197 standard_spaces * (num_separators - 1);
5198 for (l = 0; l < num_separators; l++)
5199 {
5200 int dislocation = (l == (num_separators - 1)) ?
5201 final_spaces : standard_spaces;
5202 dislocation *= MB_CHAR2LEN(fillchar);
5203 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5204 char_u *seploc = start + dislocation;
5205 STRMOVE(seploc, start);
5206 for (s = start; s < seploc;)
5207 MB_CHAR2BYTES(fillchar, s);
5208
5209 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5210 stl_items[i].stl_start += dislocation;
5211 }
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 width = maxwidth;
5214 }
5215 }
5216
Bram Moolenaarc667da52019-11-30 20:52:27 +01005217 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005218 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005220 *hltab = stl_hltab;
5221 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 for (l = 0; l < itemcnt; l++)
5223 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005224 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005226 sp->start = stl_items[l].stl_start;
5227 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005228 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005231 sp->start = NULL;
5232 sp->userhl = 0;
5233 }
5234
Bram Moolenaarc667da52019-11-30 20:52:27 +01005235 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005236 if (tabtab != NULL)
5237 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005238 *tabtab = stl_tabtab;
5239 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005240 for (l = 0; l < itemcnt; l++)
5241 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005242 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005243 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005244 sp->start = stl_items[l].stl_start;
5245 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005246 sp++;
5247 }
5248 }
5249 sp->start = NULL;
5250 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005253 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005254
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005255 // A user function may reset KeyTyped, restore it.
5256 KeyTyped = save_KeyTyped;
5257
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005258 // Check for an error. If there is one the display will be messed up and
5259 // might loop redrawing. Avoid that by making the corresponding option
5260 // empty.
5261 // TODO: find out why using called_emsg_before makes tests fail, does it
5262 // matter?
5263 // if (called_emsg > called_emsg_before)
5264 if (did_emsg > did_emsg_before)
5265 set_string_option_direct(opt_name, -1, (char_u *)"",
5266 OPT_FREE | opt_scope, SID_ERROR);
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 return width;
5269}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005270#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005273 * Get relative cursor position in window into "buf[buflen]", in the localized
5274 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 */
John Marriotta21240b2025-01-08 20:10:59 +01005276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005277get_rel_pos(
5278 win_T *wp,
5279 char_u *buf,
5280 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005282 long above; // number of lines above window
5283 long below; // number of lines below window
John Marriotta21240b2025-01-08 20:10:59 +01005284 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaarc667da52019-11-30 20:52:27 +01005286 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005287 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 above = wp->w_topline - 1;
5289#ifdef FEAT_DIFF
5290 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005291 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005292 above = 0; // All buffer lines are displayed and there is an
5293 // indication of filler lines, that can be considered
5294 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
5296 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5297 if (below <= 0)
John Marriotta21240b2025-01-08 20:10:59 +01005298 len = vim_snprintf((char *)buf, buflen, "%s", (above == 0) ? _("All") : _("Bot"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else if (above <= 0)
John Marriotta21240b2025-01-08 20:10:59 +01005300 len = vim_snprintf((char *)buf, buflen, "%s", _("Top"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005302 {
5303 int perc = (above > 1000000L)
John Marriotta21240b2025-01-08 20:10:59 +01005304 ? (int)(above / ((above + below) / 100L))
5305 : (int)(above * 100L / (above + below));
Emir SARI971cd2b2023-04-29 12:09:53 +01005306
Emir SARI971cd2b2023-04-29 12:09:53 +01005307 // localized percentage value
John Marriotta21240b2025-01-08 20:10:59 +01005308 len = vim_snprintf((char *)buf, buflen, _("%s%d%%"), (perc < 10) ? " " : "", perc);
Emir SARI971cd2b2023-04-29 12:09:53 +01005309 }
John Marriotta21240b2025-01-08 20:10:59 +01005310 if (len < 0)
5311 {
5312 buf[0] = NUL;
5313 len = 0;
5314 }
5315 else if (len > buflen - 1)
5316 len = buflen - 1;
5317
5318 return len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
5321/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005322 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 * Return TRUE if it was appended.
5324 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005326append_arg_number(
5327 win_T *wp,
5328 char_u *buf,
5329 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005330 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005332 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 return FALSE;
5334
Bram Moolenaara8490a42023-05-23 18:00:58 +01005335 char *msg;
5336 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005338 case 0: msg = _(" (%d of %d)"); break;
5339 case 1: msg = _(" ((%d) of %d)"); break;
5340 case 2: msg = _(" (file %d of %d)"); break;
5341 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005343
5344 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5345 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5346 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 return TRUE;
5348}
5349
5350/*
5351 * If fname is not a full path, make it a full path.
5352 * Returns pointer to allocated memory (NULL for failure).
5353 */
5354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005355fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 /*
5358 * Force expanding the path always for Unix, because symbolic links may
5359 * mess up the full path name, even though it starts with a '/'.
5360 * Also expand when there is ".." in the file name, try to remove it,
5361 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005362 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 * For MS-Windows also expand names like "longna~1" to "longname".
5364 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005365#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 return FullName_save(fname, TRUE);
5367#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005368 if (!vim_isAbsName(fname)
5369 || strstr((char *)fname, "..") != NULL
5370 || strstr((char *)fname, "//") != NULL
5371# ifdef BACKSLASH_IN_FILENAME
5372 || strstr((char *)fname, "\\\\") != NULL
5373# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005374# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 )
5378 return FullName_save(fname, FALSE);
5379
5380 fname = vim_strsave(fname);
5381
Bram Moolenaar9b942202007-10-03 12:31:33 +00005382# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005383 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005384 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387 return fname;
5388#endif
5389}
5390
5391/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005392 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5393 * "*ffname" becomes a pointer to allocated memory (or NULL).
5394 * When resolving a link both "*sfname" and "*ffname" will point to the same
5395 * allocated memory.
5396 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005397 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005400fname_expand(
5401 buf_T *buf UNUSED,
5402 char_u **ffname,
5403 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005405 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005407 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005409 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411#ifdef FEAT_SHORTCUT
5412 if (!buf->b_p_bin)
5413 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005414 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005416 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005417 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005418 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 vim_free(*ffname);
5421 *ffname = rfname;
5422 *sfname = rfname;
5423 }
5424 }
5425#endif
5426}
5427
5428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * Open a window for a number of buffers.
5430 */
5431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005432ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434 buf_T *buf;
5435 win_T *wp, *wpnext;
5436 int split_ret = OK;
5437 int p_ea_save;
5438 int open_wins = 0;
5439 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005440 int count; // Maximum number of windows to open.
5441 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005442 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005443 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaarc667da52019-11-30 20:52:27 +01005445 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 count = 9999;
5447 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005448 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5450 all = FALSE;
5451 else
5452 all = TRUE;
5453
Pavel Mayorove1121b12023-02-20 14:35:20 +00005454 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5455 // switching to another buffer.
5456 reset_VIsual_and_resel();
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 setpcmark();
5459
5460#ifdef FEAT_GUI
5461 need_mouse_correct = TRUE;
5462#endif
5463
5464 /*
5465 * Close superfluous windows (two windows for the same buffer).
5466 * Also close windows that are not full-width.
5467 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005468 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005469 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005470 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005472 tpnext = curtab->tp_next;
5473 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005475 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005476 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005477 || ((cmdmod.cmod_split & WSP_VERT)
5478 ? wp->w_height + wp->w_status_height < Rows - p_ch
5479 - tabline_height()
5480 : wp->w_width != Columns)
5481 || (had_tab > 0 && wp != firstwin))
5482 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005483 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005484 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005485 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005486 if (win_close(wp, FALSE) == FAIL)
5487 break;
5488 // Just in case an autocommand does something strange with
5489 // windows: start all over...
5490 wpnext = firstwin;
5491 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005492 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005493 }
5494 else
5495 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005497
Bram Moolenaarc667da52019-11-30 20:52:27 +01005498 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005499 if (had_tab == 0 || tpnext == NULL)
5500 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005501 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
5503
5504 /*
5505 * Go through the buffer list. When a buffer doesn't have a window yet,
5506 * open one. Otherwise move the window to the right position.
5507 * Watch out for autocommands that delete buffers or windows!
5508 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005509 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005515 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5517 continue;
5518
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005519 if (had_tab != 0)
5520 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005521 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005522 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005523 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005524 else
5525 wp = NULL;
5526 }
5527 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005528 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005529 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005530 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005531 if (wp->w_buffer == buf)
5532 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005533 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005534 if (wp != NULL)
5535 win_move_after(wp, curwin);
5536 }
5537
5538 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005540 bufref_T bufref;
5541
5542 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005543
Bram Moolenaarc667da52019-11-30 20:52:27 +01005544 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005546 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5548 ++open_wins;
5549 p_ea = p_ea_save;
5550 if (split_ret == FAIL)
5551 continue;
5552
Bram Moolenaarc667da52019-11-30 20:52:27 +01005553 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005556 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005558 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 break;
5561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 if (swap_exists_action == SEA_QUIT)
5563 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005564#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005565 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005566
Bram Moolenaarc667da52019-11-30 20:52:27 +01005567 // Reset the error/interrupt/exception state here so that
5568 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005569 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005570#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005571
Bram Moolenaarc667da52019-11-30 20:52:27 +01005572 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 win_close(curwin, TRUE);
5574 --open_wins;
5575 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005576 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005577
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005578#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005579 // Restore the error/interrupt/exception state if not
5580 // discarded by a new aborting error, interrupt, or uncaught
5581 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005582 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 else
5586 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588
5589 ui_breakcheck();
5590 if (got_int)
5591 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005592 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 break;
5594 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005595#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005596 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005597 if (aborting())
5598 break;
5599#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005600 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005601 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005602 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005605 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608 /*
5609 * Close superfluous windows.
5610 */
5611 for (wp = lastwin; open_wins > count; )
5612 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005613 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 if (!win_valid(wp))
5616 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005617 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 wp = lastwin;
5619 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005620 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005622 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 --open_wins;
5624 wp = lastwin;
5625 }
5626 else
5627 {
5628 wp = wp->w_prev;
5629 if (wp == NULL)
5630 break;
5631 }
5632 }
5633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005636static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638/*
5639 * do_modelines() - process mode lines for the current file
5640 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005641 * "flags" can be:
5642 * OPT_WINONLY only set options local to window
5643 * OPT_NOWIN don't set options local to window
5644 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 * Returns immediately if the "ml" option isn't set.
5646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005648do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005650 linenr_T lnum;
5651 int nmlines;
5652 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5655 return;
5656
Bram Moolenaarc667da52019-11-30 20:52:27 +01005657 // Disallow recursive entry here. Can happen when executing a modeline
5658 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (entered)
5660 return;
5661
5662 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005663 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005665 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 nmlines = 0;
5667
Hu Jialun9dcd3492021-08-28 20:42:50 +02005668 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005670 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 nmlines = 0;
5672 --entered;
5673}
5674
Bram Moolenaarc667da52019-11-30 20:52:27 +01005675#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677/*
5678 * chk_modeline() - check a single line for a mode string
5679 * Return FAIL if an error encountered.
5680 */
5681 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005682chk_modeline(
5683 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005684 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 char_u *s;
5687 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005688 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 int prev;
5690 int vers;
5691 int end;
5692 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005693 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005694 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
5696 prev = -1;
5697 for (s = ml_get(lnum); *s != NUL; ++s)
5698 {
5699 if (prev == -1 || vim_isspace(prev))
5700 {
5701 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5702 || STRNCMP(s, "vi:", (size_t)3) == 0)
5703 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005704 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005705 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5708 e = s + 4;
5709 else
5710 e = s + 3;
5711 vers = getdigits(&e);
5712 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005713 && (s[0] != 'V'
5714 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005716 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 || (VIM_VERSION_100 < vers && s[3] == '<')
5718 || (VIM_VERSION_100 > vers && s[3] == '>')
5719 || (VIM_VERSION_100 == vers && s[3] == '=')))
5720 break;
5721 }
5722 }
5723 prev = *s;
5724 }
5725
5726 if (*s)
5727 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005728 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 ++s;
5730 while (s[-1] != ':');
5731
Bram Moolenaarc667da52019-11-30 20:52:27 +01005732 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (linecopy == NULL)
5734 return FAIL;
5735
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005736 // prepare for emsg()
5737 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005738 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 end = FALSE;
5741 while (end == FALSE)
5742 {
5743 s = skipwhite(s);
5744 if (*s == NUL)
5745 break;
5746
5747 /*
5748 * Find end of set command: ':' or end of line.
5749 * Skip over "\:", replacing it with ":".
5750 */
5751 for (e = s; *e != ':' && *e != NUL; ++e)
5752 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005753 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (*e == NUL)
5755 end = TRUE;
5756
5757 /*
5758 * If there is a "set" command, require a terminating ':' and
5759 * ignore the stuff after the ':'.
5760 * "vi:set opt opt opt: foo" -- foo not interpreted
5761 * "vi:opt opt opt: foo" -- foo interpreted
5762 * Accept "se" for compatibility with Elvis.
5763 */
5764 if (STRNCMP(s, "set ", (size_t)4) == 0
5765 || STRNCMP(s, "se ", (size_t)3) == 0)
5766 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005767 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 break;
5769 end = TRUE;
5770 s = vim_strchr(s, ' ') + 1;
5771 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005772 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
Bram Moolenaarc667da52019-11-30 20:52:27 +01005774 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005776 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005777
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005778 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005779 current_sctx.sc_version = 1;
5780#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005781 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005782 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005783 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005785
Bram Moolenaar5958f952018-11-20 04:25:21 +01005786 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005787 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005788
Bram Moolenaara3227e22006-03-08 21:32:40 +00005789 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005790
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005791 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005792 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005793 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005796 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
5798
ichizok7e5fe382023-04-15 13:17:50 +01005799 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005800 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 vim_free(linecopy);
5802 }
5803 return retval;
5804}
5805
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005806/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005807 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5808 */
5809 int
5810bt_normal(buf_T *buf)
5811{
5812 return buf != NULL && buf->b_p_bt[0] == NUL;
5813}
5814
5815/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005816 * Return TRUE if "buf" is the quickfix buffer.
5817 */
5818 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005819bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005820{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005821#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005822 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005823#else
5824 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005825#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005826}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005827
5828/*
5829 * Return TRUE if "buf" is a terminal buffer.
5830 */
5831 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005832bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005833{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005834#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005835 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005836#else
5837 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005838#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005839}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005840
5841/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005842 * Return TRUE if "buf" is a help buffer.
5843 */
5844 int
5845bt_help(buf_T *buf)
5846{
5847 return buf != NULL && buf->b_help;
5848}
5849
5850/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005851 * Return TRUE if "buf" is a prompt buffer.
5852 */
5853 int
5854bt_prompt(buf_T *buf)
5855{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005856 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5857}
5858
Dominique Pelle748b3082022-01-08 12:41:16 +00005859#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005860/*
5861 * Return TRUE if "buf" is a buffer for a popup window.
5862 */
5863 int
5864bt_popup(buf_T *buf)
5865{
5866 return buf != NULL && buf->b_p_bt != NULL
5867 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005868}
Dominique Pelle748b3082022-01-08 12:41:16 +00005869#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005870
5871/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005872 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005873 * buffer. This means the buffer name may not be a file name, at least not for
5874 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005875 */
5876 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005877bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005878{
5879 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5880 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005881 || buf->b_p_bt[0] == 't'
5882 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005883}
5884
Bram Moolenaarc3126192022-08-26 12:58:17 +01005885/*
5886 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5887 * buffer. This means the buffer is not to be read from a file.
5888 */
5889 static int
5890bt_nofileread(buf_T *buf)
5891{
5892 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5893 || buf->b_p_bt[0] == 't'
5894 || buf->b_p_bt[0] == 'q'
5895 || buf->b_p_bt[0] == 'p');
5896}
5897
Dominique Pelle748b3082022-01-08 12:41:16 +00005898#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005899/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005900 * Return TRUE if "buf" has 'buftype' set to "nofile".
5901 */
5902 int
5903bt_nofile(buf_T *buf)
5904{
5905 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5906}
Dominique Pelle748b3082022-01-08 12:41:16 +00005907#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005908
5909/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005910 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5911 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005912 */
5913 int
5914bt_dontwrite(buf_T *buf)
5915{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005916 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005917 || buf->b_p_bt[0] == 't'
5918 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005919}
5920
5921 int
5922bt_dontwrite_msg(buf_T *buf)
5923{
5924 if (bt_dontwrite(buf))
5925 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005926 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005927 return TRUE;
5928 }
5929 return FALSE;
5930}
5931
5932/*
5933 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5934 * and 'bufhidden'.
5935 */
5936 int
5937buf_hide(buf_T *buf)
5938{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005939 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005940 switch (buf->b_p_bh[0])
5941 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005942 case 'u': // "unload"
5943 case 'w': // "wipe"
5944 case 'd': return FALSE; // "delete"
5945 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005946 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005947 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005948}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950/*
5951 * Return special buffer name.
5952 * Returns NULL when the buffer has a normal file name.
5953 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005954 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005955buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005957#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005959 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005960 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005961 * Differentiate between the quickfix and location list buffers using
5962 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005963 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005964 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005965 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005966 else
5967 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005970
Bram Moolenaarc667da52019-11-30 20:52:27 +01005971 // There is no _file_ when 'buftype' is "nofile", b_sfname
5972 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005973 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005975#ifdef FEAT_TERMINAL
5976 if (buf->b_term != NULL)
5977 return term_get_status_text(buf->b_term);
5978#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005979 if (buf->b_fname != NULL)
5980 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005981 if (buf == cmdwin_buf)
5982 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005983#ifdef FEAT_JOB_CHANNEL
5984 if (bt_prompt(buf))
5985 return (char_u *)_("[Prompt]");
5986#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005987#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005988 if (bt_popup(buf))
5989 return (char_u *)_("[Popup]");
5990#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005991 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005995 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 return NULL;
5997}
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006000 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6001 */
6002 char_u *
6003buf_get_fname(buf_T *buf)
6004{
6005 if (buf->b_fname == NULL)
6006 return (char_u *)_("[No Name]");
6007 return buf->b_fname;
6008}
6009
6010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6012 */
6013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006016 if (on == curbuf->b_p_bl)
6017 return;
6018
6019 curbuf->b_p_bl = on;
6020 if (on)
6021 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6022 else
6023 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024}
6025
6026/*
6027 * Read the file for "buf" again and check if the contents changed.
6028 * Return TRUE if it changed or this could not be checked.
6029 */
6030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 buf_T *newbuf;
6034 int differ = TRUE;
6035 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 exarg_T ea;
6038
Bram Moolenaarc667da52019-11-30 20:52:27 +01006039 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6041 if (newbuf == NULL)
6042 return TRUE;
6043
Bram Moolenaarc667da52019-11-30 20:52:27 +01006044 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (prep_exarg(&ea, buf) == FAIL)
6046 {
6047 wipe_buffer(newbuf, FALSE);
6048 return TRUE;
6049 }
6050
Bram Moolenaare76062c2022-11-28 18:51:43 +00006051 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006053 if (curbuf != newbuf)
6054 {
6055 // Failed to find a window for "newbuf".
6056 wipe_buffer(newbuf, FALSE);
6057 return TRUE;
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006060 // We don't want to trigger autocommands now, they may have nasty
6061 // side-effects like wiping buffers
6062 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006063 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 && readfile(buf->b_ffname, buf->b_fname,
6065 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6066 &ea, READ_NEW | READ_DUMMY) == OK)
6067 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006068 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6070 {
6071 differ = FALSE;
6072 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6073 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6074 {
6075 differ = TRUE;
6076 break;
6077 }
6078 }
6079 }
6080 vim_free(ea.cmd);
6081
Bram Moolenaarc667da52019-11-30 20:52:27 +01006082 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
Bram Moolenaarc667da52019-11-30 20:52:27 +01006085 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 wipe_buffer(newbuf, FALSE);
6087
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006088 unblock_autocmds();
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 return differ;
6091}
6092
6093/*
6094 * Wipe out a buffer and decrement the last buffer number if it was used for
6095 * this buffer. Call this to wipe out a temp buffer that does not contain any
6096 * marks.
6097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099wipe_buffer(
6100 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006101 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 if (buf->b_fnum == top_file_num - 1)
6104 --top_file_num;
6105
Bram Moolenaarc667da52019-11-30 20:52:27 +01006106 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006107 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006108
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006109 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006112 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113}