blob: 447ce76d49a328fd843e8408a48fa5a78e99d5c0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
shadmansaleh30e3de22021-05-15 17:23:28 +020030
31#ifdef FEAT_EVAL
32// Determines how deeply nested %{} blocks will be evaluated in statusline.
33# define MAX_STL_EVAL_DEPTH 100
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void enter_buffer(buf_T *buf);
37static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020041static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
42static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
43static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +020047static int value_changed(char_u *str, char_u **last);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010048static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
49static void free_buffer(buf_T *);
50static void free_buffer_stuff(buf_T *buf, int free_options);
Bram Moolenaarc3126192022-08-26 12:58:17 +010051static int bt_nofileread(buf_T *buf);
Yee Cheng Chin15b314f2022-10-09 18:53:32 +010052static void no_write_message_buf(buf_T *buf);
LemonBoy893eeeb2024-07-10 20:20:48 +020053static int do_buffer_ext(int action, int start, int dir, int count, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55#ifdef UNIX
56# define dev_T dev_t
57#else
58# define dev_T unsigned
59#endif
60
Bram Moolenaar00d253e2020-04-06 22:13:01 +020061#define FOR_ALL_BUFS_FROM_LAST(buf) \
62 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
63
Bram Moolenaar4033c552017-09-16 20:54:51 +020064#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020065static char *msg_loclist = N_("[Location List]");
66static char *msg_qflist = N_("[Quickfix List]");
67#endif
68
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020069// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070static int buf_free_count = 0;
71
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020072static int top_file_num = 1; // highest file number
73static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
74
75/*
76 * Return the highest possible buffer number.
77 */
78 int
79get_highest_fnum(void)
80{
81 return top_file_num - 1;
82}
83
Bram Moolenaarc024b462019-06-08 18:07:21 +020084/*
85 * Read data from buffer for retrying.
86 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020087 static int
88read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010089 int read_stdin, // read file from stdin, otherwise fifo
90 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
91 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020092{
93 int retval = OK;
94 linenr_T line_count;
95
Bram Moolenaarc5935a82021-10-19 20:48:52 +010096 // Read from the buffer which the text is already filled in and append at
97 // the end. This makes it possible to retry when 'fileformat' or
98 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020099 line_count = curbuf->b_ml.ml_line_count;
100 retval = readfile(
101 read_stdin ? NULL : curbuf->b_ffname,
102 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100103 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200104 flags | READ_BUFFER);
105 if (retval == OK)
106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100107 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200108 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200109 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200110 }
111 else
112 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100113 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200115 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200116 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100117 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200118 curwin->w_cursor.lnum = 1;
119 curwin->w_cursor.col = 0;
120
121 if (read_stdin)
122 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100123 // Set or reset 'modified' before executing autocommands, so that
124 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100125 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100127 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200128 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100130 if (retval == OK)
131 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100133 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100134 curbuf, &retval);
135#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100136 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100138 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200139 }
140 return retval;
141}
142
Dominique Pelle748b3082022-01-08 12:41:16 +0000143#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200145 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
146 */
147 void
148buffer_ensure_loaded(buf_T *buf)
149{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000150 if (buf->b_ml.ml_mfp != NULL)
151 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200152
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000153 aco_save_T aco;
154
155 // Make sure the buffer is in a window. If not then skip it.
156 aucmd_prepbuf(&aco, buf);
157 if (curbuf == buf)
158 {
159 if (swap_exists_action != SEA_READONLY)
160 swap_exists_action = SEA_NONE;
161 open_buffer(FALSE, NULL, 0);
162 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200163 }
164}
Dominique Pelle748b3082022-01-08 12:41:16 +0000165#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200166
167/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200168 * Open current buffer, that is: open the memfile and read the file into
169 * memory.
170 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 */
172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100173open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100174 int read_stdin, // read file from stdin
175 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100176 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100178 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200180 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100181#ifdef FEAT_SYN_HL
182 long old_tw = curbuf->b_p_tw;
183#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200184 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100186 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
187 // When re-entering the same buffer, it should not change, because the
188 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (readonlymode && curbuf->b_ffname != NULL
190 && (curbuf->b_flags & BF_NEVERLOADED))
191 curbuf->b_p_ro = TRUE;
192
Bram Moolenaar4770d092006-01-12 23:22:24 +0000193 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100195 // There MUST be a memfile, otherwise we can't do anything
196 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100197 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200198 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 if (curbuf->b_ml.ml_mfp != NULL)
200 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100201 // If there is no memfile at all, exit.
202 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 if (curbuf == NULL)
204 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000205 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200206
207 // Don't try to do any saving, with "curbuf" NULL almost nothing
208 // will work.
209 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 getout(2);
211 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200212
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000213 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100215#ifdef FEAT_SYN_HL
216 if (old_tw != curbuf->b_p_tw)
217 check_colorcolumn(curwin);
218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 return FAIL;
220 }
221
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100222 // Do not sync this buffer yet, may first want to read the file.
223 if (curbuf->b_ml.ml_mfp != NULL)
224 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
225
Bram Moolenaarc667da52019-11-30 20:52:27 +0100226 // The autocommands in readfile() may change the buffer, but only AFTER
227 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200228 set_bufref(&old_curbuf, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +0200229 curbuf->b_modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
Bram Moolenaarc667da52019-11-30 20:52:27 +0100231 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100232 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100234 // A buffer without an actual file should not use the buffer name to read a
235 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100236 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100237 flags |= READ_NOFILE;
238
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100239 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 if (curbuf->b_ffname != NULL
241#ifdef FEAT_NETBEANS_INTG
242 && netbeansReadFile
243#endif
244 )
245 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100246 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200247#ifdef UNIX
248 int save_bin = curbuf->b_p_bin;
249 int perm;
250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_NETBEANS_INTG
252 int oldFire = netbeansFireChanges;
253
254 netbeansFireChanges = 0;
255#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200256#ifdef UNIX
257 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200258 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200259 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200260# ifdef OPEN_CHR_FILES
261 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
262# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200263 ))
264 read_fifo = TRUE;
265 if (read_fifo)
266 curbuf->b_p_bin = TRUE;
267#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100268 if (shortmess(SHM_FILEINFO))
269 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200271 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200272 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
273#ifdef UNIX
274 if (read_fifo)
275 {
276 curbuf->b_p_bin = save_bin;
277 if (retval == OK)
278 retval = read_buffer(FALSE, eap, flags);
279 }
280#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100281 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282#ifdef FEAT_NETBEANS_INTG
283 netbeansFireChanges = oldFire;
284#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100285 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200286 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 fix_help_buffer();
288 }
289 else if (read_stdin)
290 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200291 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100293 // First read the text in binary mode into the buffer.
294 // Then read from that same buffer and append at the end. This makes
295 // it possible to retry when 'fileformat' or 'fileencoding' was
296 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 curbuf->b_p_bin = TRUE;
298 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200299 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
300 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 curbuf->b_p_bin = save_bin;
302 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200303 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100306 // Can now sync this buffer in ml_sync_all().
307 if (curbuf->b_ml.ml_mfp != NULL
308 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
309 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
310
Bram Moolenaarc667da52019-11-30 20:52:27 +0100311 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100315 parse_cino(curbuf);
316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100318 // Set/reset the Changed flag first, autocmds may change the buffer.
319 // Apply the automatic commands, before processing the modelines.
320 // So the modelines have priority over autocommands.
321 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100322 // When reading stdin, the buffer contents always needs writing, so set
323 // the changed flag. Unless in readonly mode: "ls | gview -".
324 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000325 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200326 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100327#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000330 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100332 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200333 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100334 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaarc667da52019-11-30 20:52:27 +0100336 // Set last_changedtick to avoid triggering a TextChanged autocommand right
337 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100338 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100339 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100340 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100341
Bram Moolenaarc667da52019-11-30 20:52:27 +0100342 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#ifdef FEAT_EVAL
344 if (aborting())
345#else
346 if (got_int)
347#endif
348 curbuf->b_flags |= BF_READERR;
349
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000350#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100351 // Need to update automatic folding. Do this before the autocommands,
352 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000353 foldUpdateAll(curwin);
354#endif
355
Bram Moolenaarc667da52019-11-30 20:52:27 +0100356 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (!(curwin->w_valid & VALID_TOPLINE))
358 {
359 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100360#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100364#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#endif
369
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000370 if (retval != OK)
371 return retval;
372
373 // The autocommands may have changed the current buffer. Apply the
374 // modelines to the correct buffer, if it still exists and is loaded.
375 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000377 aco_save_T aco;
378
379 // Go to the buffer that was opened, make sure it is in a window.
380 // If not then skip it.
381 aucmd_prepbuf(&aco, old_curbuf.br_buf);
382 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000384 do_modelines(0);
385 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000387 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100388#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000389 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
390 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100391#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000392 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
393 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000396 // restore curwin/curbuf and a few other things
397 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 return retval;
402}
403
404/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200405 * Store "buf" in "bufref" and set the free count.
406 */
407 void
408set_bufref(bufref_T *bufref, buf_T *buf)
409{
410 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200411 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200412 bufref->br_buf_free_count = buf_free_count;
413}
414
415/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200416 * Return TRUE if "bufref->br_buf" points to the same buffer as when
417 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200418 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200419 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
420 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200421 */
422 int
423bufref_valid(bufref_T *bufref)
424{
425 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200426 ? TRUE : buf_valid(bufref->br_buf)
427 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200428}
429
430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200432 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 */
434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100435buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 buf_T *bp;
438
Bram Moolenaarc667da52019-11-30 20:52:27 +0100439 // Assume that we more often have a recent buffer, start with the last
440 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200441 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (bp == buf)
443 return TRUE;
444 return FALSE;
445}
446
447/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200448 * A hash table used to quickly lookup a buffer by its number.
449 */
450static hashtab_T buf_hashtab;
451
452 static void
453buf_hashtab_add(buf_T *buf)
454{
455 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000456 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000457 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200458}
459
460 static void
461buf_hashtab_remove(buf_T *buf)
462{
463 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
464
465 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000466 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200467}
468
Bram Moolenaar94f01952018-09-01 15:30:03 +0200469/*
470 * Return TRUE when buffer "buf" can be unloaded.
471 * Give an error message and return FALSE when the buffer is locked or the
472 * screen is being redrawn and the buffer is in a window.
473 */
474 static int
475can_unload_buffer(buf_T *buf)
476{
477 int can_unload = !buf->b_locked;
478
479 if (can_unload && updating_screen)
480 {
481 win_T *wp;
482
483 FOR_ALL_WINDOWS(wp)
484 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200485 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200486 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200487 break;
488 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200489 }
490 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100491 {
492 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
493
494 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
495 fname != NULL ? fname : (char_u *)"[No Name]");
496 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200497 return can_unload;
498}
Bram Moolenaara997b452018-04-17 23:24:06 +0200499
Bram Moolenaar480778b2016-07-14 22:09:39 +0200500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 * Close the link to a buffer.
502 * "action" is used when there is no longer a window for the buffer.
503 * It can be:
504 * 0 buffer becomes hidden
505 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100506 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200508 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * When doing all but the first one on the current buffer, the caller should
510 * get a new buffer very soon!
511 *
512 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100513 *
514 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
515 * cause there to be only one window with this buffer. e.g. when ":quit" is
516 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100517 *
518 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100519 *
520 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100523close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100524 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100525 buf_T *buf,
526 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100527 int abort_if_last,
528 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100531 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200532 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100533 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200534 win_T *the_curwin = curwin;
535 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200537 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
538 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaarcee52202020-03-11 14:19:58 +0100540 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100541
542 // Force unloading or deleting when 'bufhidden' says so.
543 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
544 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100545 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200546 {
547 del_buf = TRUE;
548 unload_buf = TRUE;
549 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100550 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200551 {
552 del_buf = TRUE;
553 unload_buf = TRUE;
554 wipe_buf = TRUE;
555 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100556 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200557 unload_buf = TRUE;
558
Bram Moolenaar94053a52017-08-01 21:44:33 +0200559#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100560 // depending on how we get here b_nwindows may already be zero
561 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200562 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100563 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200564 if (term_job_running(buf->b_term))
565 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200566 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200567 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200568 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100569 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200570
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200572 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100573
574 // A terminal buffer is wiped out when job has finished.
575 del_buf = TRUE;
576 unload_buf = TRUE;
577 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200578 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200579 else
580 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100581 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200582 del_buf = FALSE;
583 unload_buf = FALSE;
584 }
585 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100586 else if (buf->b_p_bh[0] == 'h' && !del_buf)
587 {
588 // Hide a terminal buffer.
589 unload_buf = FALSE;
590 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200591 else
592 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100593 if (del_buf || unload_buf)
594 {
595 // A terminal buffer is wiped out if the job has finished.
596 // We only do this when there's an intention to unload the
597 // buffer. This way, :hide and other similar commands won't
598 // wipe the buffer.
599 del_buf = TRUE;
600 unload_buf = TRUE;
601 wipe_buf = TRUE;
602 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200603 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100604 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200605 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaarc667da52019-11-30 20:52:27 +0100608 // Disallow deleting the buffer when it is locked (already being closed or
609 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200610 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100611 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200612
Bram Moolenaarc667da52019-11-30 20:52:27 +0100613 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200614 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100616 // Set b_last_cursor when closing the last window for the buffer.
617 // Remember the last cursor position and window options of the buffer.
618 // This used to be only for the current window, but then options like
619 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (buf->b_nwindows == 1)
621 set_last_cursor(win);
622 buflist_setfpos(buf, win,
623 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
624 win->w_cursor.col, TRUE);
625 }
626
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200627 set_bufref(&bufref, buf);
628
Bram Moolenaarc667da52019-11-30 20:52:27 +0100629 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (buf->b_nwindows == 1)
631 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200632 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100633 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200634 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
635 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200636 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100637 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100638 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200639aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000640 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100641 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100642 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200643 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100644 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200645 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100646 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200647 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaarc667da52019-11-30 20:52:27 +0100649 // When the buffer becomes hidden, but is not unloaded, trigger
650 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (!unload_buf)
652 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200653 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100654 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200655 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
656 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200657 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100658 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200659 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200660 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100661 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200662 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100663 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200664 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100666#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100667 // autocmds may abort script processing
668 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100669 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200672
Bram Moolenaarc667da52019-11-30 20:52:27 +0100673 // If the buffer was in curwin and the window has changed, go back to that
674 // window, if it still exists. This avoids that ":edit x" triggering a
675 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200676 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
677 {
678 block_autocmds();
679 goto_tabpage_win(the_curtab, the_curwin);
680 unblock_autocmds();
681 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaarc667da52019-11-30 20:52:27 +0100685 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (buf->b_nwindows > 0)
687 --buf->b_nwindows;
688
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100689#ifdef FEAT_DIFF
690 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100691 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100692#endif
693
Bram Moolenaarc667da52019-11-30 20:52:27 +0100694 // Return when a window is displaying the buffer or when it's not
695 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100697 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaarc667da52019-11-30 20:52:27 +0100699 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (buf->b_ffname == NULL)
701 del_buf = TRUE;
702
Bram Moolenaarc667da52019-11-30 20:52:27 +0100703 // When closing the current buffer stop Visual mode before freeing
704 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200705 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200706#if defined(EXITFREE)
707 && !entered_free_all_mem
708#endif
709 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200710 end_visual_mode();
711
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100712 // Free all things allocated for this buffer.
713 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
714 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100715 // Remember if we are closing the current buffer. Restore the number of
716 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 is_curbuf = (buf == curbuf);
718 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100720 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100721 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100722 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaarc667da52019-11-30 20:52:27 +0100724 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200725 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100726 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100727#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100728 // autocmds may abort script processing
729 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100730 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100733 // It's possible that autocommands change curbuf to the one being deleted.
734 // This might cause the previous curbuf to be deleted unexpectedly. But
735 // in some cases it's OK to delete the curbuf, because a new one is
736 // obtained anyway. Therefore only return if curbuf changed to the
737 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100739 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200740
Bram Moolenaar4033c552017-09-16 20:54:51 +0200741 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100742 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200743
Bram Moolenaarc667da52019-11-30 20:52:27 +0100744 // Autocommands may have opened or closed windows for this buffer.
745 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200746 if (buf->b_nwindows > 0)
747 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 /*
750 * Remove the buffer from the list.
751 */
752 if (wipe_buf)
753 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200754 tabpage_T *tp;
755 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200756
Bram Moolenaar347538f2022-03-26 16:28:06 +0000757 // Do not wipe out the buffer if it is used in a window.
758 if (buf->b_nwindows > 0)
759 return FALSE;
760
zeertzjq2e7d89b2024-07-10 19:36:36 +0200761 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200762 mark_forget_file(wp, buf->b_fnum);
763
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200764 if (action == DOBUF_WIPE_REUSE)
765 {
766 // we can re-use this buffer number, store it
767 if (buf_reuse.ga_itemsize == 0)
768 ga_init2(&buf_reuse, sizeof(int), 50);
769 if (ga_grow(&buf_reuse, 1) == OK)
770 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
771 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200772 if (buf->b_sfname != buf->b_ffname)
773 VIM_CLEAR(buf->b_sfname);
774 else
775 buf->b_sfname = NULL;
776 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (buf->b_prev == NULL)
778 firstbuf = buf->b_next;
779 else
780 buf->b_prev->b_next = buf->b_next;
781 if (buf->b_next == NULL)
782 lastbuf = buf->b_prev;
783 else
784 buf->b_next->b_prev = buf->b_prev;
785 free_buffer(buf);
786 }
787 else
788 {
789 if (del_buf)
790 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100791 // Free all internal variables and reset option values, to make
792 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 free_buffer_stuff(buf, TRUE);
794
Bram Moolenaarc667da52019-11-30 20:52:27 +0100795 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
797
Bram Moolenaarc667da52019-11-30 20:52:27 +0100798 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 buf->b_p_initialized = FALSE;
800 }
801 buf_clear_file(buf);
802 if (del_buf)
803 buf->b_p_bl = FALSE;
804 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100805 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100806 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807}
808
809/*
810 * Make buffer not contain a file.
811 */
812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100813buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200816 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100818 buf->b_p_eof = FALSE;
819 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 buf->b_p_eol = TRUE;
821 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000823 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100825 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#ifdef FEAT_NETBEANS_INTG
827 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#endif
829}
830
831/*
832 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200833 * the file. Careful: get here with "curwin" NULL when exiting.
834 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100835 * BFA_DEL buffer is going to be deleted
836 * BFA_WIPE buffer is going to be wiped out
837 * BFA_KEEP_UNDO do not free undo information
838 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100841buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200844 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200845 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200846 win_T *the_curwin = curwin;
847 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaarc667da52019-11-30 20:52:27 +0100849 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200850 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100851 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200852 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200853 if (buf->b_ml.ml_mfp != NULL)
854 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200855 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
856 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200857 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100858 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200859 return;
860 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200861 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200863 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
864 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200865 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100866 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 return;
868 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200869 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200871 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
872 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200873 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100874 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 return;
876 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200877 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100878 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200879
Bram Moolenaarc667da52019-11-30 20:52:27 +0100880 // If the buffer was in curwin and the window has changed, go back to that
881 // window, if it still exists. This avoids that ":edit x" triggering a
882 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200883 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
884 {
885 block_autocmds();
886 goto_tabpage_win(the_curtab, the_curwin);
887 unblock_autocmds();
888 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200889
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100890#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100891 // autocmds may abort script processing
892 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100896 // It's possible that autocommands change curbuf to the one being deleted.
897 // This might cause curbuf to be deleted unexpectedly. But in some cases
898 // it's OK to delete the curbuf, because a new one is obtained anyway.
899 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (buf == curbuf && !is_curbuf)
901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100903 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200905#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100906 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200907 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100908 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200909#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000910
911#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100912 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000913 {
914 win_T *win;
915 tabpage_T *tp;
916
917 FOR_ALL_TAB_WINDOWS(tp, win)
918 if (win->w_buffer == buf)
919 clearFolding(win);
920 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000921#endif
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_TCL
924 tcl_buffer_free(buf);
925#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100926 ml_close(buf, TRUE); // close and delete the memline/memfile
927 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200928 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100929 // free the memory allocated for undo
930 // and reset all undo information
931 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100933 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100935#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100936 clear_buf_prop_types(buf);
937#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100938 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939}
940
941/*
942 * Free a buffer structure and the things it contains related to the buffer
943 * itself (not the file, that must have been done already).
944 */
945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200948 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200950#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100951 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000952 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200953 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200954 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200955#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200956#ifdef FEAT_LUA
957 lua_buffer_free(buf);
958#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000959#ifdef FEAT_MZSCHEME
960 mzscheme_buffer_free(buf);
961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962#ifdef FEAT_PERL
963 perl_buf_free(buf);
964#endif
965#ifdef FEAT_PYTHON
966 python_buffer_free(buf);
967#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968#ifdef FEAT_PYTHON3
969 python3_buffer_free(buf);
970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#ifdef FEAT_RUBY
972 ruby_buffer_free(buf);
973#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200974#ifdef FEAT_JOB_CHANNEL
975 channel_buffer_free(buf);
976#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200977#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200978 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200979#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200980#ifdef FEAT_JOB_CHANNEL
981 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200982 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200983 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200984#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200985
986 buf_hashtab_remove(buf);
987
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200988 aubuflocal_remove(buf);
989
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200990 if (autocmd_busy)
991 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100992 // Do not free the buffer structure while autocommands are executing,
993 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200994 buf->b_next = au_pending_free_buf;
995 au_pending_free_buf = buf;
996 }
997 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100998 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200999 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001000 if (curbuf == buf)
1001 curbuf = NULL; // make clear it's not to be used
1002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
1004
1005/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001006 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001007 */
1008 static void
1009init_changedtick(buf_T *buf)
1010{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001011 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001012
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001013 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1014 di->di_tv.v_type = VAR_NUMBER;
1015 di->di_tv.v_lock = VAR_FIXED;
1016 di->di_tv.vval.v_number = 0;
1017
Bram Moolenaar92769c32017-02-25 15:41:37 +01001018#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001019 STRCPY(buf->b_ct_di.di_key, "changedtick");
1020 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001021#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001022}
1023
1024/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001025 * Free the b_wininfo list for buffer "buf".
1026 */
1027 static void
1028clear_wininfo(buf_T *buf)
1029{
1030 wininfo_T *wip;
1031
1032 while (buf->b_wininfo != NULL)
1033 {
1034 wip = buf->b_wininfo;
1035 buf->b_wininfo = wip->wi_next;
1036 free_wininfo(wip);
1037 }
1038}
1039
1040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1042 */
1043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001044free_buffer_stuff(
1045 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001046 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 if (free_options)
1049 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001050 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001052#ifdef FEAT_SPELL
1053 ga_clear(&buf->b_s.b_langp);
1054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
1056#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001057 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001058 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001059
Bram Moolenaarc667da52019-11-30 20:52:27 +01001060 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001061 hash_init(&buf->b_vars->dv_hashtab);
1062 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001063 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001064 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001067 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001069 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001071#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001072 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001073#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001074#ifdef FEAT_PROP_POPUP
1075 ga_clear_strings(&buf->b_textprop_text);
1076#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001077 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1078 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001079 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001083 * Free one wininfo_T.
1084 */
1085 void
1086free_wininfo(wininfo_T *wip)
1087{
1088 if (wip->wi_optset)
1089 {
1090 clear_winopt(&wip->wi_opt);
1091#ifdef FEAT_FOLDING
1092 deleteFoldRecurse(&wip->wi_folds);
1093#endif
1094 }
1095 vim_free(wip);
1096}
1097
1098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * Go to another buffer. Handles the result of the ATTENTION dialog.
1100 */
1101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102goto_buffer(
1103 exarg_T *eap,
1104 int start,
1105 int dir,
1106 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001109 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001110 int skip_help_buf;
1111
1112 switch (eap->cmdidx)
1113 {
1114 case CMD_bnext:
1115 case CMD_sbnext:
1116 case CMD_bNext:
1117 case CMD_bprevious:
1118 case CMD_sbNext:
1119 case CMD_sbprevious:
1120 skip_help_buf = TRUE;
1121 break;
1122 default:
1123 skip_help_buf = FALSE;
1124 break;
1125 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001126
1127 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaar188639d2022-04-04 16:57:21 +01001129 if (swap_exists_action == SEA_NONE)
1130 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001131 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1132 (eap->forceit ? DOBUF_FORCEIT : 0) |
1133 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1135 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001136#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001137 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001138
Bram Moolenaarc667da52019-11-30 20:52:27 +01001139 // Reset the error/interrupt/exception state here so that
1140 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001141 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001142#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001143
Bram Moolenaarc667da52019-11-30 20:52:27 +01001144 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001146 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001147 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001148
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001149#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001150 // Restore the error/interrupt/exception state if not discarded by a
1151 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001152 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001156 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
1160 * Handle the situation of swap_exists_action being set.
1161 * It is allowed for "old_curbuf" to be NULL or invalid.
1162 */
1163 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001164handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001166#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001167 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001168#endif
1169#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001170 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001171#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001172 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (swap_exists_action == SEA_QUIT)
1175 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001176#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001177 // Reset the error/interrupt/exception state here so that
1178 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001179 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001180#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001181
Bram Moolenaarc667da52019-11-30 20:52:27 +01001182 // User selected Quit at ATTENTION prompt. Go back to previous
1183 // buffer. If that buffer is gone or the same as the current one,
1184 // open a new, empty buffer.
1185 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001186 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001187 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001188 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1189 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001190 {
1191 // Block autocommands here because curwin->w_buffer is NULL.
1192 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001193 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001194 unblock_autocmds();
1195 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001196 else
1197 buf = old_curbuf->br_buf;
1198 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001199 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001200 int old_msg_silent = msg_silent;
1201
1202 if (shortmess(SHM_FILEINFO))
1203 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001204 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001205 // restore msg_silent, so that the command line will be shown
1206 msg_silent = old_msg_silent;
1207
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001208#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001209 if (old_tw != curbuf->b_p_tw)
1210 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001211#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001212 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001213 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001214
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001215#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001216 // Restore the error/interrupt/exception state if not discarded by a
1217 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001218 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221 else if (swap_exists_action == SEA_RECOVER)
1222 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001223#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001224 // Reset the error/interrupt/exception state here so that
1225 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001226 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001227#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001228
Bram Moolenaarc667da52019-11-30 20:52:27 +01001229 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001231 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001232 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001234 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001235
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001236#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001237 // Restore the error/interrupt/exception state if not discarded by a
1238 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001239 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242 swap_exists_action = SEA_NONE;
1243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001246 * Make the current buffer empty.
1247 * Used when it is wiped out and it's the last buffer.
1248 */
1249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001250empty_curbuf(
1251 int close_others,
1252 int forceit,
1253 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254{
1255 int retval;
1256 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001257 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001258
1259 if (action == DOBUF_UNLOAD)
1260 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001261 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001262 return FAIL;
1263 }
1264
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001265 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001266 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001267 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001268 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001269
1270 setpcmark();
1271 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1272 forceit ? ECMD_FORCEIT : 0, curwin);
1273
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001274 // do_ecmd() may create a new buffer, then we have to delete
1275 // the old one. But do_ecmd() may have done that already, check
1276 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001277 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001278 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001279 if (!close_others)
1280 need_fileinfo = FALSE;
1281 return retval;
1282}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284/*
1285 * Implementation of the commands for the buffer list.
1286 *
1287 * action == DOBUF_GOTO go to specified buffer
1288 * action == DOBUF_SPLIT split window and go to specified buffer
1289 * action == DOBUF_UNLOAD unload specified buffer(s)
1290 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1291 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001292 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 *
1294 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1295 * start == DOBUF_FIRST go to "count" buffer from first buffer
1296 * start == DOBUF_LAST go to "count" buffer from last buffer
1297 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1298 *
1299 * Return FAIL or OK.
1300 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001301 static int
1302do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001303 int action,
1304 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001305 int dir, // FORWARD or BACKWARD
1306 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001307 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 buf_T *buf;
1310 buf_T *bp;
1311 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001312 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 switch (start)
1315 {
1316 case DOBUF_FIRST: buf = firstbuf; break;
1317 case DOBUF_LAST: buf = lastbuf; break;
1318 default: buf = curbuf; break;
1319 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001320 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 {
1322 while (count-- > 0)
1323 {
1324 do
1325 {
1326 buf = buf->b_next;
1327 if (buf == NULL)
1328 buf = firstbuf;
1329 }
1330 while (buf != curbuf && !bufIsChanged(buf));
1331 }
1332 if (!bufIsChanged(buf))
1333 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001334 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 return FAIL;
1336 }
1337 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001338 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340 while (buf != NULL && buf->b_fnum != count)
1341 buf = buf->b_next;
1342 }
1343 else
1344 {
1345 bp = NULL;
1346 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1347 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001348 // remember the buffer where we start, we come back there when all
1349 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (bp == NULL)
1351 bp = buf;
1352 if (dir == FORWARD)
1353 {
1354 buf = buf->b_next;
1355 if (buf == NULL)
1356 buf = firstbuf;
1357 }
1358 else
1359 {
1360 buf = buf->b_prev;
1361 if (buf == NULL)
1362 buf = lastbuf;
1363 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001364 // Don't count unlisted buffers.
1365 // Avoid non-help buffers if the starting point was a non-help buffer and
1366 // vice-versa.
1367 if (unload || (buf->b_p_bl
1368 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
1370 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001371 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 if (bp == buf)
1374 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001375 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001376 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 return FAIL;
1378 }
1379 }
1380 }
1381
Bram Moolenaarc667da52019-11-30 20:52:27 +01001382 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
1384 if (start == DOBUF_FIRST)
1385 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001386 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001388 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001391 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001393 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 return FAIL;
1395 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001396#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001397 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001398 return OK;
1399#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001400 if (
1401 action == DOBUF_GOTO
1402 && buf != curbuf
1403 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1404 // disallow navigating to another buffer when 'winfixbuf' is applied
1405 return FAIL;
1406
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001407 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1408 && (buf->b_flags & BF_DUMMY))
1409 {
1410 // disallow navigating to the dummy buffer
1411 semsg(_(e_buffer_nr_does_not_exist), count);
1412 return FAIL;
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415#ifdef FEAT_GUI
1416 need_mouse_correct = TRUE;
1417#endif
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001420 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 */
1422 if (unload)
1423 {
1424 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001425 bufref_T bufref;
1426
Bram Moolenaar94f01952018-09-01 15:30:03 +02001427 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001428 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001429
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001430 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
Bram Moolenaarc667da52019-11-30 20:52:27 +01001432 // When unloading or deleting a buffer that's already unloaded and
1433 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001434 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1435 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 return FAIL;
1437
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001438 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001440#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001441 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001443# ifdef FEAT_TERMINAL
1444 if (term_job_running(buf->b_term))
1445 {
1446 if (term_confirm_stop(buf) == FAIL)
1447 return FAIL;
1448 }
1449 else
1450# endif
1451 {
1452 dialog_changed(buf, FALSE);
1453 if (!bufref_valid(&bufref))
1454 // Autocommand deleted buffer, oops! It's not changed
1455 // now.
1456 return FAIL;
1457 // If it's still changed fail silently, the dialog already
1458 // mentioned why it fails.
1459 if (bufIsChanged(buf))
1460 return FAIL;
1461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001463 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001466 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 return FAIL;
1468 }
1469 }
1470
Bram Moolenaarc667da52019-11-30 20:52:27 +01001471 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001472 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001473 end_visual_mode();
1474
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001475 // If deleting the last (listed) buffer, make it empty.
1476 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001477 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (bp->b_p_bl && bp != buf)
1479 break;
1480 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001481 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001483 // If the deleted buffer is the current one, close the current window
1484 // (unless it's the only window). Repeat this so long as we end up in
1485 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001486 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001487 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001488 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001489 {
1490 if (win_close(curwin, FALSE) == FAIL)
1491 break;
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001494 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (buf != curbuf)
1496 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001497 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001498 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001499 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 return OK;
1501 }
1502
1503 /*
1504 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001505 * There should be another, otherwise it would have been handled
1506 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001507 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * Then prefer the buffer we most recently visited.
1509 * Else try to find one that is loaded, after the current buffer,
1510 * then before the current buffer.
1511 * Finally use any buffer.
1512 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001513 buf = NULL; // selected buffer
1514 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001515 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1516 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001517 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 int jumpidx;
1520
1521 jumpidx = curwin->w_jumplistidx - 1;
1522 if (jumpidx < 0)
1523 jumpidx = curwin->w_jumplistlen - 1;
1524
1525 forward = jumpidx;
1526 while (jumpidx != curwin->w_jumplistidx)
1527 {
1528 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1529 if (buf != NULL)
1530 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001531 // Skip current and unlisted bufs. Also skip a quickfix
1532 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001533 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001534 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 else if (buf->b_ml.ml_mfp == NULL)
1536 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001537 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (bp == NULL)
1539 bp = buf;
1540 buf = NULL;
1541 }
1542 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001543 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001545 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1547 break;
1548 if (--jumpidx < 0)
1549 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001550 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 break;
1552 }
1553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarc667da52019-11-30 20:52:27 +01001555 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 forward = TRUE;
1558 buf = curbuf->b_next;
1559 for (;;)
1560 {
1561 if (buf == NULL)
1562 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001563 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 break;
1565 buf = curbuf->b_prev;
1566 forward = FALSE;
1567 continue;
1568 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001569 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001570 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001571 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001573 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 bp = buf;
1577 }
1578 if (forward)
1579 buf = buf->b_next;
1580 else
1581 buf = buf->b_prev;
1582 }
1583 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001584 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001586 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001588 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001589 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 break;
1591 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001592 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 if (curbuf->b_next != NULL)
1595 buf = curbuf->b_next;
1596 else
1597 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001598 if (bt_quickfix(buf))
1599 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602
Bram Moolenaara02471e2014-01-10 16:43:14 +01001603 if (buf == NULL)
1604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001605 // Autocommands must have wiped out all other buffers. Only option
1606 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001607 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001608 }
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001611 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001613 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001615 // If 'switchbuf' is set jump to the window containing "buf".
1616 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001617 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 return FAIL;
1621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaarc667da52019-11-30 20:52:27 +01001623 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (buf == curbuf)
1625 return OK;
1626
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001627 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001628 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001631 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001633# ifdef FEAT_TERMINAL
1634 if (term_job_running(curbuf->b_term))
1635 {
1636 if (term_confirm_stop(curbuf) == FAIL)
1637 return FAIL;
1638 // Manually kill the terminal here because this command will
1639 // hide it otherwise.
1640 free_terminal(curbuf);
1641 }
1642 else
1643# endif
1644 {
1645 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001646
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001647 set_bufref(&bufref, buf);
1648 dialog_changed(curbuf, FALSE);
1649 if (!bufref_valid(&bufref))
1650 // Autocommand deleted buffer, oops!
1651 return FAIL;
1652
1653 if (bufIsChanged(curbuf))
1654 {
1655 no_write_message();
1656 return FAIL;
1657 }
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
1662 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001663 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 return FAIL;
1665 }
1666 }
1667
Bram Moolenaarc667da52019-11-30 20:52:27 +01001668 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 set_curbuf(buf, action);
1670
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001672 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001675 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 return FAIL;
1677#endif
1678
1679 return OK;
1680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001682 int
1683do_buffer(
1684 int action,
1685 int start,
1686 int dir, // FORWARD or BACKWARD
1687 int count, // buffer number or number of buffers
1688 int forceit) // TRUE when using !
1689{
1690 return do_buffer_ext(action, start, dir, count,
1691 forceit ? DOBUF_FORCEIT : 0);
1692}
1693
1694/*
1695 * do_bufdel() - delete or unload buffer(s)
1696 *
1697 * addr_count == 0: ":bdel" - delete current buffer
1698 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1699 * buffer "end_bnr", then any other arguments.
1700 * addr_count == 2: ":N,N bdel" - delete buffers in range
1701 *
1702 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1703 * DOBUF_DEL (":bdel")
1704 *
1705 * Returns error message or NULL
1706 */
1707 char *
1708do_bufdel(
1709 int command,
1710 char_u *arg, // pointer to extra arguments
1711 int addr_count,
1712 int start_bnr, // first buffer number in a range
1713 int end_bnr, // buffer nr or last buffer nr in a range
1714 int forceit)
1715{
1716 int do_current = 0; // delete current buffer?
1717 int deleted = 0; // number of buffers deleted
1718 char *errormsg = NULL; // return value
1719 int bnr; // buffer number
1720 char_u *p;
1721
1722 if (addr_count == 0)
1723 {
1724 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1725 }
1726 else
1727 {
1728 if (addr_count == 2)
1729 {
1730 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001731 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001732 bnr = start_bnr;
1733 }
1734 else // addr_count == 1
1735 bnr = end_bnr;
1736
1737 for ( ;!got_int; ui_breakcheck())
1738 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001739 // Delete the current buffer last, otherwise when the
1740 // current buffer is deleted, the next buffer becomes
1741 // the current one and will be loaded, which may then
1742 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001743 if (bnr == curbuf->b_fnum)
1744 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001745 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001746 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1747 ++deleted;
1748
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001749 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001750 if (addr_count == 2)
1751 {
1752 if (++bnr > end_bnr)
1753 break;
1754 }
1755 else // addr_count == 1
1756 {
1757 arg = skipwhite(arg);
1758 if (*arg == NUL)
1759 break;
1760 if (!VIM_ISDIGIT(*arg))
1761 {
1762 p = skiptowhite_esc(arg);
1763 bnr = buflist_findpat(arg, p,
1764 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1765 FALSE, FALSE);
1766 if (bnr < 0) // failed
1767 break;
1768 arg = p;
1769 }
1770 else
1771 bnr = getdigits(&arg);
1772 }
1773 }
1774 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1775 FORWARD, do_current, forceit) == OK)
1776 ++deleted;
1777
1778 if (deleted == 0)
1779 {
1780 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001781 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001782 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001783 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001784 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001785 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001786 errormsg = (char *)IObuff;
1787 }
1788 else if (deleted >= p_report)
1789 {
1790 if (command == DOBUF_UNLOAD)
1791 smsg(NGETTEXT("%d buffer unloaded",
1792 "%d buffers unloaded", deleted), deleted);
1793 else if (command == DOBUF_DEL)
1794 smsg(NGETTEXT("%d buffer deleted",
1795 "%d buffers deleted", deleted), deleted);
1796 else
1797 smsg(NGETTEXT("%d buffer wiped out",
1798 "%d buffers wiped out", deleted), deleted);
1799 }
1800 }
1801
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001802 return errormsg;
1803}
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805/*
1806 * Set current buffer to "buf". Executes autocommands and closes current
1807 * buffer. "action" tells how to close the current buffer:
1808 * DOBUF_GOTO free or hide it
1809 * DOBUF_SPLIT nothing
1810 * DOBUF_UNLOAD unload it
1811 * DOBUF_DEL delete it
1812 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001813 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
1815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 buf_T *prevbuf;
1819 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001820 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001821#ifdef FEAT_SYN_HL
1822 long old_tw = curbuf->b_p_tw;
1823#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001824 bufref_T newbufref;
1825 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001826 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001827 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001830 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001831 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1832 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaarc667da52019-11-30 20:52:27 +01001834 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Bram Moolenaarc667da52019-11-30 20:52:27 +01001837 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001839 set_bufref(&prevbufref, prevbuf);
1840 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001842 // Autocommands may delete the current buffer and/or the buffer we want to
1843 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001844 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001845 || (bufref_valid(&prevbufref)
1846 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001847#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001848 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001850 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001852#ifdef FEAT_SYN_HL
1853 if (prevbuf == curwin->w_buffer)
1854 reset_synblock(curwin);
1855#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001856 // autocommands may have opened a new window
1857 // with prevbuf, grr
1858 if (unload ||
1859 (last_winid != get_last_winid() &&
1860 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001861 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001862#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001863 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001865 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001867 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001868 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001869
Bram Moolenaare615db02022-01-20 21:00:54 +00001870 // Do not sync when in Insert mode and the buffer is open in
1871 // another window, might be a timer doing something in another
1872 // window.
1873 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001874 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001875 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1877 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001878 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001879 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1880 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001881 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001882 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001883 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001886 // An autocommand may have deleted "buf", already entered it (e.g., when
1887 // it did ":bunload") or aborted the script processing.
1888 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001889 valid = buf_valid(buf);
1890 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001891#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001892 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001894 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001895 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001896 // autocommands changed curbuf and we will move to another
1897 // buffer soon, so decrement curbuf->b_nwindows
1898 if (curbuf != NULL && prevbuf != curbuf)
1899 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001900 // If the buffer is not valid but curwin->w_buffer is NULL we must
1901 // enter some buffer. Using the last one is hopefully OK.
1902 if (!valid)
1903 enter_buffer(lastbuf);
1904 else
1905 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001906#ifdef FEAT_SYN_HL
1907 if (old_tw != curbuf->b_p_tw)
1908 check_colorcolumn(curwin);
1909#endif
1910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911}
1912
1913/*
1914 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001915 * Old curbuf must have been abandoned already! This also means "curbuf" may
1916 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001919enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001921 // when closing the current buffer stop Visual mode
1922 if (VIsual_active
1923#if defined(EXITFREE)
1924 && !entered_free_all_mem
1925#endif
1926 )
1927 end_visual_mode();
1928
Bram Moolenaar010ee962019-09-25 20:37:36 +02001929 // Get the buffer in the current window.
1930 curwin->w_buffer = buf;
1931 curbuf = buf;
1932 ++curbuf->b_nwindows;
1933
1934 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1936 if (!buf->b_help)
1937 get_winopts(buf);
1938#ifdef FEAT_FOLDING
1939 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001940 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001942 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943#endif
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001946 if (curwin->w_p_diff)
1947 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#endif
1949
Bram Moolenaara971b822011-09-14 14:43:25 +02001950#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001951 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001952#endif
1953
Bram Moolenaarc667da52019-11-30 20:52:27 +01001954 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 curwin->w_cursor.lnum = 1;
1956 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001959 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
Bram Moolenaarc667da52019-11-30 20:52:27 +01001961 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001962 curwin->w_valid = 0;
1963
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001964 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1965 curbuf->b_last_cursor.col, TRUE);
1966
Bram Moolenaarc667da52019-11-30 20:52:27 +01001967 // Make sure the buffer is loaded.
1968 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001969 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001970 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001971 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001972 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001973 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02001974 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001975
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001976 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 else
1979 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001980 if (!msg_silent && !shortmess(SHM_FILEINFO))
1981 need_fileinfo = TRUE; // display file info after redraw
1982
1983 // check if file changed
1984 (void)buf_check_timestamp(curbuf, FALSE);
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001987#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1991 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
Bram Moolenaarc667da52019-11-30 20:52:27 +01001994 // If autocommands did not change the cursor position, restore cursor lnum
1995 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (curwin->w_cursor.lnum == 1 && inindent(0))
1997 buflist_getfpos();
1998
Bram Moolenaarc667da52019-11-30 20:52:27 +01001999 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002001 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002002 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002003 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
Bram Moolenaar009b2592004-10-24 19:18:58 +00002005#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002006 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002007 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002008#endif
2009
Bram Moolenaarc667da52019-11-30 20:52:27 +01002010 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002011 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013#ifdef FEAT_KEYMAP
2014 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002015 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002017#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002018 // May need to set the spell language. Can only do this after the buffer
2019 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002020 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002021 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002022#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002023#ifdef FEAT_VIMINFO
2024 curbuf->b_last_used = vim_time();
2025#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002026
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002027 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028}
2029
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002030#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2031/*
2032 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002033 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002034 */
2035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002036do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002037{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002038 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002039 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002040 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002041 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002042 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002043 last_chdir_reason = "autochdir";
2044 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002045}
2046#endif
2047
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002048 static void
2049no_write_message_buf(buf_T *buf UNUSED)
2050{
2051#ifdef FEAT_TERMINAL
2052 if (term_job_running(buf->b_term))
2053 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2054 else
2055#endif
2056 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2057 buf->b_fnum);
2058}
2059
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002060 void
2061no_write_message(void)
2062{
2063#ifdef FEAT_TERMINAL
2064 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002065 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002066 else
2067#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002068 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002069}
2070
2071 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002072no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002073{
2074#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002075 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002076 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002077 else
2078#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002079 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002080}
2081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082/*
2083 * functions for dealing with the buffer list
2084 */
2085
2086/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002087 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2088 * only one window. That means it can be re-used.
2089 */
2090 int
2091curbuf_reusable(void)
2092{
2093 return (curbuf != NULL
2094 && curbuf->b_ffname == NULL
2095 && curbuf->b_nwindows <= 1
2096 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002097 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002098 && !curbufIsChanged());
2099}
2100
2101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 * Add a file name to the buffer list. Return a pointer to the buffer.
2103 * If the same file name already exists return a pointer to that buffer.
2104 * If it does not exist, or if fname == NULL, a new entry is created.
2105 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2106 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2107 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002108 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002109 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2110 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002111 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 * This is the ONLY way to create a new buffer.
2113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002116 char_u *ffname_arg, // full path of fname or relative
2117 char_u *sfname_arg, // short fname or NULL
2118 linenr_T lnum, // preferred cursor line
2119 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002121 char_u *ffname = ffname_arg;
2122 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 buf_T *buf;
2124#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002125 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126#endif
2127
Bram Moolenaar480778b2016-07-14 22:09:39 +02002128 if (top_file_num == 1)
2129 hash_init(&buf_hashtab);
2130
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002131 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002134 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 */
2136#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002137 // On Unix we can use inode numbers when the file exists. Works better
2138 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2140 st.st_dev = (dev_T)-1;
2141#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002142 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#ifdef UNIX
2144 buflist_findname_stat(ffname, &st)
2145#else
2146 buflist_findname(ffname)
2147#endif
2148 ) != NULL)
2149 {
2150 vim_free(ffname);
2151 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002152 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2153 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002154
2155 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002156 // copy the options now, if 'cpo' doesn't have 's' and not done
2157 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002158 buf_copy_options(buf, 0);
2159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2161 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002162 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002165 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002167 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002168 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002169 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002170 return NULL;
2171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173 return buf;
2174 }
2175
2176 /*
2177 * If the current buffer has no name and no contents, use the current
2178 * buffer. Otherwise: Need to allocate a new buffer structure.
2179 *
2180 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002181 * (A spell file buffer is allocated in spell.c, but that's not a normal
2182 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 */
2184 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002185 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002188 // It's like this buffer is deleted. Watch out for autocommands that
2189 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002190 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2191 if (buf != curbuf) // autocommands deleted the buffer!
2192 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002193#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002194 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002195 {
2196 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201 if (buf != curbuf || curbuf == NULL)
2202 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002203 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (buf == NULL)
2205 {
2206 vim_free(ffname);
2207 return NULL;
2208 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002209#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002210 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002211 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002212 if (buf->b_vars == NULL)
2213 {
2214 vim_free(ffname);
2215 vim_free(buf);
2216 return NULL;
2217 }
2218 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2219#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002220 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222
2223 if (ffname != NULL)
2224 {
2225 buf->b_ffname = ffname;
2226 buf->b_sfname = vim_strsave(sfname);
2227 }
2228
2229 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002230 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2233 || buf->b_wininfo == NULL)
2234 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002235 if (buf->b_sfname != buf->b_ffname)
2236 VIM_CLEAR(buf->b_sfname);
2237 else
2238 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002239 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (buf != curbuf)
2241 free_buffer(buf);
2242 return NULL;
2243 }
2244
2245 if (buf == curbuf)
2246 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002247 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002248
Bram Moolenaarc667da52019-11-30 20:52:27 +01002249 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002250 buf->b_p_initialized = FALSE;
2251 buf_copy_options(buf, BCO_ENTER);
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002254 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 curbuf->b_kmap_state |= KEYMAP_INIT;
2256#endif
2257 }
2258 else
2259 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002260 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002262 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 buf->b_prev = NULL;
2265 firstbuf = buf;
2266 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002267 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 lastbuf->b_next = buf;
2270 buf->b_prev = lastbuf;
2271 }
2272 lastbuf = buf;
2273
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002274 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2275 {
2276 // Recycle a previously used buffer number. Used for buffers which
2277 // are normally hidden, e.g. in a popup window. Avoids that the
2278 // buffer number grows rapidly.
2279 --buf_reuse.ga_len;
2280 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002281
2282 // Move buffer to the right place in the buffer list.
2283 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2284 {
2285 buf_T *prev = buf->b_prev;
2286
2287 prev->b_next = buf->b_next;
2288 if (prev->b_next != NULL)
2289 prev->b_next->b_prev = prev;
2290 buf->b_next = prev;
2291 buf->b_prev = prev->b_prev;
2292 if (buf->b_prev != NULL)
2293 buf->b_prev->b_next = buf;
2294 prev->b_prev = buf;
2295 if (lastbuf == buf)
2296 lastbuf = prev;
2297 if (firstbuf == prev)
2298 firstbuf = buf;
2299 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002300 }
2301 else
2302 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002303 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002305 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002306 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002309 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311 top_file_num = 1;
2312 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002313 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002315 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 buf_copy_options(buf, BCO_ALWAYS);
2317 }
2318
2319 buf->b_wininfo->wi_fpos.lnum = lnum;
2320 buf->b_wininfo->wi_win = curwin;
2321
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002322#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002323 hash_init(&buf->b_s.b_keywtab);
2324 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325#endif
2326
2327 buf->b_fname = buf->b_sfname;
2328#ifdef UNIX
2329 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002330 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 else
2332 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002333 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 buf->b_dev = st.st_dev;
2335 buf->b_ino = st.st_ino;
2336 }
2337#endif
2338 buf->b_u_synced = TRUE;
2339 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002340 if (flags & BLN_DUMMY)
2341 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002343 clrallmarks(buf); // clear marks
2344 fmarks_check_names(buf); // check file marks for this file
2345 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (!(flags & BLN_DUMMY))
2347 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002348 bufref_T bufref;
2349
Bram Moolenaarc667da52019-11-30 20:52:27 +01002350 // Tricky: these autocommands may change the buffer list. They could
2351 // also split the window with re-using the one empty buffer. This may
2352 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002353 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002354 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002355 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002356 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002358 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002359 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002360 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002361 return NULL;
2362 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002363#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002364 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 return buf;
2370}
2371
2372/*
2373 * Free the memory for the options of a buffer.
2374 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2375 * 'fileencoding'.
2376 */
2377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378free_buf_options(
2379 buf_T *buf,
2380 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 if (free_p_ff)
2383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 clear_string_option(&buf->b_p_bh);
2387 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389#ifdef FEAT_FIND_ID
2390 clear_string_option(&buf->b_p_def);
2391 clear_string_option(&buf->b_p_inc);
2392# ifdef FEAT_EVAL
2393 clear_string_option(&buf->b_p_inex);
2394# endif
2395#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002396#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 clear_string_option(&buf->b_p_inde);
2398 clear_string_option(&buf->b_p_indk);
2399#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002400#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2401 clear_string_option(&buf->b_p_bexpr);
2402#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002403#if defined(FEAT_CRYPT)
2404 clear_string_option(&buf->b_p_cm);
2405#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002406 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002407#if defined(FEAT_EVAL)
2408 clear_string_option(&buf->b_p_fex);
2409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002411# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002412 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2413 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002414 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 clear_string_option(&buf->b_p_key);
2417#endif
2418 clear_string_option(&buf->b_p_kp);
2419 clear_string_option(&buf->b_p_mps);
2420 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002421 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002423#ifdef FEAT_VARTABS
2424 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002425 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002426 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002427 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002428 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430#ifdef FEAT_KEYMAP
2431 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002432 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 ga_clear(&buf->b_kmap_ga);
2434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#ifdef FEAT_FOLDING
2437 clear_string_option(&buf->b_p_cms);
2438#endif
2439 clear_string_option(&buf->b_p_nf);
2440#ifdef FEAT_SYN_HL
2441 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002442 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002443#endif
2444#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002445 clear_string_option(&buf->b_s.b_p_spc);
2446 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002447 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002448 buf->b_s.b_cap_prog = NULL;
2449 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002450 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 clear_string_option(&buf->b_p_cink);
2455 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002456 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002457 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002459 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002461#ifdef FEAT_COMPL_FUNC
2462 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002463 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002464 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002465 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002466 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002467 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469#ifdef FEAT_QUICKFIX
2470 clear_string_option(&buf->b_p_gp);
2471 clear_string_option(&buf->b_p_mp);
2472 clear_string_option(&buf->b_p_efm);
2473#endif
2474 clear_string_option(&buf->b_p_ep);
2475 clear_string_option(&buf->b_p_path);
2476 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002477 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002478#ifdef FEAT_EVAL
2479 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002480 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 clear_string_option(&buf->b_p_dict);
2483 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002484 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002486 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002487 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002488 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002489 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490}
2491
2492/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002493 * Get alternate file "n".
2494 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2495 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 * if (options & GETF_SETMARK) call setpcmark()
2497 * if (options & GETF_ALT) we are jumping to an alternate file.
2498 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2499 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002500 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 */
2502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002503buflist_getfile(
2504 int n,
2505 linenr_T lnum,
2506 int options,
2507 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 pos_T *fpos;
2512 colnr_T col;
2513
2514 buf = buflist_findnr(n);
2515 if (buf == NULL)
2516 {
2517 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002518 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002520 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 return FAIL;
2522 }
2523
Bram Moolenaarc667da52019-11-30 20:52:27 +01002524 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (buf == curbuf)
2526 return OK;
2527
Bram Moolenaar71223e22022-05-30 15:23:09 +01002528 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002529 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaarc667da52019-11-30 20:52:27 +01002531 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (lnum == 0)
2533 {
2534 fpos = buflist_findfpos(buf);
2535 lnum = fpos->lnum;
2536 col = fpos->col;
2537 }
2538 else
2539 col = 0;
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (options & GETF_SWITCH)
2542 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002543 // If 'switchbuf' is set jump to the window containing "buf".
2544 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002545
Bram Moolenaarc667da52019-11-30 20:52:27 +01002546 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2547 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002548 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002549 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002551 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002552 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002553 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2554 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002556 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002561 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002562 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2563 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002565 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (!p_sol && col != 0)
2567 {
2568 curwin->w_cursor.col = col;
2569 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 curwin->w_set_curswant = TRUE;
2572 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002573 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002575
2576 if (RedrawingDisabled > 0)
2577 --RedrawingDisabled;
2578 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579}
2580
2581/*
2582 * go to the last know line number for the current buffer
2583 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002585buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 pos_T *fpos;
2588
2589 fpos = buflist_findfpos(curbuf);
2590
2591 curwin->w_cursor.lnum = fpos->lnum;
2592 check_cursor_lnum();
2593
2594 if (p_sol)
2595 curwin->w_cursor.col = 0;
2596 else
2597 {
2598 curwin->w_cursor.col = fpos->col;
2599 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 curwin->w_set_curswant = TRUE;
2602 }
2603}
2604
Bram Moolenaar81695252004-12-29 20:58:21 +00002605/*
2606 * Find file in buffer list by name (it has to be for the current window).
2607 * Returns NULL if not found.
2608 */
2609 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002611{
2612 char_u *ffname;
2613 buf_T *buf = NULL;
2614
Bram Moolenaarc667da52019-11-30 20:52:27 +01002615 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002616 ffname = FullName_save(fname,
2617#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002618 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002619#else
2620 FALSE
2621#endif
2622 );
2623 if (ffname != NULL)
2624 {
2625 buf = buflist_findname(ffname);
2626 vim_free(ffname);
2627 }
2628 return buf;
2629}
Bram Moolenaar81695252004-12-29 20:58:21 +00002630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
2632 * Find file in buffer list by name (it has to be for the current window).
2633 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002634 * Skips dummy buffers.
2635 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 */
2637 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002638buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002641 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 if (mch_stat((char *)ffname, &st) < 0)
2644 st.st_dev = (dev_T)-1;
2645 return buflist_findname_stat(ffname, &st);
2646}
2647
2648/*
2649 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2650 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002651 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 */
2653 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002654buflist_findname_stat(
2655 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002656 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
2658#endif
2659 buf_T *buf;
2660
Bram Moolenaarc667da52019-11-30 20:52:27 +01002661 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002662 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002663 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#ifdef UNIX
2665 , stp
2666#endif
2667 ))
2668 return buf;
2669 return NULL;
2670}
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672/*
2673 * Find file in buffer list by a regexp pattern.
2674 * Return fnum of the found buffer.
2675 * Return < 0 for error.
2676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678buflist_findpat(
2679 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002680 char_u *pattern_end, // pointer to first char after pattern
2681 int unlisted, // find unlisted buffers
2682 int diffmode UNUSED, // find diff-mode buffers only
2683 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int match = -1;
2687 int find_listed;
2688 char_u *pat;
2689 char_u *patend;
2690 int attempt;
2691 char_u *p;
2692 int toggledollar;
2693
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002694 // "%" is current file, "%%" or "#" is alternate file
2695 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2696 || (in_vim9script() && pattern_end == pattern + 2
2697 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002699 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002701 else
2702 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703#ifdef FEAT_DIFF
2704 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2705 match = -1;
2706#endif
2707 }
2708
2709 /*
2710 * Try four ways of matching a listed buffer:
2711 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002712 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 * attempt == 2: with '$' at end (only match at end)
2714 * attempt == 3: with '^' at start and '$' at end (only full match)
2715 * Repeat this for finding an unlisted buffer if there was no matching
2716 * listed buffer.
2717 */
2718 else
2719 {
2720 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2721 if (pat == NULL)
2722 return -1;
2723 patend = pat + STRLEN(pat) - 1;
2724 toggledollar = (patend > pat && *patend == '$');
2725
Bram Moolenaarc667da52019-11-30 20:52:27 +01002726 // First try finding a listed buffer. If not found and "unlisted"
2727 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 find_listed = TRUE;
2729 for (;;)
2730 {
2731 for (attempt = 0; attempt <= 3; ++attempt)
2732 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002733 regmatch_T regmatch;
2734
Bram Moolenaarc667da52019-11-30 20:52:27 +01002735 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002737 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002739 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002741 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002743 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002744 {
2745 if (regmatch.regprog == NULL)
2746 {
2747 // invalid pattern, possibly after switching engine
2748 vim_free(pat);
2749 return -1;
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (buf->b_p_bl == find_listed
2752#ifdef FEAT_DIFF
2753 && (!diffmode || diff_mode_buf(buf))
2754#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002755 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002757 if (curtab_only)
2758 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002759 // Ignore the match if the buffer is not open in
2760 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002761 win_T *wp;
2762
Bram Moolenaar29323592016-07-24 22:04:11 +02002763 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002764 if (wp->w_buffer == buf)
2765 break;
2766 if (wp == NULL)
2767 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002768 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002769 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {
2771 match = -2;
2772 break;
2773 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002774 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002778 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002779 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 break;
2781 }
2782
Bram Moolenaarc667da52019-11-30 20:52:27 +01002783 // Only search for unlisted buffers if there was no match with
2784 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (!unlisted || !find_listed || match != -1)
2786 break;
2787 find_listed = FALSE;
2788 }
2789
2790 vim_free(pat);
2791 }
2792
2793 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002794 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002796 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 return match;
2798}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
Bram Moolenaar52410572019-10-27 05:12:45 +01002800#ifdef FEAT_VIMINFO
2801typedef struct {
2802 buf_T *buf;
2803 char_u *match;
2804} bufmatch_T;
2805#endif
2806
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807/*
2808 * Find all buffer names that match.
2809 * For command line expansion of ":buf" and ":sbuf".
2810 * Return OK if matches found, FAIL otherwise.
2811 */
2812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002813ExpandBufnames(
2814 char_u *pat,
2815 int *num_file,
2816 char_u ***file,
2817 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002819 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 buf_T *buf;
2821 int round;
2822 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002823 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002824#ifdef FEAT_VIMINFO
2825 bufmatch_T *matches = NULL;
2826#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002827 int fuzzy;
2828 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002829 regmatch_T regmatch;
2830 int score = 0;
2831 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaarc667da52019-11-30 20:52:27 +01002833 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 *file = NULL;
2835
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002836#ifdef FEAT_DIFF
2837 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2838 return FAIL;
2839#endif
2840
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002841 fuzzy = cmdline_fuzzy_complete(pat);
2842
2843 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2844 // expression matching)
2845 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002847 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002848 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002849 int len = (int)STRLEN(pat);
2850 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002851 if (patc == NULL)
2852 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002853 STRNCPY(patc, pat + 1, len - 1);
2854 patc[len - 1] = NUL;
2855 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002856 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002857 else if (*pat == '^')
2858 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002859 else
2860 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002861 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002862 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002863
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002864 // round == 1: Count the matches.
2865 // round == 2: Build the array to keep the matches.
2866 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002867 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002868 count = 0;
2869 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002870 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002871 if (!buf->b_p_bl) // skip unlisted buffers
2872 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002873#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002874 if (options & BUF_DIFF_FILTER)
2875 // Skip buffers not suitable for
2876 // :diffget or :diffput completion.
2877 if (buf == curbuf || !diff_mode_buf(buf))
2878 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002879#endif
2880
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002881 if (!fuzzy)
2882 {
2883 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002884 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002885 // invalid pattern, possibly after recompiling
2886 if (to_free)
2887 vim_free(patc);
2888 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002889 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002890 p = buflist_match(&regmatch, buf, p_wic);
2891 }
2892 else
2893 {
2894 p = NULL;
2895 // first try matching with the short file name
2896 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2897 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002898 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002899 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002900 // next try matching with the full path file name
2901 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2902 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002905
2906 if (p == NULL)
2907 continue;
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 if (round == 1)
2910 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002911 ++count;
2912 continue;
2913 }
2914
2915 if (options & WILD_HOME_REPLACE)
2916 p = home_replace_save(buf, p);
2917 else
2918 p = vim_strsave(p);
2919
2920 if (!fuzzy)
2921 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002922#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002923 if (matches != NULL)
2924 {
2925 matches[count].buf = buf;
2926 matches[count].match = p;
2927 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002928 }
2929 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002930#endif
2931 (*file)[count++] = p;
2932 }
2933 else
2934 {
2935 fuzmatch[count].idx = count;
2936 fuzmatch[count].str = p;
2937 fuzmatch[count].score = score;
2938 count++;
2939 }
2940 }
2941 if (count == 0) // no match found, break here
2942 break;
2943 if (round == 1)
2944 {
2945 if (!fuzzy)
2946 {
2947 *file = ALLOC_MULT(char_u *, count);
2948 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002949 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002950 vim_regfree(regmatch.regprog);
2951 if (to_free)
2952 vim_free(patc);
2953 return FAIL;
2954 }
2955#ifdef FEAT_VIMINFO
2956 if (options & WILD_BUFLASTUSED)
2957 matches = ALLOC_MULT(bufmatch_T, count);
2958#endif
2959 }
2960 else
2961 {
2962 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2963 if (fuzmatch == NULL)
2964 {
2965 *num_file = 0;
2966 *file = NULL;
2967 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002973 if (!fuzzy)
2974 {
2975 vim_regfree(regmatch.regprog);
2976 if (to_free)
2977 vim_free(patc);
2978 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002979
Bram Moolenaar52410572019-10-27 05:12:45 +01002980#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002981 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002982 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002983 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002984 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002985 int i;
2986 if (count > 1)
2987 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2988 // if the current buffer is first in the list, place it at the end
2989 if (matches[0].buf == curbuf)
2990 {
2991 for (i = 1; i < count; i++)
2992 (*file)[i-1] = matches[i].match;
2993 (*file)[count-1] = matches[0].match;
2994 }
2995 else
2996 {
2997 for (i = 0; i < count; i++)
2998 (*file)[i] = matches[i].match;
2999 }
3000 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003001 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003002 }
3003 else
3004 {
3005 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3006 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003007 }
3008#endif
3009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 *num_file = count;
3011 return (count == 0 ? FAIL : OK);
3012}
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014/*
3015 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003016 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 */
3018 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019buflist_match(
3020 regmatch_T *rmp,
3021 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003022 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 char_u *match;
3025
Bram Moolenaarc667da52019-11-30 20:52:27 +01003026 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003027 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003028 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003029 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031 return match;
3032}
3033
3034/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003035 * Try matching the regexp in "rmp->regprog" with file name "name".
3036 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 * Return "name" when there is a match, NULL when not.
3038 */
3039 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003040fname_match(
3041 regmatch_T *rmp,
3042 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003043 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 char_u *match = NULL;
3046 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003048 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003049 if (name == NULL || rmp->regprog == NULL)
3050 return NULL;
3051
3052 // Ignore case when 'fileignorecase' or the argument is set.
3053 rmp->rm_ic = p_fic || ignore_case;
3054 if (vim_regexec(rmp, name, (colnr_T)0))
3055 match = name;
3056 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003058 // Replace $(HOME) with '~' and try matching again.
3059 p = home_replace_save(NULL, name);
3060 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003062 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064
3065 return match;
3066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
3068/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003069 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
3071 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003074 char_u key[VIM_SIZEOF_INT * 2 + 1];
3075 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 if (nr == 0)
3078 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003079 sprintf((char *)key, "%x", nr);
3080 hi = hash_find(&buf_hashtab, key);
3081
3082 if (!HASHITEM_EMPTY(hi))
3083 return (buf_T *)(hi->hi_key
3084 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return NULL;
3086}
3087
3088/*
3089 * Get name of file 'n' in the buffer list.
3090 * When the file has no name an empty string is returned.
3091 * home_replace() is used to shorten the file name (used for marks).
3092 * Returns a pointer to allocated memory, of NULL when failed.
3093 */
3094 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095buflist_nr2name(
3096 int n,
3097 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003098 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 buf_T *buf;
3101
3102 buf = buflist_findnr(n);
3103 if (buf == NULL)
3104 return NULL;
3105 return home_replace_save(helptail ? buf : NULL,
3106 fullname ? buf->b_ffname : buf->b_fname);
3107}
3108
3109/*
3110 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3111 * When "copy_options" is TRUE save the local window option values.
3112 * When "lnum" is 0 only do the options.
3113 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003115buflist_setfpos(
3116 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003117 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003118 linenr_T lnum,
3119 colnr_T col,
3120 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 wininfo_T *wip;
3123
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003124 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (wip->wi_win == win)
3126 break;
3127 if (wip == NULL)
3128 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003129 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003130 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (wip == NULL)
3132 return;
3133 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003134 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 lnum = 1;
3136 }
3137 else
3138 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003139 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 if (wip->wi_prev)
3141 wip->wi_prev->wi_next = wip->wi_next;
3142 else
3143 buf->b_wininfo = wip->wi_next;
3144 if (wip->wi_next)
3145 wip->wi_next->wi_prev = wip->wi_prev;
3146 if (copy_options && wip->wi_optset)
3147 {
3148 clear_winopt(&wip->wi_opt);
3149#ifdef FEAT_FOLDING
3150 deleteFoldRecurse(&wip->wi_folds);
3151#endif
3152 }
3153 }
3154 if (lnum != 0)
3155 {
3156 wip->wi_fpos.lnum = lnum;
3157 wip->wi_fpos.col = col;
3158 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003159 if (win != NULL)
3160 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003161 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003163 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3165#ifdef FEAT_FOLDING
3166 wip->wi_fold_manual = win->w_fold_manual;
3167 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3168#endif
3169 wip->wi_optset = TRUE;
3170 }
3171
Bram Moolenaarc667da52019-11-30 20:52:27 +01003172 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 wip->wi_next = buf->b_wininfo;
3174 buf->b_wininfo = wip;
3175 wip->wi_prev = NULL;
3176 if (wip->wi_next)
3177 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178}
3179
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003180#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003181/*
3182 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3183 * page. That's because a diff is local to a tab page.
3184 */
3185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003187{
3188 win_T *wp;
3189
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003190 if (!wip->wi_opt.wo_diff)
3191 return FALSE;
3192
3193 FOR_ALL_WINDOWS(wp)
3194 // return FALSE when it's a window in the current tab page, thus
3195 // the buffer was in diff mode here
3196 if (wip->wi_win == wp)
3197 return FALSE;
3198 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003199}
3200#endif
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202/*
3203 * Find info for the current window in buffer "buf".
3204 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003205 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003206 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3207 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 * Returns NULL when there isn't any info.
3209 */
3210 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211find_wininfo(
3212 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003213 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 wininfo_T *wip;
3217
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003218 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003219 if (wip->wi_win == curwin
3220#ifdef FEAT_DIFF
3221 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3222#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003223
3224 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003226
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003227 if (wip != NULL)
3228 return wip;
3229
Bram Moolenaarc667da52019-11-30 20:52:27 +01003230 // If no wininfo for curwin, use the first in the list (that doesn't have
3231 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003232 // If "need_options" is TRUE skip entries that don't have options set,
3233 // unless the window is editing "buf", so we can copy from the window
3234 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003235#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003236 if (skip_diff_buffer)
3237 {
3238 FOR_ALL_BUF_WININFO(buf, wip)
3239 if (!wininfo_other_tab_diff(wip)
3240 && (!need_options || wip->wi_optset
3241 || (wip->wi_win != NULL
3242 && wip->wi_win->w_buffer == buf)))
3243 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003244 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003245 else
3246#endif
3247 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 return wip;
3249}
3250
3251/*
3252 * Reset the local window options to the values last used in this window.
3253 * If the buffer wasn't used in this window before, use the values from
3254 * the most recently used window. If the values were never set, use the
3255 * global values for the window.
3256 */
3257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003258get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 wininfo_T *wip;
3261
3262 clear_winopt(&curwin->w_onebuf_opt);
3263#ifdef FEAT_FOLDING
3264 clearFolding(curwin);
3265#endif
3266
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003267 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003268 if (wip != NULL && wip->wi_win != NULL
3269 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003271 // The buffer is currently displayed in the window: use the actual
3272 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003273 win_T *wp = wip->wi_win;
3274
3275 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3276#ifdef FEAT_FOLDING
3277 curwin->w_fold_manual = wp->w_fold_manual;
3278 curwin->w_foldinvalid = TRUE;
3279 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3280#endif
3281 }
3282 else if (wip != NULL && wip->wi_optset)
3283 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003284 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3286#ifdef FEAT_FOLDING
3287 curwin->w_fold_manual = wip->wi_fold_manual;
3288 curwin->w_foldinvalid = TRUE;
3289 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3290#endif
3291 }
3292 else
3293 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003294 if (wip != NULL)
3295 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003298 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (p_fdls >= 0)
3300 curwin->w_p_fdl = p_fdls;
3301#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003302 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303}
3304
3305/*
3306 * Find the position (lnum and col) for the buffer 'buf' for the current
3307 * window.
3308 * Returns a pointer to no_position if no position is found.
3309 */
3310 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003311buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003314 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003316 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (wip != NULL)
3318 return &(wip->wi_fpos);
3319 else
3320 return &no_position;
3321}
3322
3323/*
3324 * Find the lnum for the buffer 'buf' for the current window.
3325 */
3326 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 return buflist_findfpos(buf)->lnum;
3330}
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003333 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Bram Moolenaar52410572019-10-27 05:12:45 +01003338 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 int len;
3340 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003341 int ro_char;
3342 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003343#ifdef FEAT_TERMINAL
3344 int job_running;
3345 int job_none_open;
3346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar52410572019-10-27 05:12:45 +01003348#ifdef FEAT_VIMINFO
3349 garray_T buflist;
3350 buf_T **buflist_data = NULL, **p;
3351
3352 if (vim_strchr(eap->arg, 't'))
3353 {
3354 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003355 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003356 {
3357 if (ga_grow(&buflist, 1) == OK)
3358 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3359 }
3360
3361 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3362 sizeof(buf_T *), buf_compare);
3363
Bram Moolenaar3b991522019-11-06 23:26:20 +01003364 buflist_data = (buf_T **)buflist.ga_data;
3365 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003366 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003367 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003368
Bram Moolenaar3b991522019-11-06 23:26:20 +01003369 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003370 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3371 : buf->b_next)
3372#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003376#ifdef FEAT_TERMINAL
3377 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003378 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003379#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003380 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003381 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3382 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3383 || (vim_strchr(eap->arg, '+')
3384 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3385 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003386 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003387 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003388 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3389#ifdef FEAT_TERMINAL
3390 || (vim_strchr(eap->arg, 'R')
3391 && (!job_running || (job_running && job_none_open)))
3392 || (vim_strchr(eap->arg, '?')
3393 && (!job_running || (job_running && !job_none_open)))
3394 || (vim_strchr(eap->arg, 'F')
3395 && (job_running || buf->b_term == NULL))
3396#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003397 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3398 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3399 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3400 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3401 || (vim_strchr(eap->arg, '#')
3402 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003405 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 else
3407 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003408 if (message_filtered(NameBuff))
3409 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003411 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3412 : (bufIsChanged(buf) ? '+' : ' ');
3413#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003414 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003415 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003416 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003417 ro_char = '?';
3418 else
3419 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003420 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3421 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003422 }
3423 else if (buf->b_term != NULL)
3424 ro_char = 'F';
3425 else
3426#endif
3427 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3428
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003429 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003430 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 buf->b_fnum,
3432 buf->b_p_bl ? ' ' : 'u',
3433 buf == curbuf ? '%' :
3434 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3435 buf->b_ml.ml_mfp == NULL ? ' ' :
3436 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003437 ro_char,
3438 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003439 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003440 if (len > IOSIZE - 20)
3441 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaarc667da52019-11-30 20:52:27 +01003443 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 i = 40 - vim_strsize(IObuff);
3445 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003447 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003448#ifdef FEAT_VIMINFO
3449 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3450 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3451 else
3452#endif
3453 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3454 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003455 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003457 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 ui_breakcheck();
3459 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003460
3461#ifdef FEAT_VIMINFO
3462 if (buflist_data)
3463 ga_clear(&buflist);
3464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467/*
3468 * Get file name and line number for file 'fnum'.
3469 * Used by DoOneCmd() for translating '%' and '#'.
3470 * Used by insert_reg() and cmdline_paste() for '#' register.
3471 * Return FAIL if not found, OK for success.
3472 */
3473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474buflist_name_nr(
3475 int fnum,
3476 char_u **fname,
3477 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 buf_T *buf;
3480
3481 buf = buflist_findnr(fnum);
3482 if (buf == NULL || buf->b_fname == NULL)
3483 return FAIL;
3484
3485 *fname = buf->b_fname;
3486 *lnum = buflist_findlnum(buf);
3487
3488 return OK;
3489}
3490
3491/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003492 * Set the file name for "buf"' to "ffname_arg", short file name to
3493 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 * The file name with the full path is also remembered, for when :cd is used.
3495 * Returns FAIL for failure (file name already in use by other buffer)
3496 * OK otherwise.
3497 */
3498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003499setfname(
3500 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003501 char_u *ffname_arg,
3502 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003503 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003505 char_u *ffname = ffname_arg;
3506 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003507 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003509 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#endif
3511
3512 if (ffname == NULL || *ffname == NUL)
3513 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003514 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003515 if (buf->b_sfname != buf->b_ffname)
3516 VIM_CLEAR(buf->b_sfname);
3517 else
3518 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003519 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#ifdef UNIX
3521 st.st_dev = (dev_T)-1;
3522#endif
3523 }
3524 else
3525 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003526 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3527 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 return FAIL;
3529
3530 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003531 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 * - if the buffer is loaded, fail
3533 * - if the buffer is not loaded, delete it from the list
3534 */
3535#ifdef UNIX
3536 if (mch_stat((char *)ffname, &st) < 0)
3537 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003538#endif
3539 if (!(buf->b_flags & BF_DUMMY))
3540#ifdef UNIX
3541 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003543 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
3545 if (obuf != NULL && obuf != buf)
3546 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003547 win_T *win;
3548 tabpage_T *tab;
3549 int in_use = FALSE;
3550
3551 // during startup a window may use a buffer that is not loaded yet
3552 FOR_ALL_TAB_WINDOWS(tab, win)
3553 if (win->w_buffer == obuf)
3554 in_use = TRUE;
3555
3556 // it's loaded or used in a window, fail
3557 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003560 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 vim_free(ffname);
3562 return FAIL;
3563 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003564 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003565 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 sfname = vim_strsave(sfname);
3568 if (ffname == NULL || sfname == NULL)
3569 {
3570 vim_free(sfname);
3571 vim_free(ffname);
3572 return FAIL;
3573 }
3574#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003575 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003577 if (buf->b_sfname != buf->b_ffname)
3578 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 buf->b_ffname = ffname;
3581 buf->b_sfname = sfname;
3582 }
3583 buf->b_fname = buf->b_sfname;
3584#ifdef UNIX
3585 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003586 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else
3588 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003589 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 buf->b_dev = st.st_dev;
3591 buf->b_ino = st.st_ino;
3592 }
3593#endif
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 buf_name_changed(buf);
3598 return OK;
3599}
3600
3601/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003602 * Crude way of changing the name of a buffer. Use with care!
3603 * The name should be relative to the current directory.
3604 */
3605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003607{
3608 buf_T *buf;
3609
3610 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003611 if (buf == NULL)
3612 return;
3613
3614 if (buf->b_sfname != buf->b_ffname)
3615 vim_free(buf->b_sfname);
3616 vim_free(buf->b_ffname);
3617 buf->b_ffname = vim_strsave(name);
3618 buf->b_sfname = NULL;
3619 // Allocate ffname and expand into full path. Also resolves .lnk
3620 // files on Win32.
3621 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3622 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003623}
3624
3625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * Take care of what needs to be done when the name of buffer "buf" has
3627 * changed.
3628 */
3629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
3632 /*
3633 * If the file name changed, also change the name of the swapfile
3634 */
3635 if (buf->b_ml.ml_mfp != NULL)
3636 ml_setname(buf);
3637
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003638#ifdef FEAT_TERMINAL
3639 if (buf->b_term != NULL)
3640 term_clear_status_text(buf->b_term);
3641#endif
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003644 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003645 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003646 status_redraw_all(); // status lines need to be redrawn
3647 fmarks_check_names(buf); // check named file marks
3648 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649}
3650
3651/*
3652 * set alternate file name for current window
3653 *
3654 * Used by do_one_cmd(), do_write() and do_ecmd().
3655 * Return the buffer.
3656 */
3657 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658setaltfname(
3659 char_u *ffname,
3660 char_u *sfname,
3661 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 buf_T *buf;
3664
Bram Moolenaarc667da52019-11-30 20:52:27 +01003665 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003667 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 curwin->w_alt_fnum = buf->b_fnum;
3669 return buf;
3670}
3671
3672/*
3673 * Get alternate file name for current window.
3674 * Return NULL if there isn't any, and give error message if requested.
3675 */
3676 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003677getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003678 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 char_u *fname;
3681 linenr_T dummy;
3682
3683 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3684 {
3685 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003686 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return NULL;
3688 }
3689 return fname;
3690}
3691
3692/*
3693 * Add a file name to the buflist and return its number.
3694 * Uses same flags as buflist_new(), except BLN_DUMMY.
3695 *
3696 * used by qf_init(), main() and doarglist()
3697 */
3698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003699buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
3701 buf_T *buf;
3702
3703 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3704 if (buf != NULL)
3705 return buf->b_fnum;
3706 return 0;
3707}
3708
3709#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3710/*
3711 * Adjust slashes in file names. Called after 'shellslash' was set.
3712 */
3713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 buf_T *bp;
3717
Bram Moolenaar29323592016-07-24 22:04:11 +02003718 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
3720 if (bp->b_ffname != NULL)
3721 slash_adjust(bp->b_ffname);
3722 if (bp->b_sfname != NULL)
3723 slash_adjust(bp->b_sfname);
3724 }
3725}
3726#endif
3727
3728/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003729 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 * Also save the local window option values.
3731 */
3732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003733buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003735 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736}
3737
3738/*
3739 * Return TRUE if 'ffname' is not the same file as current file.
3740 * Fname must have a full path (expanded by mch_FullName()).
3741 */
3742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003743otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 return otherfile_buf(curbuf, ffname
3746#ifdef UNIX
3747 , NULL
3748#endif
3749 );
3750}
3751
3752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003753otherfile_buf(
3754 buf_T *buf,
3755 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003757 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003761 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3763 return TRUE;
3764 if (fnamecmp(ffname, buf->b_ffname) == 0)
3765 return FALSE;
3766#ifdef UNIX
3767 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003768 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaarc667da52019-11-30 20:52:27 +01003770 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (stp == NULL)
3772 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003773 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 st.st_dev = (dev_T)-1;
3775 stp = &st;
3776 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003777 // Use dev/ino to check if the files are the same, even when the names
3778 // are different (possible with links). Still need to compare the
3779 // name above, for when the file doesn't exist yet.
3780 // Problem: The dev/ino changes when a file is deleted (and created
3781 // again) and remains the same when renamed/moved. We don't want to
3782 // mch_stat() each buffer each time, that would be too slow. Get the
3783 // dev/ino again when they appear to match, but not when they appear
3784 // to be different: Could skip a buffer when it's actually the same
3785 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (buf_same_ino(buf, stp))
3787 {
3788 buf_setino(buf);
3789 if (buf_same_ino(buf, stp))
3790 return FALSE;
3791 }
3792 }
3793#endif
3794 return TRUE;
3795}
3796
3797#if defined(UNIX) || defined(PROTO)
3798/*
3799 * Set inode and device number for a buffer.
3800 * Must always be called when b_fname is changed!.
3801 */
3802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003803buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003805 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
3807 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3808 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003809 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 buf->b_dev = st.st_dev;
3811 buf->b_ino = st.st_ino;
3812 }
3813 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003814 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815}
3816
3817/*
3818 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3819 */
3820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003821buf_same_ino(
3822 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003823 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003825 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 && stp->st_dev == buf->b_dev
3827 && stp->st_ino == buf->b_ino);
3828}
3829#endif
3830
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831/*
3832 * Print info about the current buffer.
3833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003836 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003837 int shorthelp,
3838 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 char_u *name;
3841 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003842 char *p;
3843 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003844 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003846 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (buffer == NULL)
3848 return;
3849
Bram Moolenaarc667da52019-11-30 20:52:27 +01003850 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003852 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 p = buffer + STRLEN(buffer);
3854 }
3855 else
3856 p = buffer;
3857
3858 *p++ = '"';
3859 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003860 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 else
3862 {
3863 if (!fullname && curbuf->b_fname != NULL)
3864 name = curbuf->b_fname;
3865 else
3866 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003867 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 (int)(IOSIZE - (p - buffer)), TRUE);
3869 }
3870
Bram Moolenaar32526b32019-01-19 17:43:09 +01003871 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 curbufIsChanged() ? (shortmess(SHM_MOD)
3873 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003874 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003876 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003877 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003879 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 : _("[readonly]")) : "",
3881 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3882 || curbuf->b_p_ro) ?
3883 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003884 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3885 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (curwin->w_cursor.lnum > 1000000L)
3887 n = (int)(((long)curwin->w_cursor.lnum) /
3888 ((long)curbuf->b_ml.ml_line_count / 100L));
3889 else
3890 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3891 (long)curbuf->b_ml.ml_line_count);
3892 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003893 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003895 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003896 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003897 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3898 curbuf->b_ml.ml_line_count),
3899 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
3901 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003902 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 _("line %ld of %ld --%d%%-- col "),
3904 (long)curwin->w_cursor.lnum,
3905 (long)curbuf->b_ml.ml_line_count,
3906 n);
3907 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003908 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003909 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3911 }
3912
Bram Moolenaar32526b32019-01-19 17:43:09 +01003913 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3914 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 if (dont_truncate)
3917 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003918 // Temporarily set msg_scroll to avoid the message being truncated.
3919 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 msg_start();
3921 n = msg_scroll;
3922 msg_scroll = TRUE;
3923 msg(buffer);
3924 msg_scroll = n;
3925 }
3926 else
3927 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003928 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003930 // Need to repeat the message after redrawing when:
3931 // - When restart_edit is set (otherwise there will be a delay
3932 // before redrawing).
3933 // - When the screen was scrolled but there is no wait-return
3934 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003935 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937
3938 vim_free(buffer);
3939}
3940
3941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003942col_print(
3943 char_u *buf,
3944 size_t buflen,
3945 int col,
3946 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003949 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003951 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952}
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954static char_u *lasttitle = NULL;
3955static char_u *lasticon = NULL;
3956
Bram Moolenaar84a93082018-06-16 22:58:15 +02003957/*
3958 * Put the file name in the title bar and icon of the window.
3959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003961maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003964 char_u *title_str = NULL;
3965 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int maxlen = 0;
3967 int len;
3968 int mustset;
3969 char_u buf[IOSIZE];
3970 int off;
3971
3972 if (!redrawing())
3973 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003974 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 need_maketitle = TRUE;
3976 return;
3977 }
3978
3979 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003980 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003981 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
3983 if (p_title)
3984 {
3985 if (p_titlelen > 0)
3986 {
3987 maxlen = p_titlelen * Columns / 100;
3988 if (maxlen < 10)
3989 maxlen = 10;
3990 }
3991
Bram Moolenaar84a93082018-06-16 22:58:15 +02003992 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (*p_titlestring != NUL)
3994 {
3995#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003996 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003997 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
3998 (char_u *)"titlestring", 0,
3999 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 else
4001#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004002 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004 else
4005 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004006 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
Bram Moolenaar2c666692012-09-05 13:30:40 +02004008#define SPACE_FOR_FNAME (IOSIZE - 100)
4009#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004010#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02004012 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02004013#ifdef FEAT_TERMINAL
4014 else if (curbuf->b_term != NULL)
4015 {
4016 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
4017 SPACE_FOR_FNAME);
4018 }
4019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 else
4021 {
4022 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02004023 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026
Bram Moolenaar21554412017-07-24 21:44:43 +02004027#ifdef FEAT_TERMINAL
4028 if (curbuf->b_term == NULL)
4029#endif
4030 switch (bufIsChanged(curbuf)
4031 + (curbuf->b_p_ro * 2)
4032 + (!curbuf->b_p_ma * 4))
4033 {
4034 case 1: STRCAT(buf, " +"); break;
4035 case 2: STRCAT(buf, " ="); break;
4036 case 3: STRCAT(buf, " =+"); break;
4037 case 4:
4038 case 6: STRCAT(buf, " -"); break;
4039 case 5:
4040 case 7: STRCAT(buf, " -+"); break;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar21554412017-07-24 21:44:43 +02004043 if (curbuf->b_fname != NULL
4044#ifdef FEAT_TERMINAL
4045 && curbuf->b_term == NULL
4046#endif
4047 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004049 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 off = (int)STRLEN(buf);
4051 buf[off++] = ' ';
4052 buf[off++] = '(';
4053 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004054 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004056 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004057 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 off += 2;
4059#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004060 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004061 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004064 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004065 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004066 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004070
Bram Moolenaarc667da52019-11-30 20:52:27 +01004071 // Translate unprintable chars and concatenate. Keep some
4072 // room for the server name. When there is no room (very long
4073 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004074 if (off < SPACE_FOR_DIR)
4075 {
4076 p = transstr(buf + off);
4077 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4078 vim_free(p);
4079 }
4080 else
4081 {
4082 vim_strncpy(buf + off, (char_u *)"...",
4083 (size_t)(SPACE_FOR_ARGNR - off));
4084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 STRCAT(buf, ")");
4086 }
4087
Bram Moolenaar2c666692012-09-05 13:30:40 +02004088 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090#if defined(FEAT_CLIENTSERVER)
4091 if (serverName != NULL)
4092 {
4093 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004094 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 else
4097#endif
4098 STRCAT(buf, " - VIM");
4099
4100 if (maxlen > 0)
4101 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004102 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004103 if (vim_strsize(buf) > maxlen)
4104 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 }
4107 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004108 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 if (p_icon)
4111 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004112 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (*p_iconstring != NUL)
4114 {
4115#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004116 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004117 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4118 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else
4120#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004121 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 else
4124 {
4125 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004126 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004127 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004128 p = gettail(curbuf->b_ffname);
4129 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004130 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004131 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (len > 100)
4133 {
4134 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004136 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004137 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004139 STRCPY(icon_str, p);
4140 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 }
4143
Bram Moolenaar84a93082018-06-16 22:58:15 +02004144 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 if (mustset)
4147 resettitle();
4148}
4149
4150/*
4151 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4152 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004153 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 */
4155 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004156value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
4158 if ((str == NULL) != (*last == NULL)
4159 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4160 {
4161 vim_free(*last);
4162 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004165 mch_restore_title(
4166 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004171 return TRUE;
4172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 return FALSE;
4175}
4176
4177/*
4178 * Put current window title back (used after calling a shell)
4179 */
4180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004181resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 mch_settitle(lasttitle, lasticon);
4184}
Bram Moolenaarea408852005-06-25 22:49:46 +00004185
4186# if defined(EXITFREE) || defined(PROTO)
4187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004188free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004189{
4190 vim_free(lasttitle);
4191 vim_free(lasticon);
4192}
4193# endif
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004196#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004197
4198/*
4199 * Used for building in the status line.
4200 */
4201typedef struct
4202{
4203 char_u *stl_start;
4204 int stl_minwid;
4205 int stl_maxwid;
4206 enum {
4207 Normal,
4208 Empty,
4209 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004210 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004211 Highlight,
4212 TabPage,
4213 Trunc
4214 } stl_type;
4215} stl_item_T;
4216
4217static size_t stl_items_len = 20; // Initial value, grows as needed.
4218static stl_item_T *stl_items = NULL;
4219static int *stl_groupitem = NULL;
4220static stl_hlrec_T *stl_hltab = NULL;
4221static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004222static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004225 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * Return length of string in screen cells.
4227 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004228 * Normally works for window "wp", except when working for 'tabline' then it
4229 * is "curwin".
4230 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004232 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4234 *
4235 * If maxwidth is not zero, the string will be filled at any middle marker
4236 * or truncated if too long, fillchar is used for all whitespace.
4237 */
4238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004239build_stl_str_hl(
4240 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004241 char_u *out, // buffer to write into != NameBuff
4242 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004244 char_u *opt_name, // option name corresponding to "fmt"
4245 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246 int fillchar,
4247 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004248 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4249 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaar10772302019-01-20 18:25:54 +01004251 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004252 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 char_u *p;
4254 char_u *s;
4255 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004256 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004258 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004259 win_T *save_curwin;
4260 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004261 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#endif
4263 int empty_line;
4264 colnr_T virtcol;
4265 long l;
4266 long n;
4267 int prevchar_isflag;
4268 int prevchar_isitem;
4269 int itemisflag;
4270 int fillable;
4271 char_u *str;
4272 long num;
4273 int width;
4274 int itemcnt;
4275 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004276 int group_end_userhl;
4277 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004279#ifdef FEAT_EVAL
4280 int evaldepth;
4281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int minwid;
4283 int maxwid;
4284 int zeropad;
4285 char_u base;
4286 char_u opt;
4287#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004288 char_u buf_tmp[TMPLEN];
4289 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004290 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004291 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004292 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004293 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004294 // TODO: find out why using called_emsg_before makes tests fail, does it
4295 // matter?
4296 // int called_emsg_before = called_emsg;
4297 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004298
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004299 // When inside update_screen() we do not want redrawing a statusline,
4300 // ruler, title, etc. to trigger another redraw, it may cause an endless
4301 // loop.
4302 if (updating_screen)
4303 redraw_not_allowed = TRUE;
4304
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004305 if (stl_items == NULL)
4306 {
4307 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4308 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004309
4310 // Allocate one more, because the last element is used to indicate the
4311 // end of the list.
4312 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4313 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004314
4315 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004316 }
4317
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004318#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004319 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4320 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4321
4322 // When the format starts with "%!" then evaluate it as an expression and
4323 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004324 if (fmt[0] == '%' && fmt[1] == '!')
4325 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004326 typval_T tv;
4327
4328 tv.v_type = VAR_NUMBER;
4329 tv.vval.v_number = wp->w_id;
4330 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4331
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004332 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004333 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004334 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004335
4336 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004337 }
4338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 if (fillchar == 0)
4341 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004342
Bram Moolenaar10772302019-01-20 18:25:54 +01004343 // The cursor in windows other than the current one isn't always
4344 // up-to-date, esp. because of autocommands and timers.
4345 lnum = wp->w_cursor.lnum;
4346 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4347 {
4348 lnum = wp->w_buffer->b_ml.ml_line_count;
4349 wp->w_cursor.lnum = lnum;
4350 }
4351
4352 // Get line & check if empty (cursorpos will show "0-1"). Note that
4353 // p will become invalid when getting another buffer line.
4354 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004355 empty_line = (*p == NUL);
4356
Bram Moolenaar10772302019-01-20 18:25:54 +01004357 // Get the byte value now, in case we need it below. This is more efficient
4358 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004359 len = ml_get_buf_len(wp->w_buffer, lnum);
4360 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004361 {
4362 // Line may have changed since checking the cursor column, or the lnum
4363 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004364 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004365 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004366 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004367 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004368 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004369 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004372#ifdef FEAT_EVAL
4373 evaldepth = 0;
4374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 p = out;
4376 curitem = 0;
4377 prevchar_isflag = TRUE;
4378 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004379 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004381 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004382 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004383 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004384
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004385 stl_item_T *new_items =
4386 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004387 if (new_items == NULL)
4388 break;
4389 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004390
4391 int *new_groupitem =
4392 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004393 if (new_groupitem == NULL)
4394 break;
4395 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004396
4397 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004398 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004399 if (new_hlrec == NULL)
4400 break;
4401 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004402 new_hlrec = vim_realloc(stl_tabtab,
4403 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004404 if (new_hlrec == NULL)
4405 break;
4406 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004407
4408 int *new_separator_locs = vim_realloc(stl_separator_locations,
4409 sizeof(int) * new_len);
4410 if (new_separator_locs == NULL)
4411 break;
4412 stl_separator_locations = new_separator_locs;;
4413
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004414 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004415 }
4416
zeertzjq122dea72022-07-27 15:48:45 +01004417 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 prevchar_isflag = prevchar_isitem = FALSE;
4419
4420 /*
4421 * Handle up to the next '%' or the end.
4422 */
4423 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4424 *p++ = *s++;
4425 if (*s == NUL || p + 1 >= out + outlen)
4426 break;
4427
4428 /*
4429 * Handle one '%' item.
4430 */
4431 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004432 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (*s == '%')
4435 {
4436 if (p + 1 >= out + outlen)
4437 break;
4438 *p++ = *s++;
4439 prevchar_isflag = prevchar_isitem = FALSE;
4440 continue;
4441 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004442 // STL_SEPARATE: Separation between items, filled with white space.
4443 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
4445 s++;
4446 if (groupdepth > 0)
4447 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004448 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004449 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 continue;
4451 }
4452 if (*s == STL_TRUNCMARK)
4453 {
4454 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 stl_items[curitem].stl_type = Trunc;
4456 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 continue;
4458 }
4459 if (*s == ')')
4460 {
4461 s++;
4462 if (groupdepth < 1)
4463 continue;
4464 groupdepth--;
4465
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004466 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 *p = NUL;
4468 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004469 if (curitem > stl_groupitem[groupdepth] + 1
4470 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004472 // remove group if all items are empty and highlight group
4473 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004474 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004475 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004476 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004477 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004478 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004479 group_start_userhl = group_end_userhl =
4480 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004481 break;
4482 }
4483 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004484 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004485 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004486 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004488 if (stl_items[n].stl_type == Highlight)
4489 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004490 }
4491 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004493 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 p = t;
4495 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004496 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004497 {
4498 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004499 if (stl_items[n].stl_type == Highlight)
4500 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004501 // adjust the start position of TabPage to the next
4502 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004503 if (stl_items[n].stl_type == TabPage)
4504 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004508 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004510 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 if (has_mbyte)
4512 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004513 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004515 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004518 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 }
4521 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004522 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4523 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004526 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004528
4529 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004530 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004531 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaarc667da52019-11-30 20:52:27 +01004533 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004534 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
LemonBoy57ff5262022-05-09 21:03:47 +01004536 // Minus one for the leading '<' added above.
4537 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004538 if (stl_items[l].stl_start < t)
4539 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004542 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004544 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004545 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (n < 0)
4547 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004548 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 n = 0 - n;
4550 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004551 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553 else
4554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004555 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004556 l = (n - l) * MB_CHAR2LEN(fillchar);
4557 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004559 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004561 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4562 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004564 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 }
4567 continue;
4568 }
4569 minwid = 0;
4570 maxwid = 9999;
4571 zeropad = FALSE;
4572 l = 1;
4573 if (*s == '0')
4574 {
4575 s++;
4576 zeropad = TRUE;
4577 }
4578 if (*s == '-')
4579 {
4580 s++;
4581 l = -1;
4582 }
4583 if (VIM_ISDIGIT(*s))
4584 {
4585 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004586 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 minwid = 0;
4588 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004589 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004591 stl_items[curitem].stl_type = Highlight;
4592 stl_items[curitem].stl_start = p;
4593 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 s++;
4595 curitem++;
4596 continue;
4597 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004598 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4599 {
4600 if (*s == STL_TABCLOSENR)
4601 {
4602 if (minwid == 0)
4603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004604 // %X ends the close label, go back to the previously
4605 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004606 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004607 if (stl_items[n].stl_type == TabPage
4608 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004609 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004610 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004611 break;
4612 }
4613 }
4614 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004615 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004616 minwid = - minwid;
4617 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004618 stl_items[curitem].stl_type = TabPage;
4619 stl_items[curitem].stl_start = p;
4620 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004621 s++;
4622 curitem++;
4623 continue;
4624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (*s == '.')
4626 {
4627 s++;
4628 if (VIM_ISDIGIT(*s))
4629 {
4630 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004631 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 maxwid = 50;
4633 }
4634 }
4635 minwid = (minwid > 50 ? 50 : minwid) * l;
4636 if (*s == '(')
4637 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004638 stl_groupitem[groupdepth++] = curitem;
4639 stl_items[curitem].stl_type = Group;
4640 stl_items[curitem].stl_start = p;
4641 stl_items[curitem].stl_minwid = minwid;
4642 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 s++;
4644 curitem++;
4645 continue;
4646 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004647#ifdef FEAT_EVAL
4648 // Denotes end of expanded %{} block
4649 if (*s == '}' && evaldepth > 0)
4650 {
4651 s++;
4652 evaldepth--;
4653 continue;
4654 }
4655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (vim_strchr(STL_ALL, *s) == NULL)
4657 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004658 if (*s == NUL) // can happen with "%0"
4659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 s++;
4661 continue;
4662 }
4663 opt = *s++;
4664
Bram Moolenaarc667da52019-11-30 20:52:27 +01004665 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 base = 'D';
4667 itemisflag = FALSE;
4668 fillable = TRUE;
4669 num = -1;
4670 str = NULL;
4671 switch (opt)
4672 {
4673 case STL_FILEPATH:
4674 case STL_FULLPATH:
4675 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004676 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004678 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 else
4680 {
4681 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004682 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4684 }
4685 trans_characters(NameBuff, MAXPATHL);
4686 if (opt != STL_FILENAME)
4687 str = NameBuff;
4688 else
4689 str = gettail(NameBuff);
4690 break;
4691
Bram Moolenaarc667da52019-11-30 20:52:27 +01004692 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004693 {
4694#ifdef FEAT_EVAL
4695 char_u *block_start = s - 1;
4696#endif
4697 int reevaluate = (*s == '%');
4698
4699 if (reevaluate)
4700 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 itemisflag = TRUE;
4702 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004703 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4704 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004706 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 break;
4708 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004709 if (reevaluate)
4710 p[-1] = 0; // remove the % at the end of %{% expr %}
4711 else
4712 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004715 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4716 "%d", curbuf->b_fnum);
4717 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4718 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4719 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004721 save_curbuf = curbuf;
4722 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004723 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 curwin = wp;
4725 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004726 // Visual mode is only valid in the current window.
4727 if (curwin != save_curwin)
4728 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004730 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004732 curwin = save_curwin;
4733 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004734 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004735 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004736 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 if (str != NULL && *str != 0)
4739 {
4740 if (*skipdigits(str) == NUL)
4741 {
4742 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004743 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 itemisflag = FALSE;
4745 }
4746 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004747
4748 // If the output of the expression needs to be evaluated
4749 // replace the %{} block with the result of evaluation
4750 if (reevaluate && str != NULL && *str != 0
4751 && strchr((const char *)str, '%') != NULL
4752 && evaldepth < MAX_STL_EVAL_DEPTH)
4753 {
4754 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4755 size_t str_length = strlen((const char *)str);
4756 size_t fmt_length = strlen((const char *)s);
4757 size_t new_fmt_len = parsed_usefmt
4758 + str_length + fmt_length + 3;
4759 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4760 char_u *new_fmt_p = new_fmt;
4761
4762 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4763 + parsed_usefmt;
4764 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4765 + str_length;
4766 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4767 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4768 + fmt_length;
4769 *new_fmt_p = 0;
4770 new_fmt_p = NULL;
4771
4772 if (usefmt != fmt)
4773 vim_free(usefmt);
4774 VIM_CLEAR(str);
4775 usefmt = new_fmt;
4776 s = usefmt + parsed_usefmt;
4777 evaldepth++;
4778 continue;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#endif
4781 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 case STL_LINE:
4784 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4785 ? 0L : (long)(wp->w_cursor.lnum);
4786 break;
4787
4788 case STL_NUMLINES:
4789 num = wp->w_buffer->b_ml.ml_line_count;
4790 break;
4791
4792 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004793 num = (State & MODE_INSERT) == 0 && empty_line
4794 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 break;
4796
4797 case STL_VIRTCOL:
4798 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004799 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004800 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004802 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4803 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 break;
4805 num = (long)virtcol;
4806 break;
4807
4808 case STL_PERCENTAGE:
4809 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4810 (long)wp->w_buffer->b_ml.ml_line_count);
4811 break;
4812
4813 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004814 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004815 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 break;
4817
Luuk van Baalba936f62022-12-15 13:15:39 +00004818 case STL_SHOWCMD:
4819 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4820 str = showcmd_buf;
4821 break;
4822
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 case STL_ARGLISTSTAT:
4824 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004825 buf_tmp[0] = 0;
4826 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4827 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 break;
4829
4830 case STL_KEYMAP:
4831 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004832 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4833 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 break;
4835 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004836#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004837 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#else
4839 num = 0;
4840#endif
4841 break;
4842
4843 case STL_BUFNO:
4844 num = wp->w_buffer->b_fnum;
4845 break;
4846
4847 case STL_OFFSET_X:
4848 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004849 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 case STL_OFFSET:
4851#ifdef FEAT_BYTEOFF
4852 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004853 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4854 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4855 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856#endif
4857 break;
4858
4859 case STL_BYTEVAL_X:
4860 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004861 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004863 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (num == NL)
4865 num = 0;
4866 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4867 num = NL;
4868 break;
4869
4870 case STL_ROFLAG:
4871 case STL_ROFLAG_ALT:
4872 itemisflag = TRUE;
4873 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004874 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 break;
4876
4877 case STL_HELPFLAG:
4878 case STL_HELPFLAG_ALT:
4879 itemisflag = TRUE;
4880 if (wp->w_buffer->b_help)
4881 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004882 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 break;
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 case STL_FILETYPE:
4886 if (*wp->w_buffer->b_p_ft != NUL
4887 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4888 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004889 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004890 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004891 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 break;
4894
4895 case STL_FILETYPE_ALT:
4896 itemisflag = TRUE;
4897 if (*wp->w_buffer->b_p_ft != NUL
4898 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4899 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004900 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004901 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004902 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004904 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaar4033c552017-09-16 20:54:51 +02004908#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 case STL_PREVIEWFLAG:
4910 case STL_PREVIEWFLAG_ALT:
4911 itemisflag = TRUE;
4912 if (wp->w_p_pvw)
4913 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4914 : _("[Preview]"));
4915 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004916
4917 case STL_QUICKFIX:
4918 if (bt_quickfix(wp->w_buffer))
4919 str = (char_u *)(wp->w_llist_ref
4920 ? _(msg_loclist)
4921 : _(msg_qflist));
4922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923#endif
4924
4925 case STL_MODIFIED:
4926 case STL_MODIFIED_ALT:
4927 itemisflag = TRUE;
4928 switch ((opt == STL_MODIFIED_ALT)
4929 + bufIsChanged(wp->w_buffer) * 2
4930 + (!wp->w_buffer->b_p_ma) * 4)
4931 {
4932 case 2: str = (char_u *)"[+]"; break;
4933 case 3: str = (char_u *)",+"; break;
4934 case 4: str = (char_u *)"[-]"; break;
4935 case 5: str = (char_u *)",-"; break;
4936 case 6: str = (char_u *)"[+-]"; break;
4937 case 7: str = (char_u *)",+-"; break;
4938 }
4939 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004940
4941 case STL_HIGHLIGHT:
4942 t = s;
4943 while (*s != '#' && *s != NUL)
4944 ++s;
4945 if (*s == '#')
4946 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004947 stl_items[curitem].stl_type = Highlight;
4948 stl_items[curitem].stl_start = p;
4949 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004950 curitem++;
4951 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004952 if (*s != NUL)
4953 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004954 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004957 stl_items[curitem].stl_start = p;
4958 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (str != NULL && *str)
4960 {
4961 t = str;
4962 if (itemisflag)
4963 {
4964 if ((t[0] && t[1])
4965 && ((!prevchar_isitem && *t == ',')
4966 || (prevchar_isflag && *t == ' ')))
4967 t++;
4968 prevchar_isflag = TRUE;
4969 }
4970 l = vim_strsize(t);
4971 if (l > 0)
4972 prevchar_isitem = TRUE;
4973 if (l > maxwid)
4974 {
4975 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (has_mbyte)
4977 {
4978 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004979 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 l -= byte2cells(*t++);
4983 if (p + 1 >= out + outlen)
4984 break;
4985 *p++ = '<';
4986 }
4987 if (minwid > 0)
4988 {
4989 for (; l < minwid && p + 1 < out + outlen; l++)
4990 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004991 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4993 *p++ = ' ';
4994 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004995 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 minwid = 0;
4998 }
4999 else
5000 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005001 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005003 // Change a space by fillchar, unless fillchar is '-' and a
5004 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005005 if (fillable && *t == ' '
5006 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5007 MB_CHAR2BYTES(fillchar, p);
5008 else
5009 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005012 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else if (num >= 0)
5015 {
5016 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5017 char_u nstr[20];
5018
5019 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005020 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 prevchar_isitem = TRUE;
5022 t = nstr;
5023 if (opt == STL_VIRTCOL_ALT)
5024 {
5025 *t++ = '-';
5026 minwid--;
5027 }
5028 *t++ = '%';
5029 if (zeropad)
5030 *t++ = '0';
5031 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005032 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *t = 0;
5034
5035 for (n = num, l = 1; n >= nbase; n /= nbase)
5036 l++;
5037 if (opt == STL_VIRTCOL_ALT)
5038 l++;
5039 if (l > maxwid)
5040 {
5041 l += 2;
5042 n = l - maxwid;
5043 while (l-- > maxwid)
5044 num /= nbase;
5045 *t++ = '>';
5046 *t++ = '%';
5047 *t = t[-3];
5048 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005049 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5050 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005053 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5054 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 p += STRLEN(p);
5056 }
5057 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005058 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005060 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5061 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5062 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (opt == STL_VIM_EXPR)
5064 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 curitem++;
5066 }
5067 *p = NUL;
5068 itemcnt = curitem;
5069
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005070#ifdef FEAT_EVAL
5071 if (usefmt != fmt)
5072 vim_free(usefmt);
5073#endif
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 width = vim_strsize(out);
5076 if (maxwidth > 0 && width > maxwidth)
5077 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005078 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 l = 0;
5080 if (itemcnt == 0)
5081 s = out;
5082 else
5083 {
5084 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005085 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005087 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005088 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090 }
5091 if (l == itemcnt)
5092 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005093 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005094 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 l = 0;
5096 }
5097 }
5098
5099 if (width - vim_strsize(s) >= maxwidth)
5100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005101 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (has_mbyte)
5103 {
5104 s = out;
5105 width = 0;
5106 for (;;)
5107 {
5108 width += ptr2cells(s);
5109 if (width >= maxwidth)
5110 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005111 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005113 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005115 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 s = out + maxwidth - 1;
5119 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005120 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 break;
5122 itemcnt = l;
5123 *s++ = '>';
5124 *s = 0;
5125 }
5126 else
5127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (has_mbyte)
5129 {
5130 n = 0;
5131 while (width >= maxwidth)
5132 {
5133 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005134 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 }
5137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 n = width - maxwidth + 1;
5139 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005140 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 *s = '<';
5142
Bram Moolenaarc667da52019-11-30 20:52:27 +01005143 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 for (; l < itemcnt; l++)
5145 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005146 if (stl_items[l].stl_start - n >= s)
5147 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005149 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
zeertzjqd392a742023-07-01 20:24:40 +01005151
5152 // Fill up for half a double-wide character.
5153 while (++width < maxwidth)
5154 {
5155 s = s + STRLEN(s);
5156 MB_CHAR2BYTES(fillchar, s);
5157 *s = NUL;
5158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 width = maxwidth;
5161 }
5162 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5163 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005164 // Find how many separators there are, which we will use when
5165 // figuring out how many groups there are.
5166 int num_separators = 0;
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005170 if (stl_items[l].stl_type == Separate)
5171 {
5172 // Create an array of the start location for each separator
5173 // mark.
5174 stl_separator_locations[num_separators] = l;
5175 num_separators++;
5176 }
5177 }
5178
5179 // If we have separated groups, then we deal with it now
5180 if (num_separators)
5181 {
5182 int standard_spaces;
5183 int final_spaces;
5184
5185 standard_spaces = (maxwidth - width) / num_separators;
5186 final_spaces = (maxwidth - width) -
5187 standard_spaces * (num_separators - 1);
5188 for (l = 0; l < num_separators; l++)
5189 {
5190 int dislocation = (l == (num_separators - 1)) ?
5191 final_spaces : standard_spaces;
5192 dislocation *= MB_CHAR2LEN(fillchar);
5193 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5194 char_u *seploc = start + dislocation;
5195 STRMOVE(seploc, start);
5196 for (s = start; s < seploc;)
5197 MB_CHAR2BYTES(fillchar, s);
5198
5199 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5200 stl_items[i].stl_start += dislocation;
5201 }
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 width = maxwidth;
5204 }
5205 }
5206
Bram Moolenaarc667da52019-11-30 20:52:27 +01005207 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005208 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005210 *hltab = stl_hltab;
5211 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 for (l = 0; l < itemcnt; l++)
5213 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005214 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005216 sp->start = stl_items[l].stl_start;
5217 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005218 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005221 sp->start = NULL;
5222 sp->userhl = 0;
5223 }
5224
Bram Moolenaarc667da52019-11-30 20:52:27 +01005225 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005226 if (tabtab != NULL)
5227 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005228 *tabtab = stl_tabtab;
5229 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005230 for (l = 0; l < itemcnt; l++)
5231 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005232 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005233 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005234 sp->start = stl_items[l].stl_start;
5235 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005236 sp++;
5237 }
5238 }
5239 sp->start = NULL;
5240 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005243 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005244
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005245 // A user function may reset KeyTyped, restore it.
5246 KeyTyped = save_KeyTyped;
5247
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005248 // Check for an error. If there is one the display will be messed up and
5249 // might loop redrawing. Avoid that by making the corresponding option
5250 // empty.
5251 // TODO: find out why using called_emsg_before makes tests fail, does it
5252 // matter?
5253 // if (called_emsg > called_emsg_before)
5254 if (did_emsg > did_emsg_before)
5255 set_string_option_direct(opt_name, -1, (char_u *)"",
5256 OPT_FREE | opt_scope, SID_ERROR);
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 return width;
5259}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005260#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005263 * Get relative cursor position in window into "buf[buflen]", in the localized
5264 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 */
5266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005267get_rel_pos(
5268 win_T *wp,
5269 char_u *buf,
5270 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005272 long above; // number of lines above window
5273 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaarc667da52019-11-30 20:52:27 +01005275 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005276 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 above = wp->w_topline - 1;
5278#ifdef FEAT_DIFF
5279 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005280 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005281 above = 0; // All buffer lines are displayed and there is an
5282 // indication of filler lines, that can be considered
5283 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284#endif
5285 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5286 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005287 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
Emir SARI971cd2b2023-04-29 12:09:53 +01005288 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005290 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005292 {
5293 int perc = (above > 1000000L)
5294 ? (int)(above / ((above + below) / 100L))
5295 : (int)(above * 100L / (above + below));
5296
5297 char *p = (char *)buf;
5298 size_t l = buflen;
5299 if (perc < 10)
5300 {
5301 // prepend one space
5302 buf[0] = ' ';
5303 ++p;
5304 --l;
5305 }
5306 // localized percentage value
5307 vim_snprintf(p, l, _("%d%%"), perc);
5308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
5311/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005312 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 * Return TRUE if it was appended.
5314 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005316append_arg_number(
5317 win_T *wp,
5318 char_u *buf,
5319 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005320 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005322 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 return FALSE;
5324
Bram Moolenaara8490a42023-05-23 18:00:58 +01005325 char *msg;
5326 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005328 case 0: msg = _(" (%d of %d)"); break;
5329 case 1: msg = _(" ((%d) of %d)"); break;
5330 case 2: msg = _(" (file %d of %d)"); break;
5331 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005333
5334 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5335 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5336 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 return TRUE;
5338}
5339
5340/*
5341 * If fname is not a full path, make it a full path.
5342 * Returns pointer to allocated memory (NULL for failure).
5343 */
5344 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005345fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 /*
5348 * Force expanding the path always for Unix, because symbolic links may
5349 * mess up the full path name, even though it starts with a '/'.
5350 * Also expand when there is ".." in the file name, try to remove it,
5351 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005352 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 * For MS-Windows also expand names like "longna~1" to "longname".
5354 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005355#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 return FullName_save(fname, TRUE);
5357#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005358 if (!vim_isAbsName(fname)
5359 || strstr((char *)fname, "..") != NULL
5360 || strstr((char *)fname, "//") != NULL
5361# ifdef BACKSLASH_IN_FILENAME
5362 || strstr((char *)fname, "\\\\") != NULL
5363# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005364# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 )
5368 return FullName_save(fname, FALSE);
5369
5370 fname = vim_strsave(fname);
5371
Bram Moolenaar9b942202007-10-03 12:31:33 +00005372# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005373 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005374 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377 return fname;
5378#endif
5379}
5380
5381/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005382 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5383 * "*ffname" becomes a pointer to allocated memory (or NULL).
5384 * When resolving a link both "*sfname" and "*ffname" will point to the same
5385 * allocated memory.
5386 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005387 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390fname_expand(
5391 buf_T *buf UNUSED,
5392 char_u **ffname,
5393 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005395 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005397 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005399 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401#ifdef FEAT_SHORTCUT
5402 if (!buf->b_p_bin)
5403 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005404 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005406 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005407 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005408 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 vim_free(*ffname);
5411 *ffname = rfname;
5412 *sfname = rfname;
5413 }
5414 }
5415#endif
5416}
5417
5418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 * Open a window for a number of buffers.
5420 */
5421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 buf_T *buf;
5425 win_T *wp, *wpnext;
5426 int split_ret = OK;
5427 int p_ea_save;
5428 int open_wins = 0;
5429 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005430 int count; // Maximum number of windows to open.
5431 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005432 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005433 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
Bram Moolenaarc667da52019-11-30 20:52:27 +01005435 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 count = 9999;
5437 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005438 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5440 all = FALSE;
5441 else
5442 all = TRUE;
5443
Pavel Mayorove1121b12023-02-20 14:35:20 +00005444 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5445 // switching to another buffer.
5446 reset_VIsual_and_resel();
5447
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 setpcmark();
5449
5450#ifdef FEAT_GUI
5451 need_mouse_correct = TRUE;
5452#endif
5453
5454 /*
5455 * Close superfluous windows (two windows for the same buffer).
5456 * Also close windows that are not full-width.
5457 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005458 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005459 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005460 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005462 tpnext = curtab->tp_next;
5463 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005465 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005466 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005467 || ((cmdmod.cmod_split & WSP_VERT)
5468 ? wp->w_height + wp->w_status_height < Rows - p_ch
5469 - tabline_height()
5470 : wp->w_width != Columns)
5471 || (had_tab > 0 && wp != firstwin))
5472 && !ONE_WINDOW
5473 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5474 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005475 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005476 if (win_close(wp, FALSE) == FAIL)
5477 break;
5478 // Just in case an autocommand does something strange with
5479 // windows: start all over...
5480 wpnext = firstwin;
5481 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005482 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005483 }
5484 else
5485 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005487
Bram Moolenaarc667da52019-11-30 20:52:27 +01005488 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005489 if (had_tab == 0 || tpnext == NULL)
5490 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005491 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493
5494 /*
5495 * Go through the buffer list. When a buffer doesn't have a window yet,
5496 * open one. Otherwise move the window to the right position.
5497 * Watch out for autocommands that delete buffers or windows!
5498 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005499 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5504 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005505 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5507 continue;
5508
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005509 if (had_tab != 0)
5510 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005511 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005512 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005513 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005514 else
5515 wp = NULL;
5516 }
5517 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005518 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005519 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005520 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005521 if (wp->w_buffer == buf)
5522 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005523 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005524 if (wp != NULL)
5525 win_move_after(wp, curwin);
5526 }
5527
5528 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005530 bufref_T bufref;
5531
5532 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005533
Bram Moolenaarc667da52019-11-30 20:52:27 +01005534 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005536 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5538 ++open_wins;
5539 p_ea = p_ea_save;
5540 if (split_ret == FAIL)
5541 continue;
5542
Bram Moolenaarc667da52019-11-30 20:52:27 +01005543 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005546 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005548 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 break;
5551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (swap_exists_action == SEA_QUIT)
5553 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005554#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005555 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005556
Bram Moolenaarc667da52019-11-30 20:52:27 +01005557 // Reset the error/interrupt/exception state here so that
5558 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005559 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005560#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005561
Bram Moolenaarc667da52019-11-30 20:52:27 +01005562 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 win_close(curwin, TRUE);
5564 --open_wins;
5565 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005566 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005567
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005568#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005569 // Restore the error/interrupt/exception state if not
5570 // discarded by a new aborting error, interrupt, or uncaught
5571 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005572 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else
5576 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578
5579 ui_breakcheck();
5580 if (got_int)
5581 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005582 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 break;
5584 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005585#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005586 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005587 if (aborting())
5588 break;
5589#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005590 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005591 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005592 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005595 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598 /*
5599 * Close superfluous windows.
5600 */
5601 for (wp = lastwin; open_wins > count; )
5602 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005603 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (!win_valid(wp))
5606 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005607 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 wp = lastwin;
5609 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005610 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005612 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 --open_wins;
5614 wp = lastwin;
5615 }
5616 else
5617 {
5618 wp = wp->w_prev;
5619 if (wp == NULL)
5620 break;
5621 }
5622 }
5623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005626static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628/*
5629 * do_modelines() - process mode lines for the current file
5630 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005631 * "flags" can be:
5632 * OPT_WINONLY only set options local to window
5633 * OPT_NOWIN don't set options local to window
5634 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 * Returns immediately if the "ml" option isn't set.
5636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005638do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005640 linenr_T lnum;
5641 int nmlines;
5642 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
5644 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5645 return;
5646
Bram Moolenaarc667da52019-11-30 20:52:27 +01005647 // Disallow recursive entry here. Can happen when executing a modeline
5648 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (entered)
5650 return;
5651
5652 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005653 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005655 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 nmlines = 0;
5657
Hu Jialun9dcd3492021-08-28 20:42:50 +02005658 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005660 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 nmlines = 0;
5662 --entered;
5663}
5664
Bram Moolenaarc667da52019-11-30 20:52:27 +01005665#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667/*
5668 * chk_modeline() - check a single line for a mode string
5669 * Return FAIL if an error encountered.
5670 */
5671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005672chk_modeline(
5673 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005674 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 char_u *s;
5677 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005678 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 int prev;
5680 int vers;
5681 int end;
5682 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005683 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005684 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 prev = -1;
5687 for (s = ml_get(lnum); *s != NUL; ++s)
5688 {
5689 if (prev == -1 || vim_isspace(prev))
5690 {
5691 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5692 || STRNCMP(s, "vi:", (size_t)3) == 0)
5693 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005694 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005695 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5698 e = s + 4;
5699 else
5700 e = s + 3;
5701 vers = getdigits(&e);
5702 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005703 && (s[0] != 'V'
5704 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005706 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 || (VIM_VERSION_100 < vers && s[3] == '<')
5708 || (VIM_VERSION_100 > vers && s[3] == '>')
5709 || (VIM_VERSION_100 == vers && s[3] == '=')))
5710 break;
5711 }
5712 }
5713 prev = *s;
5714 }
5715
5716 if (*s)
5717 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005718 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 ++s;
5720 while (s[-1] != ':');
5721
Bram Moolenaarc667da52019-11-30 20:52:27 +01005722 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (linecopy == NULL)
5724 return FAIL;
5725
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005726 // prepare for emsg()
5727 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005728 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730 end = FALSE;
5731 while (end == FALSE)
5732 {
5733 s = skipwhite(s);
5734 if (*s == NUL)
5735 break;
5736
5737 /*
5738 * Find end of set command: ':' or end of line.
5739 * Skip over "\:", replacing it with ":".
5740 */
5741 for (e = s; *e != ':' && *e != NUL; ++e)
5742 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005743 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if (*e == NUL)
5745 end = TRUE;
5746
5747 /*
5748 * If there is a "set" command, require a terminating ':' and
5749 * ignore the stuff after the ':'.
5750 * "vi:set opt opt opt: foo" -- foo not interpreted
5751 * "vi:opt opt opt: foo" -- foo interpreted
5752 * Accept "se" for compatibility with Elvis.
5753 */
5754 if (STRNCMP(s, "set ", (size_t)4) == 0
5755 || STRNCMP(s, "se ", (size_t)3) == 0)
5756 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005757 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 break;
5759 end = TRUE;
5760 s = vim_strchr(s, ' ') + 1;
5761 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005762 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaarc667da52019-11-30 20:52:27 +01005764 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005766 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005767
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005768 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005769 current_sctx.sc_version = 1;
5770#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005771 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005772 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005773 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005775
Bram Moolenaar5958f952018-11-20 04:25:21 +01005776 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005777 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005778
Bram Moolenaara3227e22006-03-08 21:32:40 +00005779 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005780
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005781 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005782 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005783 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 break;
5785 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005786 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
5788
ichizok7e5fe382023-04-15 13:17:50 +01005789 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005790 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 vim_free(linecopy);
5792 }
5793 return retval;
5794}
5795
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005796/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005797 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5798 */
5799 int
5800bt_normal(buf_T *buf)
5801{
5802 return buf != NULL && buf->b_p_bt[0] == NUL;
5803}
5804
5805/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005806 * Return TRUE if "buf" is the quickfix buffer.
5807 */
5808 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005809bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005810{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005811#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005812 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005813#else
5814 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005815#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005816}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005817
5818/*
5819 * Return TRUE if "buf" is a terminal buffer.
5820 */
5821 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005822bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005823{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005824#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005825 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005826#else
5827 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005828#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005829}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005830
5831/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005832 * Return TRUE if "buf" is a help buffer.
5833 */
5834 int
5835bt_help(buf_T *buf)
5836{
5837 return buf != NULL && buf->b_help;
5838}
5839
5840/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005841 * Return TRUE if "buf" is a prompt buffer.
5842 */
5843 int
5844bt_prompt(buf_T *buf)
5845{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005846 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5847}
5848
Dominique Pelle748b3082022-01-08 12:41:16 +00005849#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005850/*
5851 * Return TRUE if "buf" is a buffer for a popup window.
5852 */
5853 int
5854bt_popup(buf_T *buf)
5855{
5856 return buf != NULL && buf->b_p_bt != NULL
5857 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005858}
Dominique Pelle748b3082022-01-08 12:41:16 +00005859#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005860
5861/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005862 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005863 * buffer. This means the buffer name may not be a file name, at least not for
5864 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005865 */
5866 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005867bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005868{
5869 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5870 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005871 || buf->b_p_bt[0] == 't'
5872 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005873}
5874
Bram Moolenaarc3126192022-08-26 12:58:17 +01005875/*
5876 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5877 * buffer. This means the buffer is not to be read from a file.
5878 */
5879 static int
5880bt_nofileread(buf_T *buf)
5881{
5882 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5883 || buf->b_p_bt[0] == 't'
5884 || buf->b_p_bt[0] == 'q'
5885 || buf->b_p_bt[0] == 'p');
5886}
5887
Dominique Pelle748b3082022-01-08 12:41:16 +00005888#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005889/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005890 * Return TRUE if "buf" has 'buftype' set to "nofile".
5891 */
5892 int
5893bt_nofile(buf_T *buf)
5894{
5895 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5896}
Dominique Pelle748b3082022-01-08 12:41:16 +00005897#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005898
5899/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005900 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5901 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005902 */
5903 int
5904bt_dontwrite(buf_T *buf)
5905{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005906 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005907 || buf->b_p_bt[0] == 't'
5908 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005909}
5910
5911 int
5912bt_dontwrite_msg(buf_T *buf)
5913{
5914 if (bt_dontwrite(buf))
5915 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005916 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005917 return TRUE;
5918 }
5919 return FALSE;
5920}
5921
5922/*
5923 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5924 * and 'bufhidden'.
5925 */
5926 int
5927buf_hide(buf_T *buf)
5928{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005929 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005930 switch (buf->b_p_bh[0])
5931 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005932 case 'u': // "unload"
5933 case 'w': // "wipe"
5934 case 'd': return FALSE; // "delete"
5935 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005936 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005937 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005938}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
5940/*
5941 * Return special buffer name.
5942 * Returns NULL when the buffer has a normal file name.
5943 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005944 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005945buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005947#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005949 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005950 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005951 * Differentiate between the quickfix and location list buffers using
5952 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005953 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005954 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005955 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005956 else
5957 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005960
Bram Moolenaarc667da52019-11-30 20:52:27 +01005961 // There is no _file_ when 'buftype' is "nofile", b_sfname
5962 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005963 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005965#ifdef FEAT_TERMINAL
5966 if (buf->b_term != NULL)
5967 return term_get_status_text(buf->b_term);
5968#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005969 if (buf->b_fname != NULL)
5970 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005971 if (buf == cmdwin_buf)
5972 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005973#ifdef FEAT_JOB_CHANNEL
5974 if (bt_prompt(buf))
5975 return (char_u *)_("[Prompt]");
5976#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005977#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005978 if (bt_popup(buf))
5979 return (char_u *)_("[Popup]");
5980#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005981 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005985 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 return NULL;
5987}
5988
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005990 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5991 */
5992 char_u *
5993buf_get_fname(buf_T *buf)
5994{
5995 if (buf->b_fname == NULL)
5996 return (char_u *)_("[No Name]");
5997 return buf->b_fname;
5998}
5999
6000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6002 */
6003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006006 if (on == curbuf->b_p_bl)
6007 return;
6008
6009 curbuf->b_p_bl = on;
6010 if (on)
6011 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6012 else
6013 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014}
6015
6016/*
6017 * Read the file for "buf" again and check if the contents changed.
6018 * Return TRUE if it changed or this could not be checked.
6019 */
6020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006021buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 buf_T *newbuf;
6024 int differ = TRUE;
6025 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 exarg_T ea;
6028
Bram Moolenaarc667da52019-11-30 20:52:27 +01006029 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6031 if (newbuf == NULL)
6032 return TRUE;
6033
Bram Moolenaarc667da52019-11-30 20:52:27 +01006034 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (prep_exarg(&ea, buf) == FAIL)
6036 {
6037 wipe_buffer(newbuf, FALSE);
6038 return TRUE;
6039 }
6040
Bram Moolenaare76062c2022-11-28 18:51:43 +00006041 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006043 if (curbuf != newbuf)
6044 {
6045 // Failed to find a window for "newbuf".
6046 wipe_buffer(newbuf, FALSE);
6047 return TRUE;
6048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006050 // We don't want to trigger autocommands now, they may have nasty
6051 // side-effects like wiping buffers
6052 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006053 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 && readfile(buf->b_ffname, buf->b_fname,
6055 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6056 &ea, READ_NEW | READ_DUMMY) == OK)
6057 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006058 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6060 {
6061 differ = FALSE;
6062 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6063 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6064 {
6065 differ = TRUE;
6066 break;
6067 }
6068 }
6069 }
6070 vim_free(ea.cmd);
6071
Bram Moolenaarc667da52019-11-30 20:52:27 +01006072 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
Bram Moolenaarc667da52019-11-30 20:52:27 +01006075 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 wipe_buffer(newbuf, FALSE);
6077
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006078 unblock_autocmds();
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 return differ;
6081}
6082
6083/*
6084 * Wipe out a buffer and decrement the last buffer number if it was used for
6085 * this buffer. Call this to wipe out a temp buffer that does not contain any
6086 * marks.
6087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006089wipe_buffer(
6090 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006091 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 if (buf->b_fnum == top_file_num - 1)
6094 --top_file_num;
6095
Bram Moolenaarc667da52019-11-30 20:52:27 +01006096 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006097 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006098
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006099 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006100
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006102 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103}