blob: a925552199a513e626b27b3b1924f45cb0d660e6 [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)
278 retval = read_buffer(FALSE, eap, flags);
279 }
280#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100281 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifdef FEAT_NETBEANS_INTG
283 netbeansFireChanges = oldFire;
284#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100285 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200286 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 fix_help_buffer();
288 }
289 else if (read_stdin)
290 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200291 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100293 // First read the text in binary mode into the buffer.
294 // Then read from that same buffer and append at the end. This makes
295 // it possible to retry when 'fileformat' or 'fileencoding' was
296 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 curbuf->b_p_bin = TRUE;
298 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200299 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
300 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 curbuf->b_p_bin = save_bin;
302 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200303 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100306 // Can now sync this buffer in ml_sync_all().
307 if (curbuf->b_ml.ml_mfp != NULL
308 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
309 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
310
Bram Moolenaarc667da52019-11-30 20:52:27 +0100311 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100315 parse_cino(curbuf);
316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100318 // Set/reset the Changed flag first, autocmds may change the buffer.
319 // Apply the automatic commands, before processing the modelines.
320 // So the modelines have priority over autocommands.
321 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100322 // When reading stdin, the buffer contents always needs writing, so set
323 // the changed flag. Unless in readonly mode: "ls | gview -".
324 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000325 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200326 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100327#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000330 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100332 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200333 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100334 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaarc667da52019-11-30 20:52:27 +0100336 // Set last_changedtick to avoid triggering a TextChanged autocommand right
337 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100338 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100339 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100340 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100341
Bram Moolenaarc667da52019-11-30 20:52:27 +0100342 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifdef FEAT_EVAL
344 if (aborting())
345#else
346 if (got_int)
347#endif
348 curbuf->b_flags |= BF_READERR;
349
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000350#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100351 // Need to update automatic folding. Do this before the autocommands,
352 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000353 foldUpdateAll(curwin);
354#endif
355
Bram Moolenaarc667da52019-11-30 20:52:27 +0100356 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (!(curwin->w_valid & VALID_TOPLINE))
358 {
359 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100360#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100364#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#endif
369
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000370 if (retval != OK)
371 return retval;
372
373 // The autocommands may have changed the current buffer. Apply the
374 // modelines to the correct buffer, if it still exists and is loaded.
375 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000377 aco_save_T aco;
378
379 // Go to the buffer that was opened, make sure it is in a window.
380 // If not then skip it.
381 aucmd_prepbuf(&aco, old_curbuf.br_buf);
382 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000384 do_modelines(0);
385 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000387 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100388#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000389 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
390 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100391#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000392 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
393 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000396 // restore curwin/curbuf and a few other things
397 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 return retval;
402}
403
404/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200405 * Store "buf" in "bufref" and set the free count.
406 */
407 void
408set_bufref(bufref_T *bufref, buf_T *buf)
409{
410 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200411 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200412 bufref->br_buf_free_count = buf_free_count;
413}
414
415/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200416 * Return TRUE if "bufref->br_buf" points to the same buffer as when
417 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200418 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200419 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
420 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200421 */
422 int
423bufref_valid(bufref_T *bufref)
424{
425 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200426 ? TRUE : buf_valid(bufref->br_buf)
427 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200428}
429
430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200432 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 */
434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100435buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 buf_T *bp;
438
Bram Moolenaarc667da52019-11-30 20:52:27 +0100439 // Assume that we more often have a recent buffer, start with the last
440 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200441 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (bp == buf)
443 return TRUE;
444 return FALSE;
445}
446
447/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200448 * A hash table used to quickly lookup a buffer by its number.
449 */
450static hashtab_T buf_hashtab;
451
452 static void
453buf_hashtab_add(buf_T *buf)
454{
455 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000456 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000457 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200458}
459
460 static void
461buf_hashtab_remove(buf_T *buf)
462{
463 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
464
465 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000466 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200467}
468
Bram Moolenaar94f01952018-09-01 15:30:03 +0200469/*
470 * Return TRUE when buffer "buf" can be unloaded.
471 * Give an error message and return FALSE when the buffer is locked or the
472 * screen is being redrawn and the buffer is in a window.
473 */
474 static int
475can_unload_buffer(buf_T *buf)
476{
477 int can_unload = !buf->b_locked;
478
479 if (can_unload && updating_screen)
480 {
481 win_T *wp;
482
483 FOR_ALL_WINDOWS(wp)
484 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200485 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200486 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200487 break;
488 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200489 }
490 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100491 {
492 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
493
494 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
495 fname != NULL ? fname : (char_u *)"[No Name]");
496 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200497 return can_unload;
498}
Bram Moolenaara997b452018-04-17 23:24:06 +0200499
Christian Brabandt51b62382024-10-06 17:31:10 +0200500 int
501buf_locked(buf_T *buf)
502{
503 return buf->b_locked || buf->b_locked_split;
504}
505
Bram Moolenaar480778b2016-07-14 22:09:39 +0200506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 * Close the link to a buffer.
508 * "action" is used when there is no longer a window for the buffer.
509 * It can be:
510 * 0 buffer becomes hidden
511 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100512 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200514 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * When doing all but the first one on the current buffer, the caller should
516 * get a new buffer very soon!
517 *
518 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100519 *
520 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
521 * cause there to be only one window with this buffer. e.g. when ":quit" is
522 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100523 *
524 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100525 *
526 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100529close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100530 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100531 buf_T *buf,
532 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100533 int abort_if_last,
534 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100537 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200538 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100539 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200540 win_T *the_curwin = curwin;
541 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200543 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
544 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
Bram Moolenaarcee52202020-03-11 14:19:58 +0100546 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100547
548 // Force unloading or deleting when 'bufhidden' says so.
549 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
550 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100551 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200552 {
553 del_buf = TRUE;
554 unload_buf = TRUE;
555 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100556 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200557 {
558 del_buf = TRUE;
559 unload_buf = TRUE;
560 wipe_buf = TRUE;
561 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100562 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200563 unload_buf = TRUE;
564
Bram Moolenaar94053a52017-08-01 21:44:33 +0200565#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100566 // depending on how we get here b_nwindows may already be zero
567 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200568 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100569 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200570 if (term_job_running(buf->b_term))
571 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200572 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200573 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200574 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100575 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200576
Bram Moolenaarc667da52019-11-30 20:52:27 +0100577 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200578 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100579
580 // A terminal buffer is wiped out when job has finished.
581 del_buf = TRUE;
582 unload_buf = TRUE;
583 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200584 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200585 else
586 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100587 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200588 del_buf = FALSE;
589 unload_buf = FALSE;
590 }
591 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100592 else if (buf->b_p_bh[0] == 'h' && !del_buf)
593 {
594 // Hide a terminal buffer.
595 unload_buf = FALSE;
596 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200597 else
598 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100599 if (del_buf || unload_buf)
600 {
601 // A terminal buffer is wiped out if the job has finished.
602 // We only do this when there's an intention to unload the
603 // buffer. This way, :hide and other similar commands won't
604 // wipe the buffer.
605 del_buf = TRUE;
606 unload_buf = TRUE;
607 wipe_buf = TRUE;
608 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200609 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100610 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200611 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 // Disallow deleting the buffer when it is locked (already being closed or
615 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200616 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100617 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200618
Bram Moolenaarc667da52019-11-30 20:52:27 +0100619 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200620 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100622 // Set b_last_cursor when closing the last window for the buffer.
623 // Remember the last cursor position and window options of the buffer.
624 // This used to be only for the current window, but then options like
625 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (buf->b_nwindows == 1)
627 set_last_cursor(win);
628 buflist_setfpos(buf, win,
629 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
630 win->w_cursor.col, TRUE);
631 }
632
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200633 set_bufref(&bufref, buf);
634
Bram Moolenaarc667da52019-11-30 20:52:27 +0100635 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (buf->b_nwindows == 1)
637 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200638 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100639 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200640 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
641 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200642 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100643 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100644 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200645aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000646 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100647 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100648 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200649 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100650 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200651 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100652 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200653 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaarc667da52019-11-30 20:52:27 +0100655 // When the buffer becomes hidden, but is not unloaded, trigger
656 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (!unload_buf)
658 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200659 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100660 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200661 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
662 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200663 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100664 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200665 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200666 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100667 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200668 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100669 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200670 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100672#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100673 // autocmds may abort script processing
674 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100675 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200678
Bram Moolenaarc667da52019-11-30 20:52:27 +0100679 // If the buffer was in curwin and the window has changed, go back to that
680 // window, if it still exists. This avoids that ":edit x" triggering a
681 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200682 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
683 {
684 block_autocmds();
685 goto_tabpage_win(the_curtab, the_curwin);
686 unblock_autocmds();
687 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
Bram Moolenaarc667da52019-11-30 20:52:27 +0100691 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 if (buf->b_nwindows > 0)
693 --buf->b_nwindows;
694
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100695#ifdef FEAT_DIFF
696 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100697 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100698#endif
699
Bram Moolenaarc667da52019-11-30 20:52:27 +0100700 // Return when a window is displaying the buffer or when it's not
701 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100703 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaarc667da52019-11-30 20:52:27 +0100705 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (buf->b_ffname == NULL)
707 del_buf = TRUE;
708
Bram Moolenaarc667da52019-11-30 20:52:27 +0100709 // When closing the current buffer stop Visual mode before freeing
710 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200711 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200712#if defined(EXITFREE)
713 && !entered_free_all_mem
714#endif
715 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200716 end_visual_mode();
717
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100718 // Free all things allocated for this buffer.
719 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
720 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100721 // Remember if we are closing the current buffer. Restore the number of
722 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 is_curbuf = (buf == curbuf);
724 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100726 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100727 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100728 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
Bram Moolenaarc667da52019-11-30 20:52:27 +0100730 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200731 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100732 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100733#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100734 // autocmds may abort script processing
735 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100736 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100739 // It's possible that autocommands change curbuf to the one being deleted.
740 // This might cause the previous curbuf to be deleted unexpectedly. But
741 // in some cases it's OK to delete the curbuf, because a new one is
742 // obtained anyway. Therefore only return if curbuf changed to the
743 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100745 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200746
Bram Moolenaar4033c552017-09-16 20:54:51 +0200747 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100748 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200749
Bram Moolenaarc667da52019-11-30 20:52:27 +0100750 // Autocommands may have opened or closed windows for this buffer.
751 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200752 if (buf->b_nwindows > 0)
753 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /*
756 * Remove the buffer from the list.
757 */
758 if (wipe_buf)
759 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200760 tabpage_T *tp;
761 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200762
Bram Moolenaar347538f2022-03-26 16:28:06 +0000763 // Do not wipe out the buffer if it is used in a window.
764 if (buf->b_nwindows > 0)
765 return FALSE;
766
zeertzjq2e7d89b2024-07-10 19:36:36 +0200767 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200768 mark_forget_file(wp, buf->b_fnum);
769
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200770 if (action == DOBUF_WIPE_REUSE)
771 {
772 // we can re-use this buffer number, store it
773 if (buf_reuse.ga_itemsize == 0)
774 ga_init2(&buf_reuse, sizeof(int), 50);
775 if (ga_grow(&buf_reuse, 1) == OK)
776 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
777 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200778 if (buf->b_sfname != buf->b_ffname)
779 VIM_CLEAR(buf->b_sfname);
780 else
781 buf->b_sfname = NULL;
782 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (buf->b_prev == NULL)
784 firstbuf = buf->b_next;
785 else
786 buf->b_prev->b_next = buf->b_next;
787 if (buf->b_next == NULL)
788 lastbuf = buf->b_prev;
789 else
790 buf->b_next->b_prev = buf->b_prev;
791 free_buffer(buf);
792 }
793 else
794 {
795 if (del_buf)
796 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100797 // Free all internal variables and reset option values, to make
798 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 free_buffer_stuff(buf, TRUE);
800
Bram Moolenaarc667da52019-11-30 20:52:27 +0100801 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
803
Bram Moolenaarc667da52019-11-30 20:52:27 +0100804 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 buf->b_p_initialized = FALSE;
806 }
807 buf_clear_file(buf);
808 if (del_buf)
809 buf->b_p_bl = FALSE;
810 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100811 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100812 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813}
814
815/*
816 * Make buffer not contain a file.
817 */
818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100819buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200822 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100824 buf->b_p_eof = FALSE;
825 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 buf->b_p_eol = TRUE;
827 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000829 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100831 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#ifdef FEAT_NETBEANS_INTG
833 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif
835}
836
837/*
838 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200839 * the file. Careful: get here with "curwin" NULL when exiting.
840 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100841 * BFA_DEL buffer is going to be deleted
842 * BFA_WIPE buffer is going to be wiped out
843 * BFA_KEEP_UNDO do not free undo information
844 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200850 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200851 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200852 win_T *the_curwin = curwin;
853 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaarc667da52019-11-30 20:52:27 +0100855 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200856 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100857 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200858 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200859 if (buf->b_ml.ml_mfp != NULL)
860 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200861 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
862 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200863 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100864 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200865 return;
866 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200867 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200869 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
870 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200871 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100872 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 return;
874 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200875 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200877 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
878 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200879 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100880 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 return;
882 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200883 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100884 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200885
Bram Moolenaarc667da52019-11-30 20:52:27 +0100886 // If the buffer was in curwin and the window has changed, go back to that
887 // window, if it still exists. This avoids that ":edit x" triggering a
888 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200889 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
890 {
891 block_autocmds();
892 goto_tabpage_win(the_curtab, the_curwin);
893 unblock_autocmds();
894 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200895
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100896#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100897 // autocmds may abort script processing
898 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100902 // It's possible that autocommands change curbuf to the one being deleted.
903 // This might cause curbuf to be deleted unexpectedly. But in some cases
904 // it's OK to delete the curbuf, because a new one is obtained anyway.
905 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (buf == curbuf && !is_curbuf)
907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200911#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100912 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200913 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100914 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200915#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000916
917#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100918 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000919 {
920 win_T *win;
921 tabpage_T *tp;
922
923 FOR_ALL_TAB_WINDOWS(tp, win)
924 if (win->w_buffer == buf)
925 clearFolding(win);
926 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000927#endif
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929#ifdef FEAT_TCL
930 tcl_buffer_free(buf);
931#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100932 ml_close(buf, TRUE); // close and delete the memline/memfile
933 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200934 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100935 // free the memory allocated for undo
936 // and reset all undo information
937 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100939 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100941#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100942 clear_buf_prop_types(buf);
943#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100944 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945}
946
947/*
948 * Free a buffer structure and the things it contains related to the buffer
949 * itself (not the file, that must have been done already).
950 */
951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100952free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200954 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200956#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100957 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000958 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200959 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200960 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200961#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200962#ifdef FEAT_LUA
963 lua_buffer_free(buf);
964#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000965#ifdef FEAT_MZSCHEME
966 mzscheme_buffer_free(buf);
967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_PERL
969 perl_buf_free(buf);
970#endif
971#ifdef FEAT_PYTHON
972 python_buffer_free(buf);
973#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974#ifdef FEAT_PYTHON3
975 python3_buffer_free(buf);
976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#ifdef FEAT_RUBY
978 ruby_buffer_free(buf);
979#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200980#ifdef FEAT_JOB_CHANNEL
981 channel_buffer_free(buf);
982#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200983#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200984 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200985#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200986#ifdef FEAT_JOB_CHANNEL
987 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200988 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200989 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200990#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200991
992 buf_hashtab_remove(buf);
993
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200994 aubuflocal_remove(buf);
995
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200996 if (autocmd_busy)
997 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100998 // Do not free the buffer structure while autocommands are executing,
999 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001000 buf->b_next = au_pending_free_buf;
1001 au_pending_free_buf = buf;
1002 }
1003 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001004 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001005 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001006 if (curbuf == buf)
1007 curbuf = NULL; // make clear it's not to be used
1008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001012 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001013 */
1014 static void
1015init_changedtick(buf_T *buf)
1016{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001017 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001018
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001019 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1020 di->di_tv.v_type = VAR_NUMBER;
1021 di->di_tv.v_lock = VAR_FIXED;
1022 di->di_tv.vval.v_number = 0;
1023
Bram Moolenaar92769c32017-02-25 15:41:37 +01001024#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001025 STRCPY(buf->b_ct_di.di_key, "changedtick");
1026 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001027#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001028}
1029
1030/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001031 * Free the b_wininfo list for buffer "buf".
1032 */
1033 static void
1034clear_wininfo(buf_T *buf)
1035{
1036 wininfo_T *wip;
1037
1038 while (buf->b_wininfo != NULL)
1039 {
1040 wip = buf->b_wininfo;
1041 buf->b_wininfo = wip->wi_next;
1042 free_wininfo(wip);
1043 }
1044}
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1048 */
1049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050free_buffer_stuff(
1051 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001052 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 if (free_options)
1055 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001056 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001058#ifdef FEAT_SPELL
1059 ga_clear(&buf->b_s.b_langp);
1060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001063 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001064 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001065
Bram Moolenaarc667da52019-11-30 20:52:27 +01001066 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001067 hash_init(&buf->b_vars->dv_hashtab);
1068 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001069 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001070 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001073 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001075 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001077#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001078 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001079#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001080#ifdef FEAT_PROP_POPUP
1081 ga_clear_strings(&buf->b_textprop_text);
1082#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001083 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1084 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001085 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086}
1087
1088/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001089 * Free one wininfo_T.
1090 */
1091 void
1092free_wininfo(wininfo_T *wip)
1093{
1094 if (wip->wi_optset)
1095 {
1096 clear_winopt(&wip->wi_opt);
1097#ifdef FEAT_FOLDING
1098 deleteFoldRecurse(&wip->wi_folds);
1099#endif
1100 }
1101 vim_free(wip);
1102}
1103
1104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * Go to another buffer. Handles the result of the ATTENTION dialog.
1106 */
1107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001108goto_buffer(
1109 exarg_T *eap,
1110 int start,
1111 int dir,
1112 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001114 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001115 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001116 int skip_help_buf;
1117
1118 switch (eap->cmdidx)
1119 {
1120 case CMD_bnext:
1121 case CMD_sbnext:
1122 case CMD_bNext:
1123 case CMD_bprevious:
1124 case CMD_sbNext:
1125 case CMD_sbprevious:
1126 skip_help_buf = TRUE;
1127 break;
1128 default:
1129 skip_help_buf = FALSE;
1130 break;
1131 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001132
1133 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar188639d2022-04-04 16:57:21 +01001135 if (swap_exists_action == SEA_NONE)
1136 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001137 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1138 (eap->forceit ? DOBUF_FORCEIT : 0) |
1139 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1141 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001142#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001143 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001144
Bram Moolenaarc667da52019-11-30 20:52:27 +01001145 // Reset the error/interrupt/exception state here so that
1146 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001147 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001148#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001149
Bram Moolenaarc667da52019-11-30 20:52:27 +01001150 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001152 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001153 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001154
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001155#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001156 // Restore the error/interrupt/exception state if not discarded by a
1157 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001162 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165/*
1166 * Handle the situation of swap_exists_action being set.
1167 * It is allowed for "old_curbuf" to be NULL or invalid.
1168 */
1169 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001170handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001172#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001173 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001174#endif
1175#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001176 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001177#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001178 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (swap_exists_action == SEA_QUIT)
1181 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001182#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001183 // Reset the error/interrupt/exception state here so that
1184 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001185 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001186#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001187
Bram Moolenaarc667da52019-11-30 20:52:27 +01001188 // User selected Quit at ATTENTION prompt. Go back to previous
1189 // buffer. If that buffer is gone or the same as the current one,
1190 // open a new, empty buffer.
1191 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001192 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001193 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001194 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1195 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001196 {
1197 // Block autocommands here because curwin->w_buffer is NULL.
1198 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001199 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001200 unblock_autocmds();
1201 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001202 else
1203 buf = old_curbuf->br_buf;
1204 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001205 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001206 int old_msg_silent = msg_silent;
1207
1208 if (shortmess(SHM_FILEINFO))
1209 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001210 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001211 // restore msg_silent, so that the command line will be shown
1212 msg_silent = old_msg_silent;
1213
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001214#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001215 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001216 check_colorcolumn(NULL, curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001217#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001218 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001219 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001220
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001221#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001222 // Restore the error/interrupt/exception state if not discarded by a
1223 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001224 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 else if (swap_exists_action == SEA_RECOVER)
1228 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001229#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001230 // Reset the error/interrupt/exception state here so that
1231 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001232 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001233#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001234
Bram Moolenaarc667da52019-11-30 20:52:27 +01001235 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001237 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001238 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001240 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001241
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001242#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001243 // Restore the error/interrupt/exception state if not discarded by a
1244 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001245 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248 swap_exists_action = SEA_NONE;
1249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001252 * Make the current buffer empty.
1253 * Used when it is wiped out and it's the last buffer.
1254 */
1255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256empty_curbuf(
1257 int close_others,
1258 int forceit,
1259 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001260{
1261 int retval;
1262 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001263 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001264
1265 if (action == DOBUF_UNLOAD)
1266 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001267 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001268 return FAIL;
1269 }
1270
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001271 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001272 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001273 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001274 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275
1276 setpcmark();
1277 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1278 forceit ? ECMD_FORCEIT : 0, curwin);
1279
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001280 // do_ecmd() may create a new buffer, then we have to delete
1281 // the old one. But do_ecmd() may have done that already, check
1282 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001283 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001284 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001285 if (!close_others)
1286 need_fileinfo = FALSE;
1287 return retval;
1288}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290/*
1291 * Implementation of the commands for the buffer list.
1292 *
1293 * action == DOBUF_GOTO go to specified buffer
1294 * action == DOBUF_SPLIT split window and go to specified buffer
1295 * action == DOBUF_UNLOAD unload specified buffer(s)
1296 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1297 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001298 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 *
1300 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1301 * start == DOBUF_FIRST go to "count" buffer from first buffer
1302 * start == DOBUF_LAST go to "count" buffer from last buffer
1303 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1304 *
1305 * Return FAIL or OK.
1306 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001307 static int
1308do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309 int action,
1310 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001311 int dir, // FORWARD or BACKWARD
1312 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001313 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 buf_T *buf;
1316 buf_T *bp;
1317 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001318 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320 switch (start)
1321 {
1322 case DOBUF_FIRST: buf = firstbuf; break;
1323 case DOBUF_LAST: buf = lastbuf; break;
1324 default: buf = curbuf; break;
1325 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001326 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
1328 while (count-- > 0)
1329 {
1330 do
1331 {
1332 buf = buf->b_next;
1333 if (buf == NULL)
1334 buf = firstbuf;
1335 }
1336 while (buf != curbuf && !bufIsChanged(buf));
1337 }
1338 if (!bufIsChanged(buf))
1339 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001340 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 return FAIL;
1342 }
1343 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001344 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
1346 while (buf != NULL && buf->b_fnum != count)
1347 buf = buf->b_next;
1348 }
1349 else
1350 {
1351 bp = NULL;
1352 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1353 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 // remember the buffer where we start, we come back there when all
1355 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (bp == NULL)
1357 bp = buf;
1358 if (dir == FORWARD)
1359 {
1360 buf = buf->b_next;
1361 if (buf == NULL)
1362 buf = firstbuf;
1363 }
1364 else
1365 {
1366 buf = buf->b_prev;
1367 if (buf == NULL)
1368 buf = lastbuf;
1369 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001370 // Don't count unlisted buffers.
1371 // Avoid non-help buffers if the starting point was a non-help buffer and
1372 // vice-versa.
1373 if (unload || (buf->b_p_bl
1374 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001377 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379 if (bp == buf)
1380 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001381 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001382 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 return FAIL;
1384 }
1385 }
1386 }
1387
Bram Moolenaarc667da52019-11-30 20:52:27 +01001388 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 if (start == DOBUF_FIRST)
1391 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001392 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001394 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001397 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001399 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return FAIL;
1401 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001402#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001403 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001404 return OK;
1405#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001406 if (
1407 action == DOBUF_GOTO
1408 && buf != curbuf
1409 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1410 // disallow navigating to another buffer when 'winfixbuf' is applied
1411 return FAIL;
1412
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001413 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1414 && (buf->b_flags & BF_DUMMY))
1415 {
1416 // disallow navigating to the dummy buffer
1417 semsg(_(e_buffer_nr_does_not_exist), count);
1418 return FAIL;
1419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421#ifdef FEAT_GUI
1422 need_mouse_correct = TRUE;
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001426 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 */
1428 if (unload)
1429 {
1430 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001431 bufref_T bufref;
1432
Bram Moolenaar94f01952018-09-01 15:30:03 +02001433 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001434 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001435
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001436 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaarc667da52019-11-30 20:52:27 +01001438 // When unloading or deleting a buffer that's already unloaded and
1439 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001440 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1441 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 return FAIL;
1443
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001444 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001446#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001447 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001449# ifdef FEAT_TERMINAL
1450 if (term_job_running(buf->b_term))
1451 {
1452 if (term_confirm_stop(buf) == FAIL)
1453 return FAIL;
1454 }
1455 else
1456# endif
1457 {
1458 dialog_changed(buf, FALSE);
1459 if (!bufref_valid(&bufref))
1460 // Autocommand deleted buffer, oops! It's not changed
1461 // now.
1462 return FAIL;
1463 // If it's still changed fail silently, the dialog already
1464 // mentioned why it fails.
1465 if (bufIsChanged(buf))
1466 return FAIL;
1467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001469 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001472 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 return FAIL;
1474 }
1475 }
1476
Bram Moolenaarc667da52019-11-30 20:52:27 +01001477 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001478 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001479 end_visual_mode();
1480
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001481 // If deleting the last (listed) buffer, make it empty.
1482 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001483 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (bp->b_p_bl && bp != buf)
1485 break;
1486 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001487 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001489 // If the deleted buffer is the current one, close the current window
1490 // (unless it's the only window). Repeat this so long as we end up in
1491 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001492 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001493 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001494 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001495 {
1496 if (win_close(curwin, FALSE) == FAIL)
1497 break;
1498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001500 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (buf != curbuf)
1502 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001503 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001504 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001505 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return OK;
1507 }
1508
1509 /*
1510 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001511 * There should be another, otherwise it would have been handled
1512 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001513 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * Then prefer the buffer we most recently visited.
1515 * Else try to find one that is loaded, after the current buffer,
1516 * then before the current buffer.
1517 * Finally use any buffer.
1518 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001519 buf = NULL; // selected buffer
1520 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001521 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1522 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001523 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 int jumpidx;
1526
1527 jumpidx = curwin->w_jumplistidx - 1;
1528 if (jumpidx < 0)
1529 jumpidx = curwin->w_jumplistlen - 1;
1530
1531 forward = jumpidx;
1532 while (jumpidx != curwin->w_jumplistidx)
1533 {
1534 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1535 if (buf != NULL)
1536 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001537 // Skip current and unlisted bufs. Also skip a quickfix
1538 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001539 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001540 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 else if (buf->b_ml.ml_mfp == NULL)
1542 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001543 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (bp == NULL)
1545 bp = buf;
1546 buf = NULL;
1547 }
1548 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001549 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001551 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1553 break;
1554 if (--jumpidx < 0)
1555 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001556 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 break;
1558 }
1559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaarc667da52019-11-30 20:52:27 +01001561 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 forward = TRUE;
1564 buf = curbuf->b_next;
1565 for (;;)
1566 {
1567 if (buf == NULL)
1568 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001569 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 break;
1571 buf = curbuf->b_prev;
1572 forward = FALSE;
1573 continue;
1574 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001576 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001577 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001579 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001581 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 bp = buf;
1583 }
1584 if (forward)
1585 buf = buf->b_next;
1586 else
1587 buf = buf->b_prev;
1588 }
1589 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001590 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001592 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001594 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001595 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 break;
1597 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001598 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 if (curbuf->b_next != NULL)
1601 buf = curbuf->b_next;
1602 else
1603 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001604 if (bt_quickfix(buf))
1605 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608
Bram Moolenaara02471e2014-01-10 16:43:14 +01001609 if (buf == NULL)
1610 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001611 // Autocommands must have wiped out all other buffers. Only option
1612 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001613 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001614 }
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001617 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001619 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001621 // If 'switchbuf' is set jump to the window containing "buf".
1622 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001623 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 return FAIL;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaarc667da52019-11-30 20:52:27 +01001629 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (buf == curbuf)
1631 return OK;
1632
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001633 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001634 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001637 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001639# ifdef FEAT_TERMINAL
1640 if (term_job_running(curbuf->b_term))
1641 {
1642 if (term_confirm_stop(curbuf) == FAIL)
1643 return FAIL;
1644 // Manually kill the terminal here because this command will
1645 // hide it otherwise.
1646 free_terminal(curbuf);
1647 }
1648 else
1649# endif
1650 {
1651 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001652
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001653 set_bufref(&bufref, buf);
1654 dialog_changed(curbuf, FALSE);
1655 if (!bufref_valid(&bufref))
1656 // Autocommand deleted buffer, oops!
1657 return FAIL;
1658
1659 if (bufIsChanged(curbuf))
1660 {
1661 no_write_message();
1662 return FAIL;
1663 }
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
1668 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001669 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 return FAIL;
1671 }
1672 }
1673
Bram Moolenaarc667da52019-11-30 20:52:27 +01001674 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 set_curbuf(buf, action);
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001678 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001680#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001681 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 return FAIL;
1683#endif
1684
1685 return OK;
1686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001688 int
1689do_buffer(
1690 int action,
1691 int start,
1692 int dir, // FORWARD or BACKWARD
1693 int count, // buffer number or number of buffers
1694 int forceit) // TRUE when using !
1695{
1696 return do_buffer_ext(action, start, dir, count,
1697 forceit ? DOBUF_FORCEIT : 0);
1698}
1699
1700/*
1701 * do_bufdel() - delete or unload buffer(s)
1702 *
1703 * addr_count == 0: ":bdel" - delete current buffer
1704 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1705 * buffer "end_bnr", then any other arguments.
1706 * addr_count == 2: ":N,N bdel" - delete buffers in range
1707 *
1708 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1709 * DOBUF_DEL (":bdel")
1710 *
1711 * Returns error message or NULL
1712 */
1713 char *
1714do_bufdel(
1715 int command,
1716 char_u *arg, // pointer to extra arguments
1717 int addr_count,
1718 int start_bnr, // first buffer number in a range
1719 int end_bnr, // buffer nr or last buffer nr in a range
1720 int forceit)
1721{
1722 int do_current = 0; // delete current buffer?
1723 int deleted = 0; // number of buffers deleted
1724 char *errormsg = NULL; // return value
1725 int bnr; // buffer number
1726 char_u *p;
1727
1728 if (addr_count == 0)
1729 {
1730 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1731 }
1732 else
1733 {
1734 if (addr_count == 2)
1735 {
1736 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001737 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001738 bnr = start_bnr;
1739 }
1740 else // addr_count == 1
1741 bnr = end_bnr;
1742
1743 for ( ;!got_int; ui_breakcheck())
1744 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001745 // Delete the current buffer last, otherwise when the
1746 // current buffer is deleted, the next buffer becomes
1747 // the current one and will be loaded, which may then
1748 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001749 if (bnr == curbuf->b_fnum)
1750 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001751 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001752 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1753 ++deleted;
1754
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001755 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001756 if (addr_count == 2)
1757 {
1758 if (++bnr > end_bnr)
1759 break;
1760 }
1761 else // addr_count == 1
1762 {
1763 arg = skipwhite(arg);
1764 if (*arg == NUL)
1765 break;
1766 if (!VIM_ISDIGIT(*arg))
1767 {
1768 p = skiptowhite_esc(arg);
1769 bnr = buflist_findpat(arg, p,
1770 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1771 FALSE, FALSE);
1772 if (bnr < 0) // failed
1773 break;
1774 arg = p;
1775 }
1776 else
1777 bnr = getdigits(&arg);
1778 }
1779 }
1780 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1781 FORWARD, do_current, forceit) == OK)
1782 ++deleted;
1783
1784 if (deleted == 0)
1785 {
1786 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001787 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001788 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001789 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001790 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001791 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001792 errormsg = (char *)IObuff;
1793 }
1794 else if (deleted >= p_report)
1795 {
1796 if (command == DOBUF_UNLOAD)
1797 smsg(NGETTEXT("%d buffer unloaded",
1798 "%d buffers unloaded", deleted), deleted);
1799 else if (command == DOBUF_DEL)
1800 smsg(NGETTEXT("%d buffer deleted",
1801 "%d buffers deleted", deleted), deleted);
1802 else
1803 smsg(NGETTEXT("%d buffer wiped out",
1804 "%d buffers wiped out", deleted), deleted);
1805 }
1806 }
1807
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001808 return errormsg;
1809}
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
1812 * Set current buffer to "buf". Executes autocommands and closes current
1813 * buffer. "action" tells how to close the current buffer:
1814 * DOBUF_GOTO free or hide it
1815 * DOBUF_SPLIT nothing
1816 * DOBUF_UNLOAD unload it
1817 * DOBUF_DEL delete it
1818 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001819 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 buf_T *prevbuf;
1825 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001826 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001827#ifdef FEAT_SYN_HL
1828 long old_tw = curbuf->b_p_tw;
1829#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001830 bufref_T newbufref;
1831 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001832 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001833 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001836 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001837 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1838 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaarc667da52019-11-30 20:52:27 +01001840 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001845 set_bufref(&prevbufref, prevbuf);
1846 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001848 // Autocommands may delete the current buffer and/or the buffer we want to
1849 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001850 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001851 || (bufref_valid(&prevbufref)
1852 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001853#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001854 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001856 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001858#ifdef FEAT_SYN_HL
1859 if (prevbuf == curwin->w_buffer)
1860 reset_synblock(curwin);
1861#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001862 // autocommands may have opened a new window
1863 // with prevbuf, grr
1864 if (unload ||
1865 (last_winid != get_last_winid() &&
1866 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001867 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001868#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001869 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001871 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001873 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001874 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001875
Bram Moolenaare615db02022-01-20 21:00:54 +00001876 // Do not sync when in Insert mode and the buffer is open in
1877 // another window, might be a timer doing something in another
1878 // window.
1879 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001880 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001881 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1883 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001884 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001885 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1886 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001887 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001888 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001889 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001892 // An autocommand may have deleted "buf", already entered it (e.g., when
1893 // it did ":bunload") or aborted the script processing.
1894 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001895 valid = buf_valid(buf);
1896 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001897#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001898 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001900 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001901 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001902 // autocommands changed curbuf and we will move to another
1903 // buffer soon, so decrement curbuf->b_nwindows
1904 if (curbuf != NULL && prevbuf != curbuf)
1905 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001906 // If the buffer is not valid but curwin->w_buffer is NULL we must
1907 // enter some buffer. Using the last one is hopefully OK.
1908 if (!valid)
1909 enter_buffer(lastbuf);
1910 else
1911 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001912#ifdef FEAT_SYN_HL
1913 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001914 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001915#endif
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917}
1918
1919/*
1920 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001921 * Old curbuf must have been abandoned already! This also means "curbuf" may
1922 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001925enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001927 // when closing the current buffer stop Visual mode
1928 if (VIsual_active
1929#if defined(EXITFREE)
1930 && !entered_free_all_mem
1931#endif
1932 )
1933 end_visual_mode();
1934
Bram Moolenaar010ee962019-09-25 20:37:36 +02001935 // Get the buffer in the current window.
1936 curwin->w_buffer = buf;
1937 curbuf = buf;
1938 ++curbuf->b_nwindows;
1939
1940 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1942 if (!buf->b_help)
1943 get_winopts(buf);
1944#ifdef FEAT_FOLDING
1945 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001946 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001948 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#endif
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001952 if (curwin->w_p_diff)
1953 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
1955
Bram Moolenaara971b822011-09-14 14:43:25 +02001956#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001957 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001958#endif
1959
Bram Moolenaarc667da52019-11-30 20:52:27 +01001960 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 curwin->w_cursor.lnum = 1;
1962 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001965 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
Bram Moolenaarc667da52019-11-30 20:52:27 +01001967 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001968 curwin->w_valid = 0;
1969
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001970 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1971 curbuf->b_last_cursor.col, TRUE);
1972
Bram Moolenaarc667da52019-11-30 20:52:27 +01001973 // Make sure the buffer is loaded.
1974 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001975 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001976 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001977 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001978 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001979 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02001980 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001981
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001982 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 else
1985 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001986 if (!msg_silent && !shortmess(SHM_FILEINFO))
1987 need_fileinfo = TRUE; // display file info after redraw
1988
1989 // check if file changed
1990 (void)buf_check_timestamp(curbuf, FALSE);
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001993#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1997 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999
Bram Moolenaarc667da52019-11-30 20:52:27 +01002000 // If autocommands did not change the cursor position, restore cursor lnum
2001 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (curwin->w_cursor.lnum == 1 && inindent(0))
2003 buflist_getfpos();
2004
Bram Moolenaarc667da52019-11-30 20:52:27 +01002005 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002007 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002008 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002009 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar009b2592004-10-24 19:18:58 +00002011#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002012 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002013 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002014#endif
2015
Bram Moolenaarc667da52019-11-30 20:52:27 +01002016 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002017 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019#ifdef FEAT_KEYMAP
2020 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002021 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002023#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002024 // May need to set the spell language. Can only do this after the buffer
2025 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002026 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002027 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002028#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002029#ifdef FEAT_VIMINFO
2030 curbuf->b_last_used = vim_time();
2031#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002032
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002033 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034}
2035
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002036#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2037/*
2038 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002039 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002040 */
2041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002042do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002043{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002044 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002045 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002046 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002047 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002048 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002049 last_chdir_reason = "autochdir";
2050 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002051}
2052#endif
2053
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002054 static void
2055no_write_message_buf(buf_T *buf UNUSED)
2056{
2057#ifdef FEAT_TERMINAL
2058 if (term_job_running(buf->b_term))
2059 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2060 else
2061#endif
2062 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2063 buf->b_fnum);
2064}
2065
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002066 void
2067no_write_message(void)
2068{
2069#ifdef FEAT_TERMINAL
2070 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002071 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002072 else
2073#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002074 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002075}
2076
2077 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002078no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002079{
2080#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002081 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002082 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002083 else
2084#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002085 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002086}
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088/*
2089 * functions for dealing with the buffer list
2090 */
2091
2092/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002093 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2094 * only one window. That means it can be re-used.
2095 */
2096 int
2097curbuf_reusable(void)
2098{
2099 return (curbuf != NULL
2100 && curbuf->b_ffname == NULL
2101 && curbuf->b_nwindows <= 1
2102 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002103 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002104 && !curbufIsChanged());
2105}
2106
2107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 * Add a file name to the buffer list. Return a pointer to the buffer.
2109 * If the same file name already exists return a pointer to that buffer.
2110 * If it does not exist, or if fname == NULL, a new entry is created.
2111 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2112 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2113 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002114 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002115 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2116 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002117 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 * This is the ONLY way to create a new buffer.
2119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002121buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002122 char_u *ffname_arg, // full path of fname or relative
2123 char_u *sfname_arg, // short fname or NULL
2124 linenr_T lnum, // preferred cursor line
2125 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002127 char_u *ffname = ffname_arg;
2128 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 buf_T *buf;
2130#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002131 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133
Bram Moolenaar480778b2016-07-14 22:09:39 +02002134 if (top_file_num == 1)
2135 hash_init(&buf_hashtab);
2136
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002137 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002140 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 */
2142#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002143 // On Unix we can use inode numbers when the file exists. Works better
2144 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2146 st.st_dev = (dev_T)-1;
2147#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002148 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149#ifdef UNIX
2150 buflist_findname_stat(ffname, &st)
2151#else
2152 buflist_findname(ffname)
2153#endif
2154 ) != NULL)
2155 {
2156 vim_free(ffname);
2157 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002158 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2159 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002160
2161 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002162 // copy the options now, if 'cpo' doesn't have 's' and not done
2163 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002164 buf_copy_options(buf, 0);
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2167 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002168 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002171 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002173 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002174 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002175 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002176 return NULL;
2177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179 return buf;
2180 }
2181
2182 /*
2183 * If the current buffer has no name and no contents, use the current
2184 * buffer. Otherwise: Need to allocate a new buffer structure.
2185 *
2186 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002187 * (A spell file buffer is allocated in spell.c, but that's not a normal
2188 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 */
2190 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002191 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002194 // It's like this buffer is deleted. Watch out for autocommands that
2195 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002196 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2197 if (buf != curbuf) // autocommands deleted the buffer!
2198 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002199#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002200 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002201 {
2202 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 if (buf != curbuf || curbuf == NULL)
2208 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002209 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (buf == NULL)
2211 {
2212 vim_free(ffname);
2213 return NULL;
2214 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002215#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002216 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002217 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002218 if (buf->b_vars == NULL)
2219 {
2220 vim_free(ffname);
2221 vim_free(buf);
2222 return NULL;
2223 }
2224 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2225#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002226 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228
2229 if (ffname != NULL)
2230 {
2231 buf->b_ffname = ffname;
2232 buf->b_sfname = vim_strsave(sfname);
2233 }
2234
2235 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002236 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2239 || buf->b_wininfo == NULL)
2240 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002241 if (buf->b_sfname != buf->b_ffname)
2242 VIM_CLEAR(buf->b_sfname);
2243 else
2244 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002245 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (buf != curbuf)
2247 free_buffer(buf);
2248 return NULL;
2249 }
2250
2251 if (buf == curbuf)
2252 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002253 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002254
Bram Moolenaarc667da52019-11-30 20:52:27 +01002255 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002256 buf->b_p_initialized = FALSE;
2257 buf_copy_options(buf, BCO_ENTER);
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002260 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 curbuf->b_kmap_state |= KEYMAP_INIT;
2262#endif
2263 }
2264 else
2265 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002266 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002268 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 buf->b_prev = NULL;
2271 firstbuf = buf;
2272 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002273 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 lastbuf->b_next = buf;
2276 buf->b_prev = lastbuf;
2277 }
2278 lastbuf = buf;
2279
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002280 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2281 {
2282 // Recycle a previously used buffer number. Used for buffers which
2283 // are normally hidden, e.g. in a popup window. Avoids that the
2284 // buffer number grows rapidly.
2285 --buf_reuse.ga_len;
2286 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002287
2288 // Move buffer to the right place in the buffer list.
2289 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2290 {
2291 buf_T *prev = buf->b_prev;
2292
2293 prev->b_next = buf->b_next;
2294 if (prev->b_next != NULL)
2295 prev->b_next->b_prev = prev;
2296 buf->b_next = prev;
2297 buf->b_prev = prev->b_prev;
2298 if (buf->b_prev != NULL)
2299 buf->b_prev->b_next = buf;
2300 prev->b_prev = buf;
2301 if (lastbuf == buf)
2302 lastbuf = prev;
2303 if (firstbuf == prev)
2304 firstbuf = buf;
2305 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002306 }
2307 else
2308 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002309 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002311 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002312 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002315 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 top_file_num = 1;
2318 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002319 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002321 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 buf_copy_options(buf, BCO_ALWAYS);
2323 }
2324
2325 buf->b_wininfo->wi_fpos.lnum = lnum;
2326 buf->b_wininfo->wi_win = curwin;
2327
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002328#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002329 hash_init(&buf->b_s.b_keywtab);
2330 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332
2333 buf->b_fname = buf->b_sfname;
2334#ifdef UNIX
2335 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002336 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 else
2338 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002339 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 buf->b_dev = st.st_dev;
2341 buf->b_ino = st.st_ino;
2342 }
2343#endif
2344 buf->b_u_synced = TRUE;
2345 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002346 if (flags & BLN_DUMMY)
2347 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002349 clrallmarks(buf); // clear marks
2350 fmarks_check_names(buf); // check file marks for this file
2351 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (!(flags & BLN_DUMMY))
2353 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002354 bufref_T bufref;
2355
Bram Moolenaarc667da52019-11-30 20:52:27 +01002356 // Tricky: these autocommands may change the buffer list. They could
2357 // also split the window with re-using the one empty buffer. This may
2358 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002359 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002360 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002361 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002362 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002364 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002365 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002366 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002367 return NULL;
2368 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002369#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002370 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 return buf;
2376}
2377
2378/*
2379 * Free the memory for the options of a buffer.
2380 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2381 * 'fileencoding'.
2382 */
2383 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002384free_buf_options(
2385 buf_T *buf,
2386 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 if (free_p_ff)
2389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 clear_string_option(&buf->b_p_bh);
2393 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395#ifdef FEAT_FIND_ID
2396 clear_string_option(&buf->b_p_def);
2397 clear_string_option(&buf->b_p_inc);
2398# ifdef FEAT_EVAL
2399 clear_string_option(&buf->b_p_inex);
2400# endif
2401#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002402#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 clear_string_option(&buf->b_p_inde);
2404 clear_string_option(&buf->b_p_indk);
2405#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002406#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2407 clear_string_option(&buf->b_p_bexpr);
2408#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002409#if defined(FEAT_CRYPT)
2410 clear_string_option(&buf->b_p_cm);
2411#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002412 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002413#if defined(FEAT_EVAL)
2414 clear_string_option(&buf->b_p_fex);
2415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002417# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002418 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2419 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002420 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 clear_string_option(&buf->b_p_key);
2423#endif
2424 clear_string_option(&buf->b_p_kp);
2425 clear_string_option(&buf->b_p_mps);
2426 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002427 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002429#ifdef FEAT_VARTABS
2430 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002431 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002432 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002433 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002434 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#ifdef FEAT_KEYMAP
2437 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002438 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 ga_clear(&buf->b_kmap_ga);
2440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442#ifdef FEAT_FOLDING
2443 clear_string_option(&buf->b_p_cms);
2444#endif
2445 clear_string_option(&buf->b_p_nf);
2446#ifdef FEAT_SYN_HL
2447 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002448 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002449#endif
2450#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002451 clear_string_option(&buf->b_s.b_p_spc);
2452 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002453 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002454 buf->b_s.b_cap_prog = NULL;
2455 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002456 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 clear_string_option(&buf->b_p_cink);
2461 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002462 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002463 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002465 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002467#ifdef FEAT_COMPL_FUNC
2468 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002469 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002470 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002471 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002472 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002473 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_QUICKFIX
2476 clear_string_option(&buf->b_p_gp);
2477 clear_string_option(&buf->b_p_mp);
2478 clear_string_option(&buf->b_p_efm);
2479#endif
2480 clear_string_option(&buf->b_p_ep);
2481 clear_string_option(&buf->b_p_path);
2482 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002483 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002484#ifdef FEAT_EVAL
2485 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002486 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002487 clear_string_option(&buf->b_p_ffu);
2488 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 clear_string_option(&buf->b_p_dict);
2491 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002492 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002494 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002495 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002496 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002497 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498}
2499
2500/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002501 * Get alternate file "n".
2502 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2503 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * if (options & GETF_SETMARK) call setpcmark()
2505 * if (options & GETF_ALT) we are jumping to an alternate file.
2506 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2507 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002508 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
2510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002511buflist_getfile(
2512 int n,
2513 linenr_T lnum,
2514 int options,
2515 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 pos_T *fpos;
2520 colnr_T col;
2521
2522 buf = buflist_findnr(n);
2523 if (buf == NULL)
2524 {
2525 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002526 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002528 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return FAIL;
2530 }
2531
Bram Moolenaarc667da52019-11-30 20:52:27 +01002532 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (buf == curbuf)
2534 return OK;
2535
Bram Moolenaar71223e22022-05-30 15:23:09 +01002536 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002537 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaarc667da52019-11-30 20:52:27 +01002539 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (lnum == 0)
2541 {
2542 fpos = buflist_findfpos(buf);
2543 lnum = fpos->lnum;
2544 col = fpos->col;
2545 }
2546 else
2547 col = 0;
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (options & GETF_SWITCH)
2550 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002551 // If 'switchbuf' is set jump to the window containing "buf".
2552 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002553
Bram Moolenaarc667da52019-11-30 20:52:27 +01002554 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2555 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002556 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002557 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002559 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002560 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002561 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2562 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002564 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
2566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002569 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002570 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2571 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002573 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (!p_sol && col != 0)
2575 {
2576 curwin->w_cursor.col = col;
2577 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 curwin->w_set_curswant = TRUE;
2580 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002581 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002583
2584 if (RedrawingDisabled > 0)
2585 --RedrawingDisabled;
2586 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587}
2588
2589/*
2590 * go to the last know line number for the current buffer
2591 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002593buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 pos_T *fpos;
2596
2597 fpos = buflist_findfpos(curbuf);
2598
2599 curwin->w_cursor.lnum = fpos->lnum;
2600 check_cursor_lnum();
2601
2602 if (p_sol)
2603 curwin->w_cursor.col = 0;
2604 else
2605 {
2606 curwin->w_cursor.col = fpos->col;
2607 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 curwin->w_set_curswant = TRUE;
2610 }
2611}
2612
Bram Moolenaar81695252004-12-29 20:58:21 +00002613/*
2614 * Find file in buffer list by name (it has to be for the current window).
2615 * Returns NULL if not found.
2616 */
2617 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002618buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002619{
2620 char_u *ffname;
2621 buf_T *buf = NULL;
2622
Bram Moolenaarc667da52019-11-30 20:52:27 +01002623 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002624 ffname = FullName_save(fname,
2625#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002626 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002627#else
2628 FALSE
2629#endif
2630 );
2631 if (ffname != NULL)
2632 {
2633 buf = buflist_findname(ffname);
2634 vim_free(ffname);
2635 }
2636 return buf;
2637}
Bram Moolenaar81695252004-12-29 20:58:21 +00002638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
2640 * Find file in buffer list by name (it has to be for the current window).
2641 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002642 * Skips dummy buffers.
2643 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 */
2645 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002646buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
2648#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002649 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 if (mch_stat((char *)ffname, &st) < 0)
2652 st.st_dev = (dev_T)-1;
2653 return buflist_findname_stat(ffname, &st);
2654}
2655
2656/*
2657 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2658 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002659 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 */
2661 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002662buflist_findname_stat(
2663 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002664 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666#endif
2667 buf_T *buf;
2668
Bram Moolenaarc667da52019-11-30 20:52:27 +01002669 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002670 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002671 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#ifdef UNIX
2673 , stp
2674#endif
2675 ))
2676 return buf;
2677 return NULL;
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
2681 * Find file in buffer list by a regexp pattern.
2682 * Return fnum of the found buffer.
2683 * Return < 0 for error.
2684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002686buflist_findpat(
2687 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002688 char_u *pattern_end, // pointer to first char after pattern
2689 int unlisted, // find unlisted buffers
2690 int diffmode UNUSED, // find diff-mode buffers only
2691 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
2693 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int match = -1;
2695 int find_listed;
2696 char_u *pat;
2697 char_u *patend;
2698 int attempt;
2699 char_u *p;
2700 int toggledollar;
2701
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002702 // "%" is current file, "%%" or "#" is alternate file
2703 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2704 || (in_vim9script() && pattern_end == pattern + 2
2705 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002707 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002709 else
2710 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#ifdef FEAT_DIFF
2712 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2713 match = -1;
2714#endif
2715 }
2716
2717 /*
2718 * Try four ways of matching a listed buffer:
2719 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002720 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 * attempt == 2: with '$' at end (only match at end)
2722 * attempt == 3: with '^' at start and '$' at end (only full match)
2723 * Repeat this for finding an unlisted buffer if there was no matching
2724 * listed buffer.
2725 */
2726 else
2727 {
2728 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2729 if (pat == NULL)
2730 return -1;
2731 patend = pat + STRLEN(pat) - 1;
2732 toggledollar = (patend > pat && *patend == '$');
2733
Bram Moolenaarc667da52019-11-30 20:52:27 +01002734 // First try finding a listed buffer. If not found and "unlisted"
2735 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 find_listed = TRUE;
2737 for (;;)
2738 {
2739 for (attempt = 0; attempt <= 3; ++attempt)
2740 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002741 regmatch_T regmatch;
2742
Bram Moolenaarc667da52019-11-30 20:52:27 +01002743 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002745 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002747 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002749 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002751 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002752 {
2753 if (regmatch.regprog == NULL)
2754 {
2755 // invalid pattern, possibly after switching engine
2756 vim_free(pat);
2757 return -1;
2758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (buf->b_p_bl == find_listed
2760#ifdef FEAT_DIFF
2761 && (!diffmode || diff_mode_buf(buf))
2762#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002763 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002765 if (curtab_only)
2766 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002767 // Ignore the match if the buffer is not open in
2768 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002769 win_T *wp;
2770
Bram Moolenaar29323592016-07-24 22:04:11 +02002771 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002772 if (wp->w_buffer == buf)
2773 break;
2774 if (wp == NULL)
2775 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002776 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002777 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 match = -2;
2780 break;
2781 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002782 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002786 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002787 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 break;
2789 }
2790
Bram Moolenaarc667da52019-11-30 20:52:27 +01002791 // Only search for unlisted buffers if there was no match with
2792 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (!unlisted || !find_listed || match != -1)
2794 break;
2795 find_listed = FALSE;
2796 }
2797
2798 vim_free(pat);
2799 }
2800
2801 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002802 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002804 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 return match;
2806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
Bram Moolenaar52410572019-10-27 05:12:45 +01002808#ifdef FEAT_VIMINFO
2809typedef struct {
2810 buf_T *buf;
2811 char_u *match;
2812} bufmatch_T;
2813#endif
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815/*
2816 * Find all buffer names that match.
2817 * For command line expansion of ":buf" and ":sbuf".
2818 * Return OK if matches found, FAIL otherwise.
2819 */
2820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002821ExpandBufnames(
2822 char_u *pat,
2823 int *num_file,
2824 char_u ***file,
2825 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002827 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 buf_T *buf;
2829 int round;
2830 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002831 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002832#ifdef FEAT_VIMINFO
2833 bufmatch_T *matches = NULL;
2834#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002835 int fuzzy;
2836 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002837 regmatch_T regmatch;
2838 int score = 0;
2839 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
Bram Moolenaarc667da52019-11-30 20:52:27 +01002841 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 *file = NULL;
2843
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002844#ifdef FEAT_DIFF
2845 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2846 return FAIL;
2847#endif
2848
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002849 fuzzy = cmdline_fuzzy_complete(pat);
2850
2851 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2852 // expression matching)
2853 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002855 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002856 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002857 int len = (int)STRLEN(pat);
2858 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002859 if (patc == NULL)
2860 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002861 STRNCPY(patc, pat + 1, len - 1);
2862 patc[len - 1] = NUL;
2863 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002864 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002865 else if (*pat == '^')
2866 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002867 else
2868 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002869 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002870 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002871
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002872 // round == 1: Count the matches.
2873 // round == 2: Build the array to keep the matches.
2874 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002875 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002876 count = 0;
2877 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002878 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002879 if (!buf->b_p_bl) // skip unlisted buffers
2880 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002881#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002882 if (options & BUF_DIFF_FILTER)
2883 // Skip buffers not suitable for
2884 // :diffget or :diffput completion.
2885 if (buf == curbuf || !diff_mode_buf(buf))
2886 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002887#endif
2888
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002889 if (!fuzzy)
2890 {
2891 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002892 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002893 // invalid pattern, possibly after recompiling
2894 if (to_free)
2895 vim_free(patc);
2896 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002897 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002898 p = buflist_match(&regmatch, buf, p_wic);
2899 }
2900 else
2901 {
2902 p = NULL;
2903 // first try matching with the short file name
2904 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2905 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002906 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002907 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002908 // next try matching with the full path file name
2909 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2910 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002913
2914 if (p == NULL)
2915 continue;
2916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 if (round == 1)
2918 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002919 ++count;
2920 continue;
2921 }
2922
2923 if (options & WILD_HOME_REPLACE)
2924 p = home_replace_save(buf, p);
2925 else
2926 p = vim_strsave(p);
2927
2928 if (!fuzzy)
2929 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002930#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002931 if (matches != NULL)
2932 {
2933 matches[count].buf = buf;
2934 matches[count].match = p;
2935 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002936 }
2937 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002938#endif
2939 (*file)[count++] = p;
2940 }
2941 else
2942 {
2943 fuzmatch[count].idx = count;
2944 fuzmatch[count].str = p;
2945 fuzmatch[count].score = score;
2946 count++;
2947 }
2948 }
2949 if (count == 0) // no match found, break here
2950 break;
2951 if (round == 1)
2952 {
2953 if (!fuzzy)
2954 {
2955 *file = ALLOC_MULT(char_u *, count);
2956 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002957 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002958 vim_regfree(regmatch.regprog);
2959 if (to_free)
2960 vim_free(patc);
2961 return FAIL;
2962 }
2963#ifdef FEAT_VIMINFO
2964 if (options & WILD_BUFLASTUSED)
2965 matches = ALLOC_MULT(bufmatch_T, count);
2966#endif
2967 }
2968 else
2969 {
2970 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2971 if (fuzmatch == NULL)
2972 {
2973 *num_file = 0;
2974 *file = NULL;
2975 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002981 if (!fuzzy)
2982 {
2983 vim_regfree(regmatch.regprog);
2984 if (to_free)
2985 vim_free(patc);
2986 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002987
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002988 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002989 {
826814741_6dff3c9c2024-12-10 17:15:14 +01002990#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002991 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002992 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002993 int i;
2994 if (count > 1)
2995 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2996 // if the current buffer is first in the list, place it at the end
2997 if (matches[0].buf == curbuf)
2998 {
2999 for (i = 1; i < count; i++)
3000 (*file)[i-1] = matches[i].match;
3001 (*file)[count-1] = matches[0].match;
3002 }
3003 else
3004 {
3005 for (i = 0; i < count; i++)
3006 (*file)[i] = matches[i].match;
3007 }
3008 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003009 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003010#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003011 }
3012 else
3013 {
3014 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3015 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003016 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 *num_file = count;
3019 return (count == 0 ? FAIL : OK);
3020}
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022/*
3023 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003024 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 */
3026 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003027buflist_match(
3028 regmatch_T *rmp,
3029 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003030 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 char_u *match;
3033
Bram Moolenaarc667da52019-11-30 20:52:27 +01003034 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003035 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003036 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003037 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 return match;
3040}
3041
3042/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003043 * Try matching the regexp in "rmp->regprog" with file name "name".
3044 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * Return "name" when there is a match, NULL when not.
3046 */
3047 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003048fname_match(
3049 regmatch_T *rmp,
3050 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003051 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 char_u *match = NULL;
3054 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003056 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003057 if (name == NULL || rmp->regprog == NULL)
3058 return NULL;
3059
3060 // Ignore case when 'fileignorecase' or the argument is set.
3061 rmp->rm_ic = p_fic || ignore_case;
3062 if (vim_regexec(rmp, name, (colnr_T)0))
3063 match = name;
3064 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003066 // Replace $(HOME) with '~' and try matching again.
3067 p = home_replace_save(NULL, name);
3068 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003070 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072
3073 return match;
3074}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003077 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003082 char_u key[VIM_SIZEOF_INT * 2 + 1];
3083 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085 if (nr == 0)
3086 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003087 sprintf((char *)key, "%x", nr);
3088 hi = hash_find(&buf_hashtab, key);
3089
3090 if (!HASHITEM_EMPTY(hi))
3091 return (buf_T *)(hi->hi_key
3092 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return NULL;
3094}
3095
3096/*
3097 * Get name of file 'n' in the buffer list.
3098 * When the file has no name an empty string is returned.
3099 * home_replace() is used to shorten the file name (used for marks).
3100 * Returns a pointer to allocated memory, of NULL when failed.
3101 */
3102 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003103buflist_nr2name(
3104 int n,
3105 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003106 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 buf_T *buf;
3109
3110 buf = buflist_findnr(n);
3111 if (buf == NULL)
3112 return NULL;
3113 return home_replace_save(helptail ? buf : NULL,
3114 fullname ? buf->b_ffname : buf->b_fname);
3115}
3116
3117/*
3118 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3119 * When "copy_options" is TRUE save the local window option values.
3120 * When "lnum" is 0 only do the options.
3121 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003123buflist_setfpos(
3124 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003125 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003126 linenr_T lnum,
3127 colnr_T col,
3128 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 wininfo_T *wip;
3131
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003132 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 if (wip->wi_win == win)
3134 break;
3135 if (wip == NULL)
3136 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003137 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003138 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (wip == NULL)
3140 return;
3141 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003142 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 lnum = 1;
3144 }
3145 else
3146 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003147 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (wip->wi_prev)
3149 wip->wi_prev->wi_next = wip->wi_next;
3150 else
3151 buf->b_wininfo = wip->wi_next;
3152 if (wip->wi_next)
3153 wip->wi_next->wi_prev = wip->wi_prev;
3154 if (copy_options && wip->wi_optset)
3155 {
3156 clear_winopt(&wip->wi_opt);
3157#ifdef FEAT_FOLDING
3158 deleteFoldRecurse(&wip->wi_folds);
3159#endif
3160 }
3161 }
3162 if (lnum != 0)
3163 {
3164 wip->wi_fpos.lnum = lnum;
3165 wip->wi_fpos.col = col;
3166 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003167 if (win != NULL)
3168 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003169 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003171 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3173#ifdef FEAT_FOLDING
3174 wip->wi_fold_manual = win->w_fold_manual;
3175 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3176#endif
3177 wip->wi_optset = TRUE;
3178 }
3179
Bram Moolenaarc667da52019-11-30 20:52:27 +01003180 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 wip->wi_next = buf->b_wininfo;
3182 buf->b_wininfo = wip;
3183 wip->wi_prev = NULL;
3184 if (wip->wi_next)
3185 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186}
3187
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003188#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003189/*
3190 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3191 * page. That's because a diff is local to a tab page.
3192 */
3193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003195{
3196 win_T *wp;
3197
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003198 if (!wip->wi_opt.wo_diff)
3199 return FALSE;
3200
3201 FOR_ALL_WINDOWS(wp)
3202 // return FALSE when it's a window in the current tab page, thus
3203 // the buffer was in diff mode here
3204 if (wip->wi_win == wp)
3205 return FALSE;
3206 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003207}
3208#endif
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210/*
3211 * Find info for the current window in buffer "buf".
3212 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003213 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003214 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3215 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Returns NULL when there isn't any info.
3217 */
3218 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219find_wininfo(
3220 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003221 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 wininfo_T *wip;
3225
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003226 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003227 if (wip->wi_win == curwin
3228#ifdef FEAT_DIFF
3229 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3230#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003231
3232 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003234
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003235 if (wip != NULL)
3236 return wip;
3237
Bram Moolenaarc667da52019-11-30 20:52:27 +01003238 // If no wininfo for curwin, use the first in the list (that doesn't have
3239 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003240 // If "need_options" is TRUE skip entries that don't have options set,
3241 // unless the window is editing "buf", so we can copy from the window
3242 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003243#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003244 if (skip_diff_buffer)
3245 {
3246 FOR_ALL_BUF_WININFO(buf, wip)
3247 if (!wininfo_other_tab_diff(wip)
3248 && (!need_options || wip->wi_optset
3249 || (wip->wi_win != NULL
3250 && wip->wi_win->w_buffer == buf)))
3251 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003252 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003253 else
3254#endif
3255 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return wip;
3257}
3258
3259/*
3260 * Reset the local window options to the values last used in this window.
3261 * If the buffer wasn't used in this window before, use the values from
3262 * the most recently used window. If the values were never set, use the
3263 * global values for the window.
3264 */
3265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003266get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 wininfo_T *wip;
3269
3270 clear_winopt(&curwin->w_onebuf_opt);
3271#ifdef FEAT_FOLDING
3272 clearFolding(curwin);
3273#endif
3274
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003275 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003276 if (wip != NULL && wip->wi_win != NULL
3277 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003279 // The buffer is currently displayed in the window: use the actual
3280 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003281 win_T *wp = wip->wi_win;
3282
3283 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3284#ifdef FEAT_FOLDING
3285 curwin->w_fold_manual = wp->w_fold_manual;
3286 curwin->w_foldinvalid = TRUE;
3287 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3288#endif
3289 }
3290 else if (wip != NULL && wip->wi_optset)
3291 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003292 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3294#ifdef FEAT_FOLDING
3295 curwin->w_fold_manual = wip->wi_fold_manual;
3296 curwin->w_foldinvalid = TRUE;
3297 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3298#endif
3299 }
3300 else
3301 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003302 if (wip != NULL)
3303 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003306 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (p_fdls >= 0)
3308 curwin->w_p_fdl = p_fdls;
3309#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003310 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311}
3312
3313/*
3314 * Find the position (lnum and col) for the buffer 'buf' for the current
3315 * window.
3316 * Returns a pointer to no_position if no position is found.
3317 */
3318 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003319buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003322 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003324 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (wip != NULL)
3326 return &(wip->wi_fpos);
3327 else
3328 return &no_position;
3329}
3330
3331/*
3332 * Find the lnum for the buffer 'buf' for the current window.
3333 */
3334 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 return buflist_findfpos(buf)->lnum;
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003341 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaar52410572019-10-27 05:12:45 +01003346 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 int len;
3348 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003349 int ro_char;
3350 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003351#ifdef FEAT_TERMINAL
3352 int job_running;
3353 int job_none_open;
3354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaar52410572019-10-27 05:12:45 +01003356#ifdef FEAT_VIMINFO
3357 garray_T buflist;
3358 buf_T **buflist_data = NULL, **p;
3359
3360 if (vim_strchr(eap->arg, 't'))
3361 {
3362 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003363 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003364 {
3365 if (ga_grow(&buflist, 1) == OK)
3366 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3367 }
3368
3369 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3370 sizeof(buf_T *), buf_compare);
3371
Bram Moolenaar3b991522019-11-06 23:26:20 +01003372 buflist_data = (buf_T **)buflist.ga_data;
3373 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003374 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003375 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003376
Bram Moolenaar3b991522019-11-06 23:26:20 +01003377 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003378 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3379 : buf->b_next)
3380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003384#ifdef FEAT_TERMINAL
3385 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003386 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003387#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003388 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003389 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3390 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3391 || (vim_strchr(eap->arg, '+')
3392 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3393 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003394 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003395 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003396 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3397#ifdef FEAT_TERMINAL
3398 || (vim_strchr(eap->arg, 'R')
3399 && (!job_running || (job_running && job_none_open)))
3400 || (vim_strchr(eap->arg, '?')
3401 && (!job_running || (job_running && !job_none_open)))
3402 || (vim_strchr(eap->arg, 'F')
3403 && (job_running || buf->b_term == NULL))
3404#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003405 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3406 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3407 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3408 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3409 || (vim_strchr(eap->arg, '#')
3410 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003413 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else
3415 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003416 if (message_filtered(NameBuff))
3417 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003419 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3420 : (bufIsChanged(buf) ? '+' : ' ');
3421#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003422 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003423 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003424 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003425 ro_char = '?';
3426 else
3427 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3429 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003430 }
3431 else if (buf->b_term != NULL)
3432 ro_char = 'F';
3433 else
3434#endif
3435 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3436
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003437 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003438 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 buf->b_fnum,
3440 buf->b_p_bl ? ' ' : 'u',
3441 buf == curbuf ? '%' :
3442 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3443 buf->b_ml.ml_mfp == NULL ? ' ' :
3444 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003445 ro_char,
3446 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003447 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003448 if (len > IOSIZE - 20)
3449 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaarc667da52019-11-30 20:52:27 +01003451 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 i = 40 - vim_strsize(IObuff);
3453 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003455 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003456#ifdef FEAT_VIMINFO
3457 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3458 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3459 else
3460#endif
3461 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3462 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003463 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003465 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 ui_breakcheck();
3467 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003468
3469#ifdef FEAT_VIMINFO
3470 if (buflist_data)
3471 ga_clear(&buflist);
3472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475/*
3476 * Get file name and line number for file 'fnum'.
3477 * Used by DoOneCmd() for translating '%' and '#'.
3478 * Used by insert_reg() and cmdline_paste() for '#' register.
3479 * Return FAIL if not found, OK for success.
3480 */
3481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003482buflist_name_nr(
3483 int fnum,
3484 char_u **fname,
3485 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 buf_T *buf;
3488
3489 buf = buflist_findnr(fnum);
3490 if (buf == NULL || buf->b_fname == NULL)
3491 return FAIL;
3492
3493 *fname = buf->b_fname;
3494 *lnum = buflist_findlnum(buf);
3495
3496 return OK;
3497}
3498
3499/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003500 * Set the file name for "buf"' to "ffname_arg", short file name to
3501 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 * The file name with the full path is also remembered, for when :cd is used.
3503 * Returns FAIL for failure (file name already in use by other buffer)
3504 * OK otherwise.
3505 */
3506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507setfname(
3508 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003509 char_u *ffname_arg,
3510 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003511 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003513 char_u *ffname = ffname_arg;
3514 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003515 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003517 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#endif
3519
3520 if (ffname == NULL || *ffname == NUL)
3521 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003522 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003523 if (buf->b_sfname != buf->b_ffname)
3524 VIM_CLEAR(buf->b_sfname);
3525 else
3526 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003527 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#ifdef UNIX
3529 st.st_dev = (dev_T)-1;
3530#endif
3531 }
3532 else
3533 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003534 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3535 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return FAIL;
3537
3538 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003539 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * - if the buffer is loaded, fail
3541 * - if the buffer is not loaded, delete it from the list
3542 */
3543#ifdef UNIX
3544 if (mch_stat((char *)ffname, &st) < 0)
3545 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003546#endif
3547 if (!(buf->b_flags & BF_DUMMY))
3548#ifdef UNIX
3549 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003551 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#endif
3553 if (obuf != NULL && obuf != buf)
3554 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003555 win_T *win;
3556 tabpage_T *tab;
3557 int in_use = FALSE;
3558
3559 // during startup a window may use a buffer that is not loaded yet
3560 FOR_ALL_TAB_WINDOWS(tab, win)
3561 if (win->w_buffer == obuf)
3562 in_use = TRUE;
3563
3564 // it's loaded or used in a window, fail
3565 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
3567 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003568 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 vim_free(ffname);
3570 return FAIL;
3571 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003572 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003573 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 sfname = vim_strsave(sfname);
3576 if (ffname == NULL || sfname == NULL)
3577 {
3578 vim_free(sfname);
3579 vim_free(ffname);
3580 return FAIL;
3581 }
3582#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003583 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003585 if (buf->b_sfname != buf->b_ffname)
3586 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 buf->b_ffname = ffname;
3589 buf->b_sfname = sfname;
3590 }
3591 buf->b_fname = buf->b_sfname;
3592#ifdef UNIX
3593 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003594 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 else
3596 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003597 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 buf->b_dev = st.st_dev;
3599 buf->b_ino = st.st_ino;
3600 }
3601#endif
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 buf_name_changed(buf);
3606 return OK;
3607}
3608
3609/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003610 * Crude way of changing the name of a buffer. Use with care!
3611 * The name should be relative to the current directory.
3612 */
3613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003615{
3616 buf_T *buf;
3617
3618 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003619 if (buf == NULL)
3620 return;
3621
3622 if (buf->b_sfname != buf->b_ffname)
3623 vim_free(buf->b_sfname);
3624 vim_free(buf->b_ffname);
3625 buf->b_ffname = vim_strsave(name);
3626 buf->b_sfname = NULL;
3627 // Allocate ffname and expand into full path. Also resolves .lnk
3628 // files on Win32.
3629 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3630 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003631}
3632
3633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * Take care of what needs to be done when the name of buffer "buf" has
3635 * changed.
3636 */
3637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 /*
3641 * If the file name changed, also change the name of the swapfile
3642 */
3643 if (buf->b_ml.ml_mfp != NULL)
3644 ml_setname(buf);
3645
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003646#ifdef FEAT_TERMINAL
3647 if (buf->b_term != NULL)
3648 term_clear_status_text(buf->b_term);
3649#endif
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003652 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003653 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003654 status_redraw_all(); // status lines need to be redrawn
3655 fmarks_check_names(buf); // check named file marks
3656 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657}
3658
3659/*
3660 * set alternate file name for current window
3661 *
3662 * Used by do_one_cmd(), do_write() and do_ecmd().
3663 * Return the buffer.
3664 */
3665 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003666setaltfname(
3667 char_u *ffname,
3668 char_u *sfname,
3669 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
3671 buf_T *buf;
3672
Bram Moolenaarc667da52019-11-30 20:52:27 +01003673 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003675 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 curwin->w_alt_fnum = buf->b_fnum;
3677 return buf;
3678}
3679
3680/*
3681 * Get alternate file name for current window.
3682 * Return NULL if there isn't any, and give error message if requested.
3683 */
3684 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003685getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003686 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 char_u *fname;
3689 linenr_T dummy;
3690
3691 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3692 {
3693 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003694 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return NULL;
3696 }
3697 return fname;
3698}
3699
3700/*
3701 * Add a file name to the buflist and return its number.
3702 * Uses same flags as buflist_new(), except BLN_DUMMY.
3703 *
3704 * used by qf_init(), main() and doarglist()
3705 */
3706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003707buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 buf_T *buf;
3710
3711 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3712 if (buf != NULL)
3713 return buf->b_fnum;
3714 return 0;
3715}
3716
3717#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3718/*
3719 * Adjust slashes in file names. Called after 'shellslash' was set.
3720 */
3721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003722buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 buf_T *bp;
3725
Bram Moolenaar29323592016-07-24 22:04:11 +02003726 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
3728 if (bp->b_ffname != NULL)
3729 slash_adjust(bp->b_ffname);
3730 if (bp->b_sfname != NULL)
3731 slash_adjust(bp->b_sfname);
3732 }
3733}
3734#endif
3735
3736/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003737 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * Also save the local window option values.
3739 */
3740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003741buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003743 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744}
3745
3746/*
3747 * Return TRUE if 'ffname' is not the same file as current file.
3748 * Fname must have a full path (expanded by mch_FullName()).
3749 */
3750 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003751otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753 return otherfile_buf(curbuf, ffname
3754#ifdef UNIX
3755 , NULL
3756#endif
3757 );
3758}
3759
3760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761otherfile_buf(
3762 buf_T *buf,
3763 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003765 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003767 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003769 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3771 return TRUE;
3772 if (fnamecmp(ffname, buf->b_ffname) == 0)
3773 return FALSE;
3774#ifdef UNIX
3775 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003776 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaarc667da52019-11-30 20:52:27 +01003778 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (stp == NULL)
3780 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003781 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 st.st_dev = (dev_T)-1;
3783 stp = &st;
3784 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003785 // Use dev/ino to check if the files are the same, even when the names
3786 // are different (possible with links). Still need to compare the
3787 // name above, for when the file doesn't exist yet.
3788 // Problem: The dev/ino changes when a file is deleted (and created
3789 // again) and remains the same when renamed/moved. We don't want to
3790 // mch_stat() each buffer each time, that would be too slow. Get the
3791 // dev/ino again when they appear to match, but not when they appear
3792 // to be different: Could skip a buffer when it's actually the same
3793 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (buf_same_ino(buf, stp))
3795 {
3796 buf_setino(buf);
3797 if (buf_same_ino(buf, stp))
3798 return FALSE;
3799 }
3800 }
3801#endif
3802 return TRUE;
3803}
3804
3805#if defined(UNIX) || defined(PROTO)
3806/*
3807 * Set inode and device number for a buffer.
3808 * Must always be called when b_fname is changed!.
3809 */
3810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003811buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003813 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3816 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003817 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 buf->b_dev = st.st_dev;
3819 buf->b_ino = st.st_ino;
3820 }
3821 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003822 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823}
3824
3825/*
3826 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3827 */
3828 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829buf_same_ino(
3830 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003831 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003833 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 && stp->st_dev == buf->b_dev
3835 && stp->st_ino == buf->b_ino);
3836}
3837#endif
3838
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003839/*
3840 * Print info about the current buffer.
3841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003844 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845 int shorthelp,
3846 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848 char_u *name;
3849 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003850 char *p;
3851 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003852 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003854 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (buffer == NULL)
3856 return;
3857
Bram Moolenaarc667da52019-11-30 20:52:27 +01003858 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003860 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 p = buffer + STRLEN(buffer);
3862 }
3863 else
3864 p = buffer;
3865
3866 *p++ = '"';
3867 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003868 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 else
3870 {
3871 if (!fullname && curbuf->b_fname != NULL)
3872 name = curbuf->b_fname;
3873 else
3874 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003875 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 (int)(IOSIZE - (p - buffer)), TRUE);
3877 }
3878
Bram Moolenaar32526b32019-01-19 17:43:09 +01003879 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 curbufIsChanged() ? (shortmess(SHM_MOD)
3881 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003882 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003884 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003885 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003887 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 : _("[readonly]")) : "",
3889 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3890 || curbuf->b_p_ro) ?
3891 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003892 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3893 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (curwin->w_cursor.lnum > 1000000L)
3895 n = (int)(((long)curwin->w_cursor.lnum) /
3896 ((long)curbuf->b_ml.ml_line_count / 100L));
3897 else
3898 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3899 (long)curbuf->b_ml.ml_line_count);
3900 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003901 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003903 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003904 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003905 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3906 curbuf->b_ml.ml_line_count),
3907 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 else
3909 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003910 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 _("line %ld of %ld --%d%%-- col "),
3912 (long)curwin->w_cursor.lnum,
3913 (long)curbuf->b_ml.ml_line_count,
3914 n);
3915 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003916 len = STRLEN(buffer);
John Marriotta21240b2025-01-08 20:10:59 +01003917 (void)col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3919 }
3920
Bram Moolenaar32526b32019-01-19 17:43:09 +01003921 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3922 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
3924 if (dont_truncate)
3925 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003926 // Temporarily set msg_scroll to avoid the message being truncated.
3927 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 msg_start();
3929 n = msg_scroll;
3930 msg_scroll = TRUE;
3931 msg(buffer);
3932 msg_scroll = n;
3933 }
3934 else
3935 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003936 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003938 // Need to repeat the message after redrawing when:
3939 // - When restart_edit is set (otherwise there will be a delay
3940 // before redrawing).
3941 // - When the screen was scrolled but there is no wait-return
3942 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003943 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945
3946 vim_free(buffer);
3947}
3948
John Marriotta21240b2025-01-08 20:10:59 +01003949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003950col_print(
3951 char_u *buf,
3952 size_t buflen,
3953 int col,
3954 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
3956 if (col == vcol)
John Marriotta21240b2025-01-08 20:10:59 +01003957 return vim_snprintf((char *)buf, buflen, "%d", col);
3958
3959 return vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960}
3961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962static char_u *lasttitle = NULL;
3963static char_u *lasticon = NULL;
3964
Bram Moolenaar84a93082018-06-16 22:58:15 +02003965/*
3966 * Put the file name in the title bar and icon of the window.
3967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003972 char_u *title_str = NULL;
3973 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int maxlen = 0;
3975 int len;
3976 int mustset;
3977 char_u buf[IOSIZE];
3978 int off;
3979
3980 if (!redrawing())
3981 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003982 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 need_maketitle = TRUE;
3984 return;
3985 }
3986
3987 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003988 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003989 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
3991 if (p_title)
3992 {
3993 if (p_titlelen > 0)
3994 {
3995 maxlen = p_titlelen * Columns / 100;
3996 if (maxlen < 10)
3997 maxlen = 10;
3998 }
3999
Bram Moolenaar84a93082018-06-16 22:58:15 +02004000 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (*p_titlestring != NUL)
4002 {
4003#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004004 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004005 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4006 (char_u *)"titlestring", 0,
4007 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 else
4009#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004010 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 else
4013 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004014 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar2c666692012-09-05 13:30:40 +02004016#define SPACE_FOR_FNAME (IOSIZE - 100)
4017#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004018#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02004020 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02004021#ifdef FEAT_TERMINAL
4022 else if (curbuf->b_term != NULL)
4023 {
4024 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
4025 SPACE_FOR_FNAME);
4026 }
4027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 else
4029 {
4030 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02004031 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034
Bram Moolenaar21554412017-07-24 21:44:43 +02004035#ifdef FEAT_TERMINAL
4036 if (curbuf->b_term == NULL)
4037#endif
4038 switch (bufIsChanged(curbuf)
4039 + (curbuf->b_p_ro * 2)
4040 + (!curbuf->b_p_ma * 4))
4041 {
4042 case 1: STRCAT(buf, " +"); break;
4043 case 2: STRCAT(buf, " ="); break;
4044 case 3: STRCAT(buf, " =+"); break;
4045 case 4:
4046 case 6: STRCAT(buf, " -"); break;
4047 case 5:
4048 case 7: STRCAT(buf, " -+"); break;
4049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
Bram Moolenaar21554412017-07-24 21:44:43 +02004051 if (curbuf->b_fname != NULL
4052#ifdef FEAT_TERMINAL
4053 && curbuf->b_term == NULL
4054#endif
4055 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004057 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 off = (int)STRLEN(buf);
4059 buf[off++] = ' ';
4060 buf[off++] = '(';
4061 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004062 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004064 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004065 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 off += 2;
4067#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004068 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004069 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004071 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004072 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004073 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004074 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004078
Bram Moolenaarc667da52019-11-30 20:52:27 +01004079 // Translate unprintable chars and concatenate. Keep some
4080 // room for the server name. When there is no room (very long
4081 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004082 if (off < SPACE_FOR_DIR)
4083 {
4084 p = transstr(buf + off);
4085 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4086 vim_free(p);
4087 }
4088 else
4089 {
4090 vim_strncpy(buf + off, (char_u *)"...",
4091 (size_t)(SPACE_FOR_ARGNR - off));
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 STRCAT(buf, ")");
4094 }
4095
Bram Moolenaar2c666692012-09-05 13:30:40 +02004096 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098#if defined(FEAT_CLIENTSERVER)
4099 if (serverName != NULL)
4100 {
4101 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004102 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 else
4105#endif
4106 STRCAT(buf, " - VIM");
4107
4108 if (maxlen > 0)
4109 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004110 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004111 if (vim_strsize(buf) > maxlen)
4112 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 }
4115 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004116 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 if (p_icon)
4119 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004120 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (*p_iconstring != NUL)
4122 {
4123#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004124 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004125 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4126 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else
4128#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004129 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 else
4132 {
4133 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004134 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004135 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004136 p = gettail(curbuf->b_ffname);
4137 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004138 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004139 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 if (len > 100)
4141 {
4142 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004144 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004145 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004147 STRCPY(icon_str, p);
4148 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150 }
4151
Bram Moolenaar84a93082018-06-16 22:58:15 +02004152 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 if (mustset)
4155 resettitle();
4156}
4157
4158/*
4159 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4160 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004161 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 */
4163 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004164value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
4166 if ((str == NULL) != (*last == NULL)
4167 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4168 {
4169 vim_free(*last);
4170 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004173 mch_restore_title(
4174 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004179 return TRUE;
4180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 return FALSE;
4183}
4184
4185/*
4186 * Put current window title back (used after calling a shell)
4187 */
4188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004189resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190{
4191 mch_settitle(lasttitle, lasticon);
4192}
Bram Moolenaarea408852005-06-25 22:49:46 +00004193
4194# if defined(EXITFREE) || defined(PROTO)
4195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004196free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004197{
4198 vim_free(lasttitle);
4199 vim_free(lasticon);
4200}
4201# endif
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004204#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004205
4206/*
4207 * Used for building in the status line.
4208 */
4209typedef struct
4210{
4211 char_u *stl_start;
4212 int stl_minwid;
4213 int stl_maxwid;
4214 enum {
4215 Normal,
4216 Empty,
4217 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004218 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004219 Highlight,
4220 TabPage,
4221 Trunc
4222 } stl_type;
4223} stl_item_T;
4224
4225static size_t stl_items_len = 20; // Initial value, grows as needed.
4226static stl_item_T *stl_items = NULL;
4227static int *stl_groupitem = NULL;
4228static stl_hlrec_T *stl_hltab = NULL;
4229static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004230static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004233 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 * Return length of string in screen cells.
4235 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004236 * Normally works for window "wp", except when working for 'tabline' then it
4237 * is "curwin".
4238 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004240 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4242 *
4243 * If maxwidth is not zero, the string will be filled at any middle marker
4244 * or truncated if too long, fillchar is used for all whitespace.
4245 */
4246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004247build_stl_str_hl(
4248 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004249 char_u *out, // buffer to write into != NameBuff
4250 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004251 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004252 char_u *opt_name, // option name corresponding to "fmt"
4253 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004254 int fillchar,
4255 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004256 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4257 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
Bram Moolenaar10772302019-01-20 18:25:54 +01004259 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004260 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 char_u *p;
4262 char_u *s;
4263 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004264 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004266 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004267 win_T *save_curwin;
4268 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004269 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270#endif
4271 int empty_line;
4272 colnr_T virtcol;
4273 long l;
4274 long n;
4275 int prevchar_isflag;
4276 int prevchar_isitem;
4277 int itemisflag;
4278 int fillable;
4279 char_u *str;
4280 long num;
4281 int width;
4282 int itemcnt;
4283 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004284 int group_end_userhl;
4285 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004287#ifdef FEAT_EVAL
4288 int evaldepth;
4289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 int minwid;
4291 int maxwid;
4292 int zeropad;
4293 char_u base;
4294 char_u opt;
4295#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004296 char_u buf_tmp[TMPLEN];
4297 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004298 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004299 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004300 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004301 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004302 // TODO: find out why using called_emsg_before makes tests fail, does it
4303 // matter?
4304 // int called_emsg_before = called_emsg;
4305 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004306
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004307 // When inside update_screen() we do not want redrawing a statusline,
4308 // ruler, title, etc. to trigger another redraw, it may cause an endless
4309 // loop.
4310 if (updating_screen)
4311 redraw_not_allowed = TRUE;
4312
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004313 if (stl_items == NULL)
4314 {
4315 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4316 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004317
4318 // Allocate one more, because the last element is used to indicate the
4319 // end of the list.
4320 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4321 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004322
4323 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004324 }
4325
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004326#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004327 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4328 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4329
4330 // When the format starts with "%!" then evaluate it as an expression and
4331 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004332 if (fmt[0] == '%' && fmt[1] == '!')
4333 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004334 typval_T tv;
4335
4336 tv.v_type = VAR_NUMBER;
4337 tv.vval.v_number = wp->w_id;
4338 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4339
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004340 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004341 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004342 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004343
4344 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004345 }
4346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 if (fillchar == 0)
4349 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004350
Bram Moolenaar10772302019-01-20 18:25:54 +01004351 // The cursor in windows other than the current one isn't always
4352 // up-to-date, esp. because of autocommands and timers.
4353 lnum = wp->w_cursor.lnum;
4354 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4355 {
4356 lnum = wp->w_buffer->b_ml.ml_line_count;
4357 wp->w_cursor.lnum = lnum;
4358 }
4359
4360 // Get line & check if empty (cursorpos will show "0-1"). Note that
4361 // p will become invalid when getting another buffer line.
4362 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004363 empty_line = (*p == NUL);
4364
Bram Moolenaar10772302019-01-20 18:25:54 +01004365 // Get the byte value now, in case we need it below. This is more efficient
4366 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004367 len = ml_get_buf_len(wp->w_buffer, lnum);
4368 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004369 {
4370 // Line may have changed since checking the cursor column, or the lnum
4371 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004372 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004373 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004374 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004375 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004376 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004377 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004380#ifdef FEAT_EVAL
4381 evaldepth = 0;
4382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 p = out;
4384 curitem = 0;
4385 prevchar_isflag = TRUE;
4386 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004387 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004389 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004390 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004391 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004392
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004393 stl_item_T *new_items =
4394 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004395 if (new_items == NULL)
4396 break;
4397 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004398
4399 int *new_groupitem =
4400 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004401 if (new_groupitem == NULL)
4402 break;
4403 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004404
4405 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004406 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004407 if (new_hlrec == NULL)
4408 break;
4409 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004410 new_hlrec = vim_realloc(stl_tabtab,
4411 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004412 if (new_hlrec == NULL)
4413 break;
4414 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004415
4416 int *new_separator_locs = vim_realloc(stl_separator_locations,
4417 sizeof(int) * new_len);
4418 if (new_separator_locs == NULL)
4419 break;
4420 stl_separator_locations = new_separator_locs;;
4421
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004422 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004423 }
4424
zeertzjq122dea72022-07-27 15:48:45 +01004425 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 prevchar_isflag = prevchar_isitem = FALSE;
4427
4428 /*
4429 * Handle up to the next '%' or the end.
4430 */
4431 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4432 *p++ = *s++;
4433 if (*s == NUL || p + 1 >= out + outlen)
4434 break;
4435
4436 /*
4437 * Handle one '%' item.
4438 */
4439 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004440 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 if (*s == '%')
4443 {
4444 if (p + 1 >= out + outlen)
4445 break;
4446 *p++ = *s++;
4447 prevchar_isflag = prevchar_isitem = FALSE;
4448 continue;
4449 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004450 // STL_SEPARATE: Separation between items, filled with white space.
4451 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 s++;
4454 if (groupdepth > 0)
4455 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004456 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004457 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 continue;
4459 }
4460 if (*s == STL_TRUNCMARK)
4461 {
4462 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004463 stl_items[curitem].stl_type = Trunc;
4464 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 continue;
4466 }
4467 if (*s == ')')
4468 {
4469 s++;
4470 if (groupdepth < 1)
4471 continue;
4472 groupdepth--;
4473
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 *p = NUL;
4476 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004477 if (curitem > stl_groupitem[groupdepth] + 1
4478 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004480 // remove group if all items are empty and highlight group
4481 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004482 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004483 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004484 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004485 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004486 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004487 group_start_userhl = group_end_userhl =
4488 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004489 break;
4490 }
4491 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004492 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004493 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004494 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004496 if (stl_items[n].stl_type == Highlight)
4497 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004498 }
4499 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004501 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 p = t;
4503 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004504 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004505 {
4506 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004507 if (stl_items[n].stl_type == Highlight)
4508 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004509 // adjust the start position of TabPage to the next
4510 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004511 if (stl_items[n].stl_type == TabPage)
4512 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004516 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004518 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (has_mbyte)
4520 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004521 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004523 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
4525 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004526 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 }
4529 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004530 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4531 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004534 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004536
4537 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004538 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004539 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
Bram Moolenaarc667da52019-11-30 20:52:27 +01004541 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004542 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
LemonBoy57ff5262022-05-09 21:03:47 +01004544 // Minus one for the leading '<' added above.
4545 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004546 if (stl_items[l].stl_start < t)
4547 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004550 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004552 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004553 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (n < 0)
4555 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004556 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 n = 0 - n;
4558 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004559 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 else
4562 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004563 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004564 l = (n - l) * MB_CHAR2LEN(fillchar);
4565 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004567 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004569 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4570 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004572 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
4574 }
4575 continue;
4576 }
4577 minwid = 0;
4578 maxwid = 9999;
4579 zeropad = FALSE;
4580 l = 1;
4581 if (*s == '0')
4582 {
4583 s++;
4584 zeropad = TRUE;
4585 }
4586 if (*s == '-')
4587 {
4588 s++;
4589 l = -1;
4590 }
4591 if (VIM_ISDIGIT(*s))
4592 {
4593 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004594 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 minwid = 0;
4596 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004597 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004599 stl_items[curitem].stl_type = Highlight;
4600 stl_items[curitem].stl_start = p;
4601 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 s++;
4603 curitem++;
4604 continue;
4605 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004606 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4607 {
4608 if (*s == STL_TABCLOSENR)
4609 {
4610 if (minwid == 0)
4611 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004612 // %X ends the close label, go back to the previously
4613 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004614 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004615 if (stl_items[n].stl_type == TabPage
4616 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004617 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004618 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004619 break;
4620 }
4621 }
4622 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004623 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004624 minwid = - minwid;
4625 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004626 stl_items[curitem].stl_type = TabPage;
4627 stl_items[curitem].stl_start = p;
4628 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004629 s++;
4630 curitem++;
4631 continue;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (*s == '.')
4634 {
4635 s++;
4636 if (VIM_ISDIGIT(*s))
4637 {
4638 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004639 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 maxwid = 50;
4641 }
4642 }
4643 minwid = (minwid > 50 ? 50 : minwid) * l;
4644 if (*s == '(')
4645 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004646 stl_groupitem[groupdepth++] = curitem;
4647 stl_items[curitem].stl_type = Group;
4648 stl_items[curitem].stl_start = p;
4649 stl_items[curitem].stl_minwid = minwid;
4650 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 s++;
4652 curitem++;
4653 continue;
4654 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004655#ifdef FEAT_EVAL
4656 // Denotes end of expanded %{} block
4657 if (*s == '}' && evaldepth > 0)
4658 {
4659 s++;
4660 evaldepth--;
4661 continue;
4662 }
4663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (vim_strchr(STL_ALL, *s) == NULL)
4665 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004666 if (*s == NUL) // can happen with "%0"
4667 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 s++;
4669 continue;
4670 }
4671 opt = *s++;
4672
Bram Moolenaarc667da52019-11-30 20:52:27 +01004673 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 base = 'D';
4675 itemisflag = FALSE;
4676 fillable = TRUE;
4677 num = -1;
4678 str = NULL;
4679 switch (opt)
4680 {
4681 case STL_FILEPATH:
4682 case STL_FULLPATH:
4683 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004684 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004686 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 else
4688 {
4689 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004690 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4692 }
4693 trans_characters(NameBuff, MAXPATHL);
4694 if (opt != STL_FILENAME)
4695 str = NameBuff;
4696 else
4697 str = gettail(NameBuff);
4698 break;
4699
Bram Moolenaarc667da52019-11-30 20:52:27 +01004700 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004701 {
4702#ifdef FEAT_EVAL
4703 char_u *block_start = s - 1;
4704#endif
4705 int reevaluate = (*s == '%');
4706
4707 if (reevaluate)
4708 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 itemisflag = TRUE;
4710 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004711 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4712 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004714 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 break;
4716 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004717 if (reevaluate)
4718 p[-1] = 0; // remove the % at the end of %{% expr %}
4719 else
4720 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004723 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4724 "%d", curbuf->b_fnum);
4725 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4726 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4727 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004729 save_curbuf = curbuf;
4730 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004731 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 curwin = wp;
4733 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004734 // Visual mode is only valid in the current window.
4735 if (curwin != save_curwin)
4736 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004738 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004740 curwin = save_curwin;
4741 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004742 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004743 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004744 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 if (str != NULL && *str != 0)
4747 {
4748 if (*skipdigits(str) == NUL)
4749 {
4750 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004751 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 itemisflag = FALSE;
4753 }
4754 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004755
4756 // If the output of the expression needs to be evaluated
4757 // replace the %{} block with the result of evaluation
4758 if (reevaluate && str != NULL && *str != 0
4759 && strchr((const char *)str, '%') != NULL
4760 && evaldepth < MAX_STL_EVAL_DEPTH)
4761 {
4762 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4763 size_t str_length = strlen((const char *)str);
4764 size_t fmt_length = strlen((const char *)s);
4765 size_t new_fmt_len = parsed_usefmt
4766 + str_length + fmt_length + 3;
4767 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4768 char_u *new_fmt_p = new_fmt;
4769
4770 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4771 + parsed_usefmt;
4772 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4773 + str_length;
4774 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4775 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4776 + fmt_length;
4777 *new_fmt_p = 0;
4778 new_fmt_p = NULL;
4779
4780 if (usefmt != fmt)
4781 vim_free(usefmt);
4782 VIM_CLEAR(str);
4783 usefmt = new_fmt;
4784 s = usefmt + parsed_usefmt;
4785 evaldepth++;
4786 continue;
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788#endif
4789 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 case STL_LINE:
4792 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4793 ? 0L : (long)(wp->w_cursor.lnum);
4794 break;
4795
4796 case STL_NUMLINES:
4797 num = wp->w_buffer->b_ml.ml_line_count;
4798 break;
4799
4800 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004801 num = (State & MODE_INSERT) == 0 && empty_line
4802 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 break;
4804
4805 case STL_VIRTCOL:
4806 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004807 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004808 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004810 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4811 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 break;
4813 num = (long)virtcol;
4814 break;
4815
4816 case STL_PERCENTAGE:
4817 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4818 (long)wp->w_buffer->b_ml.ml_line_count);
4819 break;
4820
4821 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004822 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004823 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 break;
4825
Luuk van Baalba936f62022-12-15 13:15:39 +00004826 case STL_SHOWCMD:
4827 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4828 str = showcmd_buf;
4829 break;
4830
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 case STL_ARGLISTSTAT:
4832 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004833 buf_tmp[0] = 0;
4834 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4835 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 break;
4837
4838 case STL_KEYMAP:
4839 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004840 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004841 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 break;
4843 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004844#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004845 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#else
4847 num = 0;
4848#endif
4849 break;
4850
4851 case STL_BUFNO:
4852 num = wp->w_buffer->b_fnum;
4853 break;
4854
4855 case STL_OFFSET_X:
4856 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004857 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 case STL_OFFSET:
4859#ifdef FEAT_BYTEOFF
4860 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004861 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4862 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4863 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
4865 break;
4866
4867 case STL_BYTEVAL_X:
4868 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004869 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004871 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (num == NL)
4873 num = 0;
4874 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4875 num = NL;
4876 break;
4877
4878 case STL_ROFLAG:
4879 case STL_ROFLAG_ALT:
4880 itemisflag = TRUE;
4881 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004882 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 break;
4884
4885 case STL_HELPFLAG:
4886 case STL_HELPFLAG_ALT:
4887 itemisflag = TRUE;
4888 if (wp->w_buffer->b_help)
4889 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004890 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 break;
4892
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 case STL_FILETYPE:
4894 if (*wp->w_buffer->b_p_ft != NUL
4895 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4896 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004897 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004898 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004899 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 break;
4902
4903 case STL_FILETYPE_ALT:
4904 itemisflag = TRUE;
4905 if (*wp->w_buffer->b_p_ft != NUL
4906 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4907 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004908 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004909 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004910 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004912 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
Bram Moolenaar4033c552017-09-16 20:54:51 +02004916#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 case STL_PREVIEWFLAG:
4918 case STL_PREVIEWFLAG_ALT:
4919 itemisflag = TRUE;
4920 if (wp->w_p_pvw)
4921 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4922 : _("[Preview]"));
4923 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004924
4925 case STL_QUICKFIX:
4926 if (bt_quickfix(wp->w_buffer))
4927 str = (char_u *)(wp->w_llist_ref
4928 ? _(msg_loclist)
4929 : _(msg_qflist));
4930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931#endif
4932
4933 case STL_MODIFIED:
4934 case STL_MODIFIED_ALT:
4935 itemisflag = TRUE;
4936 switch ((opt == STL_MODIFIED_ALT)
4937 + bufIsChanged(wp->w_buffer) * 2
4938 + (!wp->w_buffer->b_p_ma) * 4)
4939 {
4940 case 2: str = (char_u *)"[+]"; break;
4941 case 3: str = (char_u *)",+"; break;
4942 case 4: str = (char_u *)"[-]"; break;
4943 case 5: str = (char_u *)",-"; break;
4944 case 6: str = (char_u *)"[+-]"; break;
4945 case 7: str = (char_u *)",+-"; break;
4946 }
4947 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004948
4949 case STL_HIGHLIGHT:
4950 t = s;
4951 while (*s != '#' && *s != NUL)
4952 ++s;
4953 if (*s == '#')
4954 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004955 stl_items[curitem].stl_type = Highlight;
4956 stl_items[curitem].stl_start = p;
4957 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004958 curitem++;
4959 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004960 if (*s != NUL)
4961 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004962 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004965 stl_items[curitem].stl_start = p;
4966 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (str != NULL && *str)
4968 {
4969 t = str;
4970 if (itemisflag)
4971 {
4972 if ((t[0] && t[1])
4973 && ((!prevchar_isitem && *t == ',')
4974 || (prevchar_isflag && *t == ' ')))
4975 t++;
4976 prevchar_isflag = TRUE;
4977 }
4978 l = vim_strsize(t);
4979 if (l > 0)
4980 prevchar_isitem = TRUE;
4981 if (l > maxwid)
4982 {
4983 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (has_mbyte)
4985 {
4986 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004987 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 l -= byte2cells(*t++);
4991 if (p + 1 >= out + outlen)
4992 break;
4993 *p++ = '<';
4994 }
4995 if (minwid > 0)
4996 {
4997 for (; l < minwid && p + 1 < out + outlen; l++)
4998 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004999 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5001 *p++ = ' ';
5002 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005003 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 minwid = 0;
5006 }
5007 else
5008 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005009 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005011 // Change a space by fillchar, unless fillchar is '-' and a
5012 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005013 if (fillable && *t == ' '
5014 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5015 MB_CHAR2BYTES(fillchar, p);
5016 else
5017 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005020 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 else if (num >= 0)
5023 {
5024 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5025 char_u nstr[20];
5026
5027 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005028 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 prevchar_isitem = TRUE;
5030 t = nstr;
5031 if (opt == STL_VIRTCOL_ALT)
5032 {
5033 *t++ = '-';
5034 minwid--;
5035 }
5036 *t++ = '%';
5037 if (zeropad)
5038 *t++ = '0';
5039 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005040 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *t = 0;
5042
5043 for (n = num, l = 1; n >= nbase; n /= nbase)
5044 l++;
5045 if (opt == STL_VIRTCOL_ALT)
5046 l++;
5047 if (l > maxwid)
5048 {
5049 l += 2;
5050 n = l - maxwid;
5051 while (l-- > maxwid)
5052 num /= nbase;
5053 *t++ = '>';
5054 *t++ = '%';
5055 *t = t[-3];
5056 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005057 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5058 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005061 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5062 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 p += STRLEN(p);
5064 }
5065 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005066 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005068 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5069 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5070 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (opt == STL_VIM_EXPR)
5072 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 curitem++;
5074 }
5075 *p = NUL;
5076 itemcnt = curitem;
5077
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005078#ifdef FEAT_EVAL
5079 if (usefmt != fmt)
5080 vim_free(usefmt);
5081#endif
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 width = vim_strsize(out);
5084 if (maxwidth > 0 && width > maxwidth)
5085 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005086 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 l = 0;
5088 if (itemcnt == 0)
5089 s = out;
5090 else
5091 {
5092 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005093 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005095 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005096 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 }
5099 if (l == itemcnt)
5100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005101 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005102 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 l = 0;
5104 }
5105 }
5106
5107 if (width - vim_strsize(s) >= maxwidth)
5108 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005109 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if (has_mbyte)
5111 {
5112 s = out;
5113 width = 0;
5114 for (;;)
5115 {
5116 width += ptr2cells(s);
5117 if (width >= maxwidth)
5118 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005119 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005121 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005123 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 s = out + maxwidth - 1;
5127 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005128 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130 itemcnt = l;
5131 *s++ = '>';
5132 *s = 0;
5133 }
5134 else
5135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (has_mbyte)
5137 {
5138 n = 0;
5139 while (width >= maxwidth)
5140 {
5141 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005142 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 }
5145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 n = width - maxwidth + 1;
5147 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005148 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 *s = '<';
5150
Bram Moolenaarc667da52019-11-30 20:52:27 +01005151 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 for (; l < itemcnt; l++)
5153 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005154 if (stl_items[l].stl_start - n >= s)
5155 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005157 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
zeertzjqd392a742023-07-01 20:24:40 +01005159
5160 // Fill up for half a double-wide character.
5161 while (++width < maxwidth)
5162 {
5163 s = s + STRLEN(s);
5164 MB_CHAR2BYTES(fillchar, s);
5165 *s = NUL;
5166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 width = maxwidth;
5169 }
5170 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5171 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005172 // Find how many separators there are, which we will use when
5173 // figuring out how many groups there are.
5174 int num_separators = 0;
5175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005178 if (stl_items[l].stl_type == Separate)
5179 {
5180 // Create an array of the start location for each separator
5181 // mark.
5182 stl_separator_locations[num_separators] = l;
5183 num_separators++;
5184 }
5185 }
5186
5187 // If we have separated groups, then we deal with it now
5188 if (num_separators)
5189 {
5190 int standard_spaces;
5191 int final_spaces;
5192
5193 standard_spaces = (maxwidth - width) / num_separators;
5194 final_spaces = (maxwidth - width) -
5195 standard_spaces * (num_separators - 1);
5196 for (l = 0; l < num_separators; l++)
5197 {
5198 int dislocation = (l == (num_separators - 1)) ?
5199 final_spaces : standard_spaces;
5200 dislocation *= MB_CHAR2LEN(fillchar);
5201 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5202 char_u *seploc = start + dislocation;
5203 STRMOVE(seploc, start);
5204 for (s = start; s < seploc;)
5205 MB_CHAR2BYTES(fillchar, s);
5206
5207 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5208 stl_items[i].stl_start += dislocation;
5209 }
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 width = maxwidth;
5212 }
5213 }
5214
Bram Moolenaarc667da52019-11-30 20:52:27 +01005215 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005216 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005218 *hltab = stl_hltab;
5219 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 for (l = 0; l < itemcnt; l++)
5221 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005222 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005224 sp->start = stl_items[l].stl_start;
5225 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005226 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005229 sp->start = NULL;
5230 sp->userhl = 0;
5231 }
5232
Bram Moolenaarc667da52019-11-30 20:52:27 +01005233 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005234 if (tabtab != NULL)
5235 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005236 *tabtab = stl_tabtab;
5237 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005238 for (l = 0; l < itemcnt; l++)
5239 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005240 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005241 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005242 sp->start = stl_items[l].stl_start;
5243 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005244 sp++;
5245 }
5246 }
5247 sp->start = NULL;
5248 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005251 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005252
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005253 // A user function may reset KeyTyped, restore it.
5254 KeyTyped = save_KeyTyped;
5255
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005256 // Check for an error. If there is one the display will be messed up and
5257 // might loop redrawing. Avoid that by making the corresponding option
5258 // empty.
5259 // TODO: find out why using called_emsg_before makes tests fail, does it
5260 // matter?
5261 // if (called_emsg > called_emsg_before)
5262 if (did_emsg > did_emsg_before)
5263 set_string_option_direct(opt_name, -1, (char_u *)"",
5264 OPT_FREE | opt_scope, SID_ERROR);
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 return width;
5267}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005268#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005271 * Get relative cursor position in window into "buf[buflen]", in the localized
5272 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 */
John Marriotta21240b2025-01-08 20:10:59 +01005274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005275get_rel_pos(
5276 win_T *wp,
5277 char_u *buf,
5278 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005280 long above; // number of lines above window
5281 long below; // number of lines below window
John Marriotta21240b2025-01-08 20:10:59 +01005282 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
Bram Moolenaarc667da52019-11-30 20:52:27 +01005284 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005285 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 above = wp->w_topline - 1;
5287#ifdef FEAT_DIFF
5288 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005289 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005290 above = 0; // All buffer lines are displayed and there is an
5291 // indication of filler lines, that can be considered
5292 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#endif
5294 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5295 if (below <= 0)
John Marriotta21240b2025-01-08 20:10:59 +01005296 len = vim_snprintf((char *)buf, buflen, "%s", (above == 0) ? _("All") : _("Bot"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 else if (above <= 0)
John Marriotta21240b2025-01-08 20:10:59 +01005298 len = vim_snprintf((char *)buf, buflen, "%s", _("Top"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005300 {
5301 int perc = (above > 1000000L)
John Marriotta21240b2025-01-08 20:10:59 +01005302 ? (int)(above / ((above + below) / 100L))
5303 : (int)(above * 100L / (above + below));
Emir SARI971cd2b2023-04-29 12:09:53 +01005304
Emir SARI971cd2b2023-04-29 12:09:53 +01005305 // localized percentage value
John Marriotta21240b2025-01-08 20:10:59 +01005306 len = vim_snprintf((char *)buf, buflen, _("%s%d%%"), (perc < 10) ? " " : "", perc);
Emir SARI971cd2b2023-04-29 12:09:53 +01005307 }
John Marriotta21240b2025-01-08 20:10:59 +01005308 if (len < 0)
5309 {
5310 buf[0] = NUL;
5311 len = 0;
5312 }
5313 else if (len > buflen - 1)
5314 len = buflen - 1;
5315
5316 return len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
5319/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005320 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 * Return TRUE if it was appended.
5322 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005324append_arg_number(
5325 win_T *wp,
5326 char_u *buf,
5327 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005328 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005330 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 return FALSE;
5332
Bram Moolenaara8490a42023-05-23 18:00:58 +01005333 char *msg;
5334 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005336 case 0: msg = _(" (%d of %d)"); break;
5337 case 1: msg = _(" ((%d) of %d)"); break;
5338 case 2: msg = _(" (file %d of %d)"); break;
5339 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005341
5342 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5343 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5344 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 return TRUE;
5346}
5347
5348/*
5349 * If fname is not a full path, make it a full path.
5350 * Returns pointer to allocated memory (NULL for failure).
5351 */
5352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005353fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 /*
5356 * Force expanding the path always for Unix, because symbolic links may
5357 * mess up the full path name, even though it starts with a '/'.
5358 * Also expand when there is ".." in the file name, try to remove it,
5359 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005360 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 * For MS-Windows also expand names like "longna~1" to "longname".
5362 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005363#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return FullName_save(fname, TRUE);
5365#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005366 if (!vim_isAbsName(fname)
5367 || strstr((char *)fname, "..") != NULL
5368 || strstr((char *)fname, "//") != NULL
5369# ifdef BACKSLASH_IN_FILENAME
5370 || strstr((char *)fname, "\\\\") != NULL
5371# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005372# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 )
5376 return FullName_save(fname, FALSE);
5377
5378 fname = vim_strsave(fname);
5379
Bram Moolenaar9b942202007-10-03 12:31:33 +00005380# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005381 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005382 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 return fname;
5386#endif
5387}
5388
5389/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005390 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5391 * "*ffname" becomes a pointer to allocated memory (or NULL).
5392 * When resolving a link both "*sfname" and "*ffname" will point to the same
5393 * allocated memory.
5394 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005395 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398fname_expand(
5399 buf_T *buf UNUSED,
5400 char_u **ffname,
5401 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005403 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005405 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005407 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409#ifdef FEAT_SHORTCUT
5410 if (!buf->b_p_bin)
5411 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005412 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005414 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005415 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005416 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 vim_free(*ffname);
5419 *ffname = rfname;
5420 *sfname = rfname;
5421 }
5422 }
5423#endif
5424}
5425
5426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 * Open a window for a number of buffers.
5428 */
5429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005430ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 buf_T *buf;
5433 win_T *wp, *wpnext;
5434 int split_ret = OK;
5435 int p_ea_save;
5436 int open_wins = 0;
5437 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005438 int count; // Maximum number of windows to open.
5439 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005440 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005441 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
Bram Moolenaarc667da52019-11-30 20:52:27 +01005443 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 count = 9999;
5445 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005446 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5448 all = FALSE;
5449 else
5450 all = TRUE;
5451
Pavel Mayorove1121b12023-02-20 14:35:20 +00005452 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5453 // switching to another buffer.
5454 reset_VIsual_and_resel();
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 setpcmark();
5457
5458#ifdef FEAT_GUI
5459 need_mouse_correct = TRUE;
5460#endif
5461
5462 /*
5463 * Close superfluous windows (two windows for the same buffer).
5464 * Also close windows that are not full-width.
5465 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005466 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005467 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005468 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005470 tpnext = curtab->tp_next;
5471 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005473 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005474 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005475 || ((cmdmod.cmod_split & WSP_VERT)
5476 ? wp->w_height + wp->w_status_height < Rows - p_ch
5477 - tabline_height()
5478 : wp->w_width != Columns)
5479 || (had_tab > 0 && wp != firstwin))
5480 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005481 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005482 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005483 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005484 if (win_close(wp, FALSE) == FAIL)
5485 break;
5486 // Just in case an autocommand does something strange with
5487 // windows: start all over...
5488 wpnext = firstwin;
5489 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005490 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005491 }
5492 else
5493 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005495
Bram Moolenaarc667da52019-11-30 20:52:27 +01005496 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005497 if (had_tab == 0 || tpnext == NULL)
5498 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005499 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501
5502 /*
5503 * Go through the buffer list. When a buffer doesn't have a window yet,
5504 * open one. Otherwise move the window to the right position.
5505 * Watch out for autocommands that delete buffers or windows!
5506 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005507 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5512 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005513 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5515 continue;
5516
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005517 if (had_tab != 0)
5518 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005519 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005520 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005521 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005522 else
5523 wp = NULL;
5524 }
5525 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005526 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005527 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005528 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005529 if (wp->w_buffer == buf)
5530 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005531 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005532 if (wp != NULL)
5533 win_move_after(wp, curwin);
5534 }
5535
5536 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005538 bufref_T bufref;
5539
5540 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005541
Bram Moolenaarc667da52019-11-30 20:52:27 +01005542 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005544 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5546 ++open_wins;
5547 p_ea = p_ea_save;
5548 if (split_ret == FAIL)
5549 continue;
5550
Bram Moolenaarc667da52019-11-30 20:52:27 +01005551 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005554 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005556 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 break;
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (swap_exists_action == SEA_QUIT)
5561 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005562#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005563 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005564
Bram Moolenaarc667da52019-11-30 20:52:27 +01005565 // Reset the error/interrupt/exception state here so that
5566 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005567 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005568#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005569
Bram Moolenaarc667da52019-11-30 20:52:27 +01005570 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 win_close(curwin, TRUE);
5572 --open_wins;
5573 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005574 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005575
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005576#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005577 // Restore the error/interrupt/exception state if not
5578 // discarded by a new aborting error, interrupt, or uncaught
5579 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005580 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 else
5584 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
5586
5587 ui_breakcheck();
5588 if (got_int)
5589 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005590 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 break;
5592 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005593#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005594 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005595 if (aborting())
5596 break;
5597#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005598 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005599 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005600 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005603 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606 /*
5607 * Close superfluous windows.
5608 */
5609 for (wp = lastwin; open_wins > count; )
5610 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005611 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if (!win_valid(wp))
5614 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005615 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 wp = lastwin;
5617 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005618 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005620 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 --open_wins;
5622 wp = lastwin;
5623 }
5624 else
5625 {
5626 wp = wp->w_prev;
5627 if (wp == NULL)
5628 break;
5629 }
5630 }
5631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005634static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005635
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636/*
5637 * do_modelines() - process mode lines for the current file
5638 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005639 * "flags" can be:
5640 * OPT_WINONLY only set options local to window
5641 * OPT_NOWIN don't set options local to window
5642 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 * Returns immediately if the "ml" option isn't set.
5644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005646do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005648 linenr_T lnum;
5649 int nmlines;
5650 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5653 return;
5654
Bram Moolenaarc667da52019-11-30 20:52:27 +01005655 // Disallow recursive entry here. Can happen when executing a modeline
5656 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (entered)
5658 return;
5659
5660 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005661 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005663 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 nmlines = 0;
5665
Hu Jialun9dcd3492021-08-28 20:42:50 +02005666 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005668 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 nmlines = 0;
5670 --entered;
5671}
5672
Bram Moolenaarc667da52019-11-30 20:52:27 +01005673#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675/*
5676 * chk_modeline() - check a single line for a mode string
5677 * Return FAIL if an error encountered.
5678 */
5679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005680chk_modeline(
5681 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005682 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 char_u *s;
5685 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005686 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 int prev;
5688 int vers;
5689 int end;
5690 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005691 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005692 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 prev = -1;
5695 for (s = ml_get(lnum); *s != NUL; ++s)
5696 {
5697 if (prev == -1 || vim_isspace(prev))
5698 {
5699 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5700 || STRNCMP(s, "vi:", (size_t)3) == 0)
5701 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005702 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005703 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5706 e = s + 4;
5707 else
5708 e = s + 3;
5709 vers = getdigits(&e);
5710 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005711 && (s[0] != 'V'
5712 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005714 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 || (VIM_VERSION_100 < vers && s[3] == '<')
5716 || (VIM_VERSION_100 > vers && s[3] == '>')
5717 || (VIM_VERSION_100 == vers && s[3] == '=')))
5718 break;
5719 }
5720 }
5721 prev = *s;
5722 }
5723
5724 if (*s)
5725 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005726 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 ++s;
5728 while (s[-1] != ':');
5729
Bram Moolenaarc667da52019-11-30 20:52:27 +01005730 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (linecopy == NULL)
5732 return FAIL;
5733
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005734 // prepare for emsg()
5735 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005736 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 end = FALSE;
5739 while (end == FALSE)
5740 {
5741 s = skipwhite(s);
5742 if (*s == NUL)
5743 break;
5744
5745 /*
5746 * Find end of set command: ':' or end of line.
5747 * Skip over "\:", replacing it with ":".
5748 */
5749 for (e = s; *e != ':' && *e != NUL; ++e)
5750 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005751 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (*e == NUL)
5753 end = TRUE;
5754
5755 /*
5756 * If there is a "set" command, require a terminating ':' and
5757 * ignore the stuff after the ':'.
5758 * "vi:set opt opt opt: foo" -- foo not interpreted
5759 * "vi:opt opt opt: foo" -- foo interpreted
5760 * Accept "se" for compatibility with Elvis.
5761 */
5762 if (STRNCMP(s, "set ", (size_t)4) == 0
5763 || STRNCMP(s, "se ", (size_t)3) == 0)
5764 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005765 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 break;
5767 end = TRUE;
5768 s = vim_strchr(s, ' ') + 1;
5769 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005770 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
Bram Moolenaarc667da52019-11-30 20:52:27 +01005772 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005774 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005775
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005776 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005777 current_sctx.sc_version = 1;
5778#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005779 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005780 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005781 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005783
Bram Moolenaar5958f952018-11-20 04:25:21 +01005784 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005785 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005786
Bram Moolenaara3227e22006-03-08 21:32:40 +00005787 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005788
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005789 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005790 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005791 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005794 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
5796
ichizok7e5fe382023-04-15 13:17:50 +01005797 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005798 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 vim_free(linecopy);
5800 }
5801 return retval;
5802}
5803
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005804/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005805 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5806 */
5807 int
5808bt_normal(buf_T *buf)
5809{
5810 return buf != NULL && buf->b_p_bt[0] == NUL;
5811}
5812
5813/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005814 * Return TRUE if "buf" is the quickfix buffer.
5815 */
5816 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005817bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005818{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005819#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005820 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005821#else
5822 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005823#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005824}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005825
5826/*
5827 * Return TRUE if "buf" is a terminal buffer.
5828 */
5829 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005830bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005831{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005832#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005833 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005834#else
5835 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005836#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005837}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005838
5839/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005840 * Return TRUE if "buf" is a help buffer.
5841 */
5842 int
5843bt_help(buf_T *buf)
5844{
5845 return buf != NULL && buf->b_help;
5846}
5847
5848/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005849 * Return TRUE if "buf" is a prompt buffer.
5850 */
5851 int
5852bt_prompt(buf_T *buf)
5853{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005854 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5855}
5856
Dominique Pelle748b3082022-01-08 12:41:16 +00005857#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005858/*
5859 * Return TRUE if "buf" is a buffer for a popup window.
5860 */
5861 int
5862bt_popup(buf_T *buf)
5863{
5864 return buf != NULL && buf->b_p_bt != NULL
5865 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005866}
Dominique Pelle748b3082022-01-08 12:41:16 +00005867#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005868
5869/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005870 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005871 * buffer. This means the buffer name may not be a file name, at least not for
5872 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005873 */
5874 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005875bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005876{
5877 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5878 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005879 || buf->b_p_bt[0] == 't'
5880 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005881}
5882
Bram Moolenaarc3126192022-08-26 12:58:17 +01005883/*
5884 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5885 * buffer. This means the buffer is not to be read from a file.
5886 */
5887 static int
5888bt_nofileread(buf_T *buf)
5889{
5890 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5891 || buf->b_p_bt[0] == 't'
5892 || buf->b_p_bt[0] == 'q'
5893 || buf->b_p_bt[0] == 'p');
5894}
5895
Dominique Pelle748b3082022-01-08 12:41:16 +00005896#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005897/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005898 * Return TRUE if "buf" has 'buftype' set to "nofile".
5899 */
5900 int
5901bt_nofile(buf_T *buf)
5902{
5903 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5904}
Dominique Pelle748b3082022-01-08 12:41:16 +00005905#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005906
5907/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005908 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5909 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005910 */
5911 int
5912bt_dontwrite(buf_T *buf)
5913{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005914 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005915 || buf->b_p_bt[0] == 't'
5916 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005917}
5918
5919 int
5920bt_dontwrite_msg(buf_T *buf)
5921{
5922 if (bt_dontwrite(buf))
5923 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005924 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005925 return TRUE;
5926 }
5927 return FALSE;
5928}
5929
5930/*
5931 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5932 * and 'bufhidden'.
5933 */
5934 int
5935buf_hide(buf_T *buf)
5936{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005937 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005938 switch (buf->b_p_bh[0])
5939 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005940 case 'u': // "unload"
5941 case 'w': // "wipe"
5942 case 'd': return FALSE; // "delete"
5943 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005944 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005945 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948/*
5949 * Return special buffer name.
5950 * Returns NULL when the buffer has a normal file name.
5951 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005952 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005953buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005955#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005957 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005958 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005959 * Differentiate between the quickfix and location list buffers using
5960 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005961 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005962 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005963 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005964 else
5965 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005968
Bram Moolenaarc667da52019-11-30 20:52:27 +01005969 // There is no _file_ when 'buftype' is "nofile", b_sfname
5970 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005971 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005973#ifdef FEAT_TERMINAL
5974 if (buf->b_term != NULL)
5975 return term_get_status_text(buf->b_term);
5976#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005977 if (buf->b_fname != NULL)
5978 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005979 if (buf == cmdwin_buf)
5980 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005981#ifdef FEAT_JOB_CHANNEL
5982 if (bt_prompt(buf))
5983 return (char_u *)_("[Prompt]");
5984#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005985#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005986 if (bt_popup(buf))
5987 return (char_u *)_("[Popup]");
5988#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005989 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005993 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 return NULL;
5995}
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005998 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5999 */
6000 char_u *
6001buf_get_fname(buf_T *buf)
6002{
6003 if (buf->b_fname == NULL)
6004 return (char_u *)_("[No Name]");
6005 return buf->b_fname;
6006}
6007
6008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6010 */
6011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006014 if (on == curbuf->b_p_bl)
6015 return;
6016
6017 curbuf->b_p_bl = on;
6018 if (on)
6019 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6020 else
6021 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022}
6023
6024/*
6025 * Read the file for "buf" again and check if the contents changed.
6026 * Return TRUE if it changed or this could not be checked.
6027 */
6028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006029buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 buf_T *newbuf;
6032 int differ = TRUE;
6033 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 exarg_T ea;
6036
Bram Moolenaarc667da52019-11-30 20:52:27 +01006037 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6039 if (newbuf == NULL)
6040 return TRUE;
6041
Bram Moolenaarc667da52019-11-30 20:52:27 +01006042 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 if (prep_exarg(&ea, buf) == FAIL)
6044 {
6045 wipe_buffer(newbuf, FALSE);
6046 return TRUE;
6047 }
6048
Bram Moolenaare76062c2022-11-28 18:51:43 +00006049 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006051 if (curbuf != newbuf)
6052 {
6053 // Failed to find a window for "newbuf".
6054 wipe_buffer(newbuf, FALSE);
6055 return TRUE;
6056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006058 // We don't want to trigger autocommands now, they may have nasty
6059 // side-effects like wiping buffers
6060 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006061 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 && readfile(buf->b_ffname, buf->b_fname,
6063 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6064 &ea, READ_NEW | READ_DUMMY) == OK)
6065 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006066 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6068 {
6069 differ = FALSE;
6070 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6071 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6072 {
6073 differ = TRUE;
6074 break;
6075 }
6076 }
6077 }
6078 vim_free(ea.cmd);
6079
Bram Moolenaarc667da52019-11-30 20:52:27 +01006080 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
Bram Moolenaarc667da52019-11-30 20:52:27 +01006083 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 wipe_buffer(newbuf, FALSE);
6085
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006086 unblock_autocmds();
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 return differ;
6089}
6090
6091/*
6092 * Wipe out a buffer and decrement the last buffer number if it was used for
6093 * this buffer. Call this to wipe out a temp buffer that does not contain any
6094 * marks.
6095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097wipe_buffer(
6098 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006099 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 if (buf->b_fnum == top_file_num - 1)
6102 --top_file_num;
6103
Bram Moolenaarc667da52019-11-30 20:52:27 +01006104 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006105 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006106
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006107 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006110 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111}