blob: 90be301e857083f0f51d319e2627a1ddddf1980b [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
Christian Brabandt51b62382024-10-06 17:31:10 +0200500 int
501buf_locked(buf_T *buf)
502{
503 return buf->b_locked || buf->b_locked_split;
504}
505
Bram Moolenaar480778b2016-07-14 22:09:39 +0200506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 * Close the link to a buffer.
508 * "action" is used when there is no longer a window for the buffer.
509 * It can be:
510 * 0 buffer becomes hidden
511 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100512 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200514 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * When doing all but the first one on the current buffer, the caller should
516 * get a new buffer very soon!
517 *
518 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100519 *
520 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
521 * cause there to be only one window with this buffer. e.g. when ":quit" is
522 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100523 *
524 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100525 *
526 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100529close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100530 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100531 buf_T *buf,
532 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100533 int abort_if_last,
534 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100537 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200538 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100539 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200540 win_T *the_curwin = curwin;
541 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200543 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
544 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
Bram Moolenaarcee52202020-03-11 14:19:58 +0100546 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100547
548 // Force unloading or deleting when 'bufhidden' says so.
549 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
550 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100551 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200552 {
553 del_buf = TRUE;
554 unload_buf = TRUE;
555 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100556 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200557 {
558 del_buf = TRUE;
559 unload_buf = TRUE;
560 wipe_buf = TRUE;
561 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100562 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200563 unload_buf = TRUE;
564
Bram Moolenaar94053a52017-08-01 21:44:33 +0200565#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100566 // depending on how we get here b_nwindows may already be zero
567 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200568 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100569 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200570 if (term_job_running(buf->b_term))
571 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200572 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200573 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200574 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100575 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200576
Bram Moolenaarc667da52019-11-30 20:52:27 +0100577 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200578 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100579
580 // A terminal buffer is wiped out when job has finished.
581 del_buf = TRUE;
582 unload_buf = TRUE;
583 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200584 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200585 else
586 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100587 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200588 del_buf = FALSE;
589 unload_buf = FALSE;
590 }
591 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100592 else if (buf->b_p_bh[0] == 'h' && !del_buf)
593 {
594 // Hide a terminal buffer.
595 unload_buf = FALSE;
596 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200597 else
598 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100599 if (del_buf || unload_buf)
600 {
601 // A terminal buffer is wiped out if the job has finished.
602 // We only do this when there's an intention to unload the
603 // buffer. This way, :hide and other similar commands won't
604 // wipe the buffer.
605 del_buf = TRUE;
606 unload_buf = TRUE;
607 wipe_buf = TRUE;
608 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200609 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100610 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200611 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 // Disallow deleting the buffer when it is locked (already being closed or
615 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200616 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100617 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200618
Bram Moolenaarc667da52019-11-30 20:52:27 +0100619 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200620 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100622 // Set b_last_cursor when closing the last window for the buffer.
623 // Remember the last cursor position and window options of the buffer.
624 // This used to be only for the current window, but then options like
625 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (buf->b_nwindows == 1)
627 set_last_cursor(win);
628 buflist_setfpos(buf, win,
629 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
630 win->w_cursor.col, TRUE);
631 }
632
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200633 set_bufref(&bufref, buf);
634
Bram Moolenaarc667da52019-11-30 20:52:27 +0100635 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (buf->b_nwindows == 1)
637 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200638 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100639 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200640 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
641 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200642 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100643 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100644 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200645aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000646 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100647 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100648 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200649 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100650 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200651 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100652 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200653 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaarc667da52019-11-30 20:52:27 +0100655 // When the buffer becomes hidden, but is not unloaded, trigger
656 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (!unload_buf)
658 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200659 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100660 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200661 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
662 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200663 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100664 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200665 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200666 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100667 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200668 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100669 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200670 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100672#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100673 // autocmds may abort script processing
674 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100675 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200678
Bram Moolenaarc667da52019-11-30 20:52:27 +0100679 // If the buffer was in curwin and the window has changed, go back to that
680 // window, if it still exists. This avoids that ":edit x" triggering a
681 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200682 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
683 {
684 block_autocmds();
685 goto_tabpage_win(the_curtab, the_curwin);
686 unblock_autocmds();
687 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
Bram Moolenaarc667da52019-11-30 20:52:27 +0100691 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 if (buf->b_nwindows > 0)
693 --buf->b_nwindows;
694
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100695#ifdef FEAT_DIFF
696 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100697 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100698#endif
699
Bram Moolenaarc667da52019-11-30 20:52:27 +0100700 // Return when a window is displaying the buffer or when it's not
701 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100703 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaarc667da52019-11-30 20:52:27 +0100705 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (buf->b_ffname == NULL)
707 del_buf = TRUE;
708
Bram Moolenaarc667da52019-11-30 20:52:27 +0100709 // When closing the current buffer stop Visual mode before freeing
710 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200711 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200712#if defined(EXITFREE)
713 && !entered_free_all_mem
714#endif
715 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200716 end_visual_mode();
717
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100718 // Free all things allocated for this buffer.
719 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
720 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100721 // Remember if we are closing the current buffer. Restore the number of
722 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 is_curbuf = (buf == curbuf);
724 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100726 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100727 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100728 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
Bram Moolenaarc667da52019-11-30 20:52:27 +0100730 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200731 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100732 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100733#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100734 // autocmds may abort script processing
735 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100736 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100739 // It's possible that autocommands change curbuf to the one being deleted.
740 // This might cause the previous curbuf to be deleted unexpectedly. But
741 // in some cases it's OK to delete the curbuf, because a new one is
742 // obtained anyway. Therefore only return if curbuf changed to the
743 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100745 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200746
Bram Moolenaar4033c552017-09-16 20:54:51 +0200747 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100748 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200749
Bram Moolenaarc667da52019-11-30 20:52:27 +0100750 // Autocommands may have opened or closed windows for this buffer.
751 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200752 if (buf->b_nwindows > 0)
753 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /*
756 * Remove the buffer from the list.
757 */
758 if (wipe_buf)
759 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200760 tabpage_T *tp;
761 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200762
Bram Moolenaar347538f2022-03-26 16:28:06 +0000763 // Do not wipe out the buffer if it is used in a window.
764 if (buf->b_nwindows > 0)
765 return FALSE;
766
zeertzjq2e7d89b2024-07-10 19:36:36 +0200767 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200768 mark_forget_file(wp, buf->b_fnum);
769
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200770 if (action == DOBUF_WIPE_REUSE)
771 {
772 // we can re-use this buffer number, store it
773 if (buf_reuse.ga_itemsize == 0)
774 ga_init2(&buf_reuse, sizeof(int), 50);
775 if (ga_grow(&buf_reuse, 1) == OK)
776 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
777 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200778 if (buf->b_sfname != buf->b_ffname)
779 VIM_CLEAR(buf->b_sfname);
780 else
781 buf->b_sfname = NULL;
782 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (buf->b_prev == NULL)
784 firstbuf = buf->b_next;
785 else
786 buf->b_prev->b_next = buf->b_next;
787 if (buf->b_next == NULL)
788 lastbuf = buf->b_prev;
789 else
790 buf->b_next->b_prev = buf->b_prev;
791 free_buffer(buf);
792 }
793 else
794 {
795 if (del_buf)
796 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100797 // Free all internal variables and reset option values, to make
798 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 free_buffer_stuff(buf, TRUE);
800
Bram Moolenaarc667da52019-11-30 20:52:27 +0100801 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
803
Bram Moolenaarc667da52019-11-30 20:52:27 +0100804 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 buf->b_p_initialized = FALSE;
806 }
807 buf_clear_file(buf);
808 if (del_buf)
809 buf->b_p_bl = FALSE;
810 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100811 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100812 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813}
814
815/*
816 * Make buffer not contain a file.
817 */
818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100819buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200822 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100824 buf->b_p_eof = FALSE;
825 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 buf->b_p_eol = TRUE;
827 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000829 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100831 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#ifdef FEAT_NETBEANS_INTG
833 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif
835}
836
837/*
838 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200839 * the file. Careful: get here with "curwin" NULL when exiting.
840 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100841 * BFA_DEL buffer is going to be deleted
842 * BFA_WIPE buffer is going to be wiped out
843 * BFA_KEEP_UNDO do not free undo information
844 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200850 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200851 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200852 win_T *the_curwin = curwin;
853 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaarc667da52019-11-30 20:52:27 +0100855 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200856 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100857 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200858 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200859 if (buf->b_ml.ml_mfp != NULL)
860 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200861 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
862 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200863 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100864 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200865 return;
866 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200867 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200869 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
870 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200871 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100872 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 return;
874 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200875 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200877 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
878 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200879 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100880 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 return;
882 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200883 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100884 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200885
Bram Moolenaarc667da52019-11-30 20:52:27 +0100886 // If the buffer was in curwin and the window has changed, go back to that
887 // window, if it still exists. This avoids that ":edit x" triggering a
888 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200889 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
890 {
891 block_autocmds();
892 goto_tabpage_win(the_curtab, the_curwin);
893 unblock_autocmds();
894 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200895
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100896#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100897 // autocmds may abort script processing
898 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100902 // It's possible that autocommands change curbuf to the one being deleted.
903 // This might cause curbuf to be deleted unexpectedly. But in some cases
904 // it's OK to delete the curbuf, because a new one is obtained anyway.
905 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (buf == curbuf && !is_curbuf)
907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200911#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100912 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200913 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100914 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200915#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000916
917#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100918 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000919 {
920 win_T *win;
921 tabpage_T *tp;
922
923 FOR_ALL_TAB_WINDOWS(tp, win)
924 if (win->w_buffer == buf)
925 clearFolding(win);
926 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000927#endif
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929#ifdef FEAT_TCL
930 tcl_buffer_free(buf);
931#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100932 ml_close(buf, TRUE); // close and delete the memline/memfile
933 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200934 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100935 // free the memory allocated for undo
936 // and reset all undo information
937 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100939 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100941#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100942 clear_buf_prop_types(buf);
943#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100944 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945}
946
947/*
948 * Free a buffer structure and the things it contains related to the buffer
949 * itself (not the file, that must have been done already).
950 */
951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100952free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200954 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200956#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100957 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000958 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200959 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200960 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200961#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200962#ifdef FEAT_LUA
963 lua_buffer_free(buf);
964#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000965#ifdef FEAT_MZSCHEME
966 mzscheme_buffer_free(buf);
967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_PERL
969 perl_buf_free(buf);
970#endif
971#ifdef FEAT_PYTHON
972 python_buffer_free(buf);
973#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974#ifdef FEAT_PYTHON3
975 python3_buffer_free(buf);
976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#ifdef FEAT_RUBY
978 ruby_buffer_free(buf);
979#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200980#ifdef FEAT_JOB_CHANNEL
981 channel_buffer_free(buf);
982#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200983#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200984 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200985#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200986#ifdef FEAT_JOB_CHANNEL
987 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200988 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200989 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200990#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200991
992 buf_hashtab_remove(buf);
993
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200994 aubuflocal_remove(buf);
995
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200996 if (autocmd_busy)
997 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100998 // Do not free the buffer structure while autocommands are executing,
999 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001000 buf->b_next = au_pending_free_buf;
1001 au_pending_free_buf = buf;
1002 }
1003 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001004 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001005 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001006 if (curbuf == buf)
1007 curbuf = NULL; // make clear it's not to be used
1008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001012 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001013 */
1014 static void
1015init_changedtick(buf_T *buf)
1016{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001017 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001018
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001019 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1020 di->di_tv.v_type = VAR_NUMBER;
1021 di->di_tv.v_lock = VAR_FIXED;
1022 di->di_tv.vval.v_number = 0;
1023
Bram Moolenaar92769c32017-02-25 15:41:37 +01001024#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001025 STRCPY(buf->b_ct_di.di_key, "changedtick");
1026 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001027#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001028}
1029
1030/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001031 * Free the b_wininfo list for buffer "buf".
1032 */
1033 static void
1034clear_wininfo(buf_T *buf)
1035{
1036 wininfo_T *wip;
1037
1038 while (buf->b_wininfo != NULL)
1039 {
1040 wip = buf->b_wininfo;
1041 buf->b_wininfo = wip->wi_next;
1042 free_wininfo(wip);
1043 }
1044}
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1048 */
1049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050free_buffer_stuff(
1051 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001052 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 if (free_options)
1055 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001056 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001058#ifdef FEAT_SPELL
1059 ga_clear(&buf->b_s.b_langp);
1060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001063 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001064 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001065
Bram Moolenaarc667da52019-11-30 20:52:27 +01001066 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001067 hash_init(&buf->b_vars->dv_hashtab);
1068 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001069 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001070 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001073 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001075 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001077#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001078 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001079#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001080#ifdef FEAT_PROP_POPUP
1081 ga_clear_strings(&buf->b_textprop_text);
1082#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001083 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1084 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001085 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086}
1087
1088/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001089 * Free one wininfo_T.
1090 */
1091 void
1092free_wininfo(wininfo_T *wip)
1093{
1094 if (wip->wi_optset)
1095 {
1096 clear_winopt(&wip->wi_opt);
1097#ifdef FEAT_FOLDING
1098 deleteFoldRecurse(&wip->wi_folds);
1099#endif
1100 }
1101 vim_free(wip);
1102}
1103
1104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * Go to another buffer. Handles the result of the ATTENTION dialog.
1106 */
1107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001108goto_buffer(
1109 exarg_T *eap,
1110 int start,
1111 int dir,
1112 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001114 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001115 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001116 int skip_help_buf;
1117
1118 switch (eap->cmdidx)
1119 {
1120 case CMD_bnext:
1121 case CMD_sbnext:
1122 case CMD_bNext:
1123 case CMD_bprevious:
1124 case CMD_sbNext:
1125 case CMD_sbprevious:
1126 skip_help_buf = TRUE;
1127 break;
1128 default:
1129 skip_help_buf = FALSE;
1130 break;
1131 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001132
1133 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar188639d2022-04-04 16:57:21 +01001135 if (swap_exists_action == SEA_NONE)
1136 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001137 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1138 (eap->forceit ? DOBUF_FORCEIT : 0) |
1139 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1141 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001142#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001143 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001144
Bram Moolenaarc667da52019-11-30 20:52:27 +01001145 // Reset the error/interrupt/exception state here so that
1146 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001147 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001148#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001149
Bram Moolenaarc667da52019-11-30 20:52:27 +01001150 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001152 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001153 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001154
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001155#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001156 // Restore the error/interrupt/exception state if not discarded by a
1157 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001162 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165/*
1166 * Handle the situation of swap_exists_action being set.
1167 * It is allowed for "old_curbuf" to be NULL or invalid.
1168 */
1169 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001170handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001172#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001173 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001174#endif
1175#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001176 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001177#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001178 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (swap_exists_action == SEA_QUIT)
1181 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001182#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001183 // Reset the error/interrupt/exception state here so that
1184 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001185 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001186#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001187
Bram Moolenaarc667da52019-11-30 20:52:27 +01001188 // User selected Quit at ATTENTION prompt. Go back to previous
1189 // buffer. If that buffer is gone or the same as the current one,
1190 // open a new, empty buffer.
1191 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001192 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001193 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001194 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1195 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001196 {
1197 // Block autocommands here because curwin->w_buffer is NULL.
1198 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001199 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001200 unblock_autocmds();
1201 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001202 else
1203 buf = old_curbuf->br_buf;
1204 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001205 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001206 int old_msg_silent = msg_silent;
1207
1208 if (shortmess(SHM_FILEINFO))
1209 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001210 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001211 // restore msg_silent, so that the command line will be shown
1212 msg_silent = old_msg_silent;
1213
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001214#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001215 if (old_tw != curbuf->b_p_tw)
1216 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001217#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001218 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001219 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001220
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001221#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001222 // Restore the error/interrupt/exception state if not discarded by a
1223 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001224 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 else if (swap_exists_action == SEA_RECOVER)
1228 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001229#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001230 // Reset the error/interrupt/exception state here so that
1231 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001232 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001233#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001234
Bram Moolenaarc667da52019-11-30 20:52:27 +01001235 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001237 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001238 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001240 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001241
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001242#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001243 // Restore the error/interrupt/exception state if not discarded by a
1244 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001245 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248 swap_exists_action = SEA_NONE;
1249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001252 * Make the current buffer empty.
1253 * Used when it is wiped out and it's the last buffer.
1254 */
1255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256empty_curbuf(
1257 int close_others,
1258 int forceit,
1259 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001260{
1261 int retval;
1262 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001263 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001264
1265 if (action == DOBUF_UNLOAD)
1266 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001267 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001268 return FAIL;
1269 }
1270
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001271 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001272 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001273 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001274 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275
1276 setpcmark();
1277 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1278 forceit ? ECMD_FORCEIT : 0, curwin);
1279
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001280 // do_ecmd() may create a new buffer, then we have to delete
1281 // the old one. But do_ecmd() may have done that already, check
1282 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001283 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001284 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001285 if (!close_others)
1286 need_fileinfo = FALSE;
1287 return retval;
1288}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290/*
1291 * Implementation of the commands for the buffer list.
1292 *
1293 * action == DOBUF_GOTO go to specified buffer
1294 * action == DOBUF_SPLIT split window and go to specified buffer
1295 * action == DOBUF_UNLOAD unload specified buffer(s)
1296 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1297 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001298 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 *
1300 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1301 * start == DOBUF_FIRST go to "count" buffer from first buffer
1302 * start == DOBUF_LAST go to "count" buffer from last buffer
1303 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1304 *
1305 * Return FAIL or OK.
1306 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001307 static int
1308do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309 int action,
1310 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001311 int dir, // FORWARD or BACKWARD
1312 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001313 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 buf_T *buf;
1316 buf_T *bp;
1317 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001318 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320 switch (start)
1321 {
1322 case DOBUF_FIRST: buf = firstbuf; break;
1323 case DOBUF_LAST: buf = lastbuf; break;
1324 default: buf = curbuf; break;
1325 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001326 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
1328 while (count-- > 0)
1329 {
1330 do
1331 {
1332 buf = buf->b_next;
1333 if (buf == NULL)
1334 buf = firstbuf;
1335 }
1336 while (buf != curbuf && !bufIsChanged(buf));
1337 }
1338 if (!bufIsChanged(buf))
1339 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001340 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 return FAIL;
1342 }
1343 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001344 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
1346 while (buf != NULL && buf->b_fnum != count)
1347 buf = buf->b_next;
1348 }
1349 else
1350 {
1351 bp = NULL;
1352 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1353 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 // remember the buffer where we start, we come back there when all
1355 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (bp == NULL)
1357 bp = buf;
1358 if (dir == FORWARD)
1359 {
1360 buf = buf->b_next;
1361 if (buf == NULL)
1362 buf = firstbuf;
1363 }
1364 else
1365 {
1366 buf = buf->b_prev;
1367 if (buf == NULL)
1368 buf = lastbuf;
1369 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001370 // Don't count unlisted buffers.
1371 // Avoid non-help buffers if the starting point was a non-help buffer and
1372 // vice-versa.
1373 if (unload || (buf->b_p_bl
1374 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001377 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379 if (bp == buf)
1380 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001381 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001382 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 return FAIL;
1384 }
1385 }
1386 }
1387
Bram Moolenaarc667da52019-11-30 20:52:27 +01001388 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 if (start == DOBUF_FIRST)
1391 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001392 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001394 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001397 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001399 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return FAIL;
1401 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001402#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001403 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001404 return OK;
1405#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001406 if (
1407 action == DOBUF_GOTO
1408 && buf != curbuf
1409 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1410 // disallow navigating to another buffer when 'winfixbuf' is applied
1411 return FAIL;
1412
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001413 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1414 && (buf->b_flags & BF_DUMMY))
1415 {
1416 // disallow navigating to the dummy buffer
1417 semsg(_(e_buffer_nr_does_not_exist), count);
1418 return FAIL;
1419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421#ifdef FEAT_GUI
1422 need_mouse_correct = TRUE;
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001426 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 */
1428 if (unload)
1429 {
1430 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001431 bufref_T bufref;
1432
Bram Moolenaar94f01952018-09-01 15:30:03 +02001433 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001434 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001435
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001436 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaarc667da52019-11-30 20:52:27 +01001438 // When unloading or deleting a buffer that's already unloaded and
1439 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001440 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1441 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 return FAIL;
1443
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001444 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001446#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001447 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001449# ifdef FEAT_TERMINAL
1450 if (term_job_running(buf->b_term))
1451 {
1452 if (term_confirm_stop(buf) == FAIL)
1453 return FAIL;
1454 }
1455 else
1456# endif
1457 {
1458 dialog_changed(buf, FALSE);
1459 if (!bufref_valid(&bufref))
1460 // Autocommand deleted buffer, oops! It's not changed
1461 // now.
1462 return FAIL;
1463 // If it's still changed fail silently, the dialog already
1464 // mentioned why it fails.
1465 if (bufIsChanged(buf))
1466 return FAIL;
1467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001469 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001472 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 return FAIL;
1474 }
1475 }
1476
Bram Moolenaarc667da52019-11-30 20:52:27 +01001477 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001478 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001479 end_visual_mode();
1480
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001481 // If deleting the last (listed) buffer, make it empty.
1482 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001483 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (bp->b_p_bl && bp != buf)
1485 break;
1486 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001487 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001489 // If the deleted buffer is the current one, close the current window
1490 // (unless it's the only window). Repeat this so long as we end up in
1491 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001492 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001493 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001494 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001495 {
1496 if (win_close(curwin, FALSE) == FAIL)
1497 break;
1498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001500 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (buf != curbuf)
1502 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001503 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001504 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001505 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return OK;
1507 }
1508
1509 /*
1510 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001511 * There should be another, otherwise it would have been handled
1512 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001513 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * Then prefer the buffer we most recently visited.
1515 * Else try to find one that is loaded, after the current buffer,
1516 * then before the current buffer.
1517 * Finally use any buffer.
1518 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001519 buf = NULL; // selected buffer
1520 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001521 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1522 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001523 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 int jumpidx;
1526
1527 jumpidx = curwin->w_jumplistidx - 1;
1528 if (jumpidx < 0)
1529 jumpidx = curwin->w_jumplistlen - 1;
1530
1531 forward = jumpidx;
1532 while (jumpidx != curwin->w_jumplistidx)
1533 {
1534 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1535 if (buf != NULL)
1536 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001537 // Skip current and unlisted bufs. Also skip a quickfix
1538 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001539 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001540 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 else if (buf->b_ml.ml_mfp == NULL)
1542 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001543 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (bp == NULL)
1545 bp = buf;
1546 buf = NULL;
1547 }
1548 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001549 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001551 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1553 break;
1554 if (--jumpidx < 0)
1555 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001556 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 break;
1558 }
1559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaarc667da52019-11-30 20:52:27 +01001561 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
1563 forward = TRUE;
1564 buf = curbuf->b_next;
1565 for (;;)
1566 {
1567 if (buf == NULL)
1568 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001569 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 break;
1571 buf = curbuf->b_prev;
1572 forward = FALSE;
1573 continue;
1574 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001575 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001576 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001577 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001579 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001581 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 bp = buf;
1583 }
1584 if (forward)
1585 buf = buf->b_next;
1586 else
1587 buf = buf->b_prev;
1588 }
1589 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001590 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001592 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001594 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001595 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 break;
1597 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001598 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 if (curbuf->b_next != NULL)
1601 buf = curbuf->b_next;
1602 else
1603 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001604 if (bt_quickfix(buf))
1605 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608
Bram Moolenaara02471e2014-01-10 16:43:14 +01001609 if (buf == NULL)
1610 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001611 // Autocommands must have wiped out all other buffers. Only option
1612 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001613 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001614 }
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001617 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001619 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001621 // If 'switchbuf' is set jump to the window containing "buf".
1622 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001623 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 return FAIL;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaarc667da52019-11-30 20:52:27 +01001629 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (buf == curbuf)
1631 return OK;
1632
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001633 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001634 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001637 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001639# ifdef FEAT_TERMINAL
1640 if (term_job_running(curbuf->b_term))
1641 {
1642 if (term_confirm_stop(curbuf) == FAIL)
1643 return FAIL;
1644 // Manually kill the terminal here because this command will
1645 // hide it otherwise.
1646 free_terminal(curbuf);
1647 }
1648 else
1649# endif
1650 {
1651 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001652
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001653 set_bufref(&bufref, buf);
1654 dialog_changed(curbuf, FALSE);
1655 if (!bufref_valid(&bufref))
1656 // Autocommand deleted buffer, oops!
1657 return FAIL;
1658
1659 if (bufIsChanged(curbuf))
1660 {
1661 no_write_message();
1662 return FAIL;
1663 }
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
1668 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001669 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 return FAIL;
1671 }
1672 }
1673
Bram Moolenaarc667da52019-11-30 20:52:27 +01001674 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 set_curbuf(buf, action);
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001678 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001680#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001681 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 return FAIL;
1683#endif
1684
1685 return OK;
1686}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001688 int
1689do_buffer(
1690 int action,
1691 int start,
1692 int dir, // FORWARD or BACKWARD
1693 int count, // buffer number or number of buffers
1694 int forceit) // TRUE when using !
1695{
1696 return do_buffer_ext(action, start, dir, count,
1697 forceit ? DOBUF_FORCEIT : 0);
1698}
1699
1700/*
1701 * do_bufdel() - delete or unload buffer(s)
1702 *
1703 * addr_count == 0: ":bdel" - delete current buffer
1704 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1705 * buffer "end_bnr", then any other arguments.
1706 * addr_count == 2: ":N,N bdel" - delete buffers in range
1707 *
1708 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1709 * DOBUF_DEL (":bdel")
1710 *
1711 * Returns error message or NULL
1712 */
1713 char *
1714do_bufdel(
1715 int command,
1716 char_u *arg, // pointer to extra arguments
1717 int addr_count,
1718 int start_bnr, // first buffer number in a range
1719 int end_bnr, // buffer nr or last buffer nr in a range
1720 int forceit)
1721{
1722 int do_current = 0; // delete current buffer?
1723 int deleted = 0; // number of buffers deleted
1724 char *errormsg = NULL; // return value
1725 int bnr; // buffer number
1726 char_u *p;
1727
1728 if (addr_count == 0)
1729 {
1730 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1731 }
1732 else
1733 {
1734 if (addr_count == 2)
1735 {
1736 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001737 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001738 bnr = start_bnr;
1739 }
1740 else // addr_count == 1
1741 bnr = end_bnr;
1742
1743 for ( ;!got_int; ui_breakcheck())
1744 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001745 // Delete the current buffer last, otherwise when the
1746 // current buffer is deleted, the next buffer becomes
1747 // the current one and will be loaded, which may then
1748 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001749 if (bnr == curbuf->b_fnum)
1750 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001751 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001752 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1753 ++deleted;
1754
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001755 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001756 if (addr_count == 2)
1757 {
1758 if (++bnr > end_bnr)
1759 break;
1760 }
1761 else // addr_count == 1
1762 {
1763 arg = skipwhite(arg);
1764 if (*arg == NUL)
1765 break;
1766 if (!VIM_ISDIGIT(*arg))
1767 {
1768 p = skiptowhite_esc(arg);
1769 bnr = buflist_findpat(arg, p,
1770 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1771 FALSE, FALSE);
1772 if (bnr < 0) // failed
1773 break;
1774 arg = p;
1775 }
1776 else
1777 bnr = getdigits(&arg);
1778 }
1779 }
1780 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1781 FORWARD, do_current, forceit) == OK)
1782 ++deleted;
1783
1784 if (deleted == 0)
1785 {
1786 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001787 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001788 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001789 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001790 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001791 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001792 errormsg = (char *)IObuff;
1793 }
1794 else if (deleted >= p_report)
1795 {
1796 if (command == DOBUF_UNLOAD)
1797 smsg(NGETTEXT("%d buffer unloaded",
1798 "%d buffers unloaded", deleted), deleted);
1799 else if (command == DOBUF_DEL)
1800 smsg(NGETTEXT("%d buffer deleted",
1801 "%d buffers deleted", deleted), deleted);
1802 else
1803 smsg(NGETTEXT("%d buffer wiped out",
1804 "%d buffers wiped out", deleted), deleted);
1805 }
1806 }
1807
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001808 return errormsg;
1809}
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
1812 * Set current buffer to "buf". Executes autocommands and closes current
1813 * buffer. "action" tells how to close the current buffer:
1814 * DOBUF_GOTO free or hide it
1815 * DOBUF_SPLIT nothing
1816 * DOBUF_UNLOAD unload it
1817 * DOBUF_DEL delete it
1818 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001819 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 buf_T *prevbuf;
1825 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001826 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001827#ifdef FEAT_SYN_HL
1828 long old_tw = curbuf->b_p_tw;
1829#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001830 bufref_T newbufref;
1831 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001832 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001833 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001836 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001837 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1838 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaarc667da52019-11-30 20:52:27 +01001840 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001845 set_bufref(&prevbufref, prevbuf);
1846 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001848 // Autocommands may delete the current buffer and/or the buffer we want to
1849 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001850 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001851 || (bufref_valid(&prevbufref)
1852 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001853#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001854 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001856 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001858#ifdef FEAT_SYN_HL
1859 if (prevbuf == curwin->w_buffer)
1860 reset_synblock(curwin);
1861#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001862 // autocommands may have opened a new window
1863 // with prevbuf, grr
1864 if (unload ||
1865 (last_winid != get_last_winid() &&
1866 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001867 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001868#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001869 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001871 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001873 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001874 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001875
Bram Moolenaare615db02022-01-20 21:00:54 +00001876 // Do not sync when in Insert mode and the buffer is open in
1877 // another window, might be a timer doing something in another
1878 // window.
1879 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001880 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001881 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1883 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001884 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001885 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1886 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001887 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001888 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001889 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001892 // An autocommand may have deleted "buf", already entered it (e.g., when
1893 // it did ":bunload") or aborted the script processing.
1894 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001895 valid = buf_valid(buf);
1896 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001897#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001898 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001900 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001901 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001902 // autocommands changed curbuf and we will move to another
1903 // buffer soon, so decrement curbuf->b_nwindows
1904 if (curbuf != NULL && prevbuf != curbuf)
1905 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001906 // If the buffer is not valid but curwin->w_buffer is NULL we must
1907 // enter some buffer. Using the last one is hopefully OK.
1908 if (!valid)
1909 enter_buffer(lastbuf);
1910 else
1911 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001912#ifdef FEAT_SYN_HL
1913 if (old_tw != curbuf->b_p_tw)
1914 check_colorcolumn(curwin);
1915#endif
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917}
1918
1919/*
1920 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001921 * Old curbuf must have been abandoned already! This also means "curbuf" may
1922 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001925enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001927 // when closing the current buffer stop Visual mode
1928 if (VIsual_active
1929#if defined(EXITFREE)
1930 && !entered_free_all_mem
1931#endif
1932 )
1933 end_visual_mode();
1934
Bram Moolenaar010ee962019-09-25 20:37:36 +02001935 // Get the buffer in the current window.
1936 curwin->w_buffer = buf;
1937 curbuf = buf;
1938 ++curbuf->b_nwindows;
1939
1940 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1942 if (!buf->b_help)
1943 get_winopts(buf);
1944#ifdef FEAT_FOLDING
1945 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001946 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001948 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#endif
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001952 if (curwin->w_p_diff)
1953 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
1955
Bram Moolenaara971b822011-09-14 14:43:25 +02001956#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001957 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001958#endif
1959
Bram Moolenaarc667da52019-11-30 20:52:27 +01001960 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 curwin->w_cursor.lnum = 1;
1962 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001965 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
Bram Moolenaarc667da52019-11-30 20:52:27 +01001967 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001968 curwin->w_valid = 0;
1969
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001970 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1971 curbuf->b_last_cursor.col, TRUE);
1972
Bram Moolenaarc667da52019-11-30 20:52:27 +01001973 // Make sure the buffer is loaded.
1974 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001975 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001976 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001977 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001978 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001979 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02001980 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001981
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001982 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 else
1985 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001986 if (!msg_silent && !shortmess(SHM_FILEINFO))
1987 need_fileinfo = TRUE; // display file info after redraw
1988
1989 // check if file changed
1990 (void)buf_check_timestamp(curbuf, FALSE);
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001993#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1997 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999
Bram Moolenaarc667da52019-11-30 20:52:27 +01002000 // If autocommands did not change the cursor position, restore cursor lnum
2001 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (curwin->w_cursor.lnum == 1 && inindent(0))
2003 buflist_getfpos();
2004
Bram Moolenaarc667da52019-11-30 20:52:27 +01002005 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002007 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002008 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002009 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar009b2592004-10-24 19:18:58 +00002011#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002012 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002013 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002014#endif
2015
Bram Moolenaarc667da52019-11-30 20:52:27 +01002016 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002017 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019#ifdef FEAT_KEYMAP
2020 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002021 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002023#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002024 // May need to set the spell language. Can only do this after the buffer
2025 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002026 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002027 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002028#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002029#ifdef FEAT_VIMINFO
2030 curbuf->b_last_used = vim_time();
2031#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002032
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002033 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034}
2035
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002036#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2037/*
2038 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002039 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002040 */
2041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002042do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002043{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002044 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002045 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002046 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002047 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002048 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002049 last_chdir_reason = "autochdir";
2050 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002051}
2052#endif
2053
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002054 static void
2055no_write_message_buf(buf_T *buf UNUSED)
2056{
2057#ifdef FEAT_TERMINAL
2058 if (term_job_running(buf->b_term))
2059 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2060 else
2061#endif
2062 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2063 buf->b_fnum);
2064}
2065
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002066 void
2067no_write_message(void)
2068{
2069#ifdef FEAT_TERMINAL
2070 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002071 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002072 else
2073#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002074 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002075}
2076
2077 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002078no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002079{
2080#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002081 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002082 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002083 else
2084#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002085 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002086}
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088/*
2089 * functions for dealing with the buffer list
2090 */
2091
2092/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002093 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2094 * only one window. That means it can be re-used.
2095 */
2096 int
2097curbuf_reusable(void)
2098{
2099 return (curbuf != NULL
2100 && curbuf->b_ffname == NULL
2101 && curbuf->b_nwindows <= 1
2102 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002103 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002104 && !curbufIsChanged());
2105}
2106
2107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 * Add a file name to the buffer list. Return a pointer to the buffer.
2109 * If the same file name already exists return a pointer to that buffer.
2110 * If it does not exist, or if fname == NULL, a new entry is created.
2111 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2112 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2113 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002114 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002115 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2116 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002117 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 * This is the ONLY way to create a new buffer.
2119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002121buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002122 char_u *ffname_arg, // full path of fname or relative
2123 char_u *sfname_arg, // short fname or NULL
2124 linenr_T lnum, // preferred cursor line
2125 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002127 char_u *ffname = ffname_arg;
2128 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 buf_T *buf;
2130#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002131 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133
Bram Moolenaar480778b2016-07-14 22:09:39 +02002134 if (top_file_num == 1)
2135 hash_init(&buf_hashtab);
2136
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002137 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002140 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 */
2142#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002143 // On Unix we can use inode numbers when the file exists. Works better
2144 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2146 st.st_dev = (dev_T)-1;
2147#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002148 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149#ifdef UNIX
2150 buflist_findname_stat(ffname, &st)
2151#else
2152 buflist_findname(ffname)
2153#endif
2154 ) != NULL)
2155 {
2156 vim_free(ffname);
2157 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002158 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2159 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002160
2161 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002162 // copy the options now, if 'cpo' doesn't have 's' and not done
2163 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002164 buf_copy_options(buf, 0);
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2167 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002168 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002171 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002173 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002174 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002175 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002176 return NULL;
2177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179 return buf;
2180 }
2181
2182 /*
2183 * If the current buffer has no name and no contents, use the current
2184 * buffer. Otherwise: Need to allocate a new buffer structure.
2185 *
2186 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002187 * (A spell file buffer is allocated in spell.c, but that's not a normal
2188 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 */
2190 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002191 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002194 // It's like this buffer is deleted. Watch out for autocommands that
2195 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002196 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2197 if (buf != curbuf) // autocommands deleted the buffer!
2198 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002199#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002200 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002201 {
2202 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 if (buf != curbuf || curbuf == NULL)
2208 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002209 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (buf == NULL)
2211 {
2212 vim_free(ffname);
2213 return NULL;
2214 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002215#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002216 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002217 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002218 if (buf->b_vars == NULL)
2219 {
2220 vim_free(ffname);
2221 vim_free(buf);
2222 return NULL;
2223 }
2224 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2225#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002226 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228
2229 if (ffname != NULL)
2230 {
2231 buf->b_ffname = ffname;
2232 buf->b_sfname = vim_strsave(sfname);
2233 }
2234
2235 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002236 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2239 || buf->b_wininfo == NULL)
2240 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002241 if (buf->b_sfname != buf->b_ffname)
2242 VIM_CLEAR(buf->b_sfname);
2243 else
2244 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002245 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (buf != curbuf)
2247 free_buffer(buf);
2248 return NULL;
2249 }
2250
2251 if (buf == curbuf)
2252 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002253 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002254
Bram Moolenaarc667da52019-11-30 20:52:27 +01002255 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002256 buf->b_p_initialized = FALSE;
2257 buf_copy_options(buf, BCO_ENTER);
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002260 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 curbuf->b_kmap_state |= KEYMAP_INIT;
2262#endif
2263 }
2264 else
2265 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002266 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002268 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
2270 buf->b_prev = NULL;
2271 firstbuf = buf;
2272 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002273 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 lastbuf->b_next = buf;
2276 buf->b_prev = lastbuf;
2277 }
2278 lastbuf = buf;
2279
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002280 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2281 {
2282 // Recycle a previously used buffer number. Used for buffers which
2283 // are normally hidden, e.g. in a popup window. Avoids that the
2284 // buffer number grows rapidly.
2285 --buf_reuse.ga_len;
2286 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002287
2288 // Move buffer to the right place in the buffer list.
2289 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2290 {
2291 buf_T *prev = buf->b_prev;
2292
2293 prev->b_next = buf->b_next;
2294 if (prev->b_next != NULL)
2295 prev->b_next->b_prev = prev;
2296 buf->b_next = prev;
2297 buf->b_prev = prev->b_prev;
2298 if (buf->b_prev != NULL)
2299 buf->b_prev->b_next = buf;
2300 prev->b_prev = buf;
2301 if (lastbuf == buf)
2302 lastbuf = prev;
2303 if (firstbuf == prev)
2304 firstbuf = buf;
2305 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002306 }
2307 else
2308 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002309 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002311 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002312 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002315 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 top_file_num = 1;
2318 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002319 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002321 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 buf_copy_options(buf, BCO_ALWAYS);
2323 }
2324
2325 buf->b_wininfo->wi_fpos.lnum = lnum;
2326 buf->b_wininfo->wi_win = curwin;
2327
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002328#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002329 hash_init(&buf->b_s.b_keywtab);
2330 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332
2333 buf->b_fname = buf->b_sfname;
2334#ifdef UNIX
2335 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002336 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 else
2338 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002339 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 buf->b_dev = st.st_dev;
2341 buf->b_ino = st.st_ino;
2342 }
2343#endif
2344 buf->b_u_synced = TRUE;
2345 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002346 if (flags & BLN_DUMMY)
2347 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002349 clrallmarks(buf); // clear marks
2350 fmarks_check_names(buf); // check file marks for this file
2351 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (!(flags & BLN_DUMMY))
2353 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002354 bufref_T bufref;
2355
Bram Moolenaarc667da52019-11-30 20:52:27 +01002356 // Tricky: these autocommands may change the buffer list. They could
2357 // also split the window with re-using the one empty buffer. This may
2358 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002359 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002360 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002361 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002362 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002364 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002365 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002366 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002367 return NULL;
2368 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002369#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002370 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 return buf;
2376}
2377
2378/*
2379 * Free the memory for the options of a buffer.
2380 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2381 * 'fileencoding'.
2382 */
2383 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002384free_buf_options(
2385 buf_T *buf,
2386 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 if (free_p_ff)
2389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 clear_string_option(&buf->b_p_bh);
2393 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395#ifdef FEAT_FIND_ID
2396 clear_string_option(&buf->b_p_def);
2397 clear_string_option(&buf->b_p_inc);
2398# ifdef FEAT_EVAL
2399 clear_string_option(&buf->b_p_inex);
2400# endif
2401#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002402#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 clear_string_option(&buf->b_p_inde);
2404 clear_string_option(&buf->b_p_indk);
2405#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002406#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2407 clear_string_option(&buf->b_p_bexpr);
2408#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002409#if defined(FEAT_CRYPT)
2410 clear_string_option(&buf->b_p_cm);
2411#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002412 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002413#if defined(FEAT_EVAL)
2414 clear_string_option(&buf->b_p_fex);
2415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002417# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002418 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2419 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002420 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 clear_string_option(&buf->b_p_key);
2423#endif
2424 clear_string_option(&buf->b_p_kp);
2425 clear_string_option(&buf->b_p_mps);
2426 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002427 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002429#ifdef FEAT_VARTABS
2430 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002431 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002432 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002433 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002434 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#ifdef FEAT_KEYMAP
2437 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002438 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 ga_clear(&buf->b_kmap_ga);
2440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442#ifdef FEAT_FOLDING
2443 clear_string_option(&buf->b_p_cms);
2444#endif
2445 clear_string_option(&buf->b_p_nf);
2446#ifdef FEAT_SYN_HL
2447 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002448 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002449#endif
2450#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002451 clear_string_option(&buf->b_s.b_p_spc);
2452 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002453 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002454 buf->b_s.b_cap_prog = NULL;
2455 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002456 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 clear_string_option(&buf->b_p_cink);
2461 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002462 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002463 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002465 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002467#ifdef FEAT_COMPL_FUNC
2468 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002469 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002470 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002471 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002472 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002473 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_QUICKFIX
2476 clear_string_option(&buf->b_p_gp);
2477 clear_string_option(&buf->b_p_mp);
2478 clear_string_option(&buf->b_p_efm);
2479#endif
2480 clear_string_option(&buf->b_p_ep);
2481 clear_string_option(&buf->b_p_path);
2482 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002483 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002484#ifdef FEAT_EVAL
2485 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002486 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 clear_string_option(&buf->b_p_dict);
2489 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002490 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002492 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002493 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002494 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002495 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496}
2497
2498/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002499 * Get alternate file "n".
2500 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2501 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 * if (options & GETF_SETMARK) call setpcmark()
2503 * if (options & GETF_ALT) we are jumping to an alternate file.
2504 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2505 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002506 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002509buflist_getfile(
2510 int n,
2511 linenr_T lnum,
2512 int options,
2513 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 pos_T *fpos;
2518 colnr_T col;
2519
2520 buf = buflist_findnr(n);
2521 if (buf == NULL)
2522 {
2523 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002524 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002526 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 return FAIL;
2528 }
2529
Bram Moolenaarc667da52019-11-30 20:52:27 +01002530 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (buf == curbuf)
2532 return OK;
2533
Bram Moolenaar71223e22022-05-30 15:23:09 +01002534 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002535 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaarc667da52019-11-30 20:52:27 +01002537 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (lnum == 0)
2539 {
2540 fpos = buflist_findfpos(buf);
2541 lnum = fpos->lnum;
2542 col = fpos->col;
2543 }
2544 else
2545 col = 0;
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (options & GETF_SWITCH)
2548 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002549 // If 'switchbuf' is set jump to the window containing "buf".
2550 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002551
Bram Moolenaarc667da52019-11-30 20:52:27 +01002552 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2553 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002554 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002555 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002557 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002558 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002559 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2560 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002562 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002567 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002568 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2569 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002571 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (!p_sol && col != 0)
2573 {
2574 curwin->w_cursor.col = col;
2575 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 curwin->w_set_curswant = TRUE;
2578 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002579 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002581
2582 if (RedrawingDisabled > 0)
2583 --RedrawingDisabled;
2584 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585}
2586
2587/*
2588 * go to the last know line number for the current buffer
2589 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002591buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 pos_T *fpos;
2594
2595 fpos = buflist_findfpos(curbuf);
2596
2597 curwin->w_cursor.lnum = fpos->lnum;
2598 check_cursor_lnum();
2599
2600 if (p_sol)
2601 curwin->w_cursor.col = 0;
2602 else
2603 {
2604 curwin->w_cursor.col = fpos->col;
2605 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 curwin->w_set_curswant = TRUE;
2608 }
2609}
2610
Bram Moolenaar81695252004-12-29 20:58:21 +00002611/*
2612 * Find file in buffer list by name (it has to be for the current window).
2613 * Returns NULL if not found.
2614 */
2615 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002616buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002617{
2618 char_u *ffname;
2619 buf_T *buf = NULL;
2620
Bram Moolenaarc667da52019-11-30 20:52:27 +01002621 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002622 ffname = FullName_save(fname,
2623#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002624 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002625#else
2626 FALSE
2627#endif
2628 );
2629 if (ffname != NULL)
2630 {
2631 buf = buflist_findname(ffname);
2632 vim_free(ffname);
2633 }
2634 return buf;
2635}
Bram Moolenaar81695252004-12-29 20:58:21 +00002636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637/*
2638 * Find file in buffer list by name (it has to be for the current window).
2639 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002640 * Skips dummy buffers.
2641 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 */
2643 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002644buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
2646#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002647 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649 if (mch_stat((char *)ffname, &st) < 0)
2650 st.st_dev = (dev_T)-1;
2651 return buflist_findname_stat(ffname, &st);
2652}
2653
2654/*
2655 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2656 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002657 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 */
2659 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002660buflist_findname_stat(
2661 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002662 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664#endif
2665 buf_T *buf;
2666
Bram Moolenaarc667da52019-11-30 20:52:27 +01002667 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002668 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002669 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670#ifdef UNIX
2671 , stp
2672#endif
2673 ))
2674 return buf;
2675 return NULL;
2676}
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
2679 * Find file in buffer list by a regexp pattern.
2680 * Return fnum of the found buffer.
2681 * Return < 0 for error.
2682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002684buflist_findpat(
2685 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002686 char_u *pattern_end, // pointer to first char after pattern
2687 int unlisted, // find unlisted buffers
2688 int diffmode UNUSED, // find diff-mode buffers only
2689 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int match = -1;
2693 int find_listed;
2694 char_u *pat;
2695 char_u *patend;
2696 int attempt;
2697 char_u *p;
2698 int toggledollar;
2699
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002700 // "%" is current file, "%%" or "#" is alternate file
2701 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2702 || (in_vim9script() && pattern_end == pattern + 2
2703 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002705 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002707 else
2708 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#ifdef FEAT_DIFF
2710 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2711 match = -1;
2712#endif
2713 }
2714
2715 /*
2716 * Try four ways of matching a listed buffer:
2717 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002718 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 * attempt == 2: with '$' at end (only match at end)
2720 * attempt == 3: with '^' at start and '$' at end (only full match)
2721 * Repeat this for finding an unlisted buffer if there was no matching
2722 * listed buffer.
2723 */
2724 else
2725 {
2726 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2727 if (pat == NULL)
2728 return -1;
2729 patend = pat + STRLEN(pat) - 1;
2730 toggledollar = (patend > pat && *patend == '$');
2731
Bram Moolenaarc667da52019-11-30 20:52:27 +01002732 // First try finding a listed buffer. If not found and "unlisted"
2733 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 find_listed = TRUE;
2735 for (;;)
2736 {
2737 for (attempt = 0; attempt <= 3; ++attempt)
2738 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002739 regmatch_T regmatch;
2740
Bram Moolenaarc667da52019-11-30 20:52:27 +01002741 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002743 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002745 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002747 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002749 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002750 {
2751 if (regmatch.regprog == NULL)
2752 {
2753 // invalid pattern, possibly after switching engine
2754 vim_free(pat);
2755 return -1;
2756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (buf->b_p_bl == find_listed
2758#ifdef FEAT_DIFF
2759 && (!diffmode || diff_mode_buf(buf))
2760#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002761 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002763 if (curtab_only)
2764 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002765 // Ignore the match if the buffer is not open in
2766 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002767 win_T *wp;
2768
Bram Moolenaar29323592016-07-24 22:04:11 +02002769 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002770 if (wp->w_buffer == buf)
2771 break;
2772 if (wp == NULL)
2773 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002774 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002775 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
2777 match = -2;
2778 break;
2779 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002780 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002784 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002785 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 break;
2787 }
2788
Bram Moolenaarc667da52019-11-30 20:52:27 +01002789 // Only search for unlisted buffers if there was no match with
2790 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (!unlisted || !find_listed || match != -1)
2792 break;
2793 find_listed = FALSE;
2794 }
2795
2796 vim_free(pat);
2797 }
2798
2799 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002800 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002802 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 return match;
2804}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
Bram Moolenaar52410572019-10-27 05:12:45 +01002806#ifdef FEAT_VIMINFO
2807typedef struct {
2808 buf_T *buf;
2809 char_u *match;
2810} bufmatch_T;
2811#endif
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813/*
2814 * Find all buffer names that match.
2815 * For command line expansion of ":buf" and ":sbuf".
2816 * Return OK if matches found, FAIL otherwise.
2817 */
2818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002819ExpandBufnames(
2820 char_u *pat,
2821 int *num_file,
2822 char_u ***file,
2823 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002825 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 buf_T *buf;
2827 int round;
2828 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002829 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002830#ifdef FEAT_VIMINFO
2831 bufmatch_T *matches = NULL;
2832#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002833 int fuzzy;
2834 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002835 regmatch_T regmatch;
2836 int score = 0;
2837 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
Bram Moolenaarc667da52019-11-30 20:52:27 +01002839 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 *file = NULL;
2841
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002842#ifdef FEAT_DIFF
2843 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2844 return FAIL;
2845#endif
2846
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002847 fuzzy = cmdline_fuzzy_complete(pat);
2848
2849 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2850 // expression matching)
2851 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002853 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002854 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002855 int len = (int)STRLEN(pat);
2856 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002857 if (patc == NULL)
2858 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002859 STRNCPY(patc, pat + 1, len - 1);
2860 patc[len - 1] = NUL;
2861 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002862 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002863 else if (*pat == '^')
2864 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002865 else
2866 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002867 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002868 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002869
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002870 // round == 1: Count the matches.
2871 // round == 2: Build the array to keep the matches.
2872 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002873 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002874 count = 0;
2875 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002876 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002877 if (!buf->b_p_bl) // skip unlisted buffers
2878 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002879#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002880 if (options & BUF_DIFF_FILTER)
2881 // Skip buffers not suitable for
2882 // :diffget or :diffput completion.
2883 if (buf == curbuf || !diff_mode_buf(buf))
2884 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002885#endif
2886
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002887 if (!fuzzy)
2888 {
2889 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002890 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002891 // invalid pattern, possibly after recompiling
2892 if (to_free)
2893 vim_free(patc);
2894 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002895 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002896 p = buflist_match(&regmatch, buf, p_wic);
2897 }
2898 else
2899 {
2900 p = NULL;
2901 // first try matching with the short file name
2902 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2903 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002904 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002905 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002906 // next try matching with the full path file name
2907 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2908 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002911
2912 if (p == NULL)
2913 continue;
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (round == 1)
2916 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002917 ++count;
2918 continue;
2919 }
2920
2921 if (options & WILD_HOME_REPLACE)
2922 p = home_replace_save(buf, p);
2923 else
2924 p = vim_strsave(p);
2925
2926 if (!fuzzy)
2927 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002928#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002929 if (matches != NULL)
2930 {
2931 matches[count].buf = buf;
2932 matches[count].match = p;
2933 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002934 }
2935 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002936#endif
2937 (*file)[count++] = p;
2938 }
2939 else
2940 {
2941 fuzmatch[count].idx = count;
2942 fuzmatch[count].str = p;
2943 fuzmatch[count].score = score;
2944 count++;
2945 }
2946 }
2947 if (count == 0) // no match found, break here
2948 break;
2949 if (round == 1)
2950 {
2951 if (!fuzzy)
2952 {
2953 *file = ALLOC_MULT(char_u *, count);
2954 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002955 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002956 vim_regfree(regmatch.regprog);
2957 if (to_free)
2958 vim_free(patc);
2959 return FAIL;
2960 }
2961#ifdef FEAT_VIMINFO
2962 if (options & WILD_BUFLASTUSED)
2963 matches = ALLOC_MULT(bufmatch_T, count);
2964#endif
2965 }
2966 else
2967 {
2968 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2969 if (fuzmatch == NULL)
2970 {
2971 *num_file = 0;
2972 *file = NULL;
2973 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002979 if (!fuzzy)
2980 {
2981 vim_regfree(regmatch.regprog);
2982 if (to_free)
2983 vim_free(patc);
2984 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002985
Bram Moolenaar52410572019-10-27 05:12:45 +01002986#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002987 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002988 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002989 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002990 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002991 int i;
2992 if (count > 1)
2993 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2994 // if the current buffer is first in the list, place it at the end
2995 if (matches[0].buf == curbuf)
2996 {
2997 for (i = 1; i < count; i++)
2998 (*file)[i-1] = matches[i].match;
2999 (*file)[count-1] = matches[0].match;
3000 }
3001 else
3002 {
3003 for (i = 0; i < count; i++)
3004 (*file)[i] = matches[i].match;
3005 }
3006 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003007 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003008 }
3009 else
3010 {
3011 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3012 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003013 }
3014#endif
3015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 *num_file = count;
3017 return (count == 0 ? FAIL : OK);
3018}
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020/*
3021 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003022 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 */
3024 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003025buflist_match(
3026 regmatch_T *rmp,
3027 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003028 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 char_u *match;
3031
Bram Moolenaarc667da52019-11-30 20:52:27 +01003032 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003033 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003034 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003035 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 return match;
3038}
3039
3040/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003041 * Try matching the regexp in "rmp->regprog" with file name "name".
3042 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 * Return "name" when there is a match, NULL when not.
3044 */
3045 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046fname_match(
3047 regmatch_T *rmp,
3048 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003049 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 char_u *match = NULL;
3052 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003054 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003055 if (name == NULL || rmp->regprog == NULL)
3056 return NULL;
3057
3058 // Ignore case when 'fileignorecase' or the argument is set.
3059 rmp->rm_ic = p_fic || ignore_case;
3060 if (vim_regexec(rmp, name, (colnr_T)0))
3061 match = name;
3062 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003064 // Replace $(HOME) with '~' and try matching again.
3065 p = home_replace_save(NULL, name);
3066 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003068 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 }
3070
3071 return match;
3072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
3074/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003075 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
3077 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003080 char_u key[VIM_SIZEOF_INT * 2 + 1];
3081 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 if (nr == 0)
3084 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003085 sprintf((char *)key, "%x", nr);
3086 hi = hash_find(&buf_hashtab, key);
3087
3088 if (!HASHITEM_EMPTY(hi))
3089 return (buf_T *)(hi->hi_key
3090 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 return NULL;
3092}
3093
3094/*
3095 * Get name of file 'n' in the buffer list.
3096 * When the file has no name an empty string is returned.
3097 * home_replace() is used to shorten the file name (used for marks).
3098 * Returns a pointer to allocated memory, of NULL when failed.
3099 */
3100 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003101buflist_nr2name(
3102 int n,
3103 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003104 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 buf_T *buf;
3107
3108 buf = buflist_findnr(n);
3109 if (buf == NULL)
3110 return NULL;
3111 return home_replace_save(helptail ? buf : NULL,
3112 fullname ? buf->b_ffname : buf->b_fname);
3113}
3114
3115/*
3116 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3117 * When "copy_options" is TRUE save the local window option values.
3118 * When "lnum" is 0 only do the options.
3119 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003121buflist_setfpos(
3122 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003123 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003124 linenr_T lnum,
3125 colnr_T col,
3126 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 wininfo_T *wip;
3129
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003130 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (wip->wi_win == win)
3132 break;
3133 if (wip == NULL)
3134 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003135 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003136 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (wip == NULL)
3138 return;
3139 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003140 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 lnum = 1;
3142 }
3143 else
3144 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003145 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (wip->wi_prev)
3147 wip->wi_prev->wi_next = wip->wi_next;
3148 else
3149 buf->b_wininfo = wip->wi_next;
3150 if (wip->wi_next)
3151 wip->wi_next->wi_prev = wip->wi_prev;
3152 if (copy_options && wip->wi_optset)
3153 {
3154 clear_winopt(&wip->wi_opt);
3155#ifdef FEAT_FOLDING
3156 deleteFoldRecurse(&wip->wi_folds);
3157#endif
3158 }
3159 }
3160 if (lnum != 0)
3161 {
3162 wip->wi_fpos.lnum = lnum;
3163 wip->wi_fpos.col = col;
3164 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003165 if (win != NULL)
3166 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003167 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003169 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3171#ifdef FEAT_FOLDING
3172 wip->wi_fold_manual = win->w_fold_manual;
3173 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3174#endif
3175 wip->wi_optset = TRUE;
3176 }
3177
Bram Moolenaarc667da52019-11-30 20:52:27 +01003178 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 wip->wi_next = buf->b_wininfo;
3180 buf->b_wininfo = wip;
3181 wip->wi_prev = NULL;
3182 if (wip->wi_next)
3183 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184}
3185
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003186#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003187/*
3188 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3189 * page. That's because a diff is local to a tab page.
3190 */
3191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003193{
3194 win_T *wp;
3195
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003196 if (!wip->wi_opt.wo_diff)
3197 return FALSE;
3198
3199 FOR_ALL_WINDOWS(wp)
3200 // return FALSE when it's a window in the current tab page, thus
3201 // the buffer was in diff mode here
3202 if (wip->wi_win == wp)
3203 return FALSE;
3204 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003205}
3206#endif
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208/*
3209 * Find info for the current window in buffer "buf".
3210 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003211 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003212 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3213 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * Returns NULL when there isn't any info.
3215 */
3216 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003217find_wininfo(
3218 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003219 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 wininfo_T *wip;
3223
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003224 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003225 if (wip->wi_win == curwin
3226#ifdef FEAT_DIFF
3227 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3228#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003229
3230 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003232
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003233 if (wip != NULL)
3234 return wip;
3235
Bram Moolenaarc667da52019-11-30 20:52:27 +01003236 // If no wininfo for curwin, use the first in the list (that doesn't have
3237 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003238 // If "need_options" is TRUE skip entries that don't have options set,
3239 // unless the window is editing "buf", so we can copy from the window
3240 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003241#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003242 if (skip_diff_buffer)
3243 {
3244 FOR_ALL_BUF_WININFO(buf, wip)
3245 if (!wininfo_other_tab_diff(wip)
3246 && (!need_options || wip->wi_optset
3247 || (wip->wi_win != NULL
3248 && wip->wi_win->w_buffer == buf)))
3249 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003250 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003251 else
3252#endif
3253 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 return wip;
3255}
3256
3257/*
3258 * Reset the local window options to the values last used in this window.
3259 * If the buffer wasn't used in this window before, use the values from
3260 * the most recently used window. If the values were never set, use the
3261 * global values for the window.
3262 */
3263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003264get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 wininfo_T *wip;
3267
3268 clear_winopt(&curwin->w_onebuf_opt);
3269#ifdef FEAT_FOLDING
3270 clearFolding(curwin);
3271#endif
3272
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003273 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003274 if (wip != NULL && wip->wi_win != NULL
3275 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003277 // The buffer is currently displayed in the window: use the actual
3278 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003279 win_T *wp = wip->wi_win;
3280
3281 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3282#ifdef FEAT_FOLDING
3283 curwin->w_fold_manual = wp->w_fold_manual;
3284 curwin->w_foldinvalid = TRUE;
3285 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3286#endif
3287 }
3288 else if (wip != NULL && wip->wi_optset)
3289 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003290 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3292#ifdef FEAT_FOLDING
3293 curwin->w_fold_manual = wip->wi_fold_manual;
3294 curwin->w_foldinvalid = TRUE;
3295 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3296#endif
3297 }
3298 else
3299 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003300 if (wip != NULL)
3301 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003304 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (p_fdls >= 0)
3306 curwin->w_p_fdl = p_fdls;
3307#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003308 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
3311/*
3312 * Find the position (lnum and col) for the buffer 'buf' for the current
3313 * window.
3314 * Returns a pointer to no_position if no position is found.
3315 */
3316 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003320 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003322 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (wip != NULL)
3324 return &(wip->wi_fpos);
3325 else
3326 return &no_position;
3327}
3328
3329/*
3330 * Find the lnum for the buffer 'buf' for the current window.
3331 */
3332 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 return buflist_findfpos(buf)->lnum;
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003339 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaar52410572019-10-27 05:12:45 +01003344 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 int len;
3346 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003347 int ro_char;
3348 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003349#ifdef FEAT_TERMINAL
3350 int job_running;
3351 int job_none_open;
3352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar52410572019-10-27 05:12:45 +01003354#ifdef FEAT_VIMINFO
3355 garray_T buflist;
3356 buf_T **buflist_data = NULL, **p;
3357
3358 if (vim_strchr(eap->arg, 't'))
3359 {
3360 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003361 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003362 {
3363 if (ga_grow(&buflist, 1) == OK)
3364 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3365 }
3366
3367 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3368 sizeof(buf_T *), buf_compare);
3369
Bram Moolenaar3b991522019-11-06 23:26:20 +01003370 buflist_data = (buf_T **)buflist.ga_data;
3371 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003372 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003373 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003374
Bram Moolenaar3b991522019-11-06 23:26:20 +01003375 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003376 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3377 : buf->b_next)
3378#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003382#ifdef FEAT_TERMINAL
3383 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003384 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003385#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003386 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003387 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3388 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3389 || (vim_strchr(eap->arg, '+')
3390 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3391 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003392 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003393 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003394 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3395#ifdef FEAT_TERMINAL
3396 || (vim_strchr(eap->arg, 'R')
3397 && (!job_running || (job_running && job_none_open)))
3398 || (vim_strchr(eap->arg, '?')
3399 && (!job_running || (job_running && !job_none_open)))
3400 || (vim_strchr(eap->arg, 'F')
3401 && (job_running || buf->b_term == NULL))
3402#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003403 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3404 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3405 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3406 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3407 || (vim_strchr(eap->arg, '#')
3408 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003411 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 else
3413 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003414 if (message_filtered(NameBuff))
3415 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003417 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3418 : (bufIsChanged(buf) ? '+' : ' ');
3419#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003420 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003421 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003422 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003423 ro_char = '?';
3424 else
3425 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003426 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3427 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003428 }
3429 else if (buf->b_term != NULL)
3430 ro_char = 'F';
3431 else
3432#endif
3433 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3434
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003435 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003436 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 buf->b_fnum,
3438 buf->b_p_bl ? ' ' : 'u',
3439 buf == curbuf ? '%' :
3440 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3441 buf->b_ml.ml_mfp == NULL ? ' ' :
3442 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003443 ro_char,
3444 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003445 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003446 if (len > IOSIZE - 20)
3447 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
Bram Moolenaarc667da52019-11-30 20:52:27 +01003449 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 i = 40 - vim_strsize(IObuff);
3451 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003453 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003454#ifdef FEAT_VIMINFO
3455 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3456 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3457 else
3458#endif
3459 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3460 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003461 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003463 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 ui_breakcheck();
3465 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003466
3467#ifdef FEAT_VIMINFO
3468 if (buflist_data)
3469 ga_clear(&buflist);
3470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473/*
3474 * Get file name and line number for file 'fnum'.
3475 * Used by DoOneCmd() for translating '%' and '#'.
3476 * Used by insert_reg() and cmdline_paste() for '#' register.
3477 * Return FAIL if not found, OK for success.
3478 */
3479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003480buflist_name_nr(
3481 int fnum,
3482 char_u **fname,
3483 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
3485 buf_T *buf;
3486
3487 buf = buflist_findnr(fnum);
3488 if (buf == NULL || buf->b_fname == NULL)
3489 return FAIL;
3490
3491 *fname = buf->b_fname;
3492 *lnum = buflist_findlnum(buf);
3493
3494 return OK;
3495}
3496
3497/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003498 * Set the file name for "buf"' to "ffname_arg", short file name to
3499 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 * The file name with the full path is also remembered, for when :cd is used.
3501 * Returns FAIL for failure (file name already in use by other buffer)
3502 * OK otherwise.
3503 */
3504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003505setfname(
3506 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003507 char_u *ffname_arg,
3508 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003509 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003511 char_u *ffname = ffname_arg;
3512 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003513 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003515 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
3517
3518 if (ffname == NULL || *ffname == NUL)
3519 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003520 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003521 if (buf->b_sfname != buf->b_ffname)
3522 VIM_CLEAR(buf->b_sfname);
3523 else
3524 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003525 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#ifdef UNIX
3527 st.st_dev = (dev_T)-1;
3528#endif
3529 }
3530 else
3531 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003532 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3533 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return FAIL;
3535
3536 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003537 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 * - if the buffer is loaded, fail
3539 * - if the buffer is not loaded, delete it from the list
3540 */
3541#ifdef UNIX
3542 if (mch_stat((char *)ffname, &st) < 0)
3543 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003544#endif
3545 if (!(buf->b_flags & BF_DUMMY))
3546#ifdef UNIX
3547 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003549 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif
3551 if (obuf != NULL && obuf != buf)
3552 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003553 win_T *win;
3554 tabpage_T *tab;
3555 int in_use = FALSE;
3556
3557 // during startup a window may use a buffer that is not loaded yet
3558 FOR_ALL_TAB_WINDOWS(tab, win)
3559 if (win->w_buffer == obuf)
3560 in_use = TRUE;
3561
3562 // it's loaded or used in a window, fail
3563 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003566 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 vim_free(ffname);
3568 return FAIL;
3569 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003570 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003571 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573 sfname = vim_strsave(sfname);
3574 if (ffname == NULL || sfname == NULL)
3575 {
3576 vim_free(sfname);
3577 vim_free(ffname);
3578 return FAIL;
3579 }
3580#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003581 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003583 if (buf->b_sfname != buf->b_ffname)
3584 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 buf->b_ffname = ffname;
3587 buf->b_sfname = sfname;
3588 }
3589 buf->b_fname = buf->b_sfname;
3590#ifdef UNIX
3591 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003592 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 else
3594 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003595 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 buf->b_dev = st.st_dev;
3597 buf->b_ino = st.st_ino;
3598 }
3599#endif
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 buf_name_changed(buf);
3604 return OK;
3605}
3606
3607/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003608 * Crude way of changing the name of a buffer. Use with care!
3609 * The name should be relative to the current directory.
3610 */
3611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003613{
3614 buf_T *buf;
3615
3616 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003617 if (buf == NULL)
3618 return;
3619
3620 if (buf->b_sfname != buf->b_ffname)
3621 vim_free(buf->b_sfname);
3622 vim_free(buf->b_ffname);
3623 buf->b_ffname = vim_strsave(name);
3624 buf->b_sfname = NULL;
3625 // Allocate ffname and expand into full path. Also resolves .lnk
3626 // files on Win32.
3627 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3628 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003629}
3630
3631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Take care of what needs to be done when the name of buffer "buf" has
3633 * changed.
3634 */
3635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
3638 /*
3639 * If the file name changed, also change the name of the swapfile
3640 */
3641 if (buf->b_ml.ml_mfp != NULL)
3642 ml_setname(buf);
3643
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003644#ifdef FEAT_TERMINAL
3645 if (buf->b_term != NULL)
3646 term_clear_status_text(buf->b_term);
3647#endif
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003650 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003651 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003652 status_redraw_all(); // status lines need to be redrawn
3653 fmarks_check_names(buf); // check named file marks
3654 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655}
3656
3657/*
3658 * set alternate file name for current window
3659 *
3660 * Used by do_one_cmd(), do_write() and do_ecmd().
3661 * Return the buffer.
3662 */
3663 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003664setaltfname(
3665 char_u *ffname,
3666 char_u *sfname,
3667 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669 buf_T *buf;
3670
Bram Moolenaarc667da52019-11-30 20:52:27 +01003671 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003673 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 curwin->w_alt_fnum = buf->b_fnum;
3675 return buf;
3676}
3677
3678/*
3679 * Get alternate file name for current window.
3680 * Return NULL if there isn't any, and give error message if requested.
3681 */
3682 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003683getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003684 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
3686 char_u *fname;
3687 linenr_T dummy;
3688
3689 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3690 {
3691 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003692 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return NULL;
3694 }
3695 return fname;
3696}
3697
3698/*
3699 * Add a file name to the buflist and return its number.
3700 * Uses same flags as buflist_new(), except BLN_DUMMY.
3701 *
3702 * used by qf_init(), main() and doarglist()
3703 */
3704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 buf_T *buf;
3708
3709 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3710 if (buf != NULL)
3711 return buf->b_fnum;
3712 return 0;
3713}
3714
3715#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3716/*
3717 * Adjust slashes in file names. Called after 'shellslash' was set.
3718 */
3719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 buf_T *bp;
3723
Bram Moolenaar29323592016-07-24 22:04:11 +02003724 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
3726 if (bp->b_ffname != NULL)
3727 slash_adjust(bp->b_ffname);
3728 if (bp->b_sfname != NULL)
3729 slash_adjust(bp->b_sfname);
3730 }
3731}
3732#endif
3733
3734/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003735 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Also save the local window option values.
3737 */
3738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003741 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742}
3743
3744/*
3745 * Return TRUE if 'ffname' is not the same file as current file.
3746 * Fname must have a full path (expanded by mch_FullName()).
3747 */
3748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003749otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
3751 return otherfile_buf(curbuf, ffname
3752#ifdef UNIX
3753 , NULL
3754#endif
3755 );
3756}
3757
3758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003759otherfile_buf(
3760 buf_T *buf,
3761 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003763 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003765 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003767 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3769 return TRUE;
3770 if (fnamecmp(ffname, buf->b_ffname) == 0)
3771 return FALSE;
3772#ifdef UNIX
3773 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003774 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaarc667da52019-11-30 20:52:27 +01003776 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (stp == NULL)
3778 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003779 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 st.st_dev = (dev_T)-1;
3781 stp = &st;
3782 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003783 // Use dev/ino to check if the files are the same, even when the names
3784 // are different (possible with links). Still need to compare the
3785 // name above, for when the file doesn't exist yet.
3786 // Problem: The dev/ino changes when a file is deleted (and created
3787 // again) and remains the same when renamed/moved. We don't want to
3788 // mch_stat() each buffer each time, that would be too slow. Get the
3789 // dev/ino again when they appear to match, but not when they appear
3790 // to be different: Could skip a buffer when it's actually the same
3791 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (buf_same_ino(buf, stp))
3793 {
3794 buf_setino(buf);
3795 if (buf_same_ino(buf, stp))
3796 return FALSE;
3797 }
3798 }
3799#endif
3800 return TRUE;
3801}
3802
3803#if defined(UNIX) || defined(PROTO)
3804/*
3805 * Set inode and device number for a buffer.
3806 * Must always be called when b_fname is changed!.
3807 */
3808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003809buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003811 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3814 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003815 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 buf->b_dev = st.st_dev;
3817 buf->b_ino = st.st_ino;
3818 }
3819 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003820 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821}
3822
3823/*
3824 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3825 */
3826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003827buf_same_ino(
3828 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003829 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003831 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 && stp->st_dev == buf->b_dev
3833 && stp->st_ino == buf->b_ino);
3834}
3835#endif
3836
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837/*
3838 * Print info about the current buffer.
3839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003841fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003842 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843 int shorthelp,
3844 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 char_u *name;
3847 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003848 char *p;
3849 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003850 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003852 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (buffer == NULL)
3854 return;
3855
Bram Moolenaarc667da52019-11-30 20:52:27 +01003856 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003858 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 p = buffer + STRLEN(buffer);
3860 }
3861 else
3862 p = buffer;
3863
3864 *p++ = '"';
3865 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003866 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 else
3868 {
3869 if (!fullname && curbuf->b_fname != NULL)
3870 name = curbuf->b_fname;
3871 else
3872 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003873 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 (int)(IOSIZE - (p - buffer)), TRUE);
3875 }
3876
Bram Moolenaar32526b32019-01-19 17:43:09 +01003877 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 curbufIsChanged() ? (shortmess(SHM_MOD)
3879 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003880 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003882 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003883 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003885 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 : _("[readonly]")) : "",
3887 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3888 || curbuf->b_p_ro) ?
3889 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003890 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3891 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (curwin->w_cursor.lnum > 1000000L)
3893 n = (int)(((long)curwin->w_cursor.lnum) /
3894 ((long)curbuf->b_ml.ml_line_count / 100L));
3895 else
3896 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3897 (long)curbuf->b_ml.ml_line_count);
3898 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003899 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003901 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003902 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003903 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3904 curbuf->b_ml.ml_line_count),
3905 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 else
3907 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003908 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 _("line %ld of %ld --%d%%-- col "),
3910 (long)curwin->w_cursor.lnum,
3911 (long)curbuf->b_ml.ml_line_count,
3912 n);
3913 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003914 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003915 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3917 }
3918
Bram Moolenaar32526b32019-01-19 17:43:09 +01003919 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3920 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 if (dont_truncate)
3923 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003924 // Temporarily set msg_scroll to avoid the message being truncated.
3925 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 msg_start();
3927 n = msg_scroll;
3928 msg_scroll = TRUE;
3929 msg(buffer);
3930 msg_scroll = n;
3931 }
3932 else
3933 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003934 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003936 // Need to repeat the message after redrawing when:
3937 // - When restart_edit is set (otherwise there will be a delay
3938 // before redrawing).
3939 // - When the screen was scrolled but there is no wait-return
3940 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003941 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943
3944 vim_free(buffer);
3945}
3946
3947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948col_print(
3949 char_u *buf,
3950 size_t buflen,
3951 int col,
3952 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
3954 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003955 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003957 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958}
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960static char_u *lasttitle = NULL;
3961static char_u *lasticon = NULL;
3962
Bram Moolenaar84a93082018-06-16 22:58:15 +02003963/*
3964 * Put the file name in the title bar and icon of the window.
3965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003967maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
3969 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003970 char_u *title_str = NULL;
3971 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 int maxlen = 0;
3973 int len;
3974 int mustset;
3975 char_u buf[IOSIZE];
3976 int off;
3977
3978 if (!redrawing())
3979 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003980 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 need_maketitle = TRUE;
3982 return;
3983 }
3984
3985 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003986 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003987 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 if (p_title)
3990 {
3991 if (p_titlelen > 0)
3992 {
3993 maxlen = p_titlelen * Columns / 100;
3994 if (maxlen < 10)
3995 maxlen = 10;
3996 }
3997
Bram Moolenaar84a93082018-06-16 22:58:15 +02003998 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (*p_titlestring != NUL)
4000 {
4001#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004002 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004003 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4004 (char_u *)"titlestring", 0,
4005 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 else
4007#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004008 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 else
4011 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004012 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar2c666692012-09-05 13:30:40 +02004014#define SPACE_FOR_FNAME (IOSIZE - 100)
4015#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004016#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02004018 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02004019#ifdef FEAT_TERMINAL
4020 else if (curbuf->b_term != NULL)
4021 {
4022 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
4023 SPACE_FOR_FNAME);
4024 }
4025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 else
4027 {
4028 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02004029 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
4032
Bram Moolenaar21554412017-07-24 21:44:43 +02004033#ifdef FEAT_TERMINAL
4034 if (curbuf->b_term == NULL)
4035#endif
4036 switch (bufIsChanged(curbuf)
4037 + (curbuf->b_p_ro * 2)
4038 + (!curbuf->b_p_ma * 4))
4039 {
4040 case 1: STRCAT(buf, " +"); break;
4041 case 2: STRCAT(buf, " ="); break;
4042 case 3: STRCAT(buf, " =+"); break;
4043 case 4:
4044 case 6: STRCAT(buf, " -"); break;
4045 case 5:
4046 case 7: STRCAT(buf, " -+"); break;
4047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaar21554412017-07-24 21:44:43 +02004049 if (curbuf->b_fname != NULL
4050#ifdef FEAT_TERMINAL
4051 && curbuf->b_term == NULL
4052#endif
4053 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004055 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 off = (int)STRLEN(buf);
4057 buf[off++] = ' ';
4058 buf[off++] = '(';
4059 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004060 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004062 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004063 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 off += 2;
4065#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004066 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004067 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004069 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004070 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004071 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004072 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004076
Bram Moolenaarc667da52019-11-30 20:52:27 +01004077 // Translate unprintable chars and concatenate. Keep some
4078 // room for the server name. When there is no room (very long
4079 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004080 if (off < SPACE_FOR_DIR)
4081 {
4082 p = transstr(buf + off);
4083 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4084 vim_free(p);
4085 }
4086 else
4087 {
4088 vim_strncpy(buf + off, (char_u *)"...",
4089 (size_t)(SPACE_FOR_ARGNR - off));
4090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 STRCAT(buf, ")");
4092 }
4093
Bram Moolenaar2c666692012-09-05 13:30:40 +02004094 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096#if defined(FEAT_CLIENTSERVER)
4097 if (serverName != NULL)
4098 {
4099 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004100 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102 else
4103#endif
4104 STRCAT(buf, " - VIM");
4105
4106 if (maxlen > 0)
4107 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004108 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004109 if (vim_strsize(buf) > maxlen)
4110 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 }
4113 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004114 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 if (p_icon)
4117 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004118 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (*p_iconstring != NUL)
4120 {
4121#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004122 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004123 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4124 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else
4126#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004127 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 else
4130 {
4131 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004132 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004133 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004134 p = gettail(curbuf->b_ffname);
4135 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004136 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004137 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (len > 100)
4139 {
4140 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004142 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004143 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004145 STRCPY(icon_str, p);
4146 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
4148 }
4149
Bram Moolenaar84a93082018-06-16 22:58:15 +02004150 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 if (mustset)
4153 resettitle();
4154}
4155
4156/*
4157 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4158 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004159 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 */
4161 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004162value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 if ((str == NULL) != (*last == NULL)
4165 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4166 {
4167 vim_free(*last);
4168 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004171 mch_restore_title(
4172 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004177 return TRUE;
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180 return FALSE;
4181}
4182
4183/*
4184 * Put current window title back (used after calling a shell)
4185 */
4186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004187resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
4189 mch_settitle(lasttitle, lasticon);
4190}
Bram Moolenaarea408852005-06-25 22:49:46 +00004191
4192# if defined(EXITFREE) || defined(PROTO)
4193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004194free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004195{
4196 vim_free(lasttitle);
4197 vim_free(lasticon);
4198}
4199# endif
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004202#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004203
4204/*
4205 * Used for building in the status line.
4206 */
4207typedef struct
4208{
4209 char_u *stl_start;
4210 int stl_minwid;
4211 int stl_maxwid;
4212 enum {
4213 Normal,
4214 Empty,
4215 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004216 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004217 Highlight,
4218 TabPage,
4219 Trunc
4220 } stl_type;
4221} stl_item_T;
4222
4223static size_t stl_items_len = 20; // Initial value, grows as needed.
4224static stl_item_T *stl_items = NULL;
4225static int *stl_groupitem = NULL;
4226static stl_hlrec_T *stl_hltab = NULL;
4227static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004228static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004231 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 * Return length of string in screen cells.
4233 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004234 * Normally works for window "wp", except when working for 'tabline' then it
4235 * is "curwin".
4236 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004238 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4240 *
4241 * If maxwidth is not zero, the string will be filled at any middle marker
4242 * or truncated if too long, fillchar is used for all whitespace.
4243 */
4244 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004245build_stl_str_hl(
4246 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004247 char_u *out, // buffer to write into != NameBuff
4248 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004250 char_u *opt_name, // option name corresponding to "fmt"
4251 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004252 int fillchar,
4253 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004254 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4255 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
Bram Moolenaar10772302019-01-20 18:25:54 +01004257 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004258 colnr_T len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 char_u *p;
4260 char_u *s;
4261 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004262 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004264 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004265 win_T *save_curwin;
4266 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004267 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#endif
4269 int empty_line;
4270 colnr_T virtcol;
4271 long l;
4272 long n;
4273 int prevchar_isflag;
4274 int prevchar_isitem;
4275 int itemisflag;
4276 int fillable;
4277 char_u *str;
4278 long num;
4279 int width;
4280 int itemcnt;
4281 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004282 int group_end_userhl;
4283 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004285#ifdef FEAT_EVAL
4286 int evaldepth;
4287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 int minwid;
4289 int maxwid;
4290 int zeropad;
4291 char_u base;
4292 char_u opt;
4293#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004294 char_u buf_tmp[TMPLEN];
4295 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004296 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004297 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004298 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004299 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004300 // TODO: find out why using called_emsg_before makes tests fail, does it
4301 // matter?
4302 // int called_emsg_before = called_emsg;
4303 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004304
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004305 // When inside update_screen() we do not want redrawing a statusline,
4306 // ruler, title, etc. to trigger another redraw, it may cause an endless
4307 // loop.
4308 if (updating_screen)
4309 redraw_not_allowed = TRUE;
4310
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004311 if (stl_items == NULL)
4312 {
4313 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4314 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004315
4316 // Allocate one more, because the last element is used to indicate the
4317 // end of the list.
4318 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4319 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004320
4321 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004322 }
4323
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004324#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004325 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4326 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4327
4328 // When the format starts with "%!" then evaluate it as an expression and
4329 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004330 if (fmt[0] == '%' && fmt[1] == '!')
4331 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004332 typval_T tv;
4333
4334 tv.v_type = VAR_NUMBER;
4335 tv.vval.v_number = wp->w_id;
4336 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4337
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004338 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004339 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004340 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004341
4342 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004343 }
4344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346 if (fillchar == 0)
4347 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004348
Bram Moolenaar10772302019-01-20 18:25:54 +01004349 // The cursor in windows other than the current one isn't always
4350 // up-to-date, esp. because of autocommands and timers.
4351 lnum = wp->w_cursor.lnum;
4352 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4353 {
4354 lnum = wp->w_buffer->b_ml.ml_line_count;
4355 wp->w_cursor.lnum = lnum;
4356 }
4357
4358 // Get line & check if empty (cursorpos will show "0-1"). Note that
4359 // p will become invalid when getting another buffer line.
4360 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004361 empty_line = (*p == NUL);
4362
Bram Moolenaar10772302019-01-20 18:25:54 +01004363 // Get the byte value now, in case we need it below. This is more efficient
4364 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004365 len = ml_get_buf_len(wp->w_buffer, lnum);
4366 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004367 {
4368 // Line may have changed since checking the cursor column, or the lnum
4369 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004370 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004371 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004372 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004373 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004374 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004375 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004378#ifdef FEAT_EVAL
4379 evaldepth = 0;
4380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 p = out;
4382 curitem = 0;
4383 prevchar_isflag = TRUE;
4384 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004385 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004387 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004388 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004389 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004390
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004391 stl_item_T *new_items =
4392 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004393 if (new_items == NULL)
4394 break;
4395 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004396
4397 int *new_groupitem =
4398 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004399 if (new_groupitem == NULL)
4400 break;
4401 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004402
4403 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004404 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004405 if (new_hlrec == NULL)
4406 break;
4407 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004408 new_hlrec = vim_realloc(stl_tabtab,
4409 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004410 if (new_hlrec == NULL)
4411 break;
4412 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004413
4414 int *new_separator_locs = vim_realloc(stl_separator_locations,
4415 sizeof(int) * new_len);
4416 if (new_separator_locs == NULL)
4417 break;
4418 stl_separator_locations = new_separator_locs;;
4419
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004420 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004421 }
4422
zeertzjq122dea72022-07-27 15:48:45 +01004423 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 prevchar_isflag = prevchar_isitem = FALSE;
4425
4426 /*
4427 * Handle up to the next '%' or the end.
4428 */
4429 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4430 *p++ = *s++;
4431 if (*s == NUL || p + 1 >= out + outlen)
4432 break;
4433
4434 /*
4435 * Handle one '%' item.
4436 */
4437 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004438 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (*s == '%')
4441 {
4442 if (p + 1 >= out + outlen)
4443 break;
4444 *p++ = *s++;
4445 prevchar_isflag = prevchar_isitem = FALSE;
4446 continue;
4447 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004448 // STL_SEPARATE: Separation between items, filled with white space.
4449 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 s++;
4452 if (groupdepth > 0)
4453 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004454 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 continue;
4457 }
4458 if (*s == STL_TRUNCMARK)
4459 {
4460 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004461 stl_items[curitem].stl_type = Trunc;
4462 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 continue;
4464 }
4465 if (*s == ')')
4466 {
4467 s++;
4468 if (groupdepth < 1)
4469 continue;
4470 groupdepth--;
4471
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004472 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 *p = NUL;
4474 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004475 if (curitem > stl_groupitem[groupdepth] + 1
4476 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004478 // remove group if all items are empty and highlight group
4479 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004480 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004481 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004482 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004483 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004484 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004485 group_start_userhl = group_end_userhl =
4486 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004487 break;
4488 }
4489 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004490 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004491 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004492 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004494 if (stl_items[n].stl_type == Highlight)
4495 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004496 }
4497 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004499 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 p = t;
4501 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004502 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004503 {
4504 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004505 if (stl_items[n].stl_type == Highlight)
4506 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004507 // adjust the start position of TabPage to the next
4508 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004509 if (stl_items[n].stl_type == TabPage)
4510 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004514 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004516 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (has_mbyte)
4518 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004519 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004521 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004524 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004528 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4529 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004532 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004534
4535 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004536 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004537 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
Bram Moolenaarc667da52019-11-30 20:52:27 +01004539 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004540 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
LemonBoy57ff5262022-05-09 21:03:47 +01004542 // Minus one for the leading '<' added above.
4543 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004544 if (stl_items[l].stl_start < t)
4545 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004548 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004550 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004551 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (n < 0)
4553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004554 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 n = 0 - n;
4556 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004557 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 else
4560 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004561 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004562 l = (n - l) * MB_CHAR2LEN(fillchar);
4563 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004565 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004567 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4568 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004570 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572 }
4573 continue;
4574 }
4575 minwid = 0;
4576 maxwid = 9999;
4577 zeropad = FALSE;
4578 l = 1;
4579 if (*s == '0')
4580 {
4581 s++;
4582 zeropad = TRUE;
4583 }
4584 if (*s == '-')
4585 {
4586 s++;
4587 l = -1;
4588 }
4589 if (VIM_ISDIGIT(*s))
4590 {
4591 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004592 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 minwid = 0;
4594 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004595 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004597 stl_items[curitem].stl_type = Highlight;
4598 stl_items[curitem].stl_start = p;
4599 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 s++;
4601 curitem++;
4602 continue;
4603 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004604 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4605 {
4606 if (*s == STL_TABCLOSENR)
4607 {
4608 if (minwid == 0)
4609 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004610 // %X ends the close label, go back to the previously
4611 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004612 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004613 if (stl_items[n].stl_type == TabPage
4614 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004615 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004616 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004617 break;
4618 }
4619 }
4620 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004621 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004622 minwid = - minwid;
4623 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004624 stl_items[curitem].stl_type = TabPage;
4625 stl_items[curitem].stl_start = p;
4626 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004627 s++;
4628 curitem++;
4629 continue;
4630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (*s == '.')
4632 {
4633 s++;
4634 if (VIM_ISDIGIT(*s))
4635 {
4636 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004637 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 maxwid = 50;
4639 }
4640 }
4641 minwid = (minwid > 50 ? 50 : minwid) * l;
4642 if (*s == '(')
4643 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004644 stl_groupitem[groupdepth++] = curitem;
4645 stl_items[curitem].stl_type = Group;
4646 stl_items[curitem].stl_start = p;
4647 stl_items[curitem].stl_minwid = minwid;
4648 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 s++;
4650 curitem++;
4651 continue;
4652 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004653#ifdef FEAT_EVAL
4654 // Denotes end of expanded %{} block
4655 if (*s == '}' && evaldepth > 0)
4656 {
4657 s++;
4658 evaldepth--;
4659 continue;
4660 }
4661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (vim_strchr(STL_ALL, *s) == NULL)
4663 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004664 if (*s == NUL) // can happen with "%0"
4665 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 s++;
4667 continue;
4668 }
4669 opt = *s++;
4670
Bram Moolenaarc667da52019-11-30 20:52:27 +01004671 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 base = 'D';
4673 itemisflag = FALSE;
4674 fillable = TRUE;
4675 num = -1;
4676 str = NULL;
4677 switch (opt)
4678 {
4679 case STL_FILEPATH:
4680 case STL_FULLPATH:
4681 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004682 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004684 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 else
4686 {
4687 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004688 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4690 }
4691 trans_characters(NameBuff, MAXPATHL);
4692 if (opt != STL_FILENAME)
4693 str = NameBuff;
4694 else
4695 str = gettail(NameBuff);
4696 break;
4697
Bram Moolenaarc667da52019-11-30 20:52:27 +01004698 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004699 {
4700#ifdef FEAT_EVAL
4701 char_u *block_start = s - 1;
4702#endif
4703 int reevaluate = (*s == '%');
4704
4705 if (reevaluate)
4706 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 itemisflag = TRUE;
4708 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004709 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4710 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004712 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004715 if (reevaluate)
4716 p[-1] = 0; // remove the % at the end of %{% expr %}
4717 else
4718 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004721 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4722 "%d", curbuf->b_fnum);
4723 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4724 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4725 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004727 save_curbuf = curbuf;
4728 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004729 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 curwin = wp;
4731 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004732 // Visual mode is only valid in the current window.
4733 if (curwin != save_curwin)
4734 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004736 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004738 curwin = save_curwin;
4739 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004740 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004741 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004742 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 if (str != NULL && *str != 0)
4745 {
4746 if (*skipdigits(str) == NUL)
4747 {
4748 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004749 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 itemisflag = FALSE;
4751 }
4752 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004753
4754 // If the output of the expression needs to be evaluated
4755 // replace the %{} block with the result of evaluation
4756 if (reevaluate && str != NULL && *str != 0
4757 && strchr((const char *)str, '%') != NULL
4758 && evaldepth < MAX_STL_EVAL_DEPTH)
4759 {
4760 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4761 size_t str_length = strlen((const char *)str);
4762 size_t fmt_length = strlen((const char *)s);
4763 size_t new_fmt_len = parsed_usefmt
4764 + str_length + fmt_length + 3;
4765 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4766 char_u *new_fmt_p = new_fmt;
4767
4768 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4769 + parsed_usefmt;
4770 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4771 + str_length;
4772 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4773 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4774 + fmt_length;
4775 *new_fmt_p = 0;
4776 new_fmt_p = NULL;
4777
4778 if (usefmt != fmt)
4779 vim_free(usefmt);
4780 VIM_CLEAR(str);
4781 usefmt = new_fmt;
4782 s = usefmt + parsed_usefmt;
4783 evaldepth++;
4784 continue;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#endif
4787 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 case STL_LINE:
4790 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4791 ? 0L : (long)(wp->w_cursor.lnum);
4792 break;
4793
4794 case STL_NUMLINES:
4795 num = wp->w_buffer->b_ml.ml_line_count;
4796 break;
4797
4798 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004799 num = (State & MODE_INSERT) == 0 && empty_line
4800 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 break;
4802
4803 case STL_VIRTCOL:
4804 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004805 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004806 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004808 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4809 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 break;
4811 num = (long)virtcol;
4812 break;
4813
4814 case STL_PERCENTAGE:
4815 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4816 (long)wp->w_buffer->b_ml.ml_line_count);
4817 break;
4818
4819 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004820 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004821 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 break;
4823
Luuk van Baalba936f62022-12-15 13:15:39 +00004824 case STL_SHOWCMD:
4825 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4826 str = showcmd_buf;
4827 break;
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 case STL_ARGLISTSTAT:
4830 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004831 buf_tmp[0] = 0;
4832 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4833 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 break;
4835
4836 case STL_KEYMAP:
4837 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004838 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4839 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 break;
4841 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004842#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004843 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#else
4845 num = 0;
4846#endif
4847 break;
4848
4849 case STL_BUFNO:
4850 num = wp->w_buffer->b_fnum;
4851 break;
4852
4853 case STL_OFFSET_X:
4854 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004855 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 case STL_OFFSET:
4857#ifdef FEAT_BYTEOFF
4858 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004859 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4860 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4861 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#endif
4863 break;
4864
4865 case STL_BYTEVAL_X:
4866 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004867 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004869 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (num == NL)
4871 num = 0;
4872 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4873 num = NL;
4874 break;
4875
4876 case STL_ROFLAG:
4877 case STL_ROFLAG_ALT:
4878 itemisflag = TRUE;
4879 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004880 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 break;
4882
4883 case STL_HELPFLAG:
4884 case STL_HELPFLAG_ALT:
4885 itemisflag = TRUE;
4886 if (wp->w_buffer->b_help)
4887 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004888 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 break;
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 case STL_FILETYPE:
4892 if (*wp->w_buffer->b_p_ft != NUL
4893 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4894 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004895 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004896 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004897 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 break;
4900
4901 case STL_FILETYPE_ALT:
4902 itemisflag = TRUE;
4903 if (*wp->w_buffer->b_p_ft != NUL
4904 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4905 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004906 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004907 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004908 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004910 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaar4033c552017-09-16 20:54:51 +02004914#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 case STL_PREVIEWFLAG:
4916 case STL_PREVIEWFLAG_ALT:
4917 itemisflag = TRUE;
4918 if (wp->w_p_pvw)
4919 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4920 : _("[Preview]"));
4921 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004922
4923 case STL_QUICKFIX:
4924 if (bt_quickfix(wp->w_buffer))
4925 str = (char_u *)(wp->w_llist_ref
4926 ? _(msg_loclist)
4927 : _(msg_qflist));
4928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930
4931 case STL_MODIFIED:
4932 case STL_MODIFIED_ALT:
4933 itemisflag = TRUE;
4934 switch ((opt == STL_MODIFIED_ALT)
4935 + bufIsChanged(wp->w_buffer) * 2
4936 + (!wp->w_buffer->b_p_ma) * 4)
4937 {
4938 case 2: str = (char_u *)"[+]"; break;
4939 case 3: str = (char_u *)",+"; break;
4940 case 4: str = (char_u *)"[-]"; break;
4941 case 5: str = (char_u *)",-"; break;
4942 case 6: str = (char_u *)"[+-]"; break;
4943 case 7: str = (char_u *)",+-"; break;
4944 }
4945 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004946
4947 case STL_HIGHLIGHT:
4948 t = s;
4949 while (*s != '#' && *s != NUL)
4950 ++s;
4951 if (*s == '#')
4952 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004953 stl_items[curitem].stl_type = Highlight;
4954 stl_items[curitem].stl_start = p;
4955 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004956 curitem++;
4957 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004958 if (*s != NUL)
4959 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004960 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004963 stl_items[curitem].stl_start = p;
4964 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (str != NULL && *str)
4966 {
4967 t = str;
4968 if (itemisflag)
4969 {
4970 if ((t[0] && t[1])
4971 && ((!prevchar_isitem && *t == ',')
4972 || (prevchar_isflag && *t == ' ')))
4973 t++;
4974 prevchar_isflag = TRUE;
4975 }
4976 l = vim_strsize(t);
4977 if (l > 0)
4978 prevchar_isitem = TRUE;
4979 if (l > maxwid)
4980 {
4981 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (has_mbyte)
4983 {
4984 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004985 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 l -= byte2cells(*t++);
4989 if (p + 1 >= out + outlen)
4990 break;
4991 *p++ = '<';
4992 }
4993 if (minwid > 0)
4994 {
4995 for (; l < minwid && p + 1 < out + outlen; l++)
4996 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004997 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4999 *p++ = ' ';
5000 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005001 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 minwid = 0;
5004 }
5005 else
5006 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005007 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005009 // Change a space by fillchar, unless fillchar is '-' and a
5010 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005011 if (fillable && *t == ' '
5012 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5013 MB_CHAR2BYTES(fillchar, p);
5014 else
5015 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005018 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else if (num >= 0)
5021 {
5022 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5023 char_u nstr[20];
5024
5025 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005026 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 prevchar_isitem = TRUE;
5028 t = nstr;
5029 if (opt == STL_VIRTCOL_ALT)
5030 {
5031 *t++ = '-';
5032 minwid--;
5033 }
5034 *t++ = '%';
5035 if (zeropad)
5036 *t++ = '0';
5037 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005038 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *t = 0;
5040
5041 for (n = num, l = 1; n >= nbase; n /= nbase)
5042 l++;
5043 if (opt == STL_VIRTCOL_ALT)
5044 l++;
5045 if (l > maxwid)
5046 {
5047 l += 2;
5048 n = l - maxwid;
5049 while (l-- > maxwid)
5050 num /= nbase;
5051 *t++ = '>';
5052 *t++ = '%';
5053 *t = t[-3];
5054 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005055 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5056 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005059 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5060 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 p += STRLEN(p);
5062 }
5063 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005064 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005066 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5067 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5068 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (opt == STL_VIM_EXPR)
5070 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 curitem++;
5072 }
5073 *p = NUL;
5074 itemcnt = curitem;
5075
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005076#ifdef FEAT_EVAL
5077 if (usefmt != fmt)
5078 vim_free(usefmt);
5079#endif
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 width = vim_strsize(out);
5082 if (maxwidth > 0 && width > maxwidth)
5083 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005084 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 l = 0;
5086 if (itemcnt == 0)
5087 s = out;
5088 else
5089 {
5090 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005091 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005093 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005094 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096 }
5097 if (l == itemcnt)
5098 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005099 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005100 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 l = 0;
5102 }
5103 }
5104
5105 if (width - vim_strsize(s) >= maxwidth)
5106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005107 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (has_mbyte)
5109 {
5110 s = out;
5111 width = 0;
5112 for (;;)
5113 {
5114 width += ptr2cells(s);
5115 if (width >= maxwidth)
5116 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005117 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005119 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005121 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 s = out + maxwidth - 1;
5125 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005126 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
5128 itemcnt = l;
5129 *s++ = '>';
5130 *s = 0;
5131 }
5132 else
5133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (has_mbyte)
5135 {
5136 n = 0;
5137 while (width >= maxwidth)
5138 {
5139 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005140 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 }
5143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 n = width - maxwidth + 1;
5145 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005146 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 *s = '<';
5148
Bram Moolenaarc667da52019-11-30 20:52:27 +01005149 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 for (; l < itemcnt; l++)
5151 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005152 if (stl_items[l].stl_start - n >= s)
5153 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005155 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
zeertzjqd392a742023-07-01 20:24:40 +01005157
5158 // Fill up for half a double-wide character.
5159 while (++width < maxwidth)
5160 {
5161 s = s + STRLEN(s);
5162 MB_CHAR2BYTES(fillchar, s);
5163 *s = NUL;
5164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 width = maxwidth;
5167 }
5168 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5169 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005170 // Find how many separators there are, which we will use when
5171 // figuring out how many groups there are.
5172 int num_separators = 0;
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005176 if (stl_items[l].stl_type == Separate)
5177 {
5178 // Create an array of the start location for each separator
5179 // mark.
5180 stl_separator_locations[num_separators] = l;
5181 num_separators++;
5182 }
5183 }
5184
5185 // If we have separated groups, then we deal with it now
5186 if (num_separators)
5187 {
5188 int standard_spaces;
5189 int final_spaces;
5190
5191 standard_spaces = (maxwidth - width) / num_separators;
5192 final_spaces = (maxwidth - width) -
5193 standard_spaces * (num_separators - 1);
5194 for (l = 0; l < num_separators; l++)
5195 {
5196 int dislocation = (l == (num_separators - 1)) ?
5197 final_spaces : standard_spaces;
5198 dislocation *= MB_CHAR2LEN(fillchar);
5199 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5200 char_u *seploc = start + dislocation;
5201 STRMOVE(seploc, start);
5202 for (s = start; s < seploc;)
5203 MB_CHAR2BYTES(fillchar, s);
5204
5205 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5206 stl_items[i].stl_start += dislocation;
5207 }
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 width = maxwidth;
5210 }
5211 }
5212
Bram Moolenaarc667da52019-11-30 20:52:27 +01005213 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005214 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005216 *hltab = stl_hltab;
5217 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 for (l = 0; l < itemcnt; l++)
5219 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005220 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005222 sp->start = stl_items[l].stl_start;
5223 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005224 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005227 sp->start = NULL;
5228 sp->userhl = 0;
5229 }
5230
Bram Moolenaarc667da52019-11-30 20:52:27 +01005231 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005232 if (tabtab != NULL)
5233 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005234 *tabtab = stl_tabtab;
5235 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005236 for (l = 0; l < itemcnt; l++)
5237 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005238 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005239 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005240 sp->start = stl_items[l].stl_start;
5241 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005242 sp++;
5243 }
5244 }
5245 sp->start = NULL;
5246 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005249 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005250
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005251 // A user function may reset KeyTyped, restore it.
5252 KeyTyped = save_KeyTyped;
5253
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005254 // Check for an error. If there is one the display will be messed up and
5255 // might loop redrawing. Avoid that by making the corresponding option
5256 // empty.
5257 // TODO: find out why using called_emsg_before makes tests fail, does it
5258 // matter?
5259 // if (called_emsg > called_emsg_before)
5260 if (did_emsg > did_emsg_before)
5261 set_string_option_direct(opt_name, -1, (char_u *)"",
5262 OPT_FREE | opt_scope, SID_ERROR);
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return width;
5265}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005266#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005269 * Get relative cursor position in window into "buf[buflen]", in the localized
5270 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
5272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005273get_rel_pos(
5274 win_T *wp,
5275 char_u *buf,
5276 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005278 long above; // number of lines above window
5279 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaarc667da52019-11-30 20:52:27 +01005281 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005282 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 above = wp->w_topline - 1;
5284#ifdef FEAT_DIFF
5285 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005286 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005287 above = 0; // All buffer lines are displayed and there is an
5288 // indication of filler lines, that can be considered
5289 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290#endif
5291 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5292 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005293 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
Emir SARI971cd2b2023-04-29 12:09:53 +01005294 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005296 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005298 {
5299 int perc = (above > 1000000L)
5300 ? (int)(above / ((above + below) / 100L))
5301 : (int)(above * 100L / (above + below));
5302
5303 char *p = (char *)buf;
5304 size_t l = buflen;
5305 if (perc < 10)
5306 {
5307 // prepend one space
5308 buf[0] = ' ';
5309 ++p;
5310 --l;
5311 }
5312 // localized percentage value
5313 vim_snprintf(p, l, _("%d%%"), perc);
5314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005318 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 * Return TRUE if it was appended.
5320 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005322append_arg_number(
5323 win_T *wp,
5324 char_u *buf,
5325 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005326 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005328 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 return FALSE;
5330
Bram Moolenaara8490a42023-05-23 18:00:58 +01005331 char *msg;
5332 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005334 case 0: msg = _(" (%d of %d)"); break;
5335 case 1: msg = _(" ((%d) of %d)"); break;
5336 case 2: msg = _(" (file %d of %d)"); break;
5337 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005339
5340 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5341 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5342 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return TRUE;
5344}
5345
5346/*
5347 * If fname is not a full path, make it a full path.
5348 * Returns pointer to allocated memory (NULL for failure).
5349 */
5350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005351fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 /*
5354 * Force expanding the path always for Unix, because symbolic links may
5355 * mess up the full path name, even though it starts with a '/'.
5356 * Also expand when there is ".." in the file name, try to remove it,
5357 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005358 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 * For MS-Windows also expand names like "longna~1" to "longname".
5360 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005361#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 return FullName_save(fname, TRUE);
5363#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005364 if (!vim_isAbsName(fname)
5365 || strstr((char *)fname, "..") != NULL
5366 || strstr((char *)fname, "//") != NULL
5367# ifdef BACKSLASH_IN_FILENAME
5368 || strstr((char *)fname, "\\\\") != NULL
5369# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005370# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 )
5374 return FullName_save(fname, FALSE);
5375
5376 fname = vim_strsave(fname);
5377
Bram Moolenaar9b942202007-10-03 12:31:33 +00005378# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005379 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005380 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 return fname;
5384#endif
5385}
5386
5387/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005388 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5389 * "*ffname" becomes a pointer to allocated memory (or NULL).
5390 * When resolving a link both "*sfname" and "*ffname" will point to the same
5391 * allocated memory.
5392 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005393 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005396fname_expand(
5397 buf_T *buf UNUSED,
5398 char_u **ffname,
5399 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005401 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005403 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005405 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
5407#ifdef FEAT_SHORTCUT
5408 if (!buf->b_p_bin)
5409 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005410 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005412 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005413 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005414 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 vim_free(*ffname);
5417 *ffname = rfname;
5418 *sfname = rfname;
5419 }
5420 }
5421#endif
5422}
5423
5424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 * Open a window for a number of buffers.
5426 */
5427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005428ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 buf_T *buf;
5431 win_T *wp, *wpnext;
5432 int split_ret = OK;
5433 int p_ea_save;
5434 int open_wins = 0;
5435 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005436 int count; // Maximum number of windows to open.
5437 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005438 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005439 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
Bram Moolenaarc667da52019-11-30 20:52:27 +01005441 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 count = 9999;
5443 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005444 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5446 all = FALSE;
5447 else
5448 all = TRUE;
5449
Pavel Mayorove1121b12023-02-20 14:35:20 +00005450 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5451 // switching to another buffer.
5452 reset_VIsual_and_resel();
5453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 setpcmark();
5455
5456#ifdef FEAT_GUI
5457 need_mouse_correct = TRUE;
5458#endif
5459
5460 /*
5461 * Close superfluous windows (two windows for the same buffer).
5462 * Also close windows that are not full-width.
5463 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005464 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005465 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005466 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005468 tpnext = curtab->tp_next;
5469 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005471 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005472 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005473 || ((cmdmod.cmod_split & WSP_VERT)
5474 ? wp->w_height + wp->w_status_height < Rows - p_ch
5475 - tabline_height()
5476 : wp->w_width != Columns)
5477 || (had_tab > 0 && wp != firstwin))
5478 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005479 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005480 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005481 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005482 if (win_close(wp, FALSE) == FAIL)
5483 break;
5484 // Just in case an autocommand does something strange with
5485 // windows: start all over...
5486 wpnext = firstwin;
5487 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005488 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005489 }
5490 else
5491 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005493
Bram Moolenaarc667da52019-11-30 20:52:27 +01005494 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005495 if (had_tab == 0 || tpnext == NULL)
5496 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005497 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499
5500 /*
5501 * Go through the buffer list. When a buffer doesn't have a window yet,
5502 * open one. Otherwise move the window to the right position.
5503 * Watch out for autocommands that delete buffers or windows!
5504 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005505 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5510 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005511 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5513 continue;
5514
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005515 if (had_tab != 0)
5516 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005517 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005518 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005519 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005520 else
5521 wp = NULL;
5522 }
5523 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005524 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005525 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005526 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005527 if (wp->w_buffer == buf)
5528 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005529 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005530 if (wp != NULL)
5531 win_move_after(wp, curwin);
5532 }
5533
5534 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005536 bufref_T bufref;
5537
5538 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005539
Bram Moolenaarc667da52019-11-30 20:52:27 +01005540 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005542 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5544 ++open_wins;
5545 p_ea = p_ea_save;
5546 if (split_ret == FAIL)
5547 continue;
5548
Bram Moolenaarc667da52019-11-30 20:52:27 +01005549 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005552 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005554 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 break;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (swap_exists_action == SEA_QUIT)
5559 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005560#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005561 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005562
Bram Moolenaarc667da52019-11-30 20:52:27 +01005563 // Reset the error/interrupt/exception state here so that
5564 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005565 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005566#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005567
Bram Moolenaarc667da52019-11-30 20:52:27 +01005568 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 win_close(curwin, TRUE);
5570 --open_wins;
5571 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005572 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005573
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005574#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005575 // Restore the error/interrupt/exception state if not
5576 // discarded by a new aborting error, interrupt, or uncaught
5577 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005578 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581 else
5582 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584
5585 ui_breakcheck();
5586 if (got_int)
5587 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005588 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 break;
5590 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005591#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005592 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005593 if (aborting())
5594 break;
5595#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005596 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005597 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005598 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005601 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604 /*
5605 * Close superfluous windows.
5606 */
5607 for (wp = lastwin; open_wins > count; )
5608 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005609 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (!win_valid(wp))
5612 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005613 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 wp = lastwin;
5615 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005616 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005618 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 --open_wins;
5620 wp = lastwin;
5621 }
5622 else
5623 {
5624 wp = wp->w_prev;
5625 if (wp == NULL)
5626 break;
5627 }
5628 }
5629}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005632static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634/*
5635 * do_modelines() - process mode lines for the current file
5636 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005637 * "flags" can be:
5638 * OPT_WINONLY only set options local to window
5639 * OPT_NOWIN don't set options local to window
5640 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 * Returns immediately if the "ml" option isn't set.
5642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005644do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005646 linenr_T lnum;
5647 int nmlines;
5648 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5651 return;
5652
Bram Moolenaarc667da52019-11-30 20:52:27 +01005653 // Disallow recursive entry here. Can happen when executing a modeline
5654 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (entered)
5656 return;
5657
5658 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005659 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005661 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 nmlines = 0;
5663
Hu Jialun9dcd3492021-08-28 20:42:50 +02005664 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005666 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 nmlines = 0;
5668 --entered;
5669}
5670
Bram Moolenaarc667da52019-11-30 20:52:27 +01005671#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
5673/*
5674 * chk_modeline() - check a single line for a mode string
5675 * Return FAIL if an error encountered.
5676 */
5677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005678chk_modeline(
5679 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005680 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 char_u *s;
5683 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005684 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 int prev;
5686 int vers;
5687 int end;
5688 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005689 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005690 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692 prev = -1;
5693 for (s = ml_get(lnum); *s != NUL; ++s)
5694 {
5695 if (prev == -1 || vim_isspace(prev))
5696 {
5697 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5698 || STRNCMP(s, "vi:", (size_t)3) == 0)
5699 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005700 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005701 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
5703 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5704 e = s + 4;
5705 else
5706 e = s + 3;
5707 vers = getdigits(&e);
5708 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005709 && (s[0] != 'V'
5710 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005712 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 || (VIM_VERSION_100 < vers && s[3] == '<')
5714 || (VIM_VERSION_100 > vers && s[3] == '>')
5715 || (VIM_VERSION_100 == vers && s[3] == '=')))
5716 break;
5717 }
5718 }
5719 prev = *s;
5720 }
5721
5722 if (*s)
5723 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005724 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 ++s;
5726 while (s[-1] != ':');
5727
Bram Moolenaarc667da52019-11-30 20:52:27 +01005728 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (linecopy == NULL)
5730 return FAIL;
5731
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005732 // prepare for emsg()
5733 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005734 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 end = FALSE;
5737 while (end == FALSE)
5738 {
5739 s = skipwhite(s);
5740 if (*s == NUL)
5741 break;
5742
5743 /*
5744 * Find end of set command: ':' or end of line.
5745 * Skip over "\:", replacing it with ":".
5746 */
5747 for (e = s; *e != ':' && *e != NUL; ++e)
5748 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005749 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (*e == NUL)
5751 end = TRUE;
5752
5753 /*
5754 * If there is a "set" command, require a terminating ':' and
5755 * ignore the stuff after the ':'.
5756 * "vi:set opt opt opt: foo" -- foo not interpreted
5757 * "vi:opt opt opt: foo" -- foo interpreted
5758 * Accept "se" for compatibility with Elvis.
5759 */
5760 if (STRNCMP(s, "set ", (size_t)4) == 0
5761 || STRNCMP(s, "se ", (size_t)3) == 0)
5762 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005763 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 break;
5765 end = TRUE;
5766 s = vim_strchr(s, ' ') + 1;
5767 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005768 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
Bram Moolenaarc667da52019-11-30 20:52:27 +01005770 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005772 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005773
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005774 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005775 current_sctx.sc_version = 1;
5776#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005777 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005778 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005779 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005781
Bram Moolenaar5958f952018-11-20 04:25:21 +01005782 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005783 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005784
Bram Moolenaara3227e22006-03-08 21:32:40 +00005785 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005786
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005787 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005788 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005789 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 break;
5791 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005792 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
5794
ichizok7e5fe382023-04-15 13:17:50 +01005795 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005796 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 vim_free(linecopy);
5798 }
5799 return retval;
5800}
5801
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005802/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005803 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5804 */
5805 int
5806bt_normal(buf_T *buf)
5807{
5808 return buf != NULL && buf->b_p_bt[0] == NUL;
5809}
5810
5811/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005812 * Return TRUE if "buf" is the quickfix buffer.
5813 */
5814 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005815bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005816{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005817#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005818 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005819#else
5820 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005821#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005822}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005823
5824/*
5825 * Return TRUE if "buf" is a terminal buffer.
5826 */
5827 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005828bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005829{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005830#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005831 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005832#else
5833 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005834#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005835}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005836
5837/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005838 * Return TRUE if "buf" is a help buffer.
5839 */
5840 int
5841bt_help(buf_T *buf)
5842{
5843 return buf != NULL && buf->b_help;
5844}
5845
5846/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005847 * Return TRUE if "buf" is a prompt buffer.
5848 */
5849 int
5850bt_prompt(buf_T *buf)
5851{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005852 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5853}
5854
Dominique Pelle748b3082022-01-08 12:41:16 +00005855#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005856/*
5857 * Return TRUE if "buf" is a buffer for a popup window.
5858 */
5859 int
5860bt_popup(buf_T *buf)
5861{
5862 return buf != NULL && buf->b_p_bt != NULL
5863 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005864}
Dominique Pelle748b3082022-01-08 12:41:16 +00005865#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005866
5867/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005868 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005869 * buffer. This means the buffer name may not be a file name, at least not for
5870 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005871 */
5872 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005873bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005874{
5875 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5876 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005877 || buf->b_p_bt[0] == 't'
5878 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005879}
5880
Bram Moolenaarc3126192022-08-26 12:58:17 +01005881/*
5882 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5883 * buffer. This means the buffer is not to be read from a file.
5884 */
5885 static int
5886bt_nofileread(buf_T *buf)
5887{
5888 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5889 || buf->b_p_bt[0] == 't'
5890 || buf->b_p_bt[0] == 'q'
5891 || buf->b_p_bt[0] == 'p');
5892}
5893
Dominique Pelle748b3082022-01-08 12:41:16 +00005894#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005895/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005896 * Return TRUE if "buf" has 'buftype' set to "nofile".
5897 */
5898 int
5899bt_nofile(buf_T *buf)
5900{
5901 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5902}
Dominique Pelle748b3082022-01-08 12:41:16 +00005903#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005904
5905/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005906 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5907 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005908 */
5909 int
5910bt_dontwrite(buf_T *buf)
5911{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005912 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005913 || buf->b_p_bt[0] == 't'
5914 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005915}
5916
5917 int
5918bt_dontwrite_msg(buf_T *buf)
5919{
5920 if (bt_dontwrite(buf))
5921 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005922 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005923 return TRUE;
5924 }
5925 return FALSE;
5926}
5927
5928/*
5929 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5930 * and 'bufhidden'.
5931 */
5932 int
5933buf_hide(buf_T *buf)
5934{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005935 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005936 switch (buf->b_p_bh[0])
5937 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005938 case 'u': // "unload"
5939 case 'w': // "wipe"
5940 case 'd': return FALSE; // "delete"
5941 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005942 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005943 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005944}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
5946/*
5947 * Return special buffer name.
5948 * Returns NULL when the buffer has a normal file name.
5949 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005950 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005951buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005953#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005955 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005956 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005957 * Differentiate between the quickfix and location list buffers using
5958 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005959 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005960 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005961 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005962 else
5963 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005966
Bram Moolenaarc667da52019-11-30 20:52:27 +01005967 // There is no _file_ when 'buftype' is "nofile", b_sfname
5968 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005969 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005971#ifdef FEAT_TERMINAL
5972 if (buf->b_term != NULL)
5973 return term_get_status_text(buf->b_term);
5974#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005975 if (buf->b_fname != NULL)
5976 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005977 if (buf == cmdwin_buf)
5978 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005979#ifdef FEAT_JOB_CHANNEL
5980 if (bt_prompt(buf))
5981 return (char_u *)_("[Prompt]");
5982#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005983#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005984 if (bt_popup(buf))
5985 return (char_u *)_("[Popup]");
5986#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005987 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005991 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 return NULL;
5993}
5994
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005996 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5997 */
5998 char_u *
5999buf_get_fname(buf_T *buf)
6000{
6001 if (buf->b_fname == NULL)
6002 return (char_u *)_("[No Name]");
6003 return buf->b_fname;
6004}
6005
6006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6008 */
6009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006010set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006012 if (on == curbuf->b_p_bl)
6013 return;
6014
6015 curbuf->b_p_bl = on;
6016 if (on)
6017 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6018 else
6019 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020}
6021
6022/*
6023 * Read the file for "buf" again and check if the contents changed.
6024 * Return TRUE if it changed or this could not be checked.
6025 */
6026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 buf_T *newbuf;
6030 int differ = TRUE;
6031 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 exarg_T ea;
6034
Bram Moolenaarc667da52019-11-30 20:52:27 +01006035 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6037 if (newbuf == NULL)
6038 return TRUE;
6039
Bram Moolenaarc667da52019-11-30 20:52:27 +01006040 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (prep_exarg(&ea, buf) == FAIL)
6042 {
6043 wipe_buffer(newbuf, FALSE);
6044 return TRUE;
6045 }
6046
Bram Moolenaare76062c2022-11-28 18:51:43 +00006047 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006049 if (curbuf != newbuf)
6050 {
6051 // Failed to find a window for "newbuf".
6052 wipe_buffer(newbuf, FALSE);
6053 return TRUE;
6054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006056 // We don't want to trigger autocommands now, they may have nasty
6057 // side-effects like wiping buffers
6058 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006059 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 && readfile(buf->b_ffname, buf->b_fname,
6061 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6062 &ea, READ_NEW | READ_DUMMY) == OK)
6063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006064 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6066 {
6067 differ = FALSE;
6068 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6069 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6070 {
6071 differ = TRUE;
6072 break;
6073 }
6074 }
6075 }
6076 vim_free(ea.cmd);
6077
Bram Moolenaarc667da52019-11-30 20:52:27 +01006078 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaarc667da52019-11-30 20:52:27 +01006081 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 wipe_buffer(newbuf, FALSE);
6083
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006084 unblock_autocmds();
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 return differ;
6087}
6088
6089/*
6090 * Wipe out a buffer and decrement the last buffer number if it was used for
6091 * this buffer. Call this to wipe out a temp buffer that does not contain any
6092 * marks.
6093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095wipe_buffer(
6096 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006097 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 if (buf->b_fnum == top_file_num - 1)
6100 --top_file_num;
6101
Bram Moolenaarc667da52019-11-30 20:52:27 +01006102 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006103 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006104
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006105 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006108 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109}