blob: f1e133095e8dddab54315d38c785d89d525a87c6 [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);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02002415 clear_string_option(&buf->b_p_fexpr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002418# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002419 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2420 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002421 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 clear_string_option(&buf->b_p_key);
2424#endif
2425 clear_string_option(&buf->b_p_kp);
2426 clear_string_option(&buf->b_p_mps);
2427 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002428 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002430#ifdef FEAT_VARTABS
2431 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002432 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002433 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002434 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002435 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437#ifdef FEAT_KEYMAP
2438 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002439 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 ga_clear(&buf->b_kmap_ga);
2441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#ifdef FEAT_FOLDING
2444 clear_string_option(&buf->b_p_cms);
2445#endif
2446 clear_string_option(&buf->b_p_nf);
2447#ifdef FEAT_SYN_HL
2448 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002449 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002450#endif
2451#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002452 clear_string_option(&buf->b_s.b_p_spc);
2453 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002454 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002455 buf->b_s.b_cap_prog = NULL;
2456 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002457 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 clear_string_option(&buf->b_p_cink);
2462 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002463 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002464 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002466 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002468#ifdef FEAT_COMPL_FUNC
2469 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002470 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002471 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002472 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002473 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002474 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476#ifdef FEAT_QUICKFIX
2477 clear_string_option(&buf->b_p_gp);
2478 clear_string_option(&buf->b_p_mp);
2479 clear_string_option(&buf->b_p_efm);
2480#endif
2481 clear_string_option(&buf->b_p_ep);
2482 clear_string_option(&buf->b_p_path);
2483 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002484 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002485#ifdef FEAT_EVAL
2486 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002487 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 clear_string_option(&buf->b_p_dict);
2490 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002491 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002493 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002494 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002495 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002496 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497}
2498
2499/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002500 * Get alternate file "n".
2501 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2502 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * if (options & GETF_SETMARK) call setpcmark()
2504 * if (options & GETF_ALT) we are jumping to an alternate file.
2505 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2506 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002507 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
2509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510buflist_getfile(
2511 int n,
2512 linenr_T lnum,
2513 int options,
2514 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 pos_T *fpos;
2519 colnr_T col;
2520
2521 buf = buflist_findnr(n);
2522 if (buf == NULL)
2523 {
2524 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002525 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002527 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 return FAIL;
2529 }
2530
Bram Moolenaarc667da52019-11-30 20:52:27 +01002531 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (buf == curbuf)
2533 return OK;
2534
Bram Moolenaar71223e22022-05-30 15:23:09 +01002535 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002536 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaarc667da52019-11-30 20:52:27 +01002538 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (lnum == 0)
2540 {
2541 fpos = buflist_findfpos(buf);
2542 lnum = fpos->lnum;
2543 col = fpos->col;
2544 }
2545 else
2546 col = 0;
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (options & GETF_SWITCH)
2549 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002550 // If 'switchbuf' is set jump to the window containing "buf".
2551 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002552
Bram Moolenaarc667da52019-11-30 20:52:27 +01002553 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2554 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002555 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002556 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002558 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002559 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002560 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2561 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002563 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
2567 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002568 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002569 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2570 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002572 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (!p_sol && col != 0)
2574 {
2575 curwin->w_cursor.col = col;
2576 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 curwin->w_set_curswant = TRUE;
2579 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002580 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002582
2583 if (RedrawingDisabled > 0)
2584 --RedrawingDisabled;
2585 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586}
2587
2588/*
2589 * go to the last know line number for the current buffer
2590 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002592buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 pos_T *fpos;
2595
2596 fpos = buflist_findfpos(curbuf);
2597
2598 curwin->w_cursor.lnum = fpos->lnum;
2599 check_cursor_lnum();
2600
2601 if (p_sol)
2602 curwin->w_cursor.col = 0;
2603 else
2604 {
2605 curwin->w_cursor.col = fpos->col;
2606 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 curwin->w_set_curswant = TRUE;
2609 }
2610}
2611
Bram Moolenaar81695252004-12-29 20:58:21 +00002612/*
2613 * Find file in buffer list by name (it has to be for the current window).
2614 * Returns NULL if not found.
2615 */
2616 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002617buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002618{
2619 char_u *ffname;
2620 buf_T *buf = NULL;
2621
Bram Moolenaarc667da52019-11-30 20:52:27 +01002622 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002623 ffname = FullName_save(fname,
2624#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002625 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002626#else
2627 FALSE
2628#endif
2629 );
2630 if (ffname != NULL)
2631 {
2632 buf = buflist_findname(ffname);
2633 vim_free(ffname);
2634 }
2635 return buf;
2636}
Bram Moolenaar81695252004-12-29 20:58:21 +00002637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638/*
2639 * Find file in buffer list by name (it has to be for the current window).
2640 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002641 * Skips dummy buffers.
2642 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 */
2644 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002645buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002648 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 if (mch_stat((char *)ffname, &st) < 0)
2651 st.st_dev = (dev_T)-1;
2652 return buflist_findname_stat(ffname, &st);
2653}
2654
2655/*
2656 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2657 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002658 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 */
2660 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002661buflist_findname_stat(
2662 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002663 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665#endif
2666 buf_T *buf;
2667
Bram Moolenaarc667da52019-11-30 20:52:27 +01002668 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002669 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002670 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#ifdef UNIX
2672 , stp
2673#endif
2674 ))
2675 return buf;
2676 return NULL;
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
2680 * Find file in buffer list by a regexp pattern.
2681 * Return fnum of the found buffer.
2682 * Return < 0 for error.
2683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002685buflist_findpat(
2686 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002687 char_u *pattern_end, // pointer to first char after pattern
2688 int unlisted, // find unlisted buffers
2689 int diffmode UNUSED, // find diff-mode buffers only
2690 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 int match = -1;
2694 int find_listed;
2695 char_u *pat;
2696 char_u *patend;
2697 int attempt;
2698 char_u *p;
2699 int toggledollar;
2700
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002701 // "%" is current file, "%%" or "#" is alternate file
2702 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2703 || (in_vim9script() && pattern_end == pattern + 2
2704 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002706 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002708 else
2709 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#ifdef FEAT_DIFF
2711 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2712 match = -1;
2713#endif
2714 }
2715
2716 /*
2717 * Try four ways of matching a listed buffer:
2718 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002719 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 * attempt == 2: with '$' at end (only match at end)
2721 * attempt == 3: with '^' at start and '$' at end (only full match)
2722 * Repeat this for finding an unlisted buffer if there was no matching
2723 * listed buffer.
2724 */
2725 else
2726 {
2727 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2728 if (pat == NULL)
2729 return -1;
2730 patend = pat + STRLEN(pat) - 1;
2731 toggledollar = (patend > pat && *patend == '$');
2732
Bram Moolenaarc667da52019-11-30 20:52:27 +01002733 // First try finding a listed buffer. If not found and "unlisted"
2734 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 find_listed = TRUE;
2736 for (;;)
2737 {
2738 for (attempt = 0; attempt <= 3; ++attempt)
2739 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002740 regmatch_T regmatch;
2741
Bram Moolenaarc667da52019-11-30 20:52:27 +01002742 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002744 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002746 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002748 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002750 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002751 {
2752 if (regmatch.regprog == NULL)
2753 {
2754 // invalid pattern, possibly after switching engine
2755 vim_free(pat);
2756 return -1;
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (buf->b_p_bl == find_listed
2759#ifdef FEAT_DIFF
2760 && (!diffmode || diff_mode_buf(buf))
2761#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002762 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002764 if (curtab_only)
2765 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002766 // Ignore the match if the buffer is not open in
2767 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002768 win_T *wp;
2769
Bram Moolenaar29323592016-07-24 22:04:11 +02002770 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002771 if (wp->w_buffer == buf)
2772 break;
2773 if (wp == NULL)
2774 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002775 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002776 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
2778 match = -2;
2779 break;
2780 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002781 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002785 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002786 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 break;
2788 }
2789
Bram Moolenaarc667da52019-11-30 20:52:27 +01002790 // Only search for unlisted buffers if there was no match with
2791 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (!unlisted || !find_listed || match != -1)
2793 break;
2794 find_listed = FALSE;
2795 }
2796
2797 vim_free(pat);
2798 }
2799
2800 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002801 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002803 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return match;
2805}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
Bram Moolenaar52410572019-10-27 05:12:45 +01002807#ifdef FEAT_VIMINFO
2808typedef struct {
2809 buf_T *buf;
2810 char_u *match;
2811} bufmatch_T;
2812#endif
2813
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814/*
2815 * Find all buffer names that match.
2816 * For command line expansion of ":buf" and ":sbuf".
2817 * Return OK if matches found, FAIL otherwise.
2818 */
2819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002820ExpandBufnames(
2821 char_u *pat,
2822 int *num_file,
2823 char_u ***file,
2824 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002826 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 buf_T *buf;
2828 int round;
2829 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002830 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002831#ifdef FEAT_VIMINFO
2832 bufmatch_T *matches = NULL;
2833#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002834 int fuzzy;
2835 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002836 regmatch_T regmatch;
2837 int score = 0;
2838 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaarc667da52019-11-30 20:52:27 +01002840 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 *file = NULL;
2842
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002843#ifdef FEAT_DIFF
2844 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2845 return FAIL;
2846#endif
2847
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002848 fuzzy = cmdline_fuzzy_complete(pat);
2849
2850 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2851 // expression matching)
2852 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002854 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002855 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002856 int len = (int)STRLEN(pat);
2857 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002858 if (patc == NULL)
2859 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002860 STRNCPY(patc, pat + 1, len - 1);
2861 patc[len - 1] = NUL;
2862 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002863 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002864 else if (*pat == '^')
2865 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002866 else
2867 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002868 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002869 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002870
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002871 // round == 1: Count the matches.
2872 // round == 2: Build the array to keep the matches.
2873 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002874 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002875 count = 0;
2876 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002877 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002878 if (!buf->b_p_bl) // skip unlisted buffers
2879 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002880#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002881 if (options & BUF_DIFF_FILTER)
2882 // Skip buffers not suitable for
2883 // :diffget or :diffput completion.
2884 if (buf == curbuf || !diff_mode_buf(buf))
2885 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002886#endif
2887
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002888 if (!fuzzy)
2889 {
2890 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002891 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002892 // invalid pattern, possibly after recompiling
2893 if (to_free)
2894 vim_free(patc);
2895 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002896 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002897 p = buflist_match(&regmatch, buf, p_wic);
2898 }
2899 else
2900 {
2901 p = NULL;
2902 // first try matching with the short file name
2903 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2904 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002905 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002906 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002907 // next try matching with the full path file name
2908 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2909 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
2911 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002912
2913 if (p == NULL)
2914 continue;
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (round == 1)
2917 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002918 ++count;
2919 continue;
2920 }
2921
2922 if (options & WILD_HOME_REPLACE)
2923 p = home_replace_save(buf, p);
2924 else
2925 p = vim_strsave(p);
2926
2927 if (!fuzzy)
2928 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002929#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002930 if (matches != NULL)
2931 {
2932 matches[count].buf = buf;
2933 matches[count].match = p;
2934 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002935 }
2936 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002937#endif
2938 (*file)[count++] = p;
2939 }
2940 else
2941 {
2942 fuzmatch[count].idx = count;
2943 fuzmatch[count].str = p;
2944 fuzmatch[count].score = score;
2945 count++;
2946 }
2947 }
2948 if (count == 0) // no match found, break here
2949 break;
2950 if (round == 1)
2951 {
2952 if (!fuzzy)
2953 {
2954 *file = ALLOC_MULT(char_u *, count);
2955 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002956 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002957 vim_regfree(regmatch.regprog);
2958 if (to_free)
2959 vim_free(patc);
2960 return FAIL;
2961 }
2962#ifdef FEAT_VIMINFO
2963 if (options & WILD_BUFLASTUSED)
2964 matches = ALLOC_MULT(bufmatch_T, count);
2965#endif
2966 }
2967 else
2968 {
2969 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2970 if (fuzmatch == NULL)
2971 {
2972 *num_file = 0;
2973 *file = NULL;
2974 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
2979
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002980 if (!fuzzy)
2981 {
2982 vim_regfree(regmatch.regprog);
2983 if (to_free)
2984 vim_free(patc);
2985 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002986
Bram Moolenaar52410572019-10-27 05:12:45 +01002987#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002988 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002989 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002990 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002991 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002992 int i;
2993 if (count > 1)
2994 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2995 // if the current buffer is first in the list, place it at the end
2996 if (matches[0].buf == curbuf)
2997 {
2998 for (i = 1; i < count; i++)
2999 (*file)[i-1] = matches[i].match;
3000 (*file)[count-1] = matches[0].match;
3001 }
3002 else
3003 {
3004 for (i = 0; i < count; i++)
3005 (*file)[i] = matches[i].match;
3006 }
3007 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003008 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003009 }
3010 else
3011 {
3012 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3013 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003014 }
3015#endif
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 *num_file = count;
3018 return (count == 0 ? FAIL : OK);
3019}
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021/*
3022 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003023 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 */
3025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003026buflist_match(
3027 regmatch_T *rmp,
3028 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003029 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 char_u *match;
3032
Bram Moolenaarc667da52019-11-30 20:52:27 +01003033 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003034 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003035 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003036 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038 return match;
3039}
3040
3041/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003042 * Try matching the regexp in "rmp->regprog" with file name "name".
3043 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 * Return "name" when there is a match, NULL when not.
3045 */
3046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003047fname_match(
3048 regmatch_T *rmp,
3049 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003050 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 char_u *match = NULL;
3053 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003055 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003056 if (name == NULL || rmp->regprog == NULL)
3057 return NULL;
3058
3059 // Ignore case when 'fileignorecase' or the argument is set.
3060 rmp->rm_ic = p_fic || ignore_case;
3061 if (vim_regexec(rmp, name, (colnr_T)0))
3062 match = name;
3063 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003065 // Replace $(HOME) with '~' and try matching again.
3066 p = home_replace_save(NULL, name);
3067 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003069 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071
3072 return match;
3073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
3075/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003076 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 */
3078 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003081 char_u key[VIM_SIZEOF_INT * 2 + 1];
3082 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 if (nr == 0)
3085 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003086 sprintf((char *)key, "%x", nr);
3087 hi = hash_find(&buf_hashtab, key);
3088
3089 if (!HASHITEM_EMPTY(hi))
3090 return (buf_T *)(hi->hi_key
3091 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 return NULL;
3093}
3094
3095/*
3096 * Get name of file 'n' in the buffer list.
3097 * When the file has no name an empty string is returned.
3098 * home_replace() is used to shorten the file name (used for marks).
3099 * Returns a pointer to allocated memory, of NULL when failed.
3100 */
3101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003102buflist_nr2name(
3103 int n,
3104 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003105 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 buf_T *buf;
3108
3109 buf = buflist_findnr(n);
3110 if (buf == NULL)
3111 return NULL;
3112 return home_replace_save(helptail ? buf : NULL,
3113 fullname ? buf->b_ffname : buf->b_fname);
3114}
3115
3116/*
3117 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3118 * When "copy_options" is TRUE save the local window option values.
3119 * When "lnum" is 0 only do the options.
3120 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003122buflist_setfpos(
3123 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003124 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003125 linenr_T lnum,
3126 colnr_T col,
3127 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128{
3129 wininfo_T *wip;
3130
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003131 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (wip->wi_win == win)
3133 break;
3134 if (wip == NULL)
3135 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003136 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003137 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (wip == NULL)
3139 return;
3140 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003141 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 lnum = 1;
3143 }
3144 else
3145 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003146 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (wip->wi_prev)
3148 wip->wi_prev->wi_next = wip->wi_next;
3149 else
3150 buf->b_wininfo = wip->wi_next;
3151 if (wip->wi_next)
3152 wip->wi_next->wi_prev = wip->wi_prev;
3153 if (copy_options && wip->wi_optset)
3154 {
3155 clear_winopt(&wip->wi_opt);
3156#ifdef FEAT_FOLDING
3157 deleteFoldRecurse(&wip->wi_folds);
3158#endif
3159 }
3160 }
3161 if (lnum != 0)
3162 {
3163 wip->wi_fpos.lnum = lnum;
3164 wip->wi_fpos.col = col;
3165 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003166 if (win != NULL)
3167 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003168 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003170 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3172#ifdef FEAT_FOLDING
3173 wip->wi_fold_manual = win->w_fold_manual;
3174 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3175#endif
3176 wip->wi_optset = TRUE;
3177 }
3178
Bram Moolenaarc667da52019-11-30 20:52:27 +01003179 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 wip->wi_next = buf->b_wininfo;
3181 buf->b_wininfo = wip;
3182 wip->wi_prev = NULL;
3183 if (wip->wi_next)
3184 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
3186
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003187#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003188/*
3189 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3190 * page. That's because a diff is local to a tab page.
3191 */
3192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003193wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003194{
3195 win_T *wp;
3196
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003197 if (!wip->wi_opt.wo_diff)
3198 return FALSE;
3199
3200 FOR_ALL_WINDOWS(wp)
3201 // return FALSE when it's a window in the current tab page, thus
3202 // the buffer was in diff mode here
3203 if (wip->wi_win == wp)
3204 return FALSE;
3205 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003206}
3207#endif
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209/*
3210 * Find info for the current window in buffer "buf".
3211 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003212 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003213 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3214 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 * Returns NULL when there isn't any info.
3216 */
3217 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218find_wininfo(
3219 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003220 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 wininfo_T *wip;
3224
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003225 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003226 if (wip->wi_win == curwin
3227#ifdef FEAT_DIFF
3228 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3229#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003230
3231 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003233
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003234 if (wip != NULL)
3235 return wip;
3236
Bram Moolenaarc667da52019-11-30 20:52:27 +01003237 // If no wininfo for curwin, use the first in the list (that doesn't have
3238 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003239 // If "need_options" is TRUE skip entries that don't have options set,
3240 // unless the window is editing "buf", so we can copy from the window
3241 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003242#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003243 if (skip_diff_buffer)
3244 {
3245 FOR_ALL_BUF_WININFO(buf, wip)
3246 if (!wininfo_other_tab_diff(wip)
3247 && (!need_options || wip->wi_optset
3248 || (wip->wi_win != NULL
3249 && wip->wi_win->w_buffer == buf)))
3250 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003251 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003252 else
3253#endif
3254 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 return wip;
3256}
3257
3258/*
3259 * Reset the local window options to the values last used in this window.
3260 * If the buffer wasn't used in this window before, use the values from
3261 * the most recently used window. If the values were never set, use the
3262 * global values for the window.
3263 */
3264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003265get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
3267 wininfo_T *wip;
3268
3269 clear_winopt(&curwin->w_onebuf_opt);
3270#ifdef FEAT_FOLDING
3271 clearFolding(curwin);
3272#endif
3273
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003274 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003275 if (wip != NULL && wip->wi_win != NULL
3276 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003278 // The buffer is currently displayed in the window: use the actual
3279 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003280 win_T *wp = wip->wi_win;
3281
3282 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3283#ifdef FEAT_FOLDING
3284 curwin->w_fold_manual = wp->w_fold_manual;
3285 curwin->w_foldinvalid = TRUE;
3286 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3287#endif
3288 }
3289 else if (wip != NULL && wip->wi_optset)
3290 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003291 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3293#ifdef FEAT_FOLDING
3294 curwin->w_fold_manual = wip->wi_fold_manual;
3295 curwin->w_foldinvalid = TRUE;
3296 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3297#endif
3298 }
3299 else
3300 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003301 if (wip != NULL)
3302 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003305 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (p_fdls >= 0)
3307 curwin->w_p_fdl = p_fdls;
3308#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003309 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310}
3311
3312/*
3313 * Find the position (lnum and col) for the buffer 'buf' for the current
3314 * window.
3315 * Returns a pointer to no_position if no position is found.
3316 */
3317 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003321 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003323 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (wip != NULL)
3325 return &(wip->wi_fpos);
3326 else
3327 return &no_position;
3328}
3329
3330/*
3331 * Find the lnum for the buffer 'buf' for the current window.
3332 */
3333 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003334buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 return buflist_findfpos(buf)->lnum;
3337}
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003340 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
Bram Moolenaar52410572019-10-27 05:12:45 +01003345 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 int len;
3347 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003348 int ro_char;
3349 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003350#ifdef FEAT_TERMINAL
3351 int job_running;
3352 int job_none_open;
3353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaar52410572019-10-27 05:12:45 +01003355#ifdef FEAT_VIMINFO
3356 garray_T buflist;
3357 buf_T **buflist_data = NULL, **p;
3358
3359 if (vim_strchr(eap->arg, 't'))
3360 {
3361 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003362 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003363 {
3364 if (ga_grow(&buflist, 1) == OK)
3365 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3366 }
3367
3368 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3369 sizeof(buf_T *), buf_compare);
3370
Bram Moolenaar3b991522019-11-06 23:26:20 +01003371 buflist_data = (buf_T **)buflist.ga_data;
3372 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003373 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003374 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003375
Bram Moolenaar3b991522019-11-06 23:26:20 +01003376 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003377 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3378 : buf->b_next)
3379#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003383#ifdef FEAT_TERMINAL
3384 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003385 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003386#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003387 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003388 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3389 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3390 || (vim_strchr(eap->arg, '+')
3391 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3392 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003393 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003394 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003395 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3396#ifdef FEAT_TERMINAL
3397 || (vim_strchr(eap->arg, 'R')
3398 && (!job_running || (job_running && job_none_open)))
3399 || (vim_strchr(eap->arg, '?')
3400 && (!job_running || (job_running && !job_none_open)))
3401 || (vim_strchr(eap->arg, 'F')
3402 && (job_running || buf->b_term == NULL))
3403#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003404 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3405 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3406 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3407 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3408 || (vim_strchr(eap->arg, '#')
3409 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003412 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else
3414 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003415 if (message_filtered(NameBuff))
3416 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003418 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3419 : (bufIsChanged(buf) ? '+' : ' ');
3420#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003421 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003422 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003423 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003424 ro_char = '?';
3425 else
3426 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003427 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3428 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003429 }
3430 else if (buf->b_term != NULL)
3431 ro_char = 'F';
3432 else
3433#endif
3434 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3435
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003436 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003437 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 buf->b_fnum,
3439 buf->b_p_bl ? ' ' : 'u',
3440 buf == curbuf ? '%' :
3441 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3442 buf->b_ml.ml_mfp == NULL ? ' ' :
3443 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003444 ro_char,
3445 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003446 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003447 if (len > IOSIZE - 20)
3448 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaarc667da52019-11-30 20:52:27 +01003450 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 i = 40 - vim_strsize(IObuff);
3452 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003454 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003455#ifdef FEAT_VIMINFO
3456 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3457 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3458 else
3459#endif
3460 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3461 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003462 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003464 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 ui_breakcheck();
3466 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003467
3468#ifdef FEAT_VIMINFO
3469 if (buflist_data)
3470 ga_clear(&buflist);
3471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474/*
3475 * Get file name and line number for file 'fnum'.
3476 * Used by DoOneCmd() for translating '%' and '#'.
3477 * Used by insert_reg() and cmdline_paste() for '#' register.
3478 * Return FAIL if not found, OK for success.
3479 */
3480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003481buflist_name_nr(
3482 int fnum,
3483 char_u **fname,
3484 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486 buf_T *buf;
3487
3488 buf = buflist_findnr(fnum);
3489 if (buf == NULL || buf->b_fname == NULL)
3490 return FAIL;
3491
3492 *fname = buf->b_fname;
3493 *lnum = buflist_findlnum(buf);
3494
3495 return OK;
3496}
3497
3498/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003499 * Set the file name for "buf"' to "ffname_arg", short file name to
3500 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 * The file name with the full path is also remembered, for when :cd is used.
3502 * Returns FAIL for failure (file name already in use by other buffer)
3503 * OK otherwise.
3504 */
3505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003506setfname(
3507 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003508 char_u *ffname_arg,
3509 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003510 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003512 char_u *ffname = ffname_arg;
3513 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003514 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003516 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#endif
3518
3519 if (ffname == NULL || *ffname == NUL)
3520 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003521 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003522 if (buf->b_sfname != buf->b_ffname)
3523 VIM_CLEAR(buf->b_sfname);
3524 else
3525 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003526 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527#ifdef UNIX
3528 st.st_dev = (dev_T)-1;
3529#endif
3530 }
3531 else
3532 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003533 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3534 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 return FAIL;
3536
3537 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003538 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 * - if the buffer is loaded, fail
3540 * - if the buffer is not loaded, delete it from the list
3541 */
3542#ifdef UNIX
3543 if (mch_stat((char *)ffname, &st) < 0)
3544 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003545#endif
3546 if (!(buf->b_flags & BF_DUMMY))
3547#ifdef UNIX
3548 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003550 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
3552 if (obuf != NULL && obuf != buf)
3553 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003554 win_T *win;
3555 tabpage_T *tab;
3556 int in_use = FALSE;
3557
3558 // during startup a window may use a buffer that is not loaded yet
3559 FOR_ALL_TAB_WINDOWS(tab, win)
3560 if (win->w_buffer == obuf)
3561 in_use = TRUE;
3562
3563 // it's loaded or used in a window, fail
3564 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003567 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 vim_free(ffname);
3569 return FAIL;
3570 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003571 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003572 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 sfname = vim_strsave(sfname);
3575 if (ffname == NULL || sfname == NULL)
3576 {
3577 vim_free(sfname);
3578 vim_free(ffname);
3579 return FAIL;
3580 }
3581#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003582 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003584 if (buf->b_sfname != buf->b_ffname)
3585 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 buf->b_ffname = ffname;
3588 buf->b_sfname = sfname;
3589 }
3590 buf->b_fname = buf->b_sfname;
3591#ifdef UNIX
3592 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003593 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 else
3595 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003596 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 buf->b_dev = st.st_dev;
3598 buf->b_ino = st.st_ino;
3599 }
3600#endif
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 buf_name_changed(buf);
3605 return OK;
3606}
3607
3608/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003609 * Crude way of changing the name of a buffer. Use with care!
3610 * The name should be relative to the current directory.
3611 */
3612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003613buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003614{
3615 buf_T *buf;
3616
3617 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003618 if (buf == NULL)
3619 return;
3620
3621 if (buf->b_sfname != buf->b_ffname)
3622 vim_free(buf->b_sfname);
3623 vim_free(buf->b_ffname);
3624 buf->b_ffname = vim_strsave(name);
3625 buf->b_sfname = NULL;
3626 // Allocate ffname and expand into full path. Also resolves .lnk
3627 // files on Win32.
3628 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3629 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003630}
3631
3632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 * Take care of what needs to be done when the name of buffer "buf" has
3634 * changed.
3635 */
3636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
3639 /*
3640 * If the file name changed, also change the name of the swapfile
3641 */
3642 if (buf->b_ml.ml_mfp != NULL)
3643 ml_setname(buf);
3644
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003645#ifdef FEAT_TERMINAL
3646 if (buf->b_term != NULL)
3647 term_clear_status_text(buf->b_term);
3648#endif
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003651 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003652 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003653 status_redraw_all(); // status lines need to be redrawn
3654 fmarks_check_names(buf); // check named file marks
3655 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656}
3657
3658/*
3659 * set alternate file name for current window
3660 *
3661 * Used by do_one_cmd(), do_write() and do_ecmd().
3662 * Return the buffer.
3663 */
3664 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003665setaltfname(
3666 char_u *ffname,
3667 char_u *sfname,
3668 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 buf_T *buf;
3671
Bram Moolenaarc667da52019-11-30 20:52:27 +01003672 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003674 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 curwin->w_alt_fnum = buf->b_fnum;
3676 return buf;
3677}
3678
3679/*
3680 * Get alternate file name for current window.
3681 * Return NULL if there isn't any, and give error message if requested.
3682 */
3683 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003684getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003685 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 char_u *fname;
3688 linenr_T dummy;
3689
3690 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3691 {
3692 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003693 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return NULL;
3695 }
3696 return fname;
3697}
3698
3699/*
3700 * Add a file name to the buflist and return its number.
3701 * Uses same flags as buflist_new(), except BLN_DUMMY.
3702 *
3703 * used by qf_init(), main() and doarglist()
3704 */
3705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 buf_T *buf;
3709
3710 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3711 if (buf != NULL)
3712 return buf->b_fnum;
3713 return 0;
3714}
3715
3716#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3717/*
3718 * Adjust slashes in file names. Called after 'shellslash' was set.
3719 */
3720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 buf_T *bp;
3724
Bram Moolenaar29323592016-07-24 22:04:11 +02003725 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 {
3727 if (bp->b_ffname != NULL)
3728 slash_adjust(bp->b_ffname);
3729 if (bp->b_sfname != NULL)
3730 slash_adjust(bp->b_sfname);
3731 }
3732}
3733#endif
3734
3735/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003736 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 * Also save the local window option values.
3738 */
3739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003740buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003742 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743}
3744
3745/*
3746 * Return TRUE if 'ffname' is not the same file as current file.
3747 * Fname must have a full path (expanded by mch_FullName()).
3748 */
3749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003750otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
3752 return otherfile_buf(curbuf, ffname
3753#ifdef UNIX
3754 , NULL
3755#endif
3756 );
3757}
3758
3759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003760otherfile_buf(
3761 buf_T *buf,
3762 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003764 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003768 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3770 return TRUE;
3771 if (fnamecmp(ffname, buf->b_ffname) == 0)
3772 return FALSE;
3773#ifdef UNIX
3774 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003775 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
Bram Moolenaarc667da52019-11-30 20:52:27 +01003777 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (stp == NULL)
3779 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003780 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 st.st_dev = (dev_T)-1;
3782 stp = &st;
3783 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003784 // Use dev/ino to check if the files are the same, even when the names
3785 // are different (possible with links). Still need to compare the
3786 // name above, for when the file doesn't exist yet.
3787 // Problem: The dev/ino changes when a file is deleted (and created
3788 // again) and remains the same when renamed/moved. We don't want to
3789 // mch_stat() each buffer each time, that would be too slow. Get the
3790 // dev/ino again when they appear to match, but not when they appear
3791 // to be different: Could skip a buffer when it's actually the same
3792 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (buf_same_ino(buf, stp))
3794 {
3795 buf_setino(buf);
3796 if (buf_same_ino(buf, stp))
3797 return FALSE;
3798 }
3799 }
3800#endif
3801 return TRUE;
3802}
3803
3804#if defined(UNIX) || defined(PROTO)
3805/*
3806 * Set inode and device number for a buffer.
3807 * Must always be called when b_fname is changed!.
3808 */
3809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003810buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003812 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3815 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003816 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 buf->b_dev = st.st_dev;
3818 buf->b_ino = st.st_ino;
3819 }
3820 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003821 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822}
3823
3824/*
3825 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3826 */
3827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003828buf_same_ino(
3829 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003830 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003832 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 && stp->st_dev == buf->b_dev
3834 && stp->st_ino == buf->b_ino);
3835}
3836#endif
3837
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003838/*
3839 * Print info about the current buffer.
3840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003842fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003843 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844 int shorthelp,
3845 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
3847 char_u *name;
3848 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003849 char *p;
3850 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003851 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003853 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (buffer == NULL)
3855 return;
3856
Bram Moolenaarc667da52019-11-30 20:52:27 +01003857 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003859 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 p = buffer + STRLEN(buffer);
3861 }
3862 else
3863 p = buffer;
3864
3865 *p++ = '"';
3866 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003867 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 else
3869 {
3870 if (!fullname && curbuf->b_fname != NULL)
3871 name = curbuf->b_fname;
3872 else
3873 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 (int)(IOSIZE - (p - buffer)), TRUE);
3876 }
3877
Bram Moolenaar32526b32019-01-19 17:43:09 +01003878 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 curbufIsChanged() ? (shortmess(SHM_MOD)
3880 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003881 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003883 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003884 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003886 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 : _("[readonly]")) : "",
3888 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3889 || curbuf->b_p_ro) ?
3890 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003891 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3892 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (curwin->w_cursor.lnum > 1000000L)
3894 n = (int)(((long)curwin->w_cursor.lnum) /
3895 ((long)curbuf->b_ml.ml_line_count / 100L));
3896 else
3897 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3898 (long)curbuf->b_ml.ml_line_count);
3899 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003900 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003902 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003903 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003904 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3905 curbuf->b_ml.ml_line_count),
3906 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 else
3908 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003909 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 _("line %ld of %ld --%d%%-- col "),
3911 (long)curwin->w_cursor.lnum,
3912 (long)curbuf->b_ml.ml_line_count,
3913 n);
3914 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003915 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003916 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3918 }
3919
Bram Moolenaar32526b32019-01-19 17:43:09 +01003920 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3921 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 if (dont_truncate)
3924 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003925 // Temporarily set msg_scroll to avoid the message being truncated.
3926 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 msg_start();
3928 n = msg_scroll;
3929 msg_scroll = TRUE;
3930 msg(buffer);
3931 msg_scroll = n;
3932 }
3933 else
3934 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003935 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003937 // Need to repeat the message after redrawing when:
3938 // - When restart_edit is set (otherwise there will be a delay
3939 // before redrawing).
3940 // - When the screen was scrolled but there is no wait-return
3941 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003942 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944
3945 vim_free(buffer);
3946}
3947
3948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003949col_print(
3950 char_u *buf,
3951 size_t buflen,
3952 int col,
3953 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003956 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003958 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959}
3960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961static char_u *lasttitle = NULL;
3962static char_u *lasticon = NULL;
3963
Bram Moolenaar84a93082018-06-16 22:58:15 +02003964/*
3965 * Put the file name in the title bar and icon of the window.
3966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003968maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
3970 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003971 char_u *title_str = NULL;
3972 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int maxlen = 0;
3974 int len;
3975 int mustset;
3976 char_u buf[IOSIZE];
3977 int off;
3978
3979 if (!redrawing())
3980 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003981 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 need_maketitle = TRUE;
3983 return;
3984 }
3985
3986 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003987 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003988 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 if (p_title)
3991 {
3992 if (p_titlelen > 0)
3993 {
3994 maxlen = p_titlelen * Columns / 100;
3995 if (maxlen < 10)
3996 maxlen = 10;
3997 }
3998
Bram Moolenaar84a93082018-06-16 22:58:15 +02003999 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 if (*p_titlestring != NUL)
4001 {
4002#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004003 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004004 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4005 (char_u *)"titlestring", 0,
4006 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 else
4008#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004009 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011 else
4012 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004013 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
Bram Moolenaar2c666692012-09-05 13:30:40 +02004015#define SPACE_FOR_FNAME (IOSIZE - 100)
4016#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004017#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02004019 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02004020#ifdef FEAT_TERMINAL
4021 else if (curbuf->b_term != NULL)
4022 {
4023 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
4024 SPACE_FOR_FNAME);
4025 }
4026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 else
4028 {
4029 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02004030 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033
Bram Moolenaar21554412017-07-24 21:44:43 +02004034#ifdef FEAT_TERMINAL
4035 if (curbuf->b_term == NULL)
4036#endif
4037 switch (bufIsChanged(curbuf)
4038 + (curbuf->b_p_ro * 2)
4039 + (!curbuf->b_p_ma * 4))
4040 {
4041 case 1: STRCAT(buf, " +"); break;
4042 case 2: STRCAT(buf, " ="); break;
4043 case 3: STRCAT(buf, " =+"); break;
4044 case 4:
4045 case 6: STRCAT(buf, " -"); break;
4046 case 5:
4047 case 7: STRCAT(buf, " -+"); break;
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
Bram Moolenaar21554412017-07-24 21:44:43 +02004050 if (curbuf->b_fname != NULL
4051#ifdef FEAT_TERMINAL
4052 && curbuf->b_term == NULL
4053#endif
4054 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004056 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 off = (int)STRLEN(buf);
4058 buf[off++] = ' ';
4059 buf[off++] = '(';
4060 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004061 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004063 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004064 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 off += 2;
4066#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004067 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004068 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004070 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004071 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004072 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004073 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004077
Bram Moolenaarc667da52019-11-30 20:52:27 +01004078 // Translate unprintable chars and concatenate. Keep some
4079 // room for the server name. When there is no room (very long
4080 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004081 if (off < SPACE_FOR_DIR)
4082 {
4083 p = transstr(buf + off);
4084 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4085 vim_free(p);
4086 }
4087 else
4088 {
4089 vim_strncpy(buf + off, (char_u *)"...",
4090 (size_t)(SPACE_FOR_ARGNR - off));
4091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 STRCAT(buf, ")");
4093 }
4094
Bram Moolenaar2c666692012-09-05 13:30:40 +02004095 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097#if defined(FEAT_CLIENTSERVER)
4098 if (serverName != NULL)
4099 {
4100 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004101 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 else
4104#endif
4105 STRCAT(buf, " - VIM");
4106
4107 if (maxlen > 0)
4108 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004109 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004110 if (vim_strsize(buf) > maxlen)
4111 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 }
4114 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004115 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
4117 if (p_icon)
4118 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004119 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (*p_iconstring != NUL)
4121 {
4122#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004123 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004124 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4125 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
4127#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004128 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 else
4131 {
4132 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004133 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004134 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004135 p = gettail(curbuf->b_ffname);
4136 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004137 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004138 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (len > 100)
4140 {
4141 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004143 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004144 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004146 STRCPY(icon_str, p);
4147 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149 }
4150
Bram Moolenaar84a93082018-06-16 22:58:15 +02004151 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 if (mustset)
4154 resettitle();
4155}
4156
4157/*
4158 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4159 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004160 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 */
4162 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004163value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 if ((str == NULL) != (*last == NULL)
4166 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4167 {
4168 vim_free(*last);
4169 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004172 mch_restore_title(
4173 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004178 return TRUE;
4179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 return FALSE;
4182}
4183
4184/*
4185 * Put current window title back (used after calling a shell)
4186 */
4187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004188resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 mch_settitle(lasttitle, lasticon);
4191}
Bram Moolenaarea408852005-06-25 22:49:46 +00004192
4193# if defined(EXITFREE) || defined(PROTO)
4194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004195free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004196{
4197 vim_free(lasttitle);
4198 vim_free(lasticon);
4199}
4200# endif
4201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004203#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004204
4205/*
4206 * Used for building in the status line.
4207 */
4208typedef struct
4209{
4210 char_u *stl_start;
4211 int stl_minwid;
4212 int stl_maxwid;
4213 enum {
4214 Normal,
4215 Empty,
4216 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004217 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004218 Highlight,
4219 TabPage,
4220 Trunc
4221 } stl_type;
4222} stl_item_T;
4223
4224static size_t stl_items_len = 20; // Initial value, grows as needed.
4225static stl_item_T *stl_items = NULL;
4226static int *stl_groupitem = NULL;
4227static stl_hlrec_T *stl_hltab = NULL;
4228static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004229static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004232 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 * Return length of string in screen cells.
4234 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004235 * Normally works for window "wp", except when working for 'tabline' then it
4236 * is "curwin".
4237 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004239 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4241 *
4242 * If maxwidth is not zero, the string will be filled at any middle marker
4243 * or truncated if too long, fillchar is used for all whitespace.
4244 */
4245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246build_stl_str_hl(
4247 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004248 char_u *out, // buffer to write into != NameBuff
4249 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004251 char_u *opt_name, // option name corresponding to "fmt"
4252 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004253 int fillchar,
4254 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004255 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4256 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
Bram Moolenaar10772302019-01-20 18:25:54 +01004258 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004259 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 char_u *p;
4261 char_u *s;
4262 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004263 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004265 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004266 win_T *save_curwin;
4267 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004268 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269#endif
4270 int empty_line;
4271 colnr_T virtcol;
4272 long l;
4273 long n;
4274 int prevchar_isflag;
4275 int prevchar_isitem;
4276 int itemisflag;
4277 int fillable;
4278 char_u *str;
4279 long num;
4280 int width;
4281 int itemcnt;
4282 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004283 int group_end_userhl;
4284 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004286#ifdef FEAT_EVAL
4287 int evaldepth;
4288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int minwid;
4290 int maxwid;
4291 int zeropad;
4292 char_u base;
4293 char_u opt;
4294#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004295 char_u buf_tmp[TMPLEN];
4296 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004297 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004298 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004299 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004300 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004301 // TODO: find out why using called_emsg_before makes tests fail, does it
4302 // matter?
4303 // int called_emsg_before = called_emsg;
4304 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004305
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004306 // When inside update_screen() we do not want redrawing a statusline,
4307 // ruler, title, etc. to trigger another redraw, it may cause an endless
4308 // loop.
4309 if (updating_screen)
4310 redraw_not_allowed = TRUE;
4311
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004312 if (stl_items == NULL)
4313 {
4314 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4315 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004316
4317 // Allocate one more, because the last element is used to indicate the
4318 // end of the list.
4319 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4320 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004321
4322 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004323 }
4324
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004325#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004326 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4327 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4328
4329 // When the format starts with "%!" then evaluate it as an expression and
4330 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004331 if (fmt[0] == '%' && fmt[1] == '!')
4332 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004333 typval_T tv;
4334
4335 tv.v_type = VAR_NUMBER;
4336 tv.vval.v_number = wp->w_id;
4337 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4338
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004339 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004340 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004341 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004342
4343 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004344 }
4345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 if (fillchar == 0)
4348 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004349
Bram Moolenaar10772302019-01-20 18:25:54 +01004350 // The cursor in windows other than the current one isn't always
4351 // up-to-date, esp. because of autocommands and timers.
4352 lnum = wp->w_cursor.lnum;
4353 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4354 {
4355 lnum = wp->w_buffer->b_ml.ml_line_count;
4356 wp->w_cursor.lnum = lnum;
4357 }
4358
4359 // Get line & check if empty (cursorpos will show "0-1"). Note that
4360 // p will become invalid when getting another buffer line.
4361 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004362 empty_line = (*p == NUL);
4363
Bram Moolenaar10772302019-01-20 18:25:54 +01004364 // Get the byte value now, in case we need it below. This is more efficient
4365 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004366 len = ml_get_buf_len(wp->w_buffer, lnum);
4367 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004368 {
4369 // Line may have changed since checking the cursor column, or the lnum
4370 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004371 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004372 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004373 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004374 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004375 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004376 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004379#ifdef FEAT_EVAL
4380 evaldepth = 0;
4381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 p = out;
4383 curitem = 0;
4384 prevchar_isflag = TRUE;
4385 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004386 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004388 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004389 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004390 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004391
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004392 stl_item_T *new_items =
4393 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004394 if (new_items == NULL)
4395 break;
4396 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004397
4398 int *new_groupitem =
4399 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004400 if (new_groupitem == NULL)
4401 break;
4402 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004403
4404 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004405 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004406 if (new_hlrec == NULL)
4407 break;
4408 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004409 new_hlrec = vim_realloc(stl_tabtab,
4410 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004411 if (new_hlrec == NULL)
4412 break;
4413 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004414
4415 int *new_separator_locs = vim_realloc(stl_separator_locations,
4416 sizeof(int) * new_len);
4417 if (new_separator_locs == NULL)
4418 break;
4419 stl_separator_locations = new_separator_locs;;
4420
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004421 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004422 }
4423
zeertzjq122dea72022-07-27 15:48:45 +01004424 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 prevchar_isflag = prevchar_isitem = FALSE;
4426
4427 /*
4428 * Handle up to the next '%' or the end.
4429 */
4430 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4431 *p++ = *s++;
4432 if (*s == NUL || p + 1 >= out + outlen)
4433 break;
4434
4435 /*
4436 * Handle one '%' item.
4437 */
4438 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004439 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (*s == '%')
4442 {
4443 if (p + 1 >= out + outlen)
4444 break;
4445 *p++ = *s++;
4446 prevchar_isflag = prevchar_isitem = FALSE;
4447 continue;
4448 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004449 // STL_SEPARATE: Separation between items, filled with white space.
4450 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 s++;
4453 if (groupdepth > 0)
4454 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004455 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004456 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 continue;
4458 }
4459 if (*s == STL_TRUNCMARK)
4460 {
4461 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004462 stl_items[curitem].stl_type = Trunc;
4463 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 continue;
4465 }
4466 if (*s == ')')
4467 {
4468 s++;
4469 if (groupdepth < 1)
4470 continue;
4471 groupdepth--;
4472
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004473 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 *p = NUL;
4475 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004476 if (curitem > stl_groupitem[groupdepth] + 1
4477 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004479 // remove group if all items are empty and highlight group
4480 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004481 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004482 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004483 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004484 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004485 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004486 group_start_userhl = group_end_userhl =
4487 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004488 break;
4489 }
4490 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004491 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004492 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004493 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004495 if (stl_items[n].stl_type == Highlight)
4496 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004497 }
4498 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004500 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 p = t;
4502 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004503 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004504 {
4505 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004506 if (stl_items[n].stl_type == Highlight)
4507 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004508 // adjust the start position of TabPage to the next
4509 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004510 if (stl_items[n].stl_type == TabPage)
4511 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004515 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004517 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (has_mbyte)
4519 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004520 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004522 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004525 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 }
4528 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004529 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4530 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004533 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004535
4536 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004537 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004538 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaarc667da52019-11-30 20:52:27 +01004540 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004541 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
LemonBoy57ff5262022-05-09 21:03:47 +01004543 // Minus one for the leading '<' added above.
4544 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004545 if (stl_items[l].stl_start < t)
4546 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004549 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004551 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004552 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (n < 0)
4554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004555 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 n = 0 - n;
4557 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004558 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 else
4561 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004562 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004563 l = (n - l) * MB_CHAR2LEN(fillchar);
4564 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004566 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004568 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4569 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004571 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
4573 }
4574 continue;
4575 }
4576 minwid = 0;
4577 maxwid = 9999;
4578 zeropad = FALSE;
4579 l = 1;
4580 if (*s == '0')
4581 {
4582 s++;
4583 zeropad = TRUE;
4584 }
4585 if (*s == '-')
4586 {
4587 s++;
4588 l = -1;
4589 }
4590 if (VIM_ISDIGIT(*s))
4591 {
4592 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004593 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 minwid = 0;
4595 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004596 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004598 stl_items[curitem].stl_type = Highlight;
4599 stl_items[curitem].stl_start = p;
4600 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 s++;
4602 curitem++;
4603 continue;
4604 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004605 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4606 {
4607 if (*s == STL_TABCLOSENR)
4608 {
4609 if (minwid == 0)
4610 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004611 // %X ends the close label, go back to the previously
4612 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004613 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004614 if (stl_items[n].stl_type == TabPage
4615 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004616 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004617 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004618 break;
4619 }
4620 }
4621 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004622 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004623 minwid = - minwid;
4624 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004625 stl_items[curitem].stl_type = TabPage;
4626 stl_items[curitem].stl_start = p;
4627 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004628 s++;
4629 curitem++;
4630 continue;
4631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (*s == '.')
4633 {
4634 s++;
4635 if (VIM_ISDIGIT(*s))
4636 {
4637 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004638 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 maxwid = 50;
4640 }
4641 }
4642 minwid = (minwid > 50 ? 50 : minwid) * l;
4643 if (*s == '(')
4644 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004645 stl_groupitem[groupdepth++] = curitem;
4646 stl_items[curitem].stl_type = Group;
4647 stl_items[curitem].stl_start = p;
4648 stl_items[curitem].stl_minwid = minwid;
4649 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 s++;
4651 curitem++;
4652 continue;
4653 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004654#ifdef FEAT_EVAL
4655 // Denotes end of expanded %{} block
4656 if (*s == '}' && evaldepth > 0)
4657 {
4658 s++;
4659 evaldepth--;
4660 continue;
4661 }
4662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (vim_strchr(STL_ALL, *s) == NULL)
4664 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004665 if (*s == NUL) // can happen with "%0"
4666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 s++;
4668 continue;
4669 }
4670 opt = *s++;
4671
Bram Moolenaarc667da52019-11-30 20:52:27 +01004672 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 base = 'D';
4674 itemisflag = FALSE;
4675 fillable = TRUE;
4676 num = -1;
4677 str = NULL;
4678 switch (opt)
4679 {
4680 case STL_FILEPATH:
4681 case STL_FULLPATH:
4682 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004683 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004685 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
4687 {
4688 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004689 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4691 }
4692 trans_characters(NameBuff, MAXPATHL);
4693 if (opt != STL_FILENAME)
4694 str = NameBuff;
4695 else
4696 str = gettail(NameBuff);
4697 break;
4698
Bram Moolenaarc667da52019-11-30 20:52:27 +01004699 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004700 {
4701#ifdef FEAT_EVAL
4702 char_u *block_start = s - 1;
4703#endif
4704 int reevaluate = (*s == '%');
4705
4706 if (reevaluate)
4707 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 itemisflag = TRUE;
4709 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004710 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4711 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004713 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 break;
4715 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004716 if (reevaluate)
4717 p[-1] = 0; // remove the % at the end of %{% expr %}
4718 else
4719 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004722 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4723 "%d", curbuf->b_fnum);
4724 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4725 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4726 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004728 save_curbuf = curbuf;
4729 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004730 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 curwin = wp;
4732 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004733 // Visual mode is only valid in the current window.
4734 if (curwin != save_curwin)
4735 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004737 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004739 curwin = save_curwin;
4740 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004741 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004742 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004743 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745 if (str != NULL && *str != 0)
4746 {
4747 if (*skipdigits(str) == NUL)
4748 {
4749 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004750 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 itemisflag = FALSE;
4752 }
4753 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004754
4755 // If the output of the expression needs to be evaluated
4756 // replace the %{} block with the result of evaluation
4757 if (reevaluate && str != NULL && *str != 0
4758 && strchr((const char *)str, '%') != NULL
4759 && evaldepth < MAX_STL_EVAL_DEPTH)
4760 {
4761 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4762 size_t str_length = strlen((const char *)str);
4763 size_t fmt_length = strlen((const char *)s);
4764 size_t new_fmt_len = parsed_usefmt
4765 + str_length + fmt_length + 3;
4766 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4767 char_u *new_fmt_p = new_fmt;
4768
4769 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4770 + parsed_usefmt;
4771 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4772 + str_length;
4773 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4774 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4775 + fmt_length;
4776 *new_fmt_p = 0;
4777 new_fmt_p = NULL;
4778
4779 if (usefmt != fmt)
4780 vim_free(usefmt);
4781 VIM_CLEAR(str);
4782 usefmt = new_fmt;
4783 s = usefmt + parsed_usefmt;
4784 evaldepth++;
4785 continue;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787#endif
4788 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 case STL_LINE:
4791 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4792 ? 0L : (long)(wp->w_cursor.lnum);
4793 break;
4794
4795 case STL_NUMLINES:
4796 num = wp->w_buffer->b_ml.ml_line_count;
4797 break;
4798
4799 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004800 num = (State & MODE_INSERT) == 0 && empty_line
4801 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 break;
4803
4804 case STL_VIRTCOL:
4805 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004806 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004807 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004809 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4810 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 break;
4812 num = (long)virtcol;
4813 break;
4814
4815 case STL_PERCENTAGE:
4816 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4817 (long)wp->w_buffer->b_ml.ml_line_count);
4818 break;
4819
4820 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004821 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004822 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 break;
4824
Luuk van Baalba936f62022-12-15 13:15:39 +00004825 case STL_SHOWCMD:
4826 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4827 str = showcmd_buf;
4828 break;
4829
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 case STL_ARGLISTSTAT:
4831 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004832 buf_tmp[0] = 0;
4833 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4834 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 break;
4836
4837 case STL_KEYMAP:
4838 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004839 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4840 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 break;
4842 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004843#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004844 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#else
4846 num = 0;
4847#endif
4848 break;
4849
4850 case STL_BUFNO:
4851 num = wp->w_buffer->b_fnum;
4852 break;
4853
4854 case STL_OFFSET_X:
4855 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004856 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 case STL_OFFSET:
4858#ifdef FEAT_BYTEOFF
4859 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004860 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4861 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4862 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#endif
4864 break;
4865
4866 case STL_BYTEVAL_X:
4867 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004868 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004870 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (num == NL)
4872 num = 0;
4873 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4874 num = NL;
4875 break;
4876
4877 case STL_ROFLAG:
4878 case STL_ROFLAG_ALT:
4879 itemisflag = TRUE;
4880 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004881 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 break;
4883
4884 case STL_HELPFLAG:
4885 case STL_HELPFLAG_ALT:
4886 itemisflag = TRUE;
4887 if (wp->w_buffer->b_help)
4888 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004889 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 break;
4891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 case STL_FILETYPE:
4893 if (*wp->w_buffer->b_p_ft != NUL
4894 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4895 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004896 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004897 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004898 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 break;
4901
4902 case STL_FILETYPE_ALT:
4903 itemisflag = TRUE;
4904 if (*wp->w_buffer->b_p_ft != NUL
4905 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4906 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004907 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004908 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004909 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004911 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaar4033c552017-09-16 20:54:51 +02004915#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 case STL_PREVIEWFLAG:
4917 case STL_PREVIEWFLAG_ALT:
4918 itemisflag = TRUE;
4919 if (wp->w_p_pvw)
4920 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4921 : _("[Preview]"));
4922 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004923
4924 case STL_QUICKFIX:
4925 if (bt_quickfix(wp->w_buffer))
4926 str = (char_u *)(wp->w_llist_ref
4927 ? _(msg_loclist)
4928 : _(msg_qflist));
4929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930#endif
4931
4932 case STL_MODIFIED:
4933 case STL_MODIFIED_ALT:
4934 itemisflag = TRUE;
4935 switch ((opt == STL_MODIFIED_ALT)
4936 + bufIsChanged(wp->w_buffer) * 2
4937 + (!wp->w_buffer->b_p_ma) * 4)
4938 {
4939 case 2: str = (char_u *)"[+]"; break;
4940 case 3: str = (char_u *)",+"; break;
4941 case 4: str = (char_u *)"[-]"; break;
4942 case 5: str = (char_u *)",-"; break;
4943 case 6: str = (char_u *)"[+-]"; break;
4944 case 7: str = (char_u *)",+-"; break;
4945 }
4946 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004947
4948 case STL_HIGHLIGHT:
4949 t = s;
4950 while (*s != '#' && *s != NUL)
4951 ++s;
4952 if (*s == '#')
4953 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004954 stl_items[curitem].stl_type = Highlight;
4955 stl_items[curitem].stl_start = p;
4956 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004957 curitem++;
4958 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004959 if (*s != NUL)
4960 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004961 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004964 stl_items[curitem].stl_start = p;
4965 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (str != NULL && *str)
4967 {
4968 t = str;
4969 if (itemisflag)
4970 {
4971 if ((t[0] && t[1])
4972 && ((!prevchar_isitem && *t == ',')
4973 || (prevchar_isflag && *t == ' ')))
4974 t++;
4975 prevchar_isflag = TRUE;
4976 }
4977 l = vim_strsize(t);
4978 if (l > 0)
4979 prevchar_isitem = TRUE;
4980 if (l > maxwid)
4981 {
4982 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (has_mbyte)
4984 {
4985 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004986 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 l -= byte2cells(*t++);
4990 if (p + 1 >= out + outlen)
4991 break;
4992 *p++ = '<';
4993 }
4994 if (minwid > 0)
4995 {
4996 for (; l < minwid && p + 1 < out + outlen; l++)
4997 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004998 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5000 *p++ = ' ';
5001 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005002 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
5004 minwid = 0;
5005 }
5006 else
5007 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005008 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005010 // Change a space by fillchar, unless fillchar is '-' and a
5011 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005012 if (fillable && *t == ' '
5013 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5014 MB_CHAR2BYTES(fillchar, p);
5015 else
5016 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005019 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 else if (num >= 0)
5022 {
5023 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5024 char_u nstr[20];
5025
5026 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005027 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 prevchar_isitem = TRUE;
5029 t = nstr;
5030 if (opt == STL_VIRTCOL_ALT)
5031 {
5032 *t++ = '-';
5033 minwid--;
5034 }
5035 *t++ = '%';
5036 if (zeropad)
5037 *t++ = '0';
5038 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005039 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *t = 0;
5041
5042 for (n = num, l = 1; n >= nbase; n /= nbase)
5043 l++;
5044 if (opt == STL_VIRTCOL_ALT)
5045 l++;
5046 if (l > maxwid)
5047 {
5048 l += 2;
5049 n = l - maxwid;
5050 while (l-- > maxwid)
5051 num /= nbase;
5052 *t++ = '>';
5053 *t++ = '%';
5054 *t = t[-3];
5055 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005056 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5057 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005060 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5061 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 p += STRLEN(p);
5063 }
5064 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005065 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005067 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5068 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5069 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (opt == STL_VIM_EXPR)
5071 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 curitem++;
5073 }
5074 *p = NUL;
5075 itemcnt = curitem;
5076
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005077#ifdef FEAT_EVAL
5078 if (usefmt != fmt)
5079 vim_free(usefmt);
5080#endif
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 width = vim_strsize(out);
5083 if (maxwidth > 0 && width > maxwidth)
5084 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005085 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 l = 0;
5087 if (itemcnt == 0)
5088 s = out;
5089 else
5090 {
5091 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005092 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005094 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005095 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097 }
5098 if (l == itemcnt)
5099 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005100 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005101 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 l = 0;
5103 }
5104 }
5105
5106 if (width - vim_strsize(s) >= maxwidth)
5107 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005108 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (has_mbyte)
5110 {
5111 s = out;
5112 width = 0;
5113 for (;;)
5114 {
5115 width += ptr2cells(s);
5116 if (width >= maxwidth)
5117 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005118 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005120 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005122 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 s = out + maxwidth - 1;
5126 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005127 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129 itemcnt = l;
5130 *s++ = '>';
5131 *s = 0;
5132 }
5133 else
5134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (has_mbyte)
5136 {
5137 n = 0;
5138 while (width >= maxwidth)
5139 {
5140 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005141 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 }
5144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 n = width - maxwidth + 1;
5146 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005147 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 *s = '<';
5149
Bram Moolenaarc667da52019-11-30 20:52:27 +01005150 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 for (; l < itemcnt; l++)
5152 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005153 if (stl_items[l].stl_start - n >= s)
5154 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005156 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
zeertzjqd392a742023-07-01 20:24:40 +01005158
5159 // Fill up for half a double-wide character.
5160 while (++width < maxwidth)
5161 {
5162 s = s + STRLEN(s);
5163 MB_CHAR2BYTES(fillchar, s);
5164 *s = NUL;
5165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 width = maxwidth;
5168 }
5169 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5170 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005171 // Find how many separators there are, which we will use when
5172 // figuring out how many groups there are.
5173 int num_separators = 0;
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005177 if (stl_items[l].stl_type == Separate)
5178 {
5179 // Create an array of the start location for each separator
5180 // mark.
5181 stl_separator_locations[num_separators] = l;
5182 num_separators++;
5183 }
5184 }
5185
5186 // If we have separated groups, then we deal with it now
5187 if (num_separators)
5188 {
5189 int standard_spaces;
5190 int final_spaces;
5191
5192 standard_spaces = (maxwidth - width) / num_separators;
5193 final_spaces = (maxwidth - width) -
5194 standard_spaces * (num_separators - 1);
5195 for (l = 0; l < num_separators; l++)
5196 {
5197 int dislocation = (l == (num_separators - 1)) ?
5198 final_spaces : standard_spaces;
5199 dislocation *= MB_CHAR2LEN(fillchar);
5200 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5201 char_u *seploc = start + dislocation;
5202 STRMOVE(seploc, start);
5203 for (s = start; s < seploc;)
5204 MB_CHAR2BYTES(fillchar, s);
5205
5206 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5207 stl_items[i].stl_start += dislocation;
5208 }
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 width = maxwidth;
5211 }
5212 }
5213
Bram Moolenaarc667da52019-11-30 20:52:27 +01005214 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005215 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005217 *hltab = stl_hltab;
5218 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 for (l = 0; l < itemcnt; l++)
5220 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005221 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005223 sp->start = stl_items[l].stl_start;
5224 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005225 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005228 sp->start = NULL;
5229 sp->userhl = 0;
5230 }
5231
Bram Moolenaarc667da52019-11-30 20:52:27 +01005232 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005233 if (tabtab != NULL)
5234 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005235 *tabtab = stl_tabtab;
5236 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005237 for (l = 0; l < itemcnt; l++)
5238 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005239 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005240 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005241 sp->start = stl_items[l].stl_start;
5242 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005243 sp++;
5244 }
5245 }
5246 sp->start = NULL;
5247 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005250 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005251
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005252 // A user function may reset KeyTyped, restore it.
5253 KeyTyped = save_KeyTyped;
5254
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005255 // Check for an error. If there is one the display will be messed up and
5256 // might loop redrawing. Avoid that by making the corresponding option
5257 // empty.
5258 // TODO: find out why using called_emsg_before makes tests fail, does it
5259 // matter?
5260 // if (called_emsg > called_emsg_before)
5261 if (did_emsg > did_emsg_before)
5262 set_string_option_direct(opt_name, -1, (char_u *)"",
5263 OPT_FREE | opt_scope, SID_ERROR);
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 return width;
5266}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005267#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005270 * Get relative cursor position in window into "buf[buflen]", in the localized
5271 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 */
5273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005274get_rel_pos(
5275 win_T *wp,
5276 char_u *buf,
5277 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005279 long above; // number of lines above window
5280 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
Bram Moolenaarc667da52019-11-30 20:52:27 +01005282 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005283 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 above = wp->w_topline - 1;
5285#ifdef FEAT_DIFF
5286 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005287 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005288 above = 0; // All buffer lines are displayed and there is an
5289 // indication of filler lines, that can be considered
5290 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#endif
5292 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5293 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005294 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
Emir SARI971cd2b2023-04-29 12:09:53 +01005295 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005297 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005299 {
5300 int perc = (above > 1000000L)
5301 ? (int)(above / ((above + below) / 100L))
5302 : (int)(above * 100L / (above + below));
5303
5304 char *p = (char *)buf;
5305 size_t l = buflen;
5306 if (perc < 10)
5307 {
5308 // prepend one space
5309 buf[0] = ' ';
5310 ++p;
5311 --l;
5312 }
5313 // localized percentage value
5314 vim_snprintf(p, l, _("%d%%"), perc);
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005319 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 * Return TRUE if it was appended.
5321 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005323append_arg_number(
5324 win_T *wp,
5325 char_u *buf,
5326 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005327 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005329 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 return FALSE;
5331
Bram Moolenaara8490a42023-05-23 18:00:58 +01005332 char *msg;
5333 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005335 case 0: msg = _(" (%d of %d)"); break;
5336 case 1: msg = _(" ((%d) of %d)"); break;
5337 case 2: msg = _(" (file %d of %d)"); break;
5338 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005340
5341 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5342 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5343 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 return TRUE;
5345}
5346
5347/*
5348 * If fname is not a full path, make it a full path.
5349 * Returns pointer to allocated memory (NULL for failure).
5350 */
5351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 /*
5355 * Force expanding the path always for Unix, because symbolic links may
5356 * mess up the full path name, even though it starts with a '/'.
5357 * Also expand when there is ".." in the file name, try to remove it,
5358 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005359 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 * For MS-Windows also expand names like "longna~1" to "longname".
5361 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005362#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return FullName_save(fname, TRUE);
5364#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005365 if (!vim_isAbsName(fname)
5366 || strstr((char *)fname, "..") != NULL
5367 || strstr((char *)fname, "//") != NULL
5368# ifdef BACKSLASH_IN_FILENAME
5369 || strstr((char *)fname, "\\\\") != NULL
5370# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005371# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 )
5375 return FullName_save(fname, FALSE);
5376
5377 fname = vim_strsave(fname);
5378
Bram Moolenaar9b942202007-10-03 12:31:33 +00005379# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005380 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005381 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384 return fname;
5385#endif
5386}
5387
5388/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005389 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5390 * "*ffname" becomes a pointer to allocated memory (or NULL).
5391 * When resolving a link both "*sfname" and "*ffname" will point to the same
5392 * allocated memory.
5393 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005394 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005397fname_expand(
5398 buf_T *buf UNUSED,
5399 char_u **ffname,
5400 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005402 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005404 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005406 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408#ifdef FEAT_SHORTCUT
5409 if (!buf->b_p_bin)
5410 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005411 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005413 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005414 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005415 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 vim_free(*ffname);
5418 *ffname = rfname;
5419 *sfname = rfname;
5420 }
5421 }
5422#endif
5423}
5424
5425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 * Open a window for a number of buffers.
5427 */
5428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 buf_T *buf;
5432 win_T *wp, *wpnext;
5433 int split_ret = OK;
5434 int p_ea_save;
5435 int open_wins = 0;
5436 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005437 int count; // Maximum number of windows to open.
5438 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005439 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005440 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
Bram Moolenaarc667da52019-11-30 20:52:27 +01005442 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 count = 9999;
5444 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005445 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5447 all = FALSE;
5448 else
5449 all = TRUE;
5450
Pavel Mayorove1121b12023-02-20 14:35:20 +00005451 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5452 // switching to another buffer.
5453 reset_VIsual_and_resel();
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 setpcmark();
5456
5457#ifdef FEAT_GUI
5458 need_mouse_correct = TRUE;
5459#endif
5460
5461 /*
5462 * Close superfluous windows (two windows for the same buffer).
5463 * Also close windows that are not full-width.
5464 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005465 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005466 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005467 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005469 tpnext = curtab->tp_next;
5470 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005472 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005473 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005474 || ((cmdmod.cmod_split & WSP_VERT)
5475 ? wp->w_height + wp->w_status_height < Rows - p_ch
5476 - tabline_height()
5477 : wp->w_width != Columns)
5478 || (had_tab > 0 && wp != firstwin))
5479 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005480 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005481 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005482 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005483 if (win_close(wp, FALSE) == FAIL)
5484 break;
5485 // Just in case an autocommand does something strange with
5486 // windows: start all over...
5487 wpnext = firstwin;
5488 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005489 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005490 }
5491 else
5492 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005494
Bram Moolenaarc667da52019-11-30 20:52:27 +01005495 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005496 if (had_tab == 0 || tpnext == NULL)
5497 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005498 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500
5501 /*
5502 * Go through the buffer list. When a buffer doesn't have a window yet,
5503 * open one. Otherwise move the window to the right position.
5504 * Watch out for autocommands that delete buffers or windows!
5505 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005506 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5511 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005512 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5514 continue;
5515
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005516 if (had_tab != 0)
5517 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005518 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005519 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005520 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005521 else
5522 wp = NULL;
5523 }
5524 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005525 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005526 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005527 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005528 if (wp->w_buffer == buf)
5529 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005530 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005531 if (wp != NULL)
5532 win_move_after(wp, curwin);
5533 }
5534
5535 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005537 bufref_T bufref;
5538
5539 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005540
Bram Moolenaarc667da52019-11-30 20:52:27 +01005541 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005543 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5545 ++open_wins;
5546 p_ea = p_ea_save;
5547 if (split_ret == FAIL)
5548 continue;
5549
Bram Moolenaarc667da52019-11-30 20:52:27 +01005550 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005553 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005555 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 break;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (swap_exists_action == SEA_QUIT)
5560 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005561#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005562 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005563
Bram Moolenaarc667da52019-11-30 20:52:27 +01005564 // Reset the error/interrupt/exception state here so that
5565 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005566 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005567#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005568
Bram Moolenaarc667da52019-11-30 20:52:27 +01005569 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 win_close(curwin, TRUE);
5571 --open_wins;
5572 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005573 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005574
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005575#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005576 // Restore the error/interrupt/exception state if not
5577 // discarded by a new aborting error, interrupt, or uncaught
5578 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005579 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else
5583 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585
5586 ui_breakcheck();
5587 if (got_int)
5588 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005589 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 break;
5591 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005592#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005593 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005594 if (aborting())
5595 break;
5596#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005597 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005598 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005599 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005602 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605 /*
5606 * Close superfluous windows.
5607 */
5608 for (wp = lastwin; open_wins > count; )
5609 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005610 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (!win_valid(wp))
5613 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005614 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 wp = lastwin;
5616 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005617 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005619 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 --open_wins;
5621 wp = lastwin;
5622 }
5623 else
5624 {
5625 wp = wp->w_prev;
5626 if (wp == NULL)
5627 break;
5628 }
5629 }
5630}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005633static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635/*
5636 * do_modelines() - process mode lines for the current file
5637 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005638 * "flags" can be:
5639 * OPT_WINONLY only set options local to window
5640 * OPT_NOWIN don't set options local to window
5641 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 * Returns immediately if the "ml" option isn't set.
5643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005647 linenr_T lnum;
5648 int nmlines;
5649 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5652 return;
5653
Bram Moolenaarc667da52019-11-30 20:52:27 +01005654 // Disallow recursive entry here. Can happen when executing a modeline
5655 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (entered)
5657 return;
5658
5659 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005660 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005662 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 nmlines = 0;
5664
Hu Jialun9dcd3492021-08-28 20:42:50 +02005665 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005667 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 nmlines = 0;
5669 --entered;
5670}
5671
Bram Moolenaarc667da52019-11-30 20:52:27 +01005672#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674/*
5675 * chk_modeline() - check a single line for a mode string
5676 * Return FAIL if an error encountered.
5677 */
5678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005679chk_modeline(
5680 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005681 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 char_u *s;
5684 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005685 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 int prev;
5687 int vers;
5688 int end;
5689 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005690 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005691 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
5693 prev = -1;
5694 for (s = ml_get(lnum); *s != NUL; ++s)
5695 {
5696 if (prev == -1 || vim_isspace(prev))
5697 {
5698 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5699 || STRNCMP(s, "vi:", (size_t)3) == 0)
5700 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005701 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005702 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
5704 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5705 e = s + 4;
5706 else
5707 e = s + 3;
5708 vers = getdigits(&e);
5709 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005710 && (s[0] != 'V'
5711 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005713 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 || (VIM_VERSION_100 < vers && s[3] == '<')
5715 || (VIM_VERSION_100 > vers && s[3] == '>')
5716 || (VIM_VERSION_100 == vers && s[3] == '=')))
5717 break;
5718 }
5719 }
5720 prev = *s;
5721 }
5722
5723 if (*s)
5724 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005725 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 ++s;
5727 while (s[-1] != ':');
5728
Bram Moolenaarc667da52019-11-30 20:52:27 +01005729 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (linecopy == NULL)
5731 return FAIL;
5732
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005733 // prepare for emsg()
5734 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005735 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 end = FALSE;
5738 while (end == FALSE)
5739 {
5740 s = skipwhite(s);
5741 if (*s == NUL)
5742 break;
5743
5744 /*
5745 * Find end of set command: ':' or end of line.
5746 * Skip over "\:", replacing it with ":".
5747 */
5748 for (e = s; *e != ':' && *e != NUL; ++e)
5749 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005750 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (*e == NUL)
5752 end = TRUE;
5753
5754 /*
5755 * If there is a "set" command, require a terminating ':' and
5756 * ignore the stuff after the ':'.
5757 * "vi:set opt opt opt: foo" -- foo not interpreted
5758 * "vi:opt opt opt: foo" -- foo interpreted
5759 * Accept "se" for compatibility with Elvis.
5760 */
5761 if (STRNCMP(s, "set ", (size_t)4) == 0
5762 || STRNCMP(s, "se ", (size_t)3) == 0)
5763 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005764 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 break;
5766 end = TRUE;
5767 s = vim_strchr(s, ' ') + 1;
5768 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005769 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
Bram Moolenaarc667da52019-11-30 20:52:27 +01005771 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005773 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005774
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005775 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005776 current_sctx.sc_version = 1;
5777#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005778 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005779 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005780 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005782
Bram Moolenaar5958f952018-11-20 04:25:21 +01005783 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005784 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005785
Bram Moolenaara3227e22006-03-08 21:32:40 +00005786 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005787
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005788 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005789 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005790 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 break;
5792 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005793 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
5795
ichizok7e5fe382023-04-15 13:17:50 +01005796 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005797 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 vim_free(linecopy);
5799 }
5800 return retval;
5801}
5802
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005803/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005804 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5805 */
5806 int
5807bt_normal(buf_T *buf)
5808{
5809 return buf != NULL && buf->b_p_bt[0] == NUL;
5810}
5811
5812/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005813 * Return TRUE if "buf" is the quickfix buffer.
5814 */
5815 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005816bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005817{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005818#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005819 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005820#else
5821 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005822#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005823}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005824
5825/*
5826 * Return TRUE if "buf" is a terminal buffer.
5827 */
5828 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005829bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005830{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005831#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005832 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005833#else
5834 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005835#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005836}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005837
5838/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005839 * Return TRUE if "buf" is a help buffer.
5840 */
5841 int
5842bt_help(buf_T *buf)
5843{
5844 return buf != NULL && buf->b_help;
5845}
5846
5847/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005848 * Return TRUE if "buf" is a prompt buffer.
5849 */
5850 int
5851bt_prompt(buf_T *buf)
5852{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005853 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5854}
5855
Dominique Pelle748b3082022-01-08 12:41:16 +00005856#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005857/*
5858 * Return TRUE if "buf" is a buffer for a popup window.
5859 */
5860 int
5861bt_popup(buf_T *buf)
5862{
5863 return buf != NULL && buf->b_p_bt != NULL
5864 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005865}
Dominique Pelle748b3082022-01-08 12:41:16 +00005866#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005867
5868/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005869 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005870 * buffer. This means the buffer name may not be a file name, at least not for
5871 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005872 */
5873 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005874bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005875{
5876 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5877 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005878 || buf->b_p_bt[0] == 't'
5879 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005880}
5881
Bram Moolenaarc3126192022-08-26 12:58:17 +01005882/*
5883 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5884 * buffer. This means the buffer is not to be read from a file.
5885 */
5886 static int
5887bt_nofileread(buf_T *buf)
5888{
5889 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5890 || buf->b_p_bt[0] == 't'
5891 || buf->b_p_bt[0] == 'q'
5892 || buf->b_p_bt[0] == 'p');
5893}
5894
Dominique Pelle748b3082022-01-08 12:41:16 +00005895#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005896/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005897 * Return TRUE if "buf" has 'buftype' set to "nofile".
5898 */
5899 int
5900bt_nofile(buf_T *buf)
5901{
5902 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5903}
Dominique Pelle748b3082022-01-08 12:41:16 +00005904#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005905
5906/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005907 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5908 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005909 */
5910 int
5911bt_dontwrite(buf_T *buf)
5912{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005913 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005914 || buf->b_p_bt[0] == 't'
5915 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005916}
5917
5918 int
5919bt_dontwrite_msg(buf_T *buf)
5920{
5921 if (bt_dontwrite(buf))
5922 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005923 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005924 return TRUE;
5925 }
5926 return FALSE;
5927}
5928
5929/*
5930 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5931 * and 'bufhidden'.
5932 */
5933 int
5934buf_hide(buf_T *buf)
5935{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005936 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005937 switch (buf->b_p_bh[0])
5938 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005939 case 'u': // "unload"
5940 case 'w': // "wipe"
5941 case 'd': return FALSE; // "delete"
5942 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005943 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005944 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005945}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
5947/*
5948 * Return special buffer name.
5949 * Returns NULL when the buffer has a normal file name.
5950 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005951 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005952buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005954#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005956 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005957 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005958 * Differentiate between the quickfix and location list buffers using
5959 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005960 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005961 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005962 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005963 else
5964 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005967
Bram Moolenaarc667da52019-11-30 20:52:27 +01005968 // There is no _file_ when 'buftype' is "nofile", b_sfname
5969 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005970 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005972#ifdef FEAT_TERMINAL
5973 if (buf->b_term != NULL)
5974 return term_get_status_text(buf->b_term);
5975#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005976 if (buf->b_fname != NULL)
5977 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005978 if (buf == cmdwin_buf)
5979 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005980#ifdef FEAT_JOB_CHANNEL
5981 if (bt_prompt(buf))
5982 return (char_u *)_("[Prompt]");
5983#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005984#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005985 if (bt_popup(buf))
5986 return (char_u *)_("[Popup]");
5987#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005988 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005992 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 return NULL;
5994}
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005997 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5998 */
5999 char_u *
6000buf_get_fname(buf_T *buf)
6001{
6002 if (buf->b_fname == NULL)
6003 return (char_u *)_("[No Name]");
6004 return buf->b_fname;
6005}
6006
6007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6009 */
6010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006013 if (on == curbuf->b_p_bl)
6014 return;
6015
6016 curbuf->b_p_bl = on;
6017 if (on)
6018 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6019 else
6020 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021}
6022
6023/*
6024 * Read the file for "buf" again and check if the contents changed.
6025 * Return TRUE if it changed or this could not be checked.
6026 */
6027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 buf_T *newbuf;
6031 int differ = TRUE;
6032 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 exarg_T ea;
6035
Bram Moolenaarc667da52019-11-30 20:52:27 +01006036 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6038 if (newbuf == NULL)
6039 return TRUE;
6040
Bram Moolenaarc667da52019-11-30 20:52:27 +01006041 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 if (prep_exarg(&ea, buf) == FAIL)
6043 {
6044 wipe_buffer(newbuf, FALSE);
6045 return TRUE;
6046 }
6047
Bram Moolenaare76062c2022-11-28 18:51:43 +00006048 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006050 if (curbuf != newbuf)
6051 {
6052 // Failed to find a window for "newbuf".
6053 wipe_buffer(newbuf, FALSE);
6054 return TRUE;
6055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006057 // We don't want to trigger autocommands now, they may have nasty
6058 // side-effects like wiping buffers
6059 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006060 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 && readfile(buf->b_ffname, buf->b_fname,
6062 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6063 &ea, READ_NEW | READ_DUMMY) == OK)
6064 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006065 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6067 {
6068 differ = FALSE;
6069 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6070 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6071 {
6072 differ = TRUE;
6073 break;
6074 }
6075 }
6076 }
6077 vim_free(ea.cmd);
6078
Bram Moolenaarc667da52019-11-30 20:52:27 +01006079 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
Bram Moolenaarc667da52019-11-30 20:52:27 +01006082 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 wipe_buffer(newbuf, FALSE);
6084
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006085 unblock_autocmds();
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 return differ;
6088}
6089
6090/*
6091 * Wipe out a buffer and decrement the last buffer number if it was used for
6092 * this buffer. Call this to wipe out a temp buffer that does not contain any
6093 * marks.
6094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096wipe_buffer(
6097 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006098 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 if (buf->b_fnum == top_file_num - 1)
6101 --top_file_num;
6102
Bram Moolenaarc667da52019-11-30 20:52:27 +01006103 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006104 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006105
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006106 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006109 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110}