blob: 28967342d02ea24988b159c4abbb09beb51bdc28 [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);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef UNIX
55# define dev_T dev_t
56#else
57# define dev_T unsigned
58#endif
59
Bram Moolenaar00d253e2020-04-06 22:13:01 +020060#define FOR_ALL_BUFS_FROM_LAST(buf) \
61 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
62
Bram Moolenaar4033c552017-09-16 20:54:51 +020063#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020064static char *msg_loclist = N_("[Location List]");
65static char *msg_qflist = N_("[Quickfix List]");
66#endif
67
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020068// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020069static int buf_free_count = 0;
70
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020071static int top_file_num = 1; // highest file number
72static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
73
74/*
75 * Return the highest possible buffer number.
76 */
77 int
78get_highest_fnum(void)
79{
80 return top_file_num - 1;
81}
82
Bram Moolenaarc024b462019-06-08 18:07:21 +020083/*
84 * Read data from buffer for retrying.
85 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020086 static int
87read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010088 int read_stdin, // read file from stdin, otherwise fifo
89 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
90 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020091{
92 int retval = OK;
93 linenr_T line_count;
94
Bram Moolenaarc5935a82021-10-19 20:48:52 +010095 // Read from the buffer which the text is already filled in and append at
96 // the end. This makes it possible to retry when 'fileformat' or
97 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020098 line_count = curbuf->b_ml.ml_line_count;
99 retval = readfile(
100 read_stdin ? NULL : curbuf->b_ffname,
101 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100102 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200103 flags | READ_BUFFER);
104 if (retval == OK)
105 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100106 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200107 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200108 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109 }
110 else
111 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100112 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200113 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200114 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100116 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 curwin->w_cursor.lnum = 1;
118 curwin->w_cursor.col = 0;
119
120 if (read_stdin)
121 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100122 // Set or reset 'modified' before executing autocommands, so that
123 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100124 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200125 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100126 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200127 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200128
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100129 if (retval == OK)
130 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100131#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100132 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100133 curbuf, &retval);
134#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100135 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200136#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100137 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200138 }
139 return retval;
140}
141
Dominique Pelle748b3082022-01-08 12:41:16 +0000142#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200144 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
145 */
146 void
147buffer_ensure_loaded(buf_T *buf)
148{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000149 if (buf->b_ml.ml_mfp != NULL)
150 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200151
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000152 aco_save_T aco;
153
154 // Make sure the buffer is in a window. If not then skip it.
155 aucmd_prepbuf(&aco, buf);
156 if (curbuf == buf)
157 {
158 if (swap_exists_action != SEA_READONLY)
159 swap_exists_action = SEA_NONE;
160 open_buffer(FALSE, NULL, 0);
161 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200162 }
163}
Dominique Pelle748b3082022-01-08 12:41:16 +0000164#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200165
166/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200167 * Open current buffer, that is: open the memfile and read the file into
168 * memory.
169 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100173 int read_stdin, // read file from stdin
174 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100175 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100177 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200179 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100180#ifdef FEAT_SYN_HL
181 long old_tw = curbuf->b_p_tw;
182#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200183 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100185 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
186 // When re-entering the same buffer, it should not change, because the
187 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (readonlymode && curbuf->b_ffname != NULL
189 && (curbuf->b_flags & BF_NEVERLOADED))
190 curbuf->b_p_ro = TRUE;
191
Bram Moolenaar4770d092006-01-12 23:22:24 +0000192 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100194 // There MUST be a memfile, otherwise we can't do anything
195 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100196 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200197 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 if (curbuf->b_ml.ml_mfp != NULL)
199 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100200 // If there is no memfile at all, exit.
201 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 if (curbuf == NULL)
203 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000204 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200205
206 // Don't try to do any saving, with "curbuf" NULL almost nothing
207 // will work.
208 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 getout(2);
210 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200211
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000212 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100214#ifdef FEAT_SYN_HL
215 if (old_tw != curbuf->b_p_tw)
216 check_colorcolumn(curwin);
217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 return FAIL;
219 }
220
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100221 // Do not sync this buffer yet, may first want to read the file.
222 if (curbuf->b_ml.ml_mfp != NULL)
223 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
224
Bram Moolenaarc667da52019-11-30 20:52:27 +0100225 // The autocommands in readfile() may change the buffer, but only AFTER
226 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200227 set_bufref(&old_curbuf, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +0200228 curbuf->b_modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaarc667da52019-11-30 20:52:27 +0100230 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100231 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100233 // A buffer without an actual file should not use the buffer name to read a
234 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100235 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100236 flags |= READ_NOFILE;
237
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100238 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (curbuf->b_ffname != NULL
240#ifdef FEAT_NETBEANS_INTG
241 && netbeansReadFile
242#endif
243 )
244 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100245 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200246#ifdef UNIX
247 int save_bin = curbuf->b_p_bin;
248 int perm;
249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_NETBEANS_INTG
251 int oldFire = netbeansFireChanges;
252
253 netbeansFireChanges = 0;
254#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200255#ifdef UNIX
256 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200257 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200258 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200259# ifdef OPEN_CHR_FILES
260 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
261# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200262 ))
263 read_fifo = TRUE;
264 if (read_fifo)
265 curbuf->b_p_bin = TRUE;
266#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100267 if (shortmess(SHM_FILEINFO))
268 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200270 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
272#ifdef UNIX
273 if (read_fifo)
274 {
275 curbuf->b_p_bin = save_bin;
276 if (retval == OK)
277 retval = read_buffer(FALSE, eap, flags);
278 }
279#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100280 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#ifdef FEAT_NETBEANS_INTG
282 netbeansFireChanges = oldFire;
283#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100284 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200285 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 fix_help_buffer();
287 }
288 else if (read_stdin)
289 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200290 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100292 // First read the text in binary mode into the buffer.
293 // Then read from that same buffer and append at the end. This makes
294 // it possible to retry when 'fileformat' or 'fileencoding' was
295 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 curbuf->b_p_bin = TRUE;
297 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200298 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
299 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 curbuf->b_p_bin = save_bin;
301 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200302 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 }
304
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100305 // Can now sync this buffer in ml_sync_all().
306 if (curbuf->b_ml.ml_mfp != NULL
307 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
308 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
309
Bram Moolenaarc667da52019-11-30 20:52:27 +0100310 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100314 parse_cino(curbuf);
315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100317 // Set/reset the Changed flag first, autocmds may change the buffer.
318 // Apply the automatic commands, before processing the modelines.
319 // So the modelines have priority over autocommands.
320 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100321 // When reading stdin, the buffer contents always needs writing, so set
322 // the changed flag. Unless in readonly mode: "ls | gview -".
323 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000324 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200325 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100326#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000329 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100331 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200332 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100333 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
Bram Moolenaarc667da52019-11-30 20:52:27 +0100335 // Set last_changedtick to avoid triggering a TextChanged autocommand right
336 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100337 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100338 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100339 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100340
Bram Moolenaarc667da52019-11-30 20:52:27 +0100341 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#ifdef FEAT_EVAL
343 if (aborting())
344#else
345 if (got_int)
346#endif
347 curbuf->b_flags |= BF_READERR;
348
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000349#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100350 // Need to update automatic folding. Do this before the autocommands,
351 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000352 foldUpdateAll(curwin);
353#endif
354
Bram Moolenaarc667da52019-11-30 20:52:27 +0100355 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 if (!(curwin->w_valid & VALID_TOPLINE))
357 {
358 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100359#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100363#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100365#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#endif
368
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000369 if (retval != OK)
370 return retval;
371
372 // The autocommands may have changed the current buffer. Apply the
373 // modelines to the correct buffer, if it still exists and is loaded.
374 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000376 aco_save_T aco;
377
378 // Go to the buffer that was opened, make sure it is in a window.
379 // If not then skip it.
380 aucmd_prepbuf(&aco, old_curbuf.br_buf);
381 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000383 do_modelines(0);
384 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000386 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100387#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000388 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
389 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100390#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000391 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
392 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000395 // restore curwin/curbuf and a few other things
396 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
399
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 return retval;
401}
402
403/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200404 * Store "buf" in "bufref" and set the free count.
405 */
406 void
407set_bufref(bufref_T *bufref, buf_T *buf)
408{
409 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200410 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200411 bufref->br_buf_free_count = buf_free_count;
412}
413
414/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200415 * Return TRUE if "bufref->br_buf" points to the same buffer as when
416 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200417 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200418 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
419 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200420 */
421 int
422bufref_valid(bufref_T *bufref)
423{
424 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200425 ? TRUE : buf_valid(bufref->br_buf)
426 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200431 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 */
433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100434buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
436 buf_T *bp;
437
Bram Moolenaarc667da52019-11-30 20:52:27 +0100438 // Assume that we more often have a recent buffer, start with the last
439 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200440 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 if (bp == buf)
442 return TRUE;
443 return FALSE;
444}
445
446/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200447 * A hash table used to quickly lookup a buffer by its number.
448 */
449static hashtab_T buf_hashtab;
450
451 static void
452buf_hashtab_add(buf_T *buf)
453{
454 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000455 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000456 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200457}
458
459 static void
460buf_hashtab_remove(buf_T *buf)
461{
462 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
463
464 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000465 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200466}
467
Bram Moolenaar94f01952018-09-01 15:30:03 +0200468/*
469 * Return TRUE when buffer "buf" can be unloaded.
470 * Give an error message and return FALSE when the buffer is locked or the
471 * screen is being redrawn and the buffer is in a window.
472 */
473 static int
474can_unload_buffer(buf_T *buf)
475{
476 int can_unload = !buf->b_locked;
477
478 if (can_unload && updating_screen)
479 {
480 win_T *wp;
481
482 FOR_ALL_WINDOWS(wp)
483 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200484 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200485 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200486 break;
487 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200488 }
489 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100490 {
491 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
492
493 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
494 fname != NULL ? fname : (char_u *)"[No Name]");
495 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200496 return can_unload;
497}
Bram Moolenaara997b452018-04-17 23:24:06 +0200498
Bram Moolenaar480778b2016-07-14 22:09:39 +0200499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 * Close the link to a buffer.
501 * "action" is used when there is no longer a window for the buffer.
502 * It can be:
503 * 0 buffer becomes hidden
504 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100505 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200507 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 * When doing all but the first one on the current buffer, the caller should
509 * get a new buffer very soon!
510 *
511 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100512 *
513 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
514 * cause there to be only one window with this buffer. e.g. when ":quit" is
515 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100516 *
517 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100518 *
519 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100523 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524 buf_T *buf,
525 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100526 int abort_if_last,
527 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100530 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200531 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100532 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200533 win_T *the_curwin = curwin;
534 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200536 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
537 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
Bram Moolenaarcee52202020-03-11 14:19:58 +0100539 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100540
541 // Force unloading or deleting when 'bufhidden' says so.
542 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
543 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100544 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200545 {
546 del_buf = TRUE;
547 unload_buf = TRUE;
548 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100549 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200550 {
551 del_buf = TRUE;
552 unload_buf = TRUE;
553 wipe_buf = TRUE;
554 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100555 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200556 unload_buf = TRUE;
557
Bram Moolenaar94053a52017-08-01 21:44:33 +0200558#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100559 // depending on how we get here b_nwindows may already be zero
560 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200561 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100562 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200563 if (term_job_running(buf->b_term))
564 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200565 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200566 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200567 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100568 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200569
Bram Moolenaarc667da52019-11-30 20:52:27 +0100570 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200571 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100572
573 // A terminal buffer is wiped out when job has finished.
574 del_buf = TRUE;
575 unload_buf = TRUE;
576 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200577 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200578 else
579 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100580 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200581 del_buf = FALSE;
582 unload_buf = FALSE;
583 }
584 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100585 else if (buf->b_p_bh[0] == 'h' && !del_buf)
586 {
587 // Hide a terminal buffer.
588 unload_buf = FALSE;
589 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200590 else
591 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100592 if (del_buf || unload_buf)
593 {
594 // A terminal buffer is wiped out if the job has finished.
595 // We only do this when there's an intention to unload the
596 // buffer. This way, :hide and other similar commands won't
597 // wipe the buffer.
598 del_buf = TRUE;
599 unload_buf = TRUE;
600 wipe_buf = TRUE;
601 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200602 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100603 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200604 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // Disallow deleting the buffer when it is locked (already being closed or
608 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200609 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100610 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200611
Bram Moolenaarc667da52019-11-30 20:52:27 +0100612 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200613 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100615 // Set b_last_cursor when closing the last window for the buffer.
616 // Remember the last cursor position and window options of the buffer.
617 // This used to be only for the current window, but then options like
618 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (buf->b_nwindows == 1)
620 set_last_cursor(win);
621 buflist_setfpos(buf, win,
622 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
623 win->w_cursor.col, TRUE);
624 }
625
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200626 set_bufref(&bufref, buf);
627
Bram Moolenaarc667da52019-11-30 20:52:27 +0100628 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (buf->b_nwindows == 1)
630 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200631 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100632 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200633 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
634 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200635 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100636 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200638aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000639 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100640 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100641 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200642 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100643 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200644 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100645 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200646 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaarc667da52019-11-30 20:52:27 +0100648 // When the buffer becomes hidden, but is not unloaded, trigger
649 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (!unload_buf)
651 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200652 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100653 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200654 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
655 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200656 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100657 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200658 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200659 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100660 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200661 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100662 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200663 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100665#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100666 // autocmds may abort script processing
667 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100668 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200671
Bram Moolenaarc667da52019-11-30 20:52:27 +0100672 // If the buffer was in curwin and the window has changed, go back to that
673 // window, if it still exists. This avoids that ":edit x" triggering a
674 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200675 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
676 {
677 block_autocmds();
678 goto_tabpage_win(the_curtab, the_curwin);
679 unblock_autocmds();
680 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (buf->b_nwindows > 0)
686 --buf->b_nwindows;
687
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100688#ifdef FEAT_DIFF
689 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100690 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100691#endif
692
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 // Return when a window is displaying the buffer or when it's not
694 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100696 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaarc667da52019-11-30 20:52:27 +0100698 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (buf->b_ffname == NULL)
700 del_buf = TRUE;
701
Bram Moolenaarc667da52019-11-30 20:52:27 +0100702 // When closing the current buffer stop Visual mode before freeing
703 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200704 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200705#if defined(EXITFREE)
706 && !entered_free_all_mem
707#endif
708 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200709 end_visual_mode();
710
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100711 // Free all things allocated for this buffer.
712 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
713 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100714 // Remember if we are closing the current buffer. Restore the number of
715 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 is_curbuf = (buf == curbuf);
717 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100719 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100720 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100721 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
Bram Moolenaarc667da52019-11-30 20:52:27 +0100723 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200724 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100725 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100726#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100727 // autocmds may abort script processing
728 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100729 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100732 // It's possible that autocommands change curbuf to the one being deleted.
733 // This might cause the previous curbuf to be deleted unexpectedly. But
734 // in some cases it's OK to delete the curbuf, because a new one is
735 // obtained anyway. Therefore only return if curbuf changed to the
736 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100738 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200739
Bram Moolenaar4033c552017-09-16 20:54:51 +0200740 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100741 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200742
Bram Moolenaarc667da52019-11-30 20:52:27 +0100743 // Autocommands may have opened or closed windows for this buffer.
744 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200745 if (buf->b_nwindows > 0)
746 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
749 * Remove the buffer from the list.
750 */
751 if (wipe_buf)
752 {
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200753 win_T *wp;
754
Bram Moolenaar347538f2022-03-26 16:28:06 +0000755 // Do not wipe out the buffer if it is used in a window.
756 if (buf->b_nwindows > 0)
757 return FALSE;
758
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200759 FOR_ALL_WINDOWS(wp)
760 mark_forget_file(wp, buf->b_fnum);
761
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200762 if (action == DOBUF_WIPE_REUSE)
763 {
764 // we can re-use this buffer number, store it
765 if (buf_reuse.ga_itemsize == 0)
766 ga_init2(&buf_reuse, sizeof(int), 50);
767 if (ga_grow(&buf_reuse, 1) == OK)
768 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
769 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200770 if (buf->b_sfname != buf->b_ffname)
771 VIM_CLEAR(buf->b_sfname);
772 else
773 buf->b_sfname = NULL;
774 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (buf->b_prev == NULL)
776 firstbuf = buf->b_next;
777 else
778 buf->b_prev->b_next = buf->b_next;
779 if (buf->b_next == NULL)
780 lastbuf = buf->b_prev;
781 else
782 buf->b_next->b_prev = buf->b_prev;
783 free_buffer(buf);
784 }
785 else
786 {
787 if (del_buf)
788 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100789 // Free all internal variables and reset option values, to make
790 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 free_buffer_stuff(buf, TRUE);
792
Bram Moolenaarc667da52019-11-30 20:52:27 +0100793 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
795
Bram Moolenaarc667da52019-11-30 20:52:27 +0100796 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 buf->b_p_initialized = FALSE;
798 }
799 buf_clear_file(buf);
800 if (del_buf)
801 buf->b_p_bl = FALSE;
802 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100803 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100804 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Make buffer not contain a file.
809 */
810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100811buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200814 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100816 buf->b_p_eof = FALSE;
817 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 buf->b_p_eol = TRUE;
819 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000821 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100823 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#ifdef FEAT_NETBEANS_INTG
825 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
827}
828
829/*
830 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200831 * the file. Careful: get here with "curwin" NULL when exiting.
832 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100833 * BFA_DEL buffer is going to be deleted
834 * BFA_WIPE buffer is going to be wiped out
835 * BFA_KEEP_UNDO do not free undo information
836 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200842 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200843 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200844 win_T *the_curwin = curwin;
845 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
Bram Moolenaarc667da52019-11-30 20:52:27 +0100847 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200848 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100849 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200850 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200851 if (buf->b_ml.ml_mfp != NULL)
852 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200853 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
854 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200855 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100856 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200857 return;
858 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200859 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200861 if (apply_autocmds(EVENT_BUFDELETE, 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 Moolenaar071d4272004-06-13 20:20:40 +0000865 return;
866 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200867 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200869 if (apply_autocmds(EVENT_BUFWIPEOUT, 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 Moolenaare0ab94e2016-09-04 19:50:54 +0200875 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100876 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200877
Bram Moolenaarc667da52019-11-30 20:52:27 +0100878 // If the buffer was in curwin and the window has changed, go back to that
879 // window, if it still exists. This avoids that ":edit x" triggering a
880 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200881 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
882 {
883 block_autocmds();
884 goto_tabpage_win(the_curtab, the_curwin);
885 unblock_autocmds();
886 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200887
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100888#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100889 // autocmds may abort script processing
890 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100894 // It's possible that autocommands change curbuf to the one being deleted.
895 // This might cause curbuf to be deleted unexpectedly. But in some cases
896 // it's OK to delete the curbuf, because a new one is obtained anyway.
897 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (buf == curbuf && !is_curbuf)
899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100901 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200903#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100904 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200905 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100906 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200907#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000908
909#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100910 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000911 {
912 win_T *win;
913 tabpage_T *tp;
914
915 FOR_ALL_TAB_WINDOWS(tp, win)
916 if (win->w_buffer == buf)
917 clearFolding(win);
918 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000919#endif
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921#ifdef FEAT_TCL
922 tcl_buffer_free(buf);
923#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100924 ml_close(buf, TRUE); // close and delete the memline/memfile
925 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200926 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100927 // free the memory allocated for undo
928 // and reset all undo information
929 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100931 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100933#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100934 clear_buf_prop_types(buf);
935#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100936 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939/*
940 * Free a buffer structure and the things it contains related to the buffer
941 * itself (not the file, that must have been done already).
942 */
943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100944free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200946 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200948#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100949 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000950 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200951 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200952 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200953#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200954#ifdef FEAT_LUA
955 lua_buffer_free(buf);
956#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000957#ifdef FEAT_MZSCHEME
958 mzscheme_buffer_free(buf);
959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_PERL
961 perl_buf_free(buf);
962#endif
963#ifdef FEAT_PYTHON
964 python_buffer_free(buf);
965#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200966#ifdef FEAT_PYTHON3
967 python3_buffer_free(buf);
968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#ifdef FEAT_RUBY
970 ruby_buffer_free(buf);
971#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200972#ifdef FEAT_JOB_CHANNEL
973 channel_buffer_free(buf);
974#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200975#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200976 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200977#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200978#ifdef FEAT_JOB_CHANNEL
979 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200980 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200981 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200982#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200983
984 buf_hashtab_remove(buf);
985
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200986 aubuflocal_remove(buf);
987
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200988 if (autocmd_busy)
989 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100990 // Do not free the buffer structure while autocommands are executing,
991 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200992 buf->b_next = au_pending_free_buf;
993 au_pending_free_buf = buf;
994 }
995 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100996 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200997 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100998 if (curbuf == buf)
999 curbuf = NULL; // make clear it's not to be used
1000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001004 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001005 */
1006 static void
1007init_changedtick(buf_T *buf)
1008{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001009 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001010
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001011 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1012 di->di_tv.v_type = VAR_NUMBER;
1013 di->di_tv.v_lock = VAR_FIXED;
1014 di->di_tv.vval.v_number = 0;
1015
Bram Moolenaar92769c32017-02-25 15:41:37 +01001016#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001017 STRCPY(buf->b_ct_di.di_key, "changedtick");
1018 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001019#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001020}
1021
1022/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001023 * Free the b_wininfo list for buffer "buf".
1024 */
1025 static void
1026clear_wininfo(buf_T *buf)
1027{
1028 wininfo_T *wip;
1029
1030 while (buf->b_wininfo != NULL)
1031 {
1032 wip = buf->b_wininfo;
1033 buf->b_wininfo = wip->wi_next;
1034 free_wininfo(wip);
1035 }
1036}
1037
1038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1040 */
1041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001042free_buffer_stuff(
1043 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001044 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
1046 if (free_options)
1047 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001048 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001050#ifdef FEAT_SPELL
1051 ga_clear(&buf->b_s.b_langp);
1052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
1054#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001055 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001056 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001057
Bram Moolenaarc667da52019-11-30 20:52:27 +01001058 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001059 hash_init(&buf->b_vars->dv_hashtab);
1060 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001061 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001062 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001065 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001067 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001069#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001070 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001071#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001072#ifdef FEAT_PROP_POPUP
1073 ga_clear_strings(&buf->b_textprop_text);
1074#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001075 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1076 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001077 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078}
1079
1080/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001081 * Free one wininfo_T.
1082 */
1083 void
1084free_wininfo(wininfo_T *wip)
1085{
1086 if (wip->wi_optset)
1087 {
1088 clear_winopt(&wip->wi_opt);
1089#ifdef FEAT_FOLDING
1090 deleteFoldRecurse(&wip->wi_folds);
1091#endif
1092 }
1093 vim_free(wip);
1094}
1095
1096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 * Go to another buffer. Handles the result of the ATTENTION dialog.
1098 */
1099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001100goto_buffer(
1101 exarg_T *eap,
1102 int start,
1103 int dir,
1104 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001106 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001107 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108
1109 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
Bram Moolenaar188639d2022-04-04 16:57:21 +01001111 if (swap_exists_action == SEA_NONE)
1112 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1114 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1116 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001118 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001119
Bram Moolenaarc667da52019-11-30 20:52:27 +01001120 // Reset the error/interrupt/exception state here so that
1121 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001122 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001123#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001124
Bram Moolenaarc667da52019-11-30 20:52:27 +01001125 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001127 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001128 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001129
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001130#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001131 // Restore the error/interrupt/exception state if not discarded by a
1132 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001133 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001137 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140/*
1141 * Handle the situation of swap_exists_action being set.
1142 * It is allowed for "old_curbuf" to be NULL or invalid.
1143 */
1144 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001145handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001147#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001148 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001149#endif
1150#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001151 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001152#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001153 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (swap_exists_action == SEA_QUIT)
1156 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001157#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001158 // Reset the error/interrupt/exception state here so that
1159 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001161#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001162
Bram Moolenaarc667da52019-11-30 20:52:27 +01001163 // User selected Quit at ATTENTION prompt. Go back to previous
1164 // buffer. If that buffer is gone or the same as the current one,
1165 // open a new, empty buffer.
1166 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001167 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001168 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001169 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1170 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001171 {
1172 // Block autocommands here because curwin->w_buffer is NULL.
1173 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001174 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001175 unblock_autocmds();
1176 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001177 else
1178 buf = old_curbuf->br_buf;
1179 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001180 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001181 int old_msg_silent = msg_silent;
1182
1183 if (shortmess(SHM_FILEINFO))
1184 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001185 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001186 // restore msg_silent, so that the command line will be shown
1187 msg_silent = old_msg_silent;
1188
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001189#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001190 if (old_tw != curbuf->b_p_tw)
1191 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001192#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001193 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001194 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001195
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001196#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001197 // Restore the error/interrupt/exception state if not discarded by a
1198 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001199 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 else if (swap_exists_action == SEA_RECOVER)
1203 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001204#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001205 // Reset the error/interrupt/exception state here so that
1206 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001207 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001208#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001209
Bram Moolenaarc667da52019-11-30 20:52:27 +01001210 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001212 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001213 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001215 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001216
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001217#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001218 // Restore the error/interrupt/exception state if not discarded by a
1219 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001220 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 swap_exists_action = SEA_NONE;
1224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001227 * Make the current buffer empty.
1228 * Used when it is wiped out and it's the last buffer.
1229 */
1230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001231empty_curbuf(
1232 int close_others,
1233 int forceit,
1234 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001235{
1236 int retval;
1237 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001238 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001239
1240 if (action == DOBUF_UNLOAD)
1241 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001242 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001243 return FAIL;
1244 }
1245
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001246 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001247 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001248 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001250
1251 setpcmark();
1252 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1253 forceit ? ECMD_FORCEIT : 0, curwin);
1254
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001255 // do_ecmd() may create a new buffer, then we have to delete
1256 // the old one. But do_ecmd() may have done that already, check
1257 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001258 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001259 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001260 if (!close_others)
1261 need_fileinfo = FALSE;
1262 return retval;
1263}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001264
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265/*
1266 * Implementation of the commands for the buffer list.
1267 *
1268 * action == DOBUF_GOTO go to specified buffer
1269 * action == DOBUF_SPLIT split window and go to specified buffer
1270 * action == DOBUF_UNLOAD unload specified buffer(s)
1271 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1272 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001273 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 *
1275 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1276 * start == DOBUF_FIRST go to "count" buffer from first buffer
1277 * start == DOBUF_LAST go to "count" buffer from last buffer
1278 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1279 *
1280 * Return FAIL or OK.
1281 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001282 static int
1283do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284 int action,
1285 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001286 int dir, // FORWARD or BACKWARD
1287 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001288 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 buf_T *buf;
1291 buf_T *bp;
1292 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001293 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295 switch (start)
1296 {
1297 case DOBUF_FIRST: buf = firstbuf; break;
1298 case DOBUF_LAST: buf = lastbuf; break;
1299 default: buf = curbuf; break;
1300 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001301 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
1303 while (count-- > 0)
1304 {
1305 do
1306 {
1307 buf = buf->b_next;
1308 if (buf == NULL)
1309 buf = firstbuf;
1310 }
1311 while (buf != curbuf && !bufIsChanged(buf));
1312 }
1313 if (!bufIsChanged(buf))
1314 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001315 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return FAIL;
1317 }
1318 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001319 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
1321 while (buf != NULL && buf->b_fnum != count)
1322 buf = buf->b_next;
1323 }
1324 else
1325 {
1326 bp = NULL;
1327 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1328 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001329 // remember the buffer where we start, we come back there when all
1330 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (bp == NULL)
1332 bp = buf;
1333 if (dir == FORWARD)
1334 {
1335 buf = buf->b_next;
1336 if (buf == NULL)
1337 buf = firstbuf;
1338 }
1339 else
1340 {
1341 buf = buf->b_prev;
1342 if (buf == NULL)
1343 buf = lastbuf;
1344 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001345 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (unload || buf->b_p_bl)
1347 {
1348 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001349 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351 if (bp == buf)
1352 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001353 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001354 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 return FAIL;
1356 }
1357 }
1358 }
1359
Bram Moolenaarc667da52019-11-30 20:52:27 +01001360 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
1362 if (start == DOBUF_FIRST)
1363 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001364 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001366 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001369 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001371 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 return FAIL;
1373 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001374#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001375 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001376 return OK;
1377#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001378 if (
1379 action == DOBUF_GOTO
1380 && buf != curbuf
1381 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1382 // disallow navigating to another buffer when 'winfixbuf' is applied
1383 return FAIL;
1384
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001385 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1386 && (buf->b_flags & BF_DUMMY))
1387 {
1388 // disallow navigating to the dummy buffer
1389 semsg(_(e_buffer_nr_does_not_exist), count);
1390 return FAIL;
1391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393#ifdef FEAT_GUI
1394 need_mouse_correct = TRUE;
1395#endif
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001398 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
1400 if (unload)
1401 {
1402 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001403 bufref_T bufref;
1404
Bram Moolenaar94f01952018-09-01 15:30:03 +02001405 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001406 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001407
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001408 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
Bram Moolenaarc667da52019-11-30 20:52:27 +01001410 // When unloading or deleting a buffer that's already unloaded and
1411 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001412 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1413 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return FAIL;
1415
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001416 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001418#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001419 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001421# ifdef FEAT_TERMINAL
1422 if (term_job_running(buf->b_term))
1423 {
1424 if (term_confirm_stop(buf) == FAIL)
1425 return FAIL;
1426 }
1427 else
1428# endif
1429 {
1430 dialog_changed(buf, FALSE);
1431 if (!bufref_valid(&bufref))
1432 // Autocommand deleted buffer, oops! It's not changed
1433 // now.
1434 return FAIL;
1435 // If it's still changed fail silently, the dialog already
1436 // mentioned why it fails.
1437 if (bufIsChanged(buf))
1438 return FAIL;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001441 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001444 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 return FAIL;
1446 }
1447 }
1448
Bram Moolenaarc667da52019-11-30 20:52:27 +01001449 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001450 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001451 end_visual_mode();
1452
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001453 // If deleting the last (listed) buffer, make it empty.
1454 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001455 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (bp->b_p_bl && bp != buf)
1457 break;
1458 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001459 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001461 // If the deleted buffer is the current one, close the current window
1462 // (unless it's the only window). Repeat this so long as we end up in
1463 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001464 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001465 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001466 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001467 {
1468 if (win_close(curwin, FALSE) == FAIL)
1469 break;
1470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001472 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (buf != curbuf)
1474 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001475 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001476 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001477 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return OK;
1479 }
1480
1481 /*
1482 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001483 * There should be another, otherwise it would have been handled
1484 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001485 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 * Then prefer the buffer we most recently visited.
1487 * Else try to find one that is loaded, after the current buffer,
1488 * then before the current buffer.
1489 * Finally use any buffer.
1490 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001491 buf = NULL; // selected buffer
1492 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001493 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1494 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001495 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 int jumpidx;
1498
1499 jumpidx = curwin->w_jumplistidx - 1;
1500 if (jumpidx < 0)
1501 jumpidx = curwin->w_jumplistlen - 1;
1502
1503 forward = jumpidx;
1504 while (jumpidx != curwin->w_jumplistidx)
1505 {
1506 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1507 if (buf != NULL)
1508 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001509 // Skip current and unlisted bufs. Also skip a quickfix
1510 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001511 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001512 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 else if (buf->b_ml.ml_mfp == NULL)
1514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001515 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (bp == NULL)
1517 bp = buf;
1518 buf = NULL;
1519 }
1520 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001521 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001523 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1525 break;
1526 if (--jumpidx < 0)
1527 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001528 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 break;
1530 }
1531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
1535 forward = TRUE;
1536 buf = curbuf->b_next;
1537 for (;;)
1538 {
1539 if (buf == NULL)
1540 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001541 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 break;
1543 buf = curbuf->b_prev;
1544 forward = FALSE;
1545 continue;
1546 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001547 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001548 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001549 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001551 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001553 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 bp = buf;
1555 }
1556 if (forward)
1557 buf = buf->b_next;
1558 else
1559 buf = buf->b_prev;
1560 }
1561 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001562 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001564 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001566 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001567 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 break;
1569 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001570 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 if (curbuf->b_next != NULL)
1573 buf = curbuf->b_next;
1574 else
1575 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001576 if (bt_quickfix(buf))
1577 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 }
1580
Bram Moolenaara02471e2014-01-10 16:43:14 +01001581 if (buf == NULL)
1582 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001583 // Autocommands must have wiped out all other buffers. Only option
1584 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001585 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001586 }
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001589 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001591 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001593 // If 'switchbuf' is set jump to the window containing "buf".
1594 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001595 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 return FAIL;
1599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
Bram Moolenaarc667da52019-11-30 20:52:27 +01001601 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (buf == curbuf)
1603 return OK;
1604
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001605 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001606 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001609 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001611# ifdef FEAT_TERMINAL
1612 if (term_job_running(curbuf->b_term))
1613 {
1614 if (term_confirm_stop(curbuf) == FAIL)
1615 return FAIL;
1616 // Manually kill the terminal here because this command will
1617 // hide it otherwise.
1618 free_terminal(curbuf);
1619 }
1620 else
1621# endif
1622 {
1623 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001624
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001625 set_bufref(&bufref, buf);
1626 dialog_changed(curbuf, FALSE);
1627 if (!bufref_valid(&bufref))
1628 // Autocommand deleted buffer, oops!
1629 return FAIL;
1630
1631 if (bufIsChanged(curbuf))
1632 {
1633 no_write_message();
1634 return FAIL;
1635 }
1636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639#endif
1640 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001641 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return FAIL;
1643 }
1644 }
1645
Bram Moolenaarc667da52019-11-30 20:52:27 +01001646 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 set_curbuf(buf, action);
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001650 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001652#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001653 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return FAIL;
1655#endif
1656
1657 return OK;
1658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001660 int
1661do_buffer(
1662 int action,
1663 int start,
1664 int dir, // FORWARD or BACKWARD
1665 int count, // buffer number or number of buffers
1666 int forceit) // TRUE when using !
1667{
1668 return do_buffer_ext(action, start, dir, count,
1669 forceit ? DOBUF_FORCEIT : 0);
1670}
1671
1672/*
1673 * do_bufdel() - delete or unload buffer(s)
1674 *
1675 * addr_count == 0: ":bdel" - delete current buffer
1676 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1677 * buffer "end_bnr", then any other arguments.
1678 * addr_count == 2: ":N,N bdel" - delete buffers in range
1679 *
1680 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1681 * DOBUF_DEL (":bdel")
1682 *
1683 * Returns error message or NULL
1684 */
1685 char *
1686do_bufdel(
1687 int command,
1688 char_u *arg, // pointer to extra arguments
1689 int addr_count,
1690 int start_bnr, // first buffer number in a range
1691 int end_bnr, // buffer nr or last buffer nr in a range
1692 int forceit)
1693{
1694 int do_current = 0; // delete current buffer?
1695 int deleted = 0; // number of buffers deleted
1696 char *errormsg = NULL; // return value
1697 int bnr; // buffer number
1698 char_u *p;
1699
1700 if (addr_count == 0)
1701 {
1702 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1703 }
1704 else
1705 {
1706 if (addr_count == 2)
1707 {
1708 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001709 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001710 bnr = start_bnr;
1711 }
1712 else // addr_count == 1
1713 bnr = end_bnr;
1714
1715 for ( ;!got_int; ui_breakcheck())
1716 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001717 // Delete the current buffer last, otherwise when the
1718 // current buffer is deleted, the next buffer becomes
1719 // the current one and will be loaded, which may then
1720 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001721 if (bnr == curbuf->b_fnum)
1722 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001723 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001724 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1725 ++deleted;
1726
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001727 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001728 if (addr_count == 2)
1729 {
1730 if (++bnr > end_bnr)
1731 break;
1732 }
1733 else // addr_count == 1
1734 {
1735 arg = skipwhite(arg);
1736 if (*arg == NUL)
1737 break;
1738 if (!VIM_ISDIGIT(*arg))
1739 {
1740 p = skiptowhite_esc(arg);
1741 bnr = buflist_findpat(arg, p,
1742 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1743 FALSE, FALSE);
1744 if (bnr < 0) // failed
1745 break;
1746 arg = p;
1747 }
1748 else
1749 bnr = getdigits(&arg);
1750 }
1751 }
1752 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1753 FORWARD, do_current, forceit) == OK)
1754 ++deleted;
1755
1756 if (deleted == 0)
1757 {
1758 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001759 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001760 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001761 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001762 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001763 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001764 errormsg = (char *)IObuff;
1765 }
1766 else if (deleted >= p_report)
1767 {
1768 if (command == DOBUF_UNLOAD)
1769 smsg(NGETTEXT("%d buffer unloaded",
1770 "%d buffers unloaded", deleted), deleted);
1771 else if (command == DOBUF_DEL)
1772 smsg(NGETTEXT("%d buffer deleted",
1773 "%d buffers deleted", deleted), deleted);
1774 else
1775 smsg(NGETTEXT("%d buffer wiped out",
1776 "%d buffers wiped out", deleted), deleted);
1777 }
1778 }
1779
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001780 return errormsg;
1781}
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783/*
1784 * Set current buffer to "buf". Executes autocommands and closes current
1785 * buffer. "action" tells how to close the current buffer:
1786 * DOBUF_GOTO free or hide it
1787 * DOBUF_SPLIT nothing
1788 * DOBUF_UNLOAD unload it
1789 * DOBUF_DEL delete it
1790 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001791 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 */
1793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
1796 buf_T *prevbuf;
1797 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001798 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001799#ifdef FEAT_SYN_HL
1800 long old_tw = curbuf->b_p_tw;
1801#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001802 bufref_T newbufref;
1803 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001804 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001805 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001808 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001809 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1810 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
Bram Moolenaarc667da52019-11-30 20:52:27 +01001812 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
Bram Moolenaarc667da52019-11-30 20:52:27 +01001815 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001817 set_bufref(&prevbufref, prevbuf);
1818 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001820 // Autocommands may delete the current buffer and/or the buffer we want to
1821 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001822 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001823 || (bufref_valid(&prevbufref)
1824 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001825#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001826 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001828 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001830#ifdef FEAT_SYN_HL
1831 if (prevbuf == curwin->w_buffer)
1832 reset_synblock(curwin);
1833#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001834 // autocommands may have opened a new window
1835 // with prevbuf, grr
1836 if (unload ||
1837 (last_winid != get_last_winid() &&
1838 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001839 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001840#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001841 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001843 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001845 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001846 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001847
Bram Moolenaare615db02022-01-20 21:00:54 +00001848 // Do not sync when in Insert mode and the buffer is open in
1849 // another window, might be a timer doing something in another
1850 // window.
1851 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001852 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001853 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1855 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001856 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001857 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1858 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001859 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001860 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001861 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001864 // An autocommand may have deleted "buf", already entered it (e.g., when
1865 // it did ":bunload") or aborted the script processing.
1866 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001867 valid = buf_valid(buf);
1868 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001869#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001870 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001872 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001873 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001874 // autocommands changed curbuf and we will move to another
1875 // buffer soon, so decrement curbuf->b_nwindows
1876 if (curbuf != NULL && prevbuf != curbuf)
1877 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001878 // If the buffer is not valid but curwin->w_buffer is NULL we must
1879 // enter some buffer. Using the last one is hopefully OK.
1880 if (!valid)
1881 enter_buffer(lastbuf);
1882 else
1883 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001884#ifdef FEAT_SYN_HL
1885 if (old_tw != curbuf->b_p_tw)
1886 check_colorcolumn(curwin);
1887#endif
1888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889}
1890
1891/*
1892 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001893 * Old curbuf must have been abandoned already! This also means "curbuf" may
1894 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001897enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001899 // when closing the current buffer stop Visual mode
1900 if (VIsual_active
1901#if defined(EXITFREE)
1902 && !entered_free_all_mem
1903#endif
1904 )
1905 end_visual_mode();
1906
Bram Moolenaar010ee962019-09-25 20:37:36 +02001907 // Get the buffer in the current window.
1908 curwin->w_buffer = buf;
1909 curbuf = buf;
1910 ++curbuf->b_nwindows;
1911
1912 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1914 if (!buf->b_help)
1915 get_winopts(buf);
1916#ifdef FEAT_FOLDING
1917 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001918 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001920 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001924 if (curwin->w_p_diff)
1925 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#endif
1927
Bram Moolenaara971b822011-09-14 14:43:25 +02001928#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001929 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001930#endif
1931
Bram Moolenaarc667da52019-11-30 20:52:27 +01001932 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 curwin->w_cursor.lnum = 1;
1934 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001937 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaarc667da52019-11-30 20:52:27 +01001939 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001940 curwin->w_valid = 0;
1941
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001942 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1943 curbuf->b_last_cursor.col, TRUE);
1944
Bram Moolenaarc667da52019-11-30 20:52:27 +01001945 // Make sure the buffer is loaded.
1946 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001947 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001948 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001949 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001950 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001951 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02001952 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001953
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001954 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 else
1957 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001958 if (!msg_silent && !shortmess(SHM_FILEINFO))
1959 need_fileinfo = TRUE; // display file info after redraw
1960
1961 // check if file changed
1962 (void)buf_check_timestamp(curbuf, FALSE);
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001965#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1969 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971
Bram Moolenaarc667da52019-11-30 20:52:27 +01001972 // If autocommands did not change the cursor position, restore cursor lnum
1973 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (curwin->w_cursor.lnum == 1 && inindent(0))
1975 buflist_getfpos();
1976
Bram Moolenaarc667da52019-11-30 20:52:27 +01001977 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001979 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001980 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00001981 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
Bram Moolenaar009b2592004-10-24 19:18:58 +00001983#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001984 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001985 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001986#endif
1987
Bram Moolenaarc667da52019-11-30 20:52:27 +01001988 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001989 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991#ifdef FEAT_KEYMAP
1992 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001993 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001995#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001996 // May need to set the spell language. Can only do this after the buffer
1997 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001998 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001999 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002000#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002001#ifdef FEAT_VIMINFO
2002 curbuf->b_last_used = vim_time();
2003#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002004
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002005 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006}
2007
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002008#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2009/*
2010 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002011 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002012 */
2013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002014do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002015{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002016 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002017 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002018 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002019 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002020 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002021 last_chdir_reason = "autochdir";
2022 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002023}
2024#endif
2025
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002026 static void
2027no_write_message_buf(buf_T *buf UNUSED)
2028{
2029#ifdef FEAT_TERMINAL
2030 if (term_job_running(buf->b_term))
2031 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2032 else
2033#endif
2034 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2035 buf->b_fnum);
2036}
2037
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002038 void
2039no_write_message(void)
2040{
2041#ifdef FEAT_TERMINAL
2042 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002043 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002044 else
2045#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002046 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002047}
2048
2049 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002050no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002051{
2052#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002053 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002054 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002055 else
2056#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002057 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002058}
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060/*
2061 * functions for dealing with the buffer list
2062 */
2063
2064/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002065 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2066 * only one window. That means it can be re-used.
2067 */
2068 int
2069curbuf_reusable(void)
2070{
2071 return (curbuf != NULL
2072 && curbuf->b_ffname == NULL
2073 && curbuf->b_nwindows <= 1
2074 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002075 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002076 && !curbufIsChanged());
2077}
2078
2079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 * Add a file name to the buffer list. Return a pointer to the buffer.
2081 * If the same file name already exists return a pointer to that buffer.
2082 * If it does not exist, or if fname == NULL, a new entry is created.
2083 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2084 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2085 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002086 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002087 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2088 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002089 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 * This is the ONLY way to create a new buffer.
2091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002093buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002094 char_u *ffname_arg, // full path of fname or relative
2095 char_u *sfname_arg, // short fname or NULL
2096 linenr_T lnum, // preferred cursor line
2097 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002099 char_u *ffname = ffname_arg;
2100 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 buf_T *buf;
2102#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002103 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#endif
2105
Bram Moolenaar480778b2016-07-14 22:09:39 +02002106 if (top_file_num == 1)
2107 hash_init(&buf_hashtab);
2108
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002109 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002112 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 */
2114#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002115 // On Unix we can use inode numbers when the file exists. Works better
2116 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2118 st.st_dev = (dev_T)-1;
2119#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002120 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121#ifdef UNIX
2122 buflist_findname_stat(ffname, &st)
2123#else
2124 buflist_findname(ffname)
2125#endif
2126 ) != NULL)
2127 {
2128 vim_free(ffname);
2129 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002130 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2131 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002132
2133 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002134 // copy the options now, if 'cpo' doesn't have 's' and not done
2135 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002136 buf_copy_options(buf, 0);
2137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2139 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002140 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002143 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002145 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002146 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002147 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002148 return NULL;
2149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 return buf;
2152 }
2153
2154 /*
2155 * If the current buffer has no name and no contents, use the current
2156 * buffer. Otherwise: Need to allocate a new buffer structure.
2157 *
2158 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002159 * (A spell file buffer is allocated in spell.c, but that's not a normal
2160 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 */
2162 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002163 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002166 // It's like this buffer is deleted. Watch out for autocommands that
2167 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002168 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2169 if (buf != curbuf) // autocommands deleted the buffer!
2170 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002171#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002172 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002173 {
2174 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179 if (buf != curbuf || curbuf == NULL)
2180 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002181 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (buf == NULL)
2183 {
2184 vim_free(ffname);
2185 return NULL;
2186 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002188 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002189 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 if (buf->b_vars == NULL)
2191 {
2192 vim_free(ffname);
2193 vim_free(buf);
2194 return NULL;
2195 }
2196 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2197#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002198 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
2200
2201 if (ffname != NULL)
2202 {
2203 buf->b_ffname = ffname;
2204 buf->b_sfname = vim_strsave(sfname);
2205 }
2206
2207 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002208 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2211 || buf->b_wininfo == NULL)
2212 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002213 if (buf->b_sfname != buf->b_ffname)
2214 VIM_CLEAR(buf->b_sfname);
2215 else
2216 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002217 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (buf != curbuf)
2219 free_buffer(buf);
2220 return NULL;
2221 }
2222
2223 if (buf == curbuf)
2224 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002225 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002226
Bram Moolenaarc667da52019-11-30 20:52:27 +01002227 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002228 buf->b_p_initialized = FALSE;
2229 buf_copy_options(buf, BCO_ENTER);
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002232 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 curbuf->b_kmap_state |= KEYMAP_INIT;
2234#endif
2235 }
2236 else
2237 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002238 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002240 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
2242 buf->b_prev = NULL;
2243 firstbuf = buf;
2244 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002245 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 lastbuf->b_next = buf;
2248 buf->b_prev = lastbuf;
2249 }
2250 lastbuf = buf;
2251
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002252 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2253 {
2254 // Recycle a previously used buffer number. Used for buffers which
2255 // are normally hidden, e.g. in a popup window. Avoids that the
2256 // buffer number grows rapidly.
2257 --buf_reuse.ga_len;
2258 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002259
2260 // Move buffer to the right place in the buffer list.
2261 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2262 {
2263 buf_T *prev = buf->b_prev;
2264
2265 prev->b_next = buf->b_next;
2266 if (prev->b_next != NULL)
2267 prev->b_next->b_prev = prev;
2268 buf->b_next = prev;
2269 buf->b_prev = prev->b_prev;
2270 if (buf->b_prev != NULL)
2271 buf->b_prev->b_next = buf;
2272 prev->b_prev = buf;
2273 if (lastbuf == buf)
2274 lastbuf = prev;
2275 if (firstbuf == prev)
2276 firstbuf = buf;
2277 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002278 }
2279 else
2280 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002281 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002284 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002287 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 top_file_num = 1;
2290 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002291 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002293 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 buf_copy_options(buf, BCO_ALWAYS);
2295 }
2296
2297 buf->b_wininfo->wi_fpos.lnum = lnum;
2298 buf->b_wininfo->wi_win = curwin;
2299
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002300#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002301 hash_init(&buf->b_s.b_keywtab);
2302 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304
2305 buf->b_fname = buf->b_sfname;
2306#ifdef UNIX
2307 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002308 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 else
2310 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002311 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 buf->b_dev = st.st_dev;
2313 buf->b_ino = st.st_ino;
2314 }
2315#endif
2316 buf->b_u_synced = TRUE;
2317 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002318 if (flags & BLN_DUMMY)
2319 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002321 clrallmarks(buf); // clear marks
2322 fmarks_check_names(buf); // check file marks for this file
2323 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (!(flags & BLN_DUMMY))
2325 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002326 bufref_T bufref;
2327
Bram Moolenaarc667da52019-11-30 20:52:27 +01002328 // Tricky: these autocommands may change the buffer list. They could
2329 // also split the window with re-using the one empty buffer. This may
2330 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002331 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002332 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002333 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002334 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002336 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002337 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002338 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002339 return NULL;
2340 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002341#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002342 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 return buf;
2348}
2349
2350/*
2351 * Free the memory for the options of a buffer.
2352 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2353 * 'fileencoding'.
2354 */
2355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002356free_buf_options(
2357 buf_T *buf,
2358 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 if (free_p_ff)
2361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 clear_string_option(&buf->b_p_bh);
2365 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367#ifdef FEAT_FIND_ID
2368 clear_string_option(&buf->b_p_def);
2369 clear_string_option(&buf->b_p_inc);
2370# ifdef FEAT_EVAL
2371 clear_string_option(&buf->b_p_inex);
2372# endif
2373#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002374#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 clear_string_option(&buf->b_p_inde);
2376 clear_string_option(&buf->b_p_indk);
2377#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002378#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2379 clear_string_option(&buf->b_p_bexpr);
2380#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002381#if defined(FEAT_CRYPT)
2382 clear_string_option(&buf->b_p_cm);
2383#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002384 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002385#if defined(FEAT_EVAL)
2386 clear_string_option(&buf->b_p_fex);
2387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002389# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002390 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2391 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002392 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 clear_string_option(&buf->b_p_key);
2395#endif
2396 clear_string_option(&buf->b_p_kp);
2397 clear_string_option(&buf->b_p_mps);
2398 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002399 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002401#ifdef FEAT_VARTABS
2402 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002403 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002404 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002405 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002406 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#ifdef FEAT_KEYMAP
2409 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002410 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 ga_clear(&buf->b_kmap_ga);
2412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_FOLDING
2415 clear_string_option(&buf->b_p_cms);
2416#endif
2417 clear_string_option(&buf->b_p_nf);
2418#ifdef FEAT_SYN_HL
2419 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002420 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002421#endif
2422#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002423 clear_string_option(&buf->b_s.b_p_spc);
2424 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002425 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002426 buf->b_s.b_cap_prog = NULL;
2427 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002428 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 clear_string_option(&buf->b_p_cink);
2433 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002434 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002435 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002437 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002439#ifdef FEAT_COMPL_FUNC
2440 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002441 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002442 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002443 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002444 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002445 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#ifdef FEAT_QUICKFIX
2448 clear_string_option(&buf->b_p_gp);
2449 clear_string_option(&buf->b_p_mp);
2450 clear_string_option(&buf->b_p_efm);
2451#endif
2452 clear_string_option(&buf->b_p_ep);
2453 clear_string_option(&buf->b_p_path);
2454 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002455 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002456#ifdef FEAT_EVAL
2457 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002458 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 clear_string_option(&buf->b_p_dict);
2461 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002462 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002464 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002465 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002466 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002467 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468}
2469
2470/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002471 * Get alternate file "n".
2472 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2473 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 * if (options & GETF_SETMARK) call setpcmark()
2475 * if (options & GETF_ALT) we are jumping to an alternate file.
2476 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2477 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002478 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 */
2480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002481buflist_getfile(
2482 int n,
2483 linenr_T lnum,
2484 int options,
2485 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 pos_T *fpos;
2490 colnr_T col;
2491
2492 buf = buflist_findnr(n);
2493 if (buf == NULL)
2494 {
2495 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002496 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002498 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return FAIL;
2500 }
2501
Bram Moolenaarc667da52019-11-30 20:52:27 +01002502 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (buf == curbuf)
2504 return OK;
2505
Bram Moolenaar71223e22022-05-30 15:23:09 +01002506 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002507 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
Bram Moolenaarc667da52019-11-30 20:52:27 +01002509 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (lnum == 0)
2511 {
2512 fpos = buflist_findfpos(buf);
2513 lnum = fpos->lnum;
2514 col = fpos->col;
2515 }
2516 else
2517 col = 0;
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (options & GETF_SWITCH)
2520 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002521 // If 'switchbuf' is set jump to the window containing "buf".
2522 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002523
Bram Moolenaarc667da52019-11-30 20:52:27 +01002524 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2525 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002526 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002527 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002529 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002530 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002531 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2532 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002534 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
2538 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002539 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002540 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2541 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002543 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (!p_sol && col != 0)
2545 {
2546 curwin->w_cursor.col = col;
2547 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 curwin->w_set_curswant = TRUE;
2550 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002551 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002553
2554 if (RedrawingDisabled > 0)
2555 --RedrawingDisabled;
2556 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557}
2558
2559/*
2560 * go to the last know line number for the current buffer
2561 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002563buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 pos_T *fpos;
2566
2567 fpos = buflist_findfpos(curbuf);
2568
2569 curwin->w_cursor.lnum = fpos->lnum;
2570 check_cursor_lnum();
2571
2572 if (p_sol)
2573 curwin->w_cursor.col = 0;
2574 else
2575 {
2576 curwin->w_cursor.col = fpos->col;
2577 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 curwin->w_set_curswant = TRUE;
2580 }
2581}
2582
Bram Moolenaar81695252004-12-29 20:58:21 +00002583/*
2584 * Find file in buffer list by name (it has to be for the current window).
2585 * Returns NULL if not found.
2586 */
2587 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002588buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002589{
2590 char_u *ffname;
2591 buf_T *buf = NULL;
2592
Bram Moolenaarc667da52019-11-30 20:52:27 +01002593 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002594 ffname = FullName_save(fname,
2595#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002596 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002597#else
2598 FALSE
2599#endif
2600 );
2601 if (ffname != NULL)
2602 {
2603 buf = buflist_findname(ffname);
2604 vim_free(ffname);
2605 }
2606 return buf;
2607}
Bram Moolenaar81695252004-12-29 20:58:21 +00002608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609/*
2610 * Find file in buffer list by name (it has to be for the current window).
2611 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002612 * Skips dummy buffers.
2613 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
2615 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002616buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002619 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 if (mch_stat((char *)ffname, &st) < 0)
2622 st.st_dev = (dev_T)-1;
2623 return buflist_findname_stat(ffname, &st);
2624}
2625
2626/*
2627 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2628 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002629 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 */
2631 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002632buflist_findname_stat(
2633 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002634 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636#endif
2637 buf_T *buf;
2638
Bram Moolenaarc667da52019-11-30 20:52:27 +01002639 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002640 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002641 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef UNIX
2643 , stp
2644#endif
2645 ))
2646 return buf;
2647 return NULL;
2648}
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650/*
2651 * Find file in buffer list by a regexp pattern.
2652 * Return fnum of the found buffer.
2653 * Return < 0 for error.
2654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002656buflist_findpat(
2657 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002658 char_u *pattern_end, // pointer to first char after pattern
2659 int unlisted, // find unlisted buffers
2660 int diffmode UNUSED, // find diff-mode buffers only
2661 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 int match = -1;
2665 int find_listed;
2666 char_u *pat;
2667 char_u *patend;
2668 int attempt;
2669 char_u *p;
2670 int toggledollar;
2671
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002672 // "%" is current file, "%%" or "#" is alternate file
2673 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2674 || (in_vim9script() && pattern_end == pattern + 2
2675 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002677 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002679 else
2680 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681#ifdef FEAT_DIFF
2682 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2683 match = -1;
2684#endif
2685 }
2686
2687 /*
2688 * Try four ways of matching a listed buffer:
2689 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002690 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 * attempt == 2: with '$' at end (only match at end)
2692 * attempt == 3: with '^' at start and '$' at end (only full match)
2693 * Repeat this for finding an unlisted buffer if there was no matching
2694 * listed buffer.
2695 */
2696 else
2697 {
2698 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2699 if (pat == NULL)
2700 return -1;
2701 patend = pat + STRLEN(pat) - 1;
2702 toggledollar = (patend > pat && *patend == '$');
2703
Bram Moolenaarc667da52019-11-30 20:52:27 +01002704 // First try finding a listed buffer. If not found and "unlisted"
2705 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 find_listed = TRUE;
2707 for (;;)
2708 {
2709 for (attempt = 0; attempt <= 3; ++attempt)
2710 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002711 regmatch_T regmatch;
2712
Bram Moolenaarc667da52019-11-30 20:52:27 +01002713 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002715 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002717 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002719 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002721 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002722 {
2723 if (regmatch.regprog == NULL)
2724 {
2725 // invalid pattern, possibly after switching engine
2726 vim_free(pat);
2727 return -1;
2728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (buf->b_p_bl == find_listed
2730#ifdef FEAT_DIFF
2731 && (!diffmode || diff_mode_buf(buf))
2732#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002733 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002735 if (curtab_only)
2736 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002737 // Ignore the match if the buffer is not open in
2738 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002739 win_T *wp;
2740
Bram Moolenaar29323592016-07-24 22:04:11 +02002741 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002742 if (wp->w_buffer == buf)
2743 break;
2744 if (wp == NULL)
2745 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002746 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002747 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
2749 match = -2;
2750 break;
2751 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002752 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002756 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002757 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 break;
2759 }
2760
Bram Moolenaarc667da52019-11-30 20:52:27 +01002761 // Only search for unlisted buffers if there was no match with
2762 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if (!unlisted || !find_listed || match != -1)
2764 break;
2765 find_listed = FALSE;
2766 }
2767
2768 vim_free(pat);
2769 }
2770
2771 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002772 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002774 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 return match;
2776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaar52410572019-10-27 05:12:45 +01002778#ifdef FEAT_VIMINFO
2779typedef struct {
2780 buf_T *buf;
2781 char_u *match;
2782} bufmatch_T;
2783#endif
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785/*
2786 * Find all buffer names that match.
2787 * For command line expansion of ":buf" and ":sbuf".
2788 * Return OK if matches found, FAIL otherwise.
2789 */
2790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002791ExpandBufnames(
2792 char_u *pat,
2793 int *num_file,
2794 char_u ***file,
2795 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002797 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 buf_T *buf;
2799 int round;
2800 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002801 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002802#ifdef FEAT_VIMINFO
2803 bufmatch_T *matches = NULL;
2804#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002805 int fuzzy;
2806 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002807 regmatch_T regmatch;
2808 int score = 0;
2809 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
Bram Moolenaarc667da52019-11-30 20:52:27 +01002811 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 *file = NULL;
2813
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002814#ifdef FEAT_DIFF
2815 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2816 return FAIL;
2817#endif
2818
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819 fuzzy = cmdline_fuzzy_complete(pat);
2820
2821 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2822 // expression matching)
2823 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002825 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002826 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002827 int len = (int)STRLEN(pat);
2828 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002829 if (patc == NULL)
2830 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002831 STRNCPY(patc, pat + 1, len - 1);
2832 patc[len - 1] = NUL;
2833 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002834 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002835 else if (*pat == '^')
2836 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002837 else
2838 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002839 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002840 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002841
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002842 // round == 1: Count the matches.
2843 // round == 2: Build the array to keep the matches.
2844 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002845 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002846 count = 0;
2847 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002848 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002849 if (!buf->b_p_bl) // skip unlisted buffers
2850 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002851#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002852 if (options & BUF_DIFF_FILTER)
2853 // Skip buffers not suitable for
2854 // :diffget or :diffput completion.
2855 if (buf == curbuf || !diff_mode_buf(buf))
2856 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002857#endif
2858
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002859 if (!fuzzy)
2860 {
2861 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002862 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002863 // invalid pattern, possibly after recompiling
2864 if (to_free)
2865 vim_free(patc);
2866 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002867 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002868 p = buflist_match(&regmatch, buf, p_wic);
2869 }
2870 else
2871 {
2872 p = NULL;
2873 // first try matching with the short file name
2874 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2875 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002876 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002877 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002878 // next try matching with the full path file name
2879 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2880 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002883
2884 if (p == NULL)
2885 continue;
2886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if (round == 1)
2888 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002889 ++count;
2890 continue;
2891 }
2892
2893 if (options & WILD_HOME_REPLACE)
2894 p = home_replace_save(buf, p);
2895 else
2896 p = vim_strsave(p);
2897
2898 if (!fuzzy)
2899 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002900#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002901 if (matches != NULL)
2902 {
2903 matches[count].buf = buf;
2904 matches[count].match = p;
2905 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002906 }
2907 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002908#endif
2909 (*file)[count++] = p;
2910 }
2911 else
2912 {
2913 fuzmatch[count].idx = count;
2914 fuzmatch[count].str = p;
2915 fuzmatch[count].score = score;
2916 count++;
2917 }
2918 }
2919 if (count == 0) // no match found, break here
2920 break;
2921 if (round == 1)
2922 {
2923 if (!fuzzy)
2924 {
2925 *file = ALLOC_MULT(char_u *, count);
2926 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002927 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002928 vim_regfree(regmatch.regprog);
2929 if (to_free)
2930 vim_free(patc);
2931 return FAIL;
2932 }
2933#ifdef FEAT_VIMINFO
2934 if (options & WILD_BUFLASTUSED)
2935 matches = ALLOC_MULT(bufmatch_T, count);
2936#endif
2937 }
2938 else
2939 {
2940 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2941 if (fuzmatch == NULL)
2942 {
2943 *num_file = 0;
2944 *file = NULL;
2945 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002951 if (!fuzzy)
2952 {
2953 vim_regfree(regmatch.regprog);
2954 if (to_free)
2955 vim_free(patc);
2956 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002957
Bram Moolenaar52410572019-10-27 05:12:45 +01002958#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002959 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002960 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002961 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002962 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002963 int i;
2964 if (count > 1)
2965 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2966 // if the current buffer is first in the list, place it at the end
2967 if (matches[0].buf == curbuf)
2968 {
2969 for (i = 1; i < count; i++)
2970 (*file)[i-1] = matches[i].match;
2971 (*file)[count-1] = matches[0].match;
2972 }
2973 else
2974 {
2975 for (i = 0; i < count; i++)
2976 (*file)[i] = matches[i].match;
2977 }
2978 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002979 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002980 }
2981 else
2982 {
2983 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2984 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002985 }
2986#endif
2987
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 *num_file = count;
2989 return (count == 0 ? FAIL : OK);
2990}
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
2993 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002994 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 */
2996 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002997buflist_match(
2998 regmatch_T *rmp,
2999 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003000 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 char_u *match;
3003
Bram Moolenaarc667da52019-11-30 20:52:27 +01003004 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003005 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003006 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003007 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 return match;
3010}
3011
3012/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003013 * Try matching the regexp in "rmp->regprog" with file name "name".
3014 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 * Return "name" when there is a match, NULL when not.
3016 */
3017 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003018fname_match(
3019 regmatch_T *rmp,
3020 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003021 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 char_u *match = NULL;
3024 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003026 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003027 if (name == NULL || rmp->regprog == NULL)
3028 return NULL;
3029
3030 // Ignore case when 'fileignorecase' or the argument is set.
3031 rmp->rm_ic = p_fic || ignore_case;
3032 if (vim_regexec(rmp, name, (colnr_T)0))
3033 match = name;
3034 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003036 // Replace $(HOME) with '~' and try matching again.
3037 p = home_replace_save(NULL, name);
3038 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003040 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042
3043 return match;
3044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
3046/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003047 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 */
3049 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003050buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003052 char_u key[VIM_SIZEOF_INT * 2 + 1];
3053 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 if (nr == 0)
3056 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003057 sprintf((char *)key, "%x", nr);
3058 hi = hash_find(&buf_hashtab, key);
3059
3060 if (!HASHITEM_EMPTY(hi))
3061 return (buf_T *)(hi->hi_key
3062 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 return NULL;
3064}
3065
3066/*
3067 * Get name of file 'n' in the buffer list.
3068 * When the file has no name an empty string is returned.
3069 * home_replace() is used to shorten the file name (used for marks).
3070 * Returns a pointer to allocated memory, of NULL when failed.
3071 */
3072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073buflist_nr2name(
3074 int n,
3075 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003076 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 buf_T *buf;
3079
3080 buf = buflist_findnr(n);
3081 if (buf == NULL)
3082 return NULL;
3083 return home_replace_save(helptail ? buf : NULL,
3084 fullname ? buf->b_ffname : buf->b_fname);
3085}
3086
3087/*
3088 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3089 * When "copy_options" is TRUE save the local window option values.
3090 * When "lnum" is 0 only do the options.
3091 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093buflist_setfpos(
3094 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003095 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003096 linenr_T lnum,
3097 colnr_T col,
3098 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 wininfo_T *wip;
3101
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003102 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 if (wip->wi_win == win)
3104 break;
3105 if (wip == NULL)
3106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003107 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003108 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (wip == NULL)
3110 return;
3111 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003112 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 lnum = 1;
3114 }
3115 else
3116 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003117 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (wip->wi_prev)
3119 wip->wi_prev->wi_next = wip->wi_next;
3120 else
3121 buf->b_wininfo = wip->wi_next;
3122 if (wip->wi_next)
3123 wip->wi_next->wi_prev = wip->wi_prev;
3124 if (copy_options && wip->wi_optset)
3125 {
3126 clear_winopt(&wip->wi_opt);
3127#ifdef FEAT_FOLDING
3128 deleteFoldRecurse(&wip->wi_folds);
3129#endif
3130 }
3131 }
3132 if (lnum != 0)
3133 {
3134 wip->wi_fpos.lnum = lnum;
3135 wip->wi_fpos.col = col;
3136 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003137 if (win != NULL)
3138 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003139 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003141 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3143#ifdef FEAT_FOLDING
3144 wip->wi_fold_manual = win->w_fold_manual;
3145 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3146#endif
3147 wip->wi_optset = TRUE;
3148 }
3149
Bram Moolenaarc667da52019-11-30 20:52:27 +01003150 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 wip->wi_next = buf->b_wininfo;
3152 buf->b_wininfo = wip;
3153 wip->wi_prev = NULL;
3154 if (wip->wi_next)
3155 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003158#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003159/*
3160 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3161 * page. That's because a diff is local to a tab page.
3162 */
3163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003165{
3166 win_T *wp;
3167
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003168 if (!wip->wi_opt.wo_diff)
3169 return FALSE;
3170
3171 FOR_ALL_WINDOWS(wp)
3172 // return FALSE when it's a window in the current tab page, thus
3173 // the buffer was in diff mode here
3174 if (wip->wi_win == wp)
3175 return FALSE;
3176 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003177}
3178#endif
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180/*
3181 * Find info for the current window in buffer "buf".
3182 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003183 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003184 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3185 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 * Returns NULL when there isn't any info.
3187 */
3188 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003189find_wininfo(
3190 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003191 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 wininfo_T *wip;
3195
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003196 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003197 if (wip->wi_win == curwin
3198#ifdef FEAT_DIFF
3199 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3200#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003201
3202 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003204
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003205 if (wip != NULL)
3206 return wip;
3207
Bram Moolenaarc667da52019-11-30 20:52:27 +01003208 // If no wininfo for curwin, use the first in the list (that doesn't have
3209 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003210 // If "need_options" is TRUE skip entries that don't have options set,
3211 // unless the window is editing "buf", so we can copy from the window
3212 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003213#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003214 if (skip_diff_buffer)
3215 {
3216 FOR_ALL_BUF_WININFO(buf, wip)
3217 if (!wininfo_other_tab_diff(wip)
3218 && (!need_options || wip->wi_optset
3219 || (wip->wi_win != NULL
3220 && wip->wi_win->w_buffer == buf)))
3221 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003222 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003223 else
3224#endif
3225 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 return wip;
3227}
3228
3229/*
3230 * Reset the local window options to the values last used in this window.
3231 * If the buffer wasn't used in this window before, use the values from
3232 * the most recently used window. If the values were never set, use the
3233 * global values for the window.
3234 */
3235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 wininfo_T *wip;
3239
3240 clear_winopt(&curwin->w_onebuf_opt);
3241#ifdef FEAT_FOLDING
3242 clearFolding(curwin);
3243#endif
3244
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003245 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003246 if (wip != NULL && wip->wi_win != NULL
3247 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003249 // The buffer is currently displayed in the window: use the actual
3250 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003251 win_T *wp = wip->wi_win;
3252
3253 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3254#ifdef FEAT_FOLDING
3255 curwin->w_fold_manual = wp->w_fold_manual;
3256 curwin->w_foldinvalid = TRUE;
3257 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3258#endif
3259 }
3260 else if (wip != NULL && wip->wi_optset)
3261 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003262 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3264#ifdef FEAT_FOLDING
3265 curwin->w_fold_manual = wip->wi_fold_manual;
3266 curwin->w_foldinvalid = TRUE;
3267 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3268#endif
3269 }
3270 else
3271 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003272 if (wip != NULL)
3273 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003276 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (p_fdls >= 0)
3278 curwin->w_p_fdl = p_fdls;
3279#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003280 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283/*
3284 * Find the position (lnum and col) for the buffer 'buf' for the current
3285 * window.
3286 * Returns a pointer to no_position if no position is found.
3287 */
3288 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003289buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003292 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003294 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (wip != NULL)
3296 return &(wip->wi_fpos);
3297 else
3298 return &no_position;
3299}
3300
3301/*
3302 * Find the lnum for the buffer 'buf' for the current window.
3303 */
3304 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 return buflist_findfpos(buf)->lnum;
3308}
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003311 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003314buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaar52410572019-10-27 05:12:45 +01003316 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 int len;
3318 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003319 int ro_char;
3320 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003321#ifdef FEAT_TERMINAL
3322 int job_running;
3323 int job_none_open;
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar52410572019-10-27 05:12:45 +01003326#ifdef FEAT_VIMINFO
3327 garray_T buflist;
3328 buf_T **buflist_data = NULL, **p;
3329
3330 if (vim_strchr(eap->arg, 't'))
3331 {
3332 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003333 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003334 {
3335 if (ga_grow(&buflist, 1) == OK)
3336 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3337 }
3338
3339 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3340 sizeof(buf_T *), buf_compare);
3341
Bram Moolenaar3b991522019-11-06 23:26:20 +01003342 buflist_data = (buf_T **)buflist.ga_data;
3343 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003344 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003345 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003346
Bram Moolenaar3b991522019-11-06 23:26:20 +01003347 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003348 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3349 : buf->b_next)
3350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003354#ifdef FEAT_TERMINAL
3355 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003356 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003357#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003358 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003359 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3360 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3361 || (vim_strchr(eap->arg, '+')
3362 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3363 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003364 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003365 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003366 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3367#ifdef FEAT_TERMINAL
3368 || (vim_strchr(eap->arg, 'R')
3369 && (!job_running || (job_running && job_none_open)))
3370 || (vim_strchr(eap->arg, '?')
3371 && (!job_running || (job_running && !job_none_open)))
3372 || (vim_strchr(eap->arg, 'F')
3373 && (job_running || buf->b_term == NULL))
3374#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003375 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3376 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3377 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3378 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3379 || (vim_strchr(eap->arg, '#')
3380 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003383 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 else
3385 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003386 if (message_filtered(NameBuff))
3387 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003389 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3390 : (bufIsChanged(buf) ? '+' : ' ');
3391#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003392 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003393 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003394 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003395 ro_char = '?';
3396 else
3397 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003398 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3399 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003400 }
3401 else if (buf->b_term != NULL)
3402 ro_char = 'F';
3403 else
3404#endif
3405 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3406
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003407 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003408 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 buf->b_fnum,
3410 buf->b_p_bl ? ' ' : 'u',
3411 buf == curbuf ? '%' :
3412 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3413 buf->b_ml.ml_mfp == NULL ? ' ' :
3414 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003415 ro_char,
3416 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003417 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003418 if (len > IOSIZE - 20)
3419 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaarc667da52019-11-30 20:52:27 +01003421 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 i = 40 - vim_strsize(IObuff);
3423 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003425 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003426#ifdef FEAT_VIMINFO
3427 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3428 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3429 else
3430#endif
3431 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3432 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003433 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003435 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 ui_breakcheck();
3437 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003438
3439#ifdef FEAT_VIMINFO
3440 if (buflist_data)
3441 ga_clear(&buflist);
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445/*
3446 * Get file name and line number for file 'fnum'.
3447 * Used by DoOneCmd() for translating '%' and '#'.
3448 * Used by insert_reg() and cmdline_paste() for '#' register.
3449 * Return FAIL if not found, OK for success.
3450 */
3451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003452buflist_name_nr(
3453 int fnum,
3454 char_u **fname,
3455 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 buf_T *buf;
3458
3459 buf = buflist_findnr(fnum);
3460 if (buf == NULL || buf->b_fname == NULL)
3461 return FAIL;
3462
3463 *fname = buf->b_fname;
3464 *lnum = buflist_findlnum(buf);
3465
3466 return OK;
3467}
3468
3469/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003470 * Set the file name for "buf"' to "ffname_arg", short file name to
3471 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 * The file name with the full path is also remembered, for when :cd is used.
3473 * Returns FAIL for failure (file name already in use by other buffer)
3474 * OK otherwise.
3475 */
3476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003477setfname(
3478 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003479 char_u *ffname_arg,
3480 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003481 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003483 char_u *ffname = ffname_arg;
3484 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003485 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003487 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
3489
3490 if (ffname == NULL || *ffname == NUL)
3491 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003492 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003493 if (buf->b_sfname != buf->b_ffname)
3494 VIM_CLEAR(buf->b_sfname);
3495 else
3496 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003497 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#ifdef UNIX
3499 st.st_dev = (dev_T)-1;
3500#endif
3501 }
3502 else
3503 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003504 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3505 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 return FAIL;
3507
3508 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003509 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 * - if the buffer is loaded, fail
3511 * - if the buffer is not loaded, delete it from the list
3512 */
3513#ifdef UNIX
3514 if (mch_stat((char *)ffname, &st) < 0)
3515 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003516#endif
3517 if (!(buf->b_flags & BF_DUMMY))
3518#ifdef UNIX
3519 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003521 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523 if (obuf != NULL && obuf != buf)
3524 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003525 win_T *win;
3526 tabpage_T *tab;
3527 int in_use = FALSE;
3528
3529 // during startup a window may use a buffer that is not loaded yet
3530 FOR_ALL_TAB_WINDOWS(tab, win)
3531 if (win->w_buffer == obuf)
3532 in_use = TRUE;
3533
3534 // it's loaded or used in a window, fail
3535 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003538 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 vim_free(ffname);
3540 return FAIL;
3541 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003542 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003543 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 sfname = vim_strsave(sfname);
3546 if (ffname == NULL || sfname == NULL)
3547 {
3548 vim_free(sfname);
3549 vim_free(ffname);
3550 return FAIL;
3551 }
3552#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003553 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003555 if (buf->b_sfname != buf->b_ffname)
3556 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 buf->b_ffname = ffname;
3559 buf->b_sfname = sfname;
3560 }
3561 buf->b_fname = buf->b_sfname;
3562#ifdef UNIX
3563 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003564 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
3566 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003567 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 buf->b_dev = st.st_dev;
3569 buf->b_ino = st.st_ino;
3570 }
3571#endif
3572
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 buf_name_changed(buf);
3576 return OK;
3577}
3578
3579/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003580 * Crude way of changing the name of a buffer. Use with care!
3581 * The name should be relative to the current directory.
3582 */
3583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003585{
3586 buf_T *buf;
3587
3588 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003589 if (buf == NULL)
3590 return;
3591
3592 if (buf->b_sfname != buf->b_ffname)
3593 vim_free(buf->b_sfname);
3594 vim_free(buf->b_ffname);
3595 buf->b_ffname = vim_strsave(name);
3596 buf->b_sfname = NULL;
3597 // Allocate ffname and expand into full path. Also resolves .lnk
3598 // files on Win32.
3599 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3600 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003601}
3602
3603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * Take care of what needs to be done when the name of buffer "buf" has
3605 * changed.
3606 */
3607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003608buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 /*
3611 * If the file name changed, also change the name of the swapfile
3612 */
3613 if (buf->b_ml.ml_mfp != NULL)
3614 ml_setname(buf);
3615
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003616#ifdef FEAT_TERMINAL
3617 if (buf->b_term != NULL)
3618 term_clear_status_text(buf->b_term);
3619#endif
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003622 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003623 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003624 status_redraw_all(); // status lines need to be redrawn
3625 fmarks_check_names(buf); // check named file marks
3626 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627}
3628
3629/*
3630 * set alternate file name for current window
3631 *
3632 * Used by do_one_cmd(), do_write() and do_ecmd().
3633 * Return the buffer.
3634 */
3635 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636setaltfname(
3637 char_u *ffname,
3638 char_u *sfname,
3639 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
3641 buf_T *buf;
3642
Bram Moolenaarc667da52019-11-30 20:52:27 +01003643 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003645 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 curwin->w_alt_fnum = buf->b_fnum;
3647 return buf;
3648}
3649
3650/*
3651 * Get alternate file name for current window.
3652 * Return NULL if there isn't any, and give error message if requested.
3653 */
3654 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003656 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 char_u *fname;
3659 linenr_T dummy;
3660
3661 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3662 {
3663 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003664 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return NULL;
3666 }
3667 return fname;
3668}
3669
3670/*
3671 * Add a file name to the buflist and return its number.
3672 * Uses same flags as buflist_new(), except BLN_DUMMY.
3673 *
3674 * used by qf_init(), main() and doarglist()
3675 */
3676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003677buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 buf_T *buf;
3680
3681 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3682 if (buf != NULL)
3683 return buf->b_fnum;
3684 return 0;
3685}
3686
3687#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3688/*
3689 * Adjust slashes in file names. Called after 'shellslash' was set.
3690 */
3691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003692buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
3694 buf_T *bp;
3695
Bram Moolenaar29323592016-07-24 22:04:11 +02003696 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 if (bp->b_ffname != NULL)
3699 slash_adjust(bp->b_ffname);
3700 if (bp->b_sfname != NULL)
3701 slash_adjust(bp->b_sfname);
3702 }
3703}
3704#endif
3705
3706/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003707 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 * Also save the local window option values.
3709 */
3710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003713 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714}
3715
3716/*
3717 * Return TRUE if 'ffname' is not the same file as current file.
3718 * Fname must have a full path (expanded by mch_FullName()).
3719 */
3720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 return otherfile_buf(curbuf, ffname
3724#ifdef UNIX
3725 , NULL
3726#endif
3727 );
3728}
3729
3730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003731otherfile_buf(
3732 buf_T *buf,
3733 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003735 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003739 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3741 return TRUE;
3742 if (fnamecmp(ffname, buf->b_ffname) == 0)
3743 return FALSE;
3744#ifdef UNIX
3745 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003746 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaarc667da52019-11-30 20:52:27 +01003748 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (stp == NULL)
3750 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003751 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 st.st_dev = (dev_T)-1;
3753 stp = &st;
3754 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003755 // Use dev/ino to check if the files are the same, even when the names
3756 // are different (possible with links). Still need to compare the
3757 // name above, for when the file doesn't exist yet.
3758 // Problem: The dev/ino changes when a file is deleted (and created
3759 // again) and remains the same when renamed/moved. We don't want to
3760 // mch_stat() each buffer each time, that would be too slow. Get the
3761 // dev/ino again when they appear to match, but not when they appear
3762 // to be different: Could skip a buffer when it's actually the same
3763 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (buf_same_ino(buf, stp))
3765 {
3766 buf_setino(buf);
3767 if (buf_same_ino(buf, stp))
3768 return FALSE;
3769 }
3770 }
3771#endif
3772 return TRUE;
3773}
3774
3775#if defined(UNIX) || defined(PROTO)
3776/*
3777 * Set inode and device number for a buffer.
3778 * Must always be called when b_fname is changed!.
3779 */
3780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003783 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
3785 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3786 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003787 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 buf->b_dev = st.st_dev;
3789 buf->b_ino = st.st_ino;
3790 }
3791 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003792 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793}
3794
3795/*
3796 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3797 */
3798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003799buf_same_ino(
3800 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003801 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003803 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 && stp->st_dev == buf->b_dev
3805 && stp->st_ino == buf->b_ino);
3806}
3807#endif
3808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003809/*
3810 * Print info about the current buffer.
3811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003814 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003815 int shorthelp,
3816 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 char_u *name;
3819 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003820 char *p;
3821 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003822 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003824 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (buffer == NULL)
3826 return;
3827
Bram Moolenaarc667da52019-11-30 20:52:27 +01003828 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003830 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 p = buffer + STRLEN(buffer);
3832 }
3833 else
3834 p = buffer;
3835
3836 *p++ = '"';
3837 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003838 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 else
3840 {
3841 if (!fullname && curbuf->b_fname != NULL)
3842 name = curbuf->b_fname;
3843 else
3844 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003845 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 (int)(IOSIZE - (p - buffer)), TRUE);
3847 }
3848
Bram Moolenaar32526b32019-01-19 17:43:09 +01003849 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 curbufIsChanged() ? (shortmess(SHM_MOD)
3851 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003852 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003854 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003855 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003857 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 : _("[readonly]")) : "",
3859 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3860 || curbuf->b_p_ro) ?
3861 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003862 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3863 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (curwin->w_cursor.lnum > 1000000L)
3865 n = (int)(((long)curwin->w_cursor.lnum) /
3866 ((long)curbuf->b_ml.ml_line_count / 100L));
3867 else
3868 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3869 (long)curbuf->b_ml.ml_line_count);
3870 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003871 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003873 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003875 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3876 curbuf->b_ml.ml_line_count),
3877 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
3879 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003880 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 _("line %ld of %ld --%d%%-- col "),
3882 (long)curwin->w_cursor.lnum,
3883 (long)curbuf->b_ml.ml_line_count,
3884 n);
3885 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003886 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003887 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3889 }
3890
Bram Moolenaar32526b32019-01-19 17:43:09 +01003891 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3892 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
3894 if (dont_truncate)
3895 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003896 // Temporarily set msg_scroll to avoid the message being truncated.
3897 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 msg_start();
3899 n = msg_scroll;
3900 msg_scroll = TRUE;
3901 msg(buffer);
3902 msg_scroll = n;
3903 }
3904 else
3905 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003906 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003908 // Need to repeat the message after redrawing when:
3909 // - When restart_edit is set (otherwise there will be a delay
3910 // before redrawing).
3911 // - When the screen was scrolled but there is no wait-return
3912 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003913 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915
3916 vim_free(buffer);
3917}
3918
3919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920col_print(
3921 char_u *buf,
3922 size_t buflen,
3923 int col,
3924 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003927 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003929 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930}
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932static char_u *lasttitle = NULL;
3933static char_u *lasticon = NULL;
3934
Bram Moolenaar84a93082018-06-16 22:58:15 +02003935/*
3936 * Put the file name in the title bar and icon of the window.
3937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003942 char_u *title_str = NULL;
3943 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 int maxlen = 0;
3945 int len;
3946 int mustset;
3947 char_u buf[IOSIZE];
3948 int off;
3949
3950 if (!redrawing())
3951 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003952 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 need_maketitle = TRUE;
3954 return;
3955 }
3956
3957 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003958 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003959 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961 if (p_title)
3962 {
3963 if (p_titlelen > 0)
3964 {
3965 maxlen = p_titlelen * Columns / 100;
3966 if (maxlen < 10)
3967 maxlen = 10;
3968 }
3969
Bram Moolenaar84a93082018-06-16 22:58:15 +02003970 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (*p_titlestring != NUL)
3972 {
3973#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003974 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003975 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
3976 (char_u *)"titlestring", 0,
3977 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
3979#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003980 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 else
3983 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003984 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Bram Moolenaar2c666692012-09-05 13:30:40 +02003986#define SPACE_FOR_FNAME (IOSIZE - 100)
3987#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003988#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003990 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003991#ifdef FEAT_TERMINAL
3992 else if (curbuf->b_term != NULL)
3993 {
3994 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3995 SPACE_FOR_FNAME);
3996 }
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 else
3999 {
4000 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02004001 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004
Bram Moolenaar21554412017-07-24 21:44:43 +02004005#ifdef FEAT_TERMINAL
4006 if (curbuf->b_term == NULL)
4007#endif
4008 switch (bufIsChanged(curbuf)
4009 + (curbuf->b_p_ro * 2)
4010 + (!curbuf->b_p_ma * 4))
4011 {
4012 case 1: STRCAT(buf, " +"); break;
4013 case 2: STRCAT(buf, " ="); break;
4014 case 3: STRCAT(buf, " =+"); break;
4015 case 4:
4016 case 6: STRCAT(buf, " -"); break;
4017 case 5:
4018 case 7: STRCAT(buf, " -+"); break;
4019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
Bram Moolenaar21554412017-07-24 21:44:43 +02004021 if (curbuf->b_fname != NULL
4022#ifdef FEAT_TERMINAL
4023 && curbuf->b_term == NULL
4024#endif
4025 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004027 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 off = (int)STRLEN(buf);
4029 buf[off++] = ' ';
4030 buf[off++] = '(';
4031 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004032 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004034 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004035 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 off += 2;
4037#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004038 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004039 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004041 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004042 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004043 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004044 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004048
Bram Moolenaarc667da52019-11-30 20:52:27 +01004049 // Translate unprintable chars and concatenate. Keep some
4050 // room for the server name. When there is no room (very long
4051 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004052 if (off < SPACE_FOR_DIR)
4053 {
4054 p = transstr(buf + off);
4055 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4056 vim_free(p);
4057 }
4058 else
4059 {
4060 vim_strncpy(buf + off, (char_u *)"...",
4061 (size_t)(SPACE_FOR_ARGNR - off));
4062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 STRCAT(buf, ")");
4064 }
4065
Bram Moolenaar2c666692012-09-05 13:30:40 +02004066 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068#if defined(FEAT_CLIENTSERVER)
4069 if (serverName != NULL)
4070 {
4071 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004072 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 else
4075#endif
4076 STRCAT(buf, " - VIM");
4077
4078 if (maxlen > 0)
4079 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004080 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004081 if (vim_strsize(buf) > maxlen)
4082 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 }
4085 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004086 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 if (p_icon)
4089 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004090 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (*p_iconstring != NUL)
4092 {
4093#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004094 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004095 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4096 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 else
4098#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004099 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 else
4102 {
4103 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004104 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004105 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004106 p = gettail(curbuf->b_ffname);
4107 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004108 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004109 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (len > 100)
4111 {
4112 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004114 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004115 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004117 STRCPY(icon_str, p);
4118 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 }
4121
Bram Moolenaar84a93082018-06-16 22:58:15 +02004122 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 if (mustset)
4125 resettitle();
4126}
4127
4128/*
4129 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4130 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004131 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
4133 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004134value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 if ((str == NULL) != (*last == NULL)
4137 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4138 {
4139 vim_free(*last);
4140 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004143 mch_restore_title(
4144 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004149 return TRUE;
4150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 return FALSE;
4153}
4154
4155/*
4156 * Put current window title back (used after calling a shell)
4157 */
4158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004159resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
4161 mch_settitle(lasttitle, lasticon);
4162}
Bram Moolenaarea408852005-06-25 22:49:46 +00004163
4164# if defined(EXITFREE) || defined(PROTO)
4165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004166free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004167{
4168 vim_free(lasttitle);
4169 vim_free(lasticon);
4170}
4171# endif
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004174#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004175
4176/*
4177 * Used for building in the status line.
4178 */
4179typedef struct
4180{
4181 char_u *stl_start;
4182 int stl_minwid;
4183 int stl_maxwid;
4184 enum {
4185 Normal,
4186 Empty,
4187 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004188 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004189 Highlight,
4190 TabPage,
4191 Trunc
4192 } stl_type;
4193} stl_item_T;
4194
4195static size_t stl_items_len = 20; // Initial value, grows as needed.
4196static stl_item_T *stl_items = NULL;
4197static int *stl_groupitem = NULL;
4198static stl_hlrec_T *stl_hltab = NULL;
4199static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004200static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004203 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * Return length of string in screen cells.
4205 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004206 * Normally works for window "wp", except when working for 'tabline' then it
4207 * is "curwin".
4208 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004210 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4212 *
4213 * If maxwidth is not zero, the string will be filled at any middle marker
4214 * or truncated if too long, fillchar is used for all whitespace.
4215 */
4216 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004217build_stl_str_hl(
4218 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004219 char_u *out, // buffer to write into != NameBuff
4220 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004221 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004222 char_u *opt_name, // option name corresponding to "fmt"
4223 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004224 int fillchar,
4225 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004226 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4227 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
Bram Moolenaar10772302019-01-20 18:25:54 +01004229 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004230 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u *p;
4232 char_u *s;
4233 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004234 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004236 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004237 win_T *save_curwin;
4238 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004239 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240#endif
4241 int empty_line;
4242 colnr_T virtcol;
4243 long l;
4244 long n;
4245 int prevchar_isflag;
4246 int prevchar_isitem;
4247 int itemisflag;
4248 int fillable;
4249 char_u *str;
4250 long num;
4251 int width;
4252 int itemcnt;
4253 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004254 int group_end_userhl;
4255 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004257#ifdef FEAT_EVAL
4258 int evaldepth;
4259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 int minwid;
4261 int maxwid;
4262 int zeropad;
4263 char_u base;
4264 char_u opt;
4265#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004266 char_u buf_tmp[TMPLEN];
4267 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004268 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004269 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004270 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004271 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004272 // TODO: find out why using called_emsg_before makes tests fail, does it
4273 // matter?
4274 // int called_emsg_before = called_emsg;
4275 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004276
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004277 // When inside update_screen() we do not want redrawing a statusline,
4278 // ruler, title, etc. to trigger another redraw, it may cause an endless
4279 // loop.
4280 if (updating_screen)
4281 redraw_not_allowed = TRUE;
4282
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004283 if (stl_items == NULL)
4284 {
4285 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4286 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004287
4288 // Allocate one more, because the last element is used to indicate the
4289 // end of the list.
4290 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4291 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004292
4293 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004294 }
4295
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004296#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004297 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4298 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4299
4300 // When the format starts with "%!" then evaluate it as an expression and
4301 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004302 if (fmt[0] == '%' && fmt[1] == '!')
4303 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004304 typval_T tv;
4305
4306 tv.v_type = VAR_NUMBER;
4307 tv.vval.v_number = wp->w_id;
4308 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4309
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004310 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004311 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004312 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004313
4314 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004315 }
4316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 if (fillchar == 0)
4319 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004320
Bram Moolenaar10772302019-01-20 18:25:54 +01004321 // The cursor in windows other than the current one isn't always
4322 // up-to-date, esp. because of autocommands and timers.
4323 lnum = wp->w_cursor.lnum;
4324 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4325 {
4326 lnum = wp->w_buffer->b_ml.ml_line_count;
4327 wp->w_cursor.lnum = lnum;
4328 }
4329
4330 // Get line & check if empty (cursorpos will show "0-1"). Note that
4331 // p will become invalid when getting another buffer line.
4332 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004333 empty_line = (*p == NUL);
4334
Bram Moolenaar10772302019-01-20 18:25:54 +01004335 // Get the byte value now, in case we need it below. This is more efficient
4336 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004337 len = ml_get_buf_len(wp->w_buffer, lnum);
4338 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004339 {
4340 // Line may have changed since checking the cursor column, or the lnum
4341 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004342 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004343 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004344 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004345 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004346 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004347 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004350#ifdef FEAT_EVAL
4351 evaldepth = 0;
4352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 p = out;
4354 curitem = 0;
4355 prevchar_isflag = TRUE;
4356 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004357 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004359 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004360 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004361 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004362
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004363 stl_item_T *new_items =
4364 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004365 if (new_items == NULL)
4366 break;
4367 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004368
4369 int *new_groupitem =
4370 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004371 if (new_groupitem == NULL)
4372 break;
4373 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004374
4375 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004376 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004377 if (new_hlrec == NULL)
4378 break;
4379 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004380 new_hlrec = vim_realloc(stl_tabtab,
4381 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004382 if (new_hlrec == NULL)
4383 break;
4384 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004385
4386 int *new_separator_locs = vim_realloc(stl_separator_locations,
4387 sizeof(int) * new_len);
4388 if (new_separator_locs == NULL)
4389 break;
4390 stl_separator_locations = new_separator_locs;;
4391
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004392 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004393 }
4394
zeertzjq122dea72022-07-27 15:48:45 +01004395 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 prevchar_isflag = prevchar_isitem = FALSE;
4397
4398 /*
4399 * Handle up to the next '%' or the end.
4400 */
4401 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4402 *p++ = *s++;
4403 if (*s == NUL || p + 1 >= out + outlen)
4404 break;
4405
4406 /*
4407 * Handle one '%' item.
4408 */
4409 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004410 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (*s == '%')
4413 {
4414 if (p + 1 >= out + outlen)
4415 break;
4416 *p++ = *s++;
4417 prevchar_isflag = prevchar_isitem = FALSE;
4418 continue;
4419 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004420 // STL_SEPARATE: Separation between items, filled with white space.
4421 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 s++;
4424 if (groupdepth > 0)
4425 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004426 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004427 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 continue;
4429 }
4430 if (*s == STL_TRUNCMARK)
4431 {
4432 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004433 stl_items[curitem].stl_type = Trunc;
4434 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 continue;
4436 }
4437 if (*s == ')')
4438 {
4439 s++;
4440 if (groupdepth < 1)
4441 continue;
4442 groupdepth--;
4443
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004444 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 *p = NUL;
4446 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004447 if (curitem > stl_groupitem[groupdepth] + 1
4448 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004450 // remove group if all items are empty and highlight group
4451 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004452 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004453 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004454 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004456 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004457 group_start_userhl = group_end_userhl =
4458 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004459 break;
4460 }
4461 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004462 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004463 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004464 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004466 if (stl_items[n].stl_type == Highlight)
4467 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004468 }
4469 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004471 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 p = t;
4473 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004475 {
4476 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004477 if (stl_items[n].stl_type == Highlight)
4478 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004479 // adjust the start position of TabPage to the next
4480 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004481 if (stl_items[n].stl_type == TabPage)
4482 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004486 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004488 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (has_mbyte)
4490 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004491 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004493 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
4495 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004496 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 }
4499 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004500 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4501 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004504 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004506
4507 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004508 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004509 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaarc667da52019-11-30 20:52:27 +01004511 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004512 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
LemonBoy57ff5262022-05-09 21:03:47 +01004514 // Minus one for the leading '<' added above.
4515 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004516 if (stl_items[l].stl_start < t)
4517 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004520 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004522 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004523 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (n < 0)
4525 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004526 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 n = 0 - n;
4528 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004529 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
4532 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004533 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004534 l = (n - l) * MB_CHAR2LEN(fillchar);
4535 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004537 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004539 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4540 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004542 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 }
4545 continue;
4546 }
4547 minwid = 0;
4548 maxwid = 9999;
4549 zeropad = FALSE;
4550 l = 1;
4551 if (*s == '0')
4552 {
4553 s++;
4554 zeropad = TRUE;
4555 }
4556 if (*s == '-')
4557 {
4558 s++;
4559 l = -1;
4560 }
4561 if (VIM_ISDIGIT(*s))
4562 {
4563 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004564 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 minwid = 0;
4566 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004567 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004569 stl_items[curitem].stl_type = Highlight;
4570 stl_items[curitem].stl_start = p;
4571 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 s++;
4573 curitem++;
4574 continue;
4575 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004576 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4577 {
4578 if (*s == STL_TABCLOSENR)
4579 {
4580 if (minwid == 0)
4581 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004582 // %X ends the close label, go back to the previously
4583 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004584 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004585 if (stl_items[n].stl_type == TabPage
4586 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004587 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004588 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004589 break;
4590 }
4591 }
4592 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004593 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004594 minwid = - minwid;
4595 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004596 stl_items[curitem].stl_type = TabPage;
4597 stl_items[curitem].stl_start = p;
4598 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004599 s++;
4600 curitem++;
4601 continue;
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (*s == '.')
4604 {
4605 s++;
4606 if (VIM_ISDIGIT(*s))
4607 {
4608 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004609 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 maxwid = 50;
4611 }
4612 }
4613 minwid = (minwid > 50 ? 50 : minwid) * l;
4614 if (*s == '(')
4615 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004616 stl_groupitem[groupdepth++] = curitem;
4617 stl_items[curitem].stl_type = Group;
4618 stl_items[curitem].stl_start = p;
4619 stl_items[curitem].stl_minwid = minwid;
4620 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 s++;
4622 curitem++;
4623 continue;
4624 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004625#ifdef FEAT_EVAL
4626 // Denotes end of expanded %{} block
4627 if (*s == '}' && evaldepth > 0)
4628 {
4629 s++;
4630 evaldepth--;
4631 continue;
4632 }
4633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (vim_strchr(STL_ALL, *s) == NULL)
4635 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004636 if (*s == NUL) // can happen with "%0"
4637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 s++;
4639 continue;
4640 }
4641 opt = *s++;
4642
Bram Moolenaarc667da52019-11-30 20:52:27 +01004643 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 base = 'D';
4645 itemisflag = FALSE;
4646 fillable = TRUE;
4647 num = -1;
4648 str = NULL;
4649 switch (opt)
4650 {
4651 case STL_FILEPATH:
4652 case STL_FULLPATH:
4653 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004654 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004656 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 else
4658 {
4659 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004660 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4662 }
4663 trans_characters(NameBuff, MAXPATHL);
4664 if (opt != STL_FILENAME)
4665 str = NameBuff;
4666 else
4667 str = gettail(NameBuff);
4668 break;
4669
Bram Moolenaarc667da52019-11-30 20:52:27 +01004670 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004671 {
4672#ifdef FEAT_EVAL
4673 char_u *block_start = s - 1;
4674#endif
4675 int reevaluate = (*s == '%');
4676
4677 if (reevaluate)
4678 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 itemisflag = TRUE;
4680 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004681 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4682 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004684 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 break;
4686 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004687 if (reevaluate)
4688 p[-1] = 0; // remove the % at the end of %{% expr %}
4689 else
4690 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004693 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4694 "%d", curbuf->b_fnum);
4695 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4696 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4697 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004699 save_curbuf = curbuf;
4700 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004701 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 curwin = wp;
4703 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004704 // Visual mode is only valid in the current window.
4705 if (curwin != save_curwin)
4706 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004708 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004710 curwin = save_curwin;
4711 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004712 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004713 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004714 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 if (str != NULL && *str != 0)
4717 {
4718 if (*skipdigits(str) == NUL)
4719 {
4720 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004721 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 itemisflag = FALSE;
4723 }
4724 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004725
4726 // If the output of the expression needs to be evaluated
4727 // replace the %{} block with the result of evaluation
4728 if (reevaluate && str != NULL && *str != 0
4729 && strchr((const char *)str, '%') != NULL
4730 && evaldepth < MAX_STL_EVAL_DEPTH)
4731 {
4732 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4733 size_t str_length = strlen((const char *)str);
4734 size_t fmt_length = strlen((const char *)s);
4735 size_t new_fmt_len = parsed_usefmt
4736 + str_length + fmt_length + 3;
4737 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4738 char_u *new_fmt_p = new_fmt;
4739
4740 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4741 + parsed_usefmt;
4742 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4743 + str_length;
4744 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4745 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4746 + fmt_length;
4747 *new_fmt_p = 0;
4748 new_fmt_p = NULL;
4749
4750 if (usefmt != fmt)
4751 vim_free(usefmt);
4752 VIM_CLEAR(str);
4753 usefmt = new_fmt;
4754 s = usefmt + parsed_usefmt;
4755 evaldepth++;
4756 continue;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 case STL_LINE:
4762 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4763 ? 0L : (long)(wp->w_cursor.lnum);
4764 break;
4765
4766 case STL_NUMLINES:
4767 num = wp->w_buffer->b_ml.ml_line_count;
4768 break;
4769
4770 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004771 num = (State & MODE_INSERT) == 0 && empty_line
4772 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 break;
4774
4775 case STL_VIRTCOL:
4776 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004777 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004778 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004780 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4781 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 break;
4783 num = (long)virtcol;
4784 break;
4785
4786 case STL_PERCENTAGE:
4787 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4788 (long)wp->w_buffer->b_ml.ml_line_count);
4789 break;
4790
4791 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004792 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004793 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 break;
4795
Luuk van Baalba936f62022-12-15 13:15:39 +00004796 case STL_SHOWCMD:
4797 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4798 str = showcmd_buf;
4799 break;
4800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 case STL_ARGLISTSTAT:
4802 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004803 buf_tmp[0] = 0;
4804 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4805 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 break;
4807
4808 case STL_KEYMAP:
4809 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004810 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4811 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 break;
4813 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004814#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004815 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#else
4817 num = 0;
4818#endif
4819 break;
4820
4821 case STL_BUFNO:
4822 num = wp->w_buffer->b_fnum;
4823 break;
4824
4825 case STL_OFFSET_X:
4826 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004827 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 case STL_OFFSET:
4829#ifdef FEAT_BYTEOFF
4830 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004831 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4832 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4833 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#endif
4835 break;
4836
4837 case STL_BYTEVAL_X:
4838 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004839 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004841 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (num == NL)
4843 num = 0;
4844 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4845 num = NL;
4846 break;
4847
4848 case STL_ROFLAG:
4849 case STL_ROFLAG_ALT:
4850 itemisflag = TRUE;
4851 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004852 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 break;
4854
4855 case STL_HELPFLAG:
4856 case STL_HELPFLAG_ALT:
4857 itemisflag = TRUE;
4858 if (wp->w_buffer->b_help)
4859 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004860 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 break;
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 case STL_FILETYPE:
4864 if (*wp->w_buffer->b_p_ft != NUL
4865 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4866 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004867 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004868 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004869 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 break;
4872
4873 case STL_FILETYPE_ALT:
4874 itemisflag = TRUE;
4875 if (*wp->w_buffer->b_p_ft != NUL
4876 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4877 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004878 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004879 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004880 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004882 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
Bram Moolenaar4033c552017-09-16 20:54:51 +02004886#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 case STL_PREVIEWFLAG:
4888 case STL_PREVIEWFLAG_ALT:
4889 itemisflag = TRUE;
4890 if (wp->w_p_pvw)
4891 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4892 : _("[Preview]"));
4893 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004894
4895 case STL_QUICKFIX:
4896 if (bt_quickfix(wp->w_buffer))
4897 str = (char_u *)(wp->w_llist_ref
4898 ? _(msg_loclist)
4899 : _(msg_qflist));
4900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
4902
4903 case STL_MODIFIED:
4904 case STL_MODIFIED_ALT:
4905 itemisflag = TRUE;
4906 switch ((opt == STL_MODIFIED_ALT)
4907 + bufIsChanged(wp->w_buffer) * 2
4908 + (!wp->w_buffer->b_p_ma) * 4)
4909 {
4910 case 2: str = (char_u *)"[+]"; break;
4911 case 3: str = (char_u *)",+"; break;
4912 case 4: str = (char_u *)"[-]"; break;
4913 case 5: str = (char_u *)",-"; break;
4914 case 6: str = (char_u *)"[+-]"; break;
4915 case 7: str = (char_u *)",+-"; break;
4916 }
4917 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004918
4919 case STL_HIGHLIGHT:
4920 t = s;
4921 while (*s != '#' && *s != NUL)
4922 ++s;
4923 if (*s == '#')
4924 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004925 stl_items[curitem].stl_type = Highlight;
4926 stl_items[curitem].stl_start = p;
4927 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004928 curitem++;
4929 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004930 if (*s != NUL)
4931 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004932 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004935 stl_items[curitem].stl_start = p;
4936 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (str != NULL && *str)
4938 {
4939 t = str;
4940 if (itemisflag)
4941 {
4942 if ((t[0] && t[1])
4943 && ((!prevchar_isitem && *t == ',')
4944 || (prevchar_isflag && *t == ' ')))
4945 t++;
4946 prevchar_isflag = TRUE;
4947 }
4948 l = vim_strsize(t);
4949 if (l > 0)
4950 prevchar_isitem = TRUE;
4951 if (l > maxwid)
4952 {
4953 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (has_mbyte)
4955 {
4956 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004957 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 l -= byte2cells(*t++);
4961 if (p + 1 >= out + outlen)
4962 break;
4963 *p++ = '<';
4964 }
4965 if (minwid > 0)
4966 {
4967 for (; l < minwid && p + 1 < out + outlen; l++)
4968 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004969 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4971 *p++ = ' ';
4972 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004973 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975 minwid = 0;
4976 }
4977 else
4978 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004979 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004981 // Change a space by fillchar, unless fillchar is '-' and a
4982 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004983 if (fillable && *t == ' '
4984 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4985 MB_CHAR2BYTES(fillchar, p);
4986 else
4987 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004990 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else if (num >= 0)
4993 {
4994 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4995 char_u nstr[20];
4996
4997 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004998 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 prevchar_isitem = TRUE;
5000 t = nstr;
5001 if (opt == STL_VIRTCOL_ALT)
5002 {
5003 *t++ = '-';
5004 minwid--;
5005 }
5006 *t++ = '%';
5007 if (zeropad)
5008 *t++ = '0';
5009 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005010 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 *t = 0;
5012
5013 for (n = num, l = 1; n >= nbase; n /= nbase)
5014 l++;
5015 if (opt == STL_VIRTCOL_ALT)
5016 l++;
5017 if (l > maxwid)
5018 {
5019 l += 2;
5020 n = l - maxwid;
5021 while (l-- > maxwid)
5022 num /= nbase;
5023 *t++ = '>';
5024 *t++ = '%';
5025 *t = t[-3];
5026 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005027 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5028 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005031 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5032 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 p += STRLEN(p);
5034 }
5035 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005036 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005038 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5039 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5040 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (opt == STL_VIM_EXPR)
5042 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 curitem++;
5044 }
5045 *p = NUL;
5046 itemcnt = curitem;
5047
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005048#ifdef FEAT_EVAL
5049 if (usefmt != fmt)
5050 vim_free(usefmt);
5051#endif
5052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 width = vim_strsize(out);
5054 if (maxwidth > 0 && width > maxwidth)
5055 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005056 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 l = 0;
5058 if (itemcnt == 0)
5059 s = out;
5060 else
5061 {
5062 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005063 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005065 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005066 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068 }
5069 if (l == itemcnt)
5070 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005071 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005072 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 l = 0;
5074 }
5075 }
5076
5077 if (width - vim_strsize(s) >= maxwidth)
5078 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005079 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (has_mbyte)
5081 {
5082 s = out;
5083 width = 0;
5084 for (;;)
5085 {
5086 width += ptr2cells(s);
5087 if (width >= maxwidth)
5088 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005089 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005091 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005093 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 s = out + maxwidth - 1;
5097 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005098 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 break;
5100 itemcnt = l;
5101 *s++ = '>';
5102 *s = 0;
5103 }
5104 else
5105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (has_mbyte)
5107 {
5108 n = 0;
5109 while (width >= maxwidth)
5110 {
5111 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005112 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114 }
5115 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 n = width - maxwidth + 1;
5117 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005118 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 *s = '<';
5120
Bram Moolenaarc667da52019-11-30 20:52:27 +01005121 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 for (; l < itemcnt; l++)
5123 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005124 if (stl_items[l].stl_start - n >= s)
5125 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005127 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
zeertzjqd392a742023-07-01 20:24:40 +01005129
5130 // Fill up for half a double-wide character.
5131 while (++width < maxwidth)
5132 {
5133 s = s + STRLEN(s);
5134 MB_CHAR2BYTES(fillchar, s);
5135 *s = NUL;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138 width = maxwidth;
5139 }
5140 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5141 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005142 // Find how many separators there are, which we will use when
5143 // figuring out how many groups there are.
5144 int num_separators = 0;
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005148 if (stl_items[l].stl_type == Separate)
5149 {
5150 // Create an array of the start location for each separator
5151 // mark.
5152 stl_separator_locations[num_separators] = l;
5153 num_separators++;
5154 }
5155 }
5156
5157 // If we have separated groups, then we deal with it now
5158 if (num_separators)
5159 {
5160 int standard_spaces;
5161 int final_spaces;
5162
5163 standard_spaces = (maxwidth - width) / num_separators;
5164 final_spaces = (maxwidth - width) -
5165 standard_spaces * (num_separators - 1);
5166 for (l = 0; l < num_separators; l++)
5167 {
5168 int dislocation = (l == (num_separators - 1)) ?
5169 final_spaces : standard_spaces;
5170 dislocation *= MB_CHAR2LEN(fillchar);
5171 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5172 char_u *seploc = start + dislocation;
5173 STRMOVE(seploc, start);
5174 for (s = start; s < seploc;)
5175 MB_CHAR2BYTES(fillchar, s);
5176
5177 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5178 stl_items[i].stl_start += dislocation;
5179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 width = maxwidth;
5182 }
5183 }
5184
Bram Moolenaarc667da52019-11-30 20:52:27 +01005185 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005186 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005188 *hltab = stl_hltab;
5189 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 for (l = 0; l < itemcnt; l++)
5191 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005192 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005194 sp->start = stl_items[l].stl_start;
5195 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005196 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005199 sp->start = NULL;
5200 sp->userhl = 0;
5201 }
5202
Bram Moolenaarc667da52019-11-30 20:52:27 +01005203 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005204 if (tabtab != NULL)
5205 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005206 *tabtab = stl_tabtab;
5207 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005208 for (l = 0; l < itemcnt; l++)
5209 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005210 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005211 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005212 sp->start = stl_items[l].stl_start;
5213 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005214 sp++;
5215 }
5216 }
5217 sp->start = NULL;
5218 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005221 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005222
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005223 // A user function may reset KeyTyped, restore it.
5224 KeyTyped = save_KeyTyped;
5225
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005226 // Check for an error. If there is one the display will be messed up and
5227 // might loop redrawing. Avoid that by making the corresponding option
5228 // empty.
5229 // TODO: find out why using called_emsg_before makes tests fail, does it
5230 // matter?
5231 // if (called_emsg > called_emsg_before)
5232 if (did_emsg > did_emsg_before)
5233 set_string_option_direct(opt_name, -1, (char_u *)"",
5234 OPT_FREE | opt_scope, SID_ERROR);
5235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 return width;
5237}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005238#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005241 * Get relative cursor position in window into "buf[buflen]", in the localized
5242 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 */
5244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005245get_rel_pos(
5246 win_T *wp,
5247 char_u *buf,
5248 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005250 long above; // number of lines above window
5251 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
Bram Moolenaarc667da52019-11-30 20:52:27 +01005253 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005254 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 above = wp->w_topline - 1;
5256#ifdef FEAT_DIFF
5257 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005258 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005259 above = 0; // All buffer lines are displayed and there is an
5260 // indication of filler lines, that can be considered
5261 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#endif
5263 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5264 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005265 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
Emir SARI971cd2b2023-04-29 12:09:53 +01005266 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005268 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005270 {
5271 int perc = (above > 1000000L)
5272 ? (int)(above / ((above + below) / 100L))
5273 : (int)(above * 100L / (above + below));
5274
5275 char *p = (char *)buf;
5276 size_t l = buflen;
5277 if (perc < 10)
5278 {
5279 // prepend one space
5280 buf[0] = ' ';
5281 ++p;
5282 --l;
5283 }
5284 // localized percentage value
5285 vim_snprintf(p, l, _("%d%%"), perc);
5286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005290 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 * Return TRUE if it was appended.
5292 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005294append_arg_number(
5295 win_T *wp,
5296 char_u *buf,
5297 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005298 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005300 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 return FALSE;
5302
Bram Moolenaara8490a42023-05-23 18:00:58 +01005303 char *msg;
5304 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005306 case 0: msg = _(" (%d of %d)"); break;
5307 case 1: msg = _(" ((%d) of %d)"); break;
5308 case 2: msg = _(" (file %d of %d)"); break;
5309 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005311
5312 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5313 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5314 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return TRUE;
5316}
5317
5318/*
5319 * If fname is not a full path, make it a full path.
5320 * Returns pointer to allocated memory (NULL for failure).
5321 */
5322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005323fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
5325 /*
5326 * Force expanding the path always for Unix, because symbolic links may
5327 * mess up the full path name, even though it starts with a '/'.
5328 * Also expand when there is ".." in the file name, try to remove it,
5329 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005330 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 * For MS-Windows also expand names like "longna~1" to "longname".
5332 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005333#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 return FullName_save(fname, TRUE);
5335#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005336 if (!vim_isAbsName(fname)
5337 || strstr((char *)fname, "..") != NULL
5338 || strstr((char *)fname, "//") != NULL
5339# ifdef BACKSLASH_IN_FILENAME
5340 || strstr((char *)fname, "\\\\") != NULL
5341# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005342# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 )
5346 return FullName_save(fname, FALSE);
5347
5348 fname = vim_strsave(fname);
5349
Bram Moolenaar9b942202007-10-03 12:31:33 +00005350# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005351 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005352 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 return fname;
5356#endif
5357}
5358
5359/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005360 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5361 * "*ffname" becomes a pointer to allocated memory (or NULL).
5362 * When resolving a link both "*sfname" and "*ffname" will point to the same
5363 * allocated memory.
5364 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005365 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005368fname_expand(
5369 buf_T *buf UNUSED,
5370 char_u **ffname,
5371 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005373 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005375 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005377 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
5379#ifdef FEAT_SHORTCUT
5380 if (!buf->b_p_bin)
5381 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005382 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005384 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005385 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005386 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
5388 vim_free(*ffname);
5389 *ffname = rfname;
5390 *sfname = rfname;
5391 }
5392 }
5393#endif
5394}
5395
5396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 * Open a window for a number of buffers.
5398 */
5399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005400ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 buf_T *buf;
5403 win_T *wp, *wpnext;
5404 int split_ret = OK;
5405 int p_ea_save;
5406 int open_wins = 0;
5407 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005408 int count; // Maximum number of windows to open.
5409 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005410 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005411 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
Bram Moolenaarc667da52019-11-30 20:52:27 +01005413 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 count = 9999;
5415 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005416 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5418 all = FALSE;
5419 else
5420 all = TRUE;
5421
Pavel Mayorove1121b12023-02-20 14:35:20 +00005422 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5423 // switching to another buffer.
5424 reset_VIsual_and_resel();
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 setpcmark();
5427
5428#ifdef FEAT_GUI
5429 need_mouse_correct = TRUE;
5430#endif
5431
5432 /*
5433 * Close superfluous windows (two windows for the same buffer).
5434 * Also close windows that are not full-width.
5435 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005436 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005437 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005438 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005440 tpnext = curtab->tp_next;
5441 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005443 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005444 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005445 || ((cmdmod.cmod_split & WSP_VERT)
5446 ? wp->w_height + wp->w_status_height < Rows - p_ch
5447 - tabline_height()
5448 : wp->w_width != Columns)
5449 || (had_tab > 0 && wp != firstwin))
5450 && !ONE_WINDOW
5451 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5452 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005453 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005454 if (win_close(wp, FALSE) == FAIL)
5455 break;
5456 // Just in case an autocommand does something strange with
5457 // windows: start all over...
5458 wpnext = firstwin;
5459 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005460 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005461 }
5462 else
5463 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005465
Bram Moolenaarc667da52019-11-30 20:52:27 +01005466 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005467 if (had_tab == 0 || tpnext == NULL)
5468 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005469 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471
5472 /*
5473 * Go through the buffer list. When a buffer doesn't have a window yet,
5474 * open one. Otherwise move the window to the right position.
5475 * Watch out for autocommands that delete buffers or windows!
5476 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005477 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5482 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005483 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5485 continue;
5486
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005487 if (had_tab != 0)
5488 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005489 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005490 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005491 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005492 else
5493 wp = NULL;
5494 }
5495 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005496 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005497 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005498 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005499 if (wp->w_buffer == buf)
5500 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005501 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005502 if (wp != NULL)
5503 win_move_after(wp, curwin);
5504 }
5505
5506 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005508 bufref_T bufref;
5509
5510 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005511
Bram Moolenaarc667da52019-11-30 20:52:27 +01005512 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005514 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5516 ++open_wins;
5517 p_ea = p_ea_save;
5518 if (split_ret == FAIL)
5519 continue;
5520
Bram Moolenaarc667da52019-11-30 20:52:27 +01005521 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005524 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005526 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 break;
5529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (swap_exists_action == SEA_QUIT)
5531 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005532#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005533 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005534
Bram Moolenaarc667da52019-11-30 20:52:27 +01005535 // Reset the error/interrupt/exception state here so that
5536 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005537 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005538#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005539
Bram Moolenaarc667da52019-11-30 20:52:27 +01005540 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 win_close(curwin, TRUE);
5542 --open_wins;
5543 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005544 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005545
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005546#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005547 // Restore the error/interrupt/exception state if not
5548 // discarded by a new aborting error, interrupt, or uncaught
5549 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005550 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553 else
5554 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556
5557 ui_breakcheck();
5558 if (got_int)
5559 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005560 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 break;
5562 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005563#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005564 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005565 if (aborting())
5566 break;
5567#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005568 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005569 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005570 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005573 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576 /*
5577 * Close superfluous windows.
5578 */
5579 for (wp = lastwin; open_wins > count; )
5580 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005581 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (!win_valid(wp))
5584 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005585 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 wp = lastwin;
5587 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005588 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005590 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 --open_wins;
5592 wp = lastwin;
5593 }
5594 else
5595 {
5596 wp = wp->w_prev;
5597 if (wp == NULL)
5598 break;
5599 }
5600 }
5601}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005604static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606/*
5607 * do_modelines() - process mode lines for the current file
5608 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005609 * "flags" can be:
5610 * OPT_WINONLY only set options local to window
5611 * OPT_NOWIN don't set options local to window
5612 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 * Returns immediately if the "ml" option isn't set.
5614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005616do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005618 linenr_T lnum;
5619 int nmlines;
5620 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5623 return;
5624
Bram Moolenaarc667da52019-11-30 20:52:27 +01005625 // Disallow recursive entry here. Can happen when executing a modeline
5626 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (entered)
5628 return;
5629
5630 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005631 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005633 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 nmlines = 0;
5635
Hu Jialun9dcd3492021-08-28 20:42:50 +02005636 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005638 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 nmlines = 0;
5640 --entered;
5641}
5642
Bram Moolenaarc667da52019-11-30 20:52:27 +01005643#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645/*
5646 * chk_modeline() - check a single line for a mode string
5647 * Return FAIL if an error encountered.
5648 */
5649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005650chk_modeline(
5651 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005652 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 char_u *s;
5655 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005656 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 int prev;
5658 int vers;
5659 int end;
5660 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005661 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005662 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 prev = -1;
5665 for (s = ml_get(lnum); *s != NUL; ++s)
5666 {
5667 if (prev == -1 || vim_isspace(prev))
5668 {
5669 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5670 || STRNCMP(s, "vi:", (size_t)3) == 0)
5671 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005672 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005673 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
5675 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5676 e = s + 4;
5677 else
5678 e = s + 3;
5679 vers = getdigits(&e);
5680 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005681 && (s[0] != 'V'
5682 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005684 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 || (VIM_VERSION_100 < vers && s[3] == '<')
5686 || (VIM_VERSION_100 > vers && s[3] == '>')
5687 || (VIM_VERSION_100 == vers && s[3] == '=')))
5688 break;
5689 }
5690 }
5691 prev = *s;
5692 }
5693
5694 if (*s)
5695 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005696 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 ++s;
5698 while (s[-1] != ':');
5699
Bram Moolenaarc667da52019-11-30 20:52:27 +01005700 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 if (linecopy == NULL)
5702 return FAIL;
5703
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005704 // prepare for emsg()
5705 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005706 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
5708 end = FALSE;
5709 while (end == FALSE)
5710 {
5711 s = skipwhite(s);
5712 if (*s == NUL)
5713 break;
5714
5715 /*
5716 * Find end of set command: ':' or end of line.
5717 * Skip over "\:", replacing it with ":".
5718 */
5719 for (e = s; *e != ':' && *e != NUL; ++e)
5720 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005721 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (*e == NUL)
5723 end = TRUE;
5724
5725 /*
5726 * If there is a "set" command, require a terminating ':' and
5727 * ignore the stuff after the ':'.
5728 * "vi:set opt opt opt: foo" -- foo not interpreted
5729 * "vi:opt opt opt: foo" -- foo interpreted
5730 * Accept "se" for compatibility with Elvis.
5731 */
5732 if (STRNCMP(s, "set ", (size_t)4) == 0
5733 || STRNCMP(s, "se ", (size_t)3) == 0)
5734 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005735 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 end = TRUE;
5738 s = vim_strchr(s, ' ') + 1;
5739 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005740 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaarc667da52019-11-30 20:52:27 +01005742 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005744 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005745
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005746 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005747 current_sctx.sc_version = 1;
5748#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005749 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005750 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005751 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005753
Bram Moolenaar5958f952018-11-20 04:25:21 +01005754 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005755 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005756
Bram Moolenaara3227e22006-03-08 21:32:40 +00005757 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005758
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005759 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005760 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005761 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 break;
5763 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005764 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766
ichizok7e5fe382023-04-15 13:17:50 +01005767 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005768 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 vim_free(linecopy);
5770 }
5771 return retval;
5772}
5773
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005774/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005775 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5776 */
5777 int
5778bt_normal(buf_T *buf)
5779{
5780 return buf != NULL && buf->b_p_bt[0] == NUL;
5781}
5782
5783/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005784 * Return TRUE if "buf" is the quickfix buffer.
5785 */
5786 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005787bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005788{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005789#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005790 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005791#else
5792 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005793#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005794}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005795
5796/*
5797 * Return TRUE if "buf" is a terminal buffer.
5798 */
5799 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005800bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005801{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005802#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005803 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005804#else
5805 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005806#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005807}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005808
5809/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005810 * Return TRUE if "buf" is a help buffer.
5811 */
5812 int
5813bt_help(buf_T *buf)
5814{
5815 return buf != NULL && buf->b_help;
5816}
5817
5818/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005819 * Return TRUE if "buf" is a prompt buffer.
5820 */
5821 int
5822bt_prompt(buf_T *buf)
5823{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005824 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5825}
5826
Dominique Pelle748b3082022-01-08 12:41:16 +00005827#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005828/*
5829 * Return TRUE if "buf" is a buffer for a popup window.
5830 */
5831 int
5832bt_popup(buf_T *buf)
5833{
5834 return buf != NULL && buf->b_p_bt != NULL
5835 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005836}
Dominique Pelle748b3082022-01-08 12:41:16 +00005837#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005838
5839/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005840 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005841 * buffer. This means the buffer name may not be a file name, at least not for
5842 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005843 */
5844 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005845bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005846{
5847 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5848 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005849 || buf->b_p_bt[0] == 't'
5850 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005851}
5852
Bram Moolenaarc3126192022-08-26 12:58:17 +01005853/*
5854 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5855 * buffer. This means the buffer is not to be read from a file.
5856 */
5857 static int
5858bt_nofileread(buf_T *buf)
5859{
5860 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5861 || buf->b_p_bt[0] == 't'
5862 || buf->b_p_bt[0] == 'q'
5863 || buf->b_p_bt[0] == 'p');
5864}
5865
Dominique Pelle748b3082022-01-08 12:41:16 +00005866#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005867/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005868 * Return TRUE if "buf" has 'buftype' set to "nofile".
5869 */
5870 int
5871bt_nofile(buf_T *buf)
5872{
5873 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5874}
Dominique Pelle748b3082022-01-08 12:41:16 +00005875#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005876
5877/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005878 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5879 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005880 */
5881 int
5882bt_dontwrite(buf_T *buf)
5883{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005884 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005885 || buf->b_p_bt[0] == 't'
5886 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005887}
5888
5889 int
5890bt_dontwrite_msg(buf_T *buf)
5891{
5892 if (bt_dontwrite(buf))
5893 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005894 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005895 return TRUE;
5896 }
5897 return FALSE;
5898}
5899
5900/*
5901 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5902 * and 'bufhidden'.
5903 */
5904 int
5905buf_hide(buf_T *buf)
5906{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005907 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005908 switch (buf->b_p_bh[0])
5909 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005910 case 'u': // "unload"
5911 case 'w': // "wipe"
5912 case 'd': return FALSE; // "delete"
5913 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005914 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005915 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005916}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918/*
5919 * Return special buffer name.
5920 * Returns NULL when the buffer has a normal file name.
5921 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005922 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005925#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005927 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005928 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005929 * Differentiate between the quickfix and location list buffers using
5930 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005931 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005932 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005933 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005934 else
5935 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005938
Bram Moolenaarc667da52019-11-30 20:52:27 +01005939 // There is no _file_ when 'buftype' is "nofile", b_sfname
5940 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005941 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005943#ifdef FEAT_TERMINAL
5944 if (buf->b_term != NULL)
5945 return term_get_status_text(buf->b_term);
5946#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005947 if (buf->b_fname != NULL)
5948 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005949 if (buf == cmdwin_buf)
5950 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005951#ifdef FEAT_JOB_CHANNEL
5952 if (bt_prompt(buf))
5953 return (char_u *)_("[Prompt]");
5954#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005955#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005956 if (bt_popup(buf))
5957 return (char_u *)_("[Popup]");
5958#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005959 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005963 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 return NULL;
5965}
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005968 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5969 */
5970 char_u *
5971buf_get_fname(buf_T *buf)
5972{
5973 if (buf->b_fname == NULL)
5974 return (char_u *)_("[No Name]");
5975 return buf->b_fname;
5976}
5977
5978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5980 */
5981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005982set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005984 if (on == curbuf->b_p_bl)
5985 return;
5986
5987 curbuf->b_p_bl = on;
5988 if (on)
5989 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5990 else
5991 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992}
5993
5994/*
5995 * Read the file for "buf" again and check if the contents changed.
5996 * Return TRUE if it changed or this could not be checked.
5997 */
5998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 buf_T *newbuf;
6002 int differ = TRUE;
6003 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 exarg_T ea;
6006
Bram Moolenaarc667da52019-11-30 20:52:27 +01006007 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6009 if (newbuf == NULL)
6010 return TRUE;
6011
Bram Moolenaarc667da52019-11-30 20:52:27 +01006012 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 if (prep_exarg(&ea, buf) == FAIL)
6014 {
6015 wipe_buffer(newbuf, FALSE);
6016 return TRUE;
6017 }
6018
Bram Moolenaare76062c2022-11-28 18:51:43 +00006019 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006021 if (curbuf != newbuf)
6022 {
6023 // Failed to find a window for "newbuf".
6024 wipe_buffer(newbuf, FALSE);
6025 return TRUE;
6026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006028 // We don't want to trigger autocommands now, they may have nasty
6029 // side-effects like wiping buffers
6030 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006031 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 && readfile(buf->b_ffname, buf->b_fname,
6033 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6034 &ea, READ_NEW | READ_DUMMY) == OK)
6035 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006036 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6038 {
6039 differ = FALSE;
6040 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6041 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6042 {
6043 differ = TRUE;
6044 break;
6045 }
6046 }
6047 }
6048 vim_free(ea.cmd);
6049
Bram Moolenaarc667da52019-11-30 20:52:27 +01006050 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
Bram Moolenaarc667da52019-11-30 20:52:27 +01006053 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 wipe_buffer(newbuf, FALSE);
6055
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006056 unblock_autocmds();
6057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 return differ;
6059}
6060
6061/*
6062 * Wipe out a buffer and decrement the last buffer number if it was used for
6063 * this buffer. Call this to wipe out a temp buffer that does not contain any
6064 * marks.
6065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067wipe_buffer(
6068 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006069 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 if (buf->b_fnum == top_file_num - 1)
6072 --top_file_num;
6073
Bram Moolenaarc667da52019-11-30 20:52:27 +01006074 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006075 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006076
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006077 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006078
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006080 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081}