blob: 48e8cb63bc176647832a344d7129aa3860fb5643 [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);
John Marriottec032de2025-04-10 21:34:19 +020048static int append_arg_number(win_T *wp, char_u *buf, size_t buflen, int add_file);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static 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
Christian Brabandtbaa8c902025-04-19 11:14:11 +020075 static void
76trigger_undo_ftplugin(buf_T *buf, win_T *win)
77{
78 window_layout_lock();
79 buf->b_locked++;
80 win->w_locked = TRUE;
81 // b:undo_ftplugin may be set, undo it
82 do_cmdline_cmd((char_u*)"if exists('b:undo_ftplugin') | :legacy :exe \
83 b:undo_ftplugin | endif");
84 buf->b_locked--;
85 win->w_locked = FALSE;
86 window_layout_unlock();
87}
88
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020089/*
John Marriottec032de2025-04-10 21:34:19 +020090 * Calculate the percentage that `part` is of the `whole`.
91 */
92 static int
93calc_percentage(long part, long whole)
94{
95 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
96 // causes an overflow, thus for large numbers divide instead.
97 return (part > 1000000L)
98 ? (int)(part / (whole / 100L))
99 : (int)((part * 100L) / whole);
100}
101
102/*
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200103 * Return the highest possible buffer number.
104 */
105 int
106get_highest_fnum(void)
107{
108 return top_file_num - 1;
109}
110
Bram Moolenaarc024b462019-06-08 18:07:21 +0200111/*
112 * Read data from buffer for retrying.
113 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 static int
115read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100116 int read_stdin, // read file from stdin, otherwise fifo
117 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
118 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200119{
120 int retval = OK;
121 linenr_T line_count;
122
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100123 // Read from the buffer which the text is already filled in and append at
124 // the end. This makes it possible to retry when 'fileformat' or
125 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 line_count = curbuf->b_ml.ml_line_count;
127 retval = readfile(
128 read_stdin ? NULL : curbuf->b_ffname,
129 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100130 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200131 flags | READ_BUFFER);
132 if (retval == OK)
133 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100134 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200135 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200136 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137 }
138 else
139 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100140 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200141 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200142 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200143 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100144 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200145 curwin->w_cursor.lnum = 1;
146 curwin->w_cursor.col = 0;
147
148 if (read_stdin)
149 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100150 // Set or reset 'modified' before executing autocommands, so that
151 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100152 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200153 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100154 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200155 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200156
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100157 if (retval == OK)
158 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100159#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100160 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100161 curbuf, &retval);
162#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100163 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200164#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100165 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200166 }
167 return retval;
168}
169
Dominique Pelle748b3082022-01-08 12:41:16 +0000170#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200172 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
173 */
174 void
175buffer_ensure_loaded(buf_T *buf)
176{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000177 if (buf->b_ml.ml_mfp != NULL)
178 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200179
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000180 aco_save_T aco;
181
182 // Make sure the buffer is in a window. If not then skip it.
183 aucmd_prepbuf(&aco, buf);
184 if (curbuf == buf)
185 {
186 if (swap_exists_action != SEA_READONLY)
187 swap_exists_action = SEA_NONE;
188 open_buffer(FALSE, NULL, 0);
189 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200190 }
191}
Dominique Pelle748b3082022-01-08 12:41:16 +0000192#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200193
194/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200195 * Open current buffer, that is: open the memfile and read the file into
196 * memory.
197 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100200open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100201 int read_stdin, // read file from stdin
202 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100203 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100205 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200207 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100208#ifdef FEAT_SYN_HL
209 long old_tw = curbuf->b_p_tw;
210#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200211 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100213 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
214 // When re-entering the same buffer, it should not change, because the
215 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (readonlymode && curbuf->b_ffname != NULL
217 && (curbuf->b_flags & BF_NEVERLOADED))
218 curbuf->b_p_ro = TRUE;
219
Bram Moolenaar4770d092006-01-12 23:22:24 +0000220 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100222 // There MUST be a memfile, otherwise we can't do anything
223 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100224 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200225 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (curbuf->b_ml.ml_mfp != NULL)
227 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100228 // If there is no memfile at all, exit.
229 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (curbuf == NULL)
231 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000232 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200233
234 // Don't try to do any saving, with "curbuf" NULL almost nothing
235 // will work.
236 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 getout(2);
238 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200239
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000240 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100242#ifdef FEAT_SYN_HL
243 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +0200244 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 return FAIL;
247 }
248
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100249 // Do not sync this buffer yet, may first want to read the file.
250 if (curbuf->b_ml.ml_mfp != NULL)
251 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
252
Bram Moolenaarc667da52019-11-30 20:52:27 +0100253 // The autocommands in readfile() may change the buffer, but only AFTER
254 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200255 set_bufref(&old_curbuf, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +0200256 curbuf->b_modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
Bram Moolenaarc667da52019-11-30 20:52:27 +0100258 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100259 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100261 // A buffer without an actual file should not use the buffer name to read a
262 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100263 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100264 flags |= READ_NOFILE;
265
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100266 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (curbuf->b_ffname != NULL
268#ifdef FEAT_NETBEANS_INTG
269 && netbeansReadFile
270#endif
271 )
272 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100273 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200274#ifdef UNIX
275 int save_bin = curbuf->b_p_bin;
276 int perm;
277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifdef FEAT_NETBEANS_INTG
279 int oldFire = netbeansFireChanges;
280
281 netbeansFireChanges = 0;
282#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200283#ifdef UNIX
284 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200285 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200286 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200287# ifdef OPEN_CHR_FILES
288 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
289# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200290 ))
291 read_fifo = TRUE;
292 if (read_fifo)
293 curbuf->b_p_bin = TRUE;
294#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100295 if (shortmess(SHM_FILEINFO))
296 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200298 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200299 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
300#ifdef UNIX
301 if (read_fifo)
302 {
303 curbuf->b_p_bin = save_bin;
304 if (retval == OK)
Christian Brabandtf1c31342025-02-23 09:36:56 +0100305 // don't add READ_FIFO here, otherwise we won't be able to
306 // detect the encoding
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200307 retval = read_buffer(FALSE, eap, flags);
308 }
309#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100310 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_NETBEANS_INTG
312 netbeansFireChanges = oldFire;
313#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100314 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200315 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 fix_help_buffer();
317 }
318 else if (read_stdin)
319 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200320 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100322 // First read the text in binary mode into the buffer.
323 // Then read from that same buffer and append at the end. This makes
324 // it possible to retry when 'fileformat' or 'fileencoding' was
325 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curbuf->b_p_bin = TRUE;
327 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200328 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
329 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 curbuf->b_p_bin = save_bin;
331 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200332 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
334
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100335 // Can now sync this buffer in ml_sync_all().
336 if (curbuf->b_ml.ml_mfp != NULL
337 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
338 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
339
Bram Moolenaarc667da52019-11-30 20:52:27 +0100340 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100344 parse_cino(curbuf);
345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100347 // Set/reset the Changed flag first, autocmds may change the buffer.
348 // Apply the automatic commands, before processing the modelines.
349 // So the modelines have priority over autocommands.
350 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100351 // When reading stdin, the buffer contents always needs writing, so set
352 // the changed flag. Unless in readonly mode: "ls | gview -".
353 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000354 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200355 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100356#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000359 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100361 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200362 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100363 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
Bram Moolenaarc667da52019-11-30 20:52:27 +0100365 // Set last_changedtick to avoid triggering a TextChanged autocommand right
366 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100367 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100368 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100369 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100370
Bram Moolenaarc667da52019-11-30 20:52:27 +0100371 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#ifdef FEAT_EVAL
373 if (aborting())
374#else
375 if (got_int)
376#endif
377 curbuf->b_flags |= BF_READERR;
378
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000379#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100380 // Need to update automatic folding. Do this before the autocommands,
381 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000382 foldUpdateAll(curwin);
383#endif
384
Bram Moolenaarc667da52019-11-30 20:52:27 +0100385 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (!(curwin->w_valid & VALID_TOPLINE))
387 {
388 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100389#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100393#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100395#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#endif
398
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000399 if (retval != OK)
400 return retval;
401
402 // The autocommands may have changed the current buffer. Apply the
403 // modelines to the correct buffer, if it still exists and is loaded.
404 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000406 aco_save_T aco;
407
408 // Go to the buffer that was opened, make sure it is in a window.
409 // If not then skip it.
410 aucmd_prepbuf(&aco, old_curbuf.br_buf);
411 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000413 do_modelines(0);
414 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000416 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100417#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000418 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
419 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100420#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000421 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
422 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000425 // restore curwin/curbuf and a few other things
426 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 return retval;
431}
432
433/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200434 * Store "buf" in "bufref" and set the free count.
435 */
436 void
437set_bufref(bufref_T *bufref, buf_T *buf)
438{
439 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200440 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200441 bufref->br_buf_free_count = buf_free_count;
442}
443
444/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200445 * Return TRUE if "bufref->br_buf" points to the same buffer as when
446 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200447 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200448 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
449 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200450 */
451 int
452bufref_valid(bufref_T *bufref)
453{
454 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200455 ? TRUE : buf_valid(bufref->br_buf)
456 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200457}
458
459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200461 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 */
463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100464buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 buf_T *bp;
467
Bram Moolenaarc667da52019-11-30 20:52:27 +0100468 // Assume that we more often have a recent buffer, start with the last
469 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200470 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (bp == buf)
472 return TRUE;
473 return FALSE;
474}
475
476/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200477 * A hash table used to quickly lookup a buffer by its number.
478 */
479static hashtab_T buf_hashtab;
480
481 static void
482buf_hashtab_add(buf_T *buf)
483{
John Marriottec032de2025-04-10 21:34:19 +0200484 vim_snprintf((char *)buf->b_key, sizeof(buf->b_key), "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000485 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000486 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200487}
488
489 static void
490buf_hashtab_remove(buf_T *buf)
491{
492 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
493
494 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000495 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200496}
497
Bram Moolenaar94f01952018-09-01 15:30:03 +0200498/*
499 * Return TRUE when buffer "buf" can be unloaded.
500 * Give an error message and return FALSE when the buffer is locked or the
501 * screen is being redrawn and the buffer is in a window.
502 */
503 static int
504can_unload_buffer(buf_T *buf)
505{
506 int can_unload = !buf->b_locked;
507
508 if (can_unload && updating_screen)
509 {
510 win_T *wp;
511
512 FOR_ALL_WINDOWS(wp)
513 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200514 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200515 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200516 break;
517 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200518 }
519 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100520 {
521 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
522
523 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
524 fname != NULL ? fname : (char_u *)"[No Name]");
525 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200526 return can_unload;
527}
Bram Moolenaara997b452018-04-17 23:24:06 +0200528
Bram Moolenaar480778b2016-07-14 22:09:39 +0200529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Close the link to a buffer.
531 * "action" is used when there is no longer a window for the buffer.
532 * It can be:
533 * 0 buffer becomes hidden
534 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100535 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200537 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 * When doing all but the first one on the current buffer, the caller should
539 * get a new buffer very soon!
540 *
541 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100542 *
543 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
544 * cause there to be only one window with this buffer. e.g. when ":quit" is
545 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100546 *
547 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100548 *
549 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100552close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100553 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554 buf_T *buf,
555 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100556 int abort_if_last,
557 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100560 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200561 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100562 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200563 win_T *the_curwin = curwin;
564 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200566 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
567 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaarcee52202020-03-11 14:19:58 +0100569 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100570
571 // Force unloading or deleting when 'bufhidden' says so.
572 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
573 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100574 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200575 {
576 del_buf = TRUE;
577 unload_buf = TRUE;
578 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100579 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200580 {
581 del_buf = TRUE;
582 unload_buf = TRUE;
583 wipe_buf = TRUE;
584 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100585 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200586 unload_buf = TRUE;
587
Bram Moolenaar94053a52017-08-01 21:44:33 +0200588#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100589 // depending on how we get here b_nwindows may already be zero
590 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200591 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100592 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200593 if (term_job_running(buf->b_term))
594 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200595 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200596 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200597 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100598 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200599
Bram Moolenaarc667da52019-11-30 20:52:27 +0100600 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200601 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100602
603 // A terminal buffer is wiped out when job has finished.
604 del_buf = TRUE;
605 unload_buf = TRUE;
606 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200607 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200608 else
609 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100610 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200611 del_buf = FALSE;
612 unload_buf = FALSE;
613 }
614 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100615 else if (buf->b_p_bh[0] == 'h' && !del_buf)
616 {
617 // Hide a terminal buffer.
618 unload_buf = FALSE;
619 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200620 else
621 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100622 if (del_buf || unload_buf)
623 {
624 // A terminal buffer is wiped out if the job has finished.
625 // We only do this when there's an intention to unload the
626 // buffer. This way, :hide and other similar commands won't
627 // wipe the buffer.
628 del_buf = TRUE;
629 unload_buf = TRUE;
630 wipe_buf = TRUE;
631 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200632 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100633 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200634 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // Disallow deleting the buffer when it is locked (already being closed or
638 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200639 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100640 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641
Bram Moolenaarc667da52019-11-30 20:52:27 +0100642 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200643 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100645 // Set b_last_cursor when closing the last window for the buffer.
646 // Remember the last cursor position and window options of the buffer.
647 // This used to be only for the current window, but then options like
648 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (buf->b_nwindows == 1)
650 set_last_cursor(win);
651 buflist_setfpos(buf, win,
652 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
653 win->w_cursor.col, TRUE);
654 }
655
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200656 set_bufref(&bufref, buf);
657
Bram Moolenaarc667da52019-11-30 20:52:27 +0100658 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (buf->b_nwindows == 1)
660 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200661 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100662 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200663 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
664 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200665 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100666 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200668aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000669 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100670 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100671 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200672 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100673 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200674 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100675 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200676 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
Bram Moolenaarc667da52019-11-30 20:52:27 +0100678 // When the buffer becomes hidden, but is not unloaded, trigger
679 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (!unload_buf)
681 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200682 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100683 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200684 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
685 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200686 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100687 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200688 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200689 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100690 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200691 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100692 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200693 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100695#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100696 // autocmds may abort script processing
697 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100698 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200701
Bram Moolenaarc667da52019-11-30 20:52:27 +0100702 // If the buffer was in curwin and the window has changed, go back to that
703 // window, if it still exists. This avoids that ":edit x" triggering a
704 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200705 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
706 {
707 block_autocmds();
708 goto_tabpage_win(the_curtab, the_curwin);
709 unblock_autocmds();
710 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaarc667da52019-11-30 20:52:27 +0100714 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (buf->b_nwindows > 0)
716 --buf->b_nwindows;
717
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100718#ifdef FEAT_DIFF
719 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100720 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100721#endif
722
Bram Moolenaarc667da52019-11-30 20:52:27 +0100723 // Return when a window is displaying the buffer or when it's not
724 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100726 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaarc667da52019-11-30 20:52:27 +0100728 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (buf->b_ffname == NULL)
730 del_buf = TRUE;
731
Bram Moolenaarc667da52019-11-30 20:52:27 +0100732 // When closing the current buffer stop Visual mode before freeing
733 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200734 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200735#if defined(EXITFREE)
736 && !entered_free_all_mem
737#endif
738 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200739 end_visual_mode();
740
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100741 // Free all things allocated for this buffer.
742 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
743 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100744 // Remember if we are closing the current buffer. Restore the number of
745 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 is_curbuf = (buf == curbuf);
747 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100749 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100750 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100751 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaarc667da52019-11-30 20:52:27 +0100753 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200754 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100755 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100756#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100757 // autocmds may abort script processing
758 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100759 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100762 // It's possible that autocommands change curbuf to the one being deleted.
763 // This might cause the previous curbuf to be deleted unexpectedly. But
764 // in some cases it's OK to delete the curbuf, because a new one is
765 // obtained anyway. Therefore only return if curbuf changed to the
766 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100768 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200769
Bram Moolenaar4033c552017-09-16 20:54:51 +0200770 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100771 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200772
Bram Moolenaarc667da52019-11-30 20:52:27 +0100773 // Autocommands may have opened or closed windows for this buffer.
774 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200775 if (buf->b_nwindows > 0)
776 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 /*
779 * Remove the buffer from the list.
780 */
781 if (wipe_buf)
782 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200783 tabpage_T *tp;
784 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200785
Bram Moolenaar347538f2022-03-26 16:28:06 +0000786 // Do not wipe out the buffer if it is used in a window.
787 if (buf->b_nwindows > 0)
788 return FALSE;
789
zeertzjq2e7d89b2024-07-10 19:36:36 +0200790 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200791 mark_forget_file(wp, buf->b_fnum);
792
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200793 if (action == DOBUF_WIPE_REUSE)
794 {
795 // we can re-use this buffer number, store it
796 if (buf_reuse.ga_itemsize == 0)
797 ga_init2(&buf_reuse, sizeof(int), 50);
798 if (ga_grow(&buf_reuse, 1) == OK)
799 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
800 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200801 if (buf->b_sfname != buf->b_ffname)
802 VIM_CLEAR(buf->b_sfname);
803 else
804 buf->b_sfname = NULL;
805 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (buf->b_prev == NULL)
807 firstbuf = buf->b_next;
808 else
809 buf->b_prev->b_next = buf->b_next;
810 if (buf->b_next == NULL)
811 lastbuf = buf->b_prev;
812 else
813 buf->b_next->b_prev = buf->b_prev;
814 free_buffer(buf);
815 }
816 else
817 {
818 if (del_buf)
819 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100820 // Free all internal variables and reset option values, to make
821 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 free_buffer_stuff(buf, TRUE);
823
Bram Moolenaarc667da52019-11-30 20:52:27 +0100824 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
826
Bram Moolenaarc667da52019-11-30 20:52:27 +0100827 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 buf->b_p_initialized = FALSE;
829 }
830 buf_clear_file(buf);
831 if (del_buf)
832 buf->b_p_bl = FALSE;
833 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100834 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100835 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836}
837
838/*
839 * Make buffer not contain a file.
840 */
841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100842buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200845 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100847 buf->b_p_eof = FALSE;
848 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 buf->b_p_eol = TRUE;
850 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000852 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100854 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#ifdef FEAT_NETBEANS_INTG
856 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858}
859
860/*
861 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200862 * the file. Careful: get here with "curwin" NULL when exiting.
863 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100864 * BFA_DEL buffer is going to be deleted
865 * BFA_WIPE buffer is going to be wiped out
866 * BFA_KEEP_UNDO do not free undo information
867 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100870buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200873 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200874 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200875 win_T *the_curwin = curwin;
876 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
Bram Moolenaarc667da52019-11-30 20:52:27 +0100878 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200879 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100880 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200881 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200882 if (buf->b_ml.ml_mfp != NULL)
883 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200884 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
885 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200886 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200888 return;
889 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200890 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200892 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
893 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200894 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100895 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return;
897 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200898 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200900 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
901 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200902 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100903 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 return;
905 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200906 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100907 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200908
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 // If the buffer was in curwin and the window has changed, go back to that
910 // window, if it still exists. This avoids that ":edit x" triggering a
911 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Sean Dewar31be82e2025-05-15 19:59:37 +0200912 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
Bram Moolenaar5a497892016-09-03 16:29:04 +0200913 {
914 block_autocmds();
915 goto_tabpage_win(the_curtab, the_curwin);
916 unblock_autocmds();
917 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200918
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100919#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100920 // autocmds may abort script processing
921 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100925 // It's possible that autocommands change curbuf to the one being deleted.
926 // This might cause curbuf to be deleted unexpectedly. But in some cases
927 // it's OK to delete the curbuf, because a new one is obtained anyway.
928 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (buf == curbuf && !is_curbuf)
930 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100932 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200934#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100935 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200936 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100937 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200938#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000939
940#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100941 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000942 {
943 win_T *win;
944 tabpage_T *tp;
945
946 FOR_ALL_TAB_WINDOWS(tp, win)
947 if (win->w_buffer == buf)
948 clearFolding(win);
949 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000950#endif
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#ifdef FEAT_TCL
953 tcl_buffer_free(buf);
954#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100955 ml_close(buf, TRUE); // close and delete the memline/memfile
956 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200957 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100958 // free the memory allocated for undo
959 // and reset all undo information
960 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100962 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100964#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100965 clear_buf_prop_types(buf);
966#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100967 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Free a buffer structure and the things it contains related to the buffer
972 * itself (not the file, that must have been done already).
973 */
974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100975free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200977 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200979#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100980 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000981 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200982 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200983 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200984#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200985#ifdef FEAT_LUA
986 lua_buffer_free(buf);
987#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000988#ifdef FEAT_MZSCHEME
989 mzscheme_buffer_free(buf);
990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_PERL
992 perl_buf_free(buf);
993#endif
994#ifdef FEAT_PYTHON
995 python_buffer_free(buf);
996#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997#ifdef FEAT_PYTHON3
998 python3_buffer_free(buf);
999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_RUBY
1001 ruby_buffer_free(buf);
1002#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001003#ifdef FEAT_JOB_CHANNEL
1004 channel_buffer_free(buf);
1005#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001006#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001007 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001008#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001009#ifdef FEAT_JOB_CHANNEL
1010 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001011 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +02001012 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +02001013#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +02001014
1015 buf_hashtab_remove(buf);
1016
Bram Moolenaar9280e3f2016-07-14 23:03:19 +02001017 aubuflocal_remove(buf);
1018
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001019 if (autocmd_busy)
1020 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001021 // Do not free the buffer structure while autocommands are executing,
1022 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001023 buf->b_next = au_pending_free_buf;
1024 au_pending_free_buf = buf;
1025 }
1026 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001027 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001028 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001029 if (curbuf == buf)
1030 curbuf = NULL; // make clear it's not to be used
1031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001035 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001036 */
1037 static void
1038init_changedtick(buf_T *buf)
1039{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001040 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001041
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001042 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1043 di->di_tv.v_type = VAR_NUMBER;
1044 di->di_tv.v_lock = VAR_FIXED;
1045 di->di_tv.vval.v_number = 0;
1046
Bram Moolenaar92769c32017-02-25 15:41:37 +01001047#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001048 STRCPY(buf->b_ct_di.di_key, "changedtick");
1049 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001050#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001051}
1052
1053/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001054 * Free the b_wininfo list for buffer "buf".
1055 */
1056 static void
1057clear_wininfo(buf_T *buf)
1058{
1059 wininfo_T *wip;
1060
1061 while (buf->b_wininfo != NULL)
1062 {
1063 wip = buf->b_wininfo;
1064 buf->b_wininfo = wip->wi_next;
1065 free_wininfo(wip);
1066 }
1067}
1068
1069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1071 */
1072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073free_buffer_stuff(
1074 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001075 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 if (free_options)
1078 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001079 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001081#ifdef FEAT_SPELL
1082 ga_clear(&buf->b_s.b_langp);
1083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001086 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001087 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001088
Bram Moolenaarc667da52019-11-30 20:52:27 +01001089 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001090 hash_init(&buf->b_vars->dv_hashtab);
1091 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001092 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001093 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001096 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001098 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001100#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001101 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001102#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001103#ifdef FEAT_PROP_POPUP
1104 ga_clear_strings(&buf->b_textprop_text);
1105#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001106 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1107 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001108 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109}
1110
1111/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001112 * Free one wininfo_T.
1113 */
1114 void
1115free_wininfo(wininfo_T *wip)
1116{
1117 if (wip->wi_optset)
1118 {
1119 clear_winopt(&wip->wi_opt);
1120#ifdef FEAT_FOLDING
1121 deleteFoldRecurse(&wip->wi_folds);
1122#endif
1123 }
1124 vim_free(wip);
1125}
1126
1127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * Go to another buffer. Handles the result of the ATTENTION dialog.
1129 */
1130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001131goto_buffer(
1132 exarg_T *eap,
1133 int start,
1134 int dir,
1135 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001137 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001138 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001139 int skip_help_buf;
1140
1141 switch (eap->cmdidx)
1142 {
1143 case CMD_bnext:
1144 case CMD_sbnext:
1145 case CMD_bNext:
1146 case CMD_bprevious:
1147 case CMD_sbNext:
1148 case CMD_sbprevious:
1149 skip_help_buf = TRUE;
1150 break;
1151 default:
1152 skip_help_buf = FALSE;
1153 break;
1154 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001155
1156 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar188639d2022-04-04 16:57:21 +01001158 if (swap_exists_action == SEA_NONE)
1159 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001160 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1161 (eap->forceit ? DOBUF_FORCEIT : 0) |
1162 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1164 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001165#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001166 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001167
Bram Moolenaarc667da52019-11-30 20:52:27 +01001168 // Reset the error/interrupt/exception state here so that
1169 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001170 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001171#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001172
Bram Moolenaarc667da52019-11-30 20:52:27 +01001173 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001175 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001176 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001177
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001178#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001179 // Restore the error/interrupt/exception state if not discarded by a
1180 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001181 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001185 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188/*
1189 * Handle the situation of swap_exists_action being set.
1190 * It is allowed for "old_curbuf" to be NULL or invalid.
1191 */
1192 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001193handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001195#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001196 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001197#endif
1198#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001199 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001200#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001201 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (swap_exists_action == SEA_QUIT)
1204 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001205#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001206 // Reset the error/interrupt/exception state here so that
1207 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001208 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001209#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001210
Bram Moolenaarc667da52019-11-30 20:52:27 +01001211 // User selected Quit at ATTENTION prompt. Go back to previous
1212 // buffer. If that buffer is gone or the same as the current one,
1213 // open a new, empty buffer.
1214 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001215 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001216 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001217 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1218 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001219 {
1220 // Block autocommands here because curwin->w_buffer is NULL.
1221 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001222 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001223 unblock_autocmds();
1224 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001225 else
1226 buf = old_curbuf->br_buf;
1227 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001228 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001229 int old_msg_silent = msg_silent;
1230
1231 if (shortmess(SHM_FILEINFO))
1232 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001233 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001234 // restore msg_silent, so that the command line will be shown
1235 msg_silent = old_msg_silent;
1236
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001237#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001238 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001239 check_colorcolumn(NULL, curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001240#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001241 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001242 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001243
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001244#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001245 // Restore the error/interrupt/exception state if not discarded by a
1246 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001247 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 else if (swap_exists_action == SEA_RECOVER)
1251 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001252#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001253 // Reset the error/interrupt/exception state here so that
1254 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001255 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001256#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001257
Bram Moolenaarc667da52019-11-30 20:52:27 +01001258 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001260 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001261 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001263 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001264
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001265#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001266 // Restore the error/interrupt/exception state if not discarded by a
1267 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001268 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 swap_exists_action = SEA_NONE;
1272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275 * Make the current buffer empty.
1276 * Used when it is wiped out and it's the last buffer.
1277 */
1278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279empty_curbuf(
1280 int close_others,
1281 int forceit,
1282 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001283{
1284 int retval;
1285 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001286 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001287
1288 if (action == DOBUF_UNLOAD)
1289 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001290 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001291 return FAIL;
1292 }
1293
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001294 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001295 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001296 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001297 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001298
1299 setpcmark();
1300 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1301 forceit ? ECMD_FORCEIT : 0, curwin);
1302
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001303 // do_ecmd() may create a new buffer, then we have to delete
1304 // the old one. But do_ecmd() may have done that already, check
1305 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001306 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001307 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001308 if (!close_others)
1309 need_fileinfo = FALSE;
1310 return retval;
1311}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
1314 * Implementation of the commands for the buffer list.
1315 *
1316 * action == DOBUF_GOTO go to specified buffer
1317 * action == DOBUF_SPLIT split window and go to specified buffer
1318 * action == DOBUF_UNLOAD unload specified buffer(s)
1319 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1320 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001321 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 *
1323 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1324 * start == DOBUF_FIRST go to "count" buffer from first buffer
1325 * start == DOBUF_LAST go to "count" buffer from last buffer
1326 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1327 *
1328 * Return FAIL or OK.
1329 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001330 static int
1331do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332 int action,
1333 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001334 int dir, // FORWARD or BACKWARD
1335 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001336 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 buf_T *buf;
1339 buf_T *bp;
1340 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001341 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 switch (start)
1344 {
1345 case DOBUF_FIRST: buf = firstbuf; break;
1346 case DOBUF_LAST: buf = lastbuf; break;
1347 default: buf = curbuf; break;
1348 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001349 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 while (count-- > 0)
1352 {
1353 do
1354 {
1355 buf = buf->b_next;
1356 if (buf == NULL)
1357 buf = firstbuf;
1358 }
1359 while (buf != curbuf && !bufIsChanged(buf));
1360 }
1361 if (!bufIsChanged(buf))
1362 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001363 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 return FAIL;
1365 }
1366 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001367 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369 while (buf != NULL && buf->b_fnum != count)
1370 buf = buf->b_next;
1371 }
1372 else
1373 {
1374 bp = NULL;
1375 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1376 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001377 // remember the buffer where we start, we come back there when all
1378 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (bp == NULL)
1380 bp = buf;
1381 if (dir == FORWARD)
1382 {
1383 buf = buf->b_next;
1384 if (buf == NULL)
1385 buf = firstbuf;
1386 }
1387 else
1388 {
1389 buf = buf->b_prev;
1390 if (buf == NULL)
1391 buf = lastbuf;
1392 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001393 // Don't count unlisted buffers.
1394 // Avoid non-help buffers if the starting point was a non-help buffer and
1395 // vice-versa.
1396 if (unload || (buf->b_p_bl
1397 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
1399 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001400 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 if (bp == buf)
1403 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001404 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001405 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 return FAIL;
1407 }
1408 }
1409 }
1410
Bram Moolenaarc667da52019-11-30 20:52:27 +01001411 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 if (start == DOBUF_FIRST)
1414 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001415 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001417 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001420 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001422 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 return FAIL;
1424 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001425#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001426 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001427 return OK;
1428#endif
Sean Dewar6cb1c822025-05-03 18:37:27 +02001429 if (action == DOBUF_GOTO && buf != curbuf)
1430 {
1431 if (!check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) != 0))
1432 // disallow navigating to another buffer when 'winfixbuf' is applied
1433 return FAIL;
1434 if (buf->b_locked_split)
1435 {
1436 // disallow navigating to a closing buffer, which like splitting,
1437 // can result in more windows displaying it
1438 emsg(_(e_cannot_switch_to_a_closing_buffer));
1439 return FAIL;
1440 }
1441 }
Colin Kennedy21570352024-03-03 16:16:47 +01001442
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001443 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1444 && (buf->b_flags & BF_DUMMY))
1445 {
1446 // disallow navigating to the dummy buffer
1447 semsg(_(e_buffer_nr_does_not_exist), count);
1448 return FAIL;
1449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451#ifdef FEAT_GUI
1452 need_mouse_correct = TRUE;
1453#endif
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001456 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 */
1458 if (unload)
1459 {
1460 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001461 bufref_T bufref;
1462
Bram Moolenaar94f01952018-09-01 15:30:03 +02001463 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001464 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001465
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001466 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaarc667da52019-11-30 20:52:27 +01001468 // When unloading or deleting a buffer that's already unloaded and
1469 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001470 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1471 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 return FAIL;
1473
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001474 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001476#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001477 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001479# ifdef FEAT_TERMINAL
1480 if (term_job_running(buf->b_term))
1481 {
1482 if (term_confirm_stop(buf) == FAIL)
1483 return FAIL;
1484 }
1485 else
1486# endif
1487 {
1488 dialog_changed(buf, FALSE);
1489 if (!bufref_valid(&bufref))
1490 // Autocommand deleted buffer, oops! It's not changed
1491 // now.
1492 return FAIL;
1493 // If it's still changed fail silently, the dialog already
1494 // mentioned why it fails.
1495 if (bufIsChanged(buf))
1496 return FAIL;
1497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001499 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001502 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 return FAIL;
1504 }
1505 }
1506
Bram Moolenaarc667da52019-11-30 20:52:27 +01001507 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001508 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001509 end_visual_mode();
1510
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001511 // If deleting the last (listed) buffer, make it empty.
1512 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001513 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (bp->b_p_bl && bp != buf)
1515 break;
1516 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001517 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001519 // If the deleted buffer is the current one, close the current window
1520 // (unless it's the only window). Repeat this so long as we end up in
1521 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001522 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001523 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001524 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001525 {
1526 if (win_close(curwin, FALSE) == FAIL)
1527 break;
1528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001530 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (buf != curbuf)
1532 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001533 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001534 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001535 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 return OK;
1537 }
1538
1539 /*
1540 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001541 * There should be another, otherwise it would have been handled
1542 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001543 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * Then prefer the buffer we most recently visited.
1545 * Else try to find one that is loaded, after the current buffer,
1546 * then before the current buffer.
1547 * Finally use any buffer.
1548 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001549 buf = NULL; // selected buffer
1550 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001551 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1552 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001553 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 int jumpidx;
1556
1557 jumpidx = curwin->w_jumplistidx - 1;
1558 if (jumpidx < 0)
1559 jumpidx = curwin->w_jumplistlen - 1;
1560
1561 forward = jumpidx;
1562 while (jumpidx != curwin->w_jumplistidx)
1563 {
1564 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1565 if (buf != NULL)
1566 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001567 // Skip current and unlisted bufs. Also skip a quickfix
1568 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001569 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001570 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 else if (buf->b_ml.ml_mfp == NULL)
1572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001573 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (bp == NULL)
1575 bp = buf;
1576 buf = NULL;
1577 }
1578 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001579 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001581 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1583 break;
1584 if (--jumpidx < 0)
1585 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001586 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 break;
1588 }
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaarc667da52019-11-30 20:52:27 +01001591 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 forward = TRUE;
1594 buf = curbuf->b_next;
1595 for (;;)
1596 {
1597 if (buf == NULL)
1598 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001599 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 break;
1601 buf = curbuf->b_prev;
1602 forward = FALSE;
1603 continue;
1604 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001605 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001606 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001607 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001609 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001611 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 bp = buf;
1613 }
1614 if (forward)
1615 buf = buf->b_next;
1616 else
1617 buf = buf->b_prev;
1618 }
1619 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001620 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001622 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001624 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001625 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 break;
1627 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001628 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 if (curbuf->b_next != NULL)
1631 buf = curbuf->b_next;
1632 else
1633 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001634 if (bt_quickfix(buf))
1635 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 }
1638
Bram Moolenaara02471e2014-01-10 16:43:14 +01001639 if (buf == NULL)
1640 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001641 // Autocommands must have wiped out all other buffers. Only option
1642 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001643 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001644 }
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001647 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001649 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001651 // If 'switchbuf' is set jump to the window containing "buf".
1652 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001653 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 return FAIL;
1657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaarc667da52019-11-30 20:52:27 +01001659 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (buf == curbuf)
1661 return OK;
1662
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001663 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001664 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001667 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001669# ifdef FEAT_TERMINAL
1670 if (term_job_running(curbuf->b_term))
1671 {
1672 if (term_confirm_stop(curbuf) == FAIL)
1673 return FAIL;
1674 // Manually kill the terminal here because this command will
1675 // hide it otherwise.
1676 free_terminal(curbuf);
1677 }
1678 else
1679# endif
1680 {
1681 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001682
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001683 set_bufref(&bufref, buf);
1684 dialog_changed(curbuf, FALSE);
1685 if (!bufref_valid(&bufref))
1686 // Autocommand deleted buffer, oops!
1687 return FAIL;
1688
1689 if (bufIsChanged(curbuf))
1690 {
1691 no_write_message();
1692 return FAIL;
1693 }
1694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001699 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return FAIL;
1701 }
1702 }
1703
Bram Moolenaarc667da52019-11-30 20:52:27 +01001704 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 set_curbuf(buf, action);
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001708 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001710#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001711 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 return FAIL;
1713#endif
1714
1715 return OK;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001718 int
1719do_buffer(
1720 int action,
1721 int start,
1722 int dir, // FORWARD or BACKWARD
1723 int count, // buffer number or number of buffers
1724 int forceit) // TRUE when using !
1725{
1726 return do_buffer_ext(action, start, dir, count,
1727 forceit ? DOBUF_FORCEIT : 0);
1728}
1729
1730/*
1731 * do_bufdel() - delete or unload buffer(s)
1732 *
1733 * addr_count == 0: ":bdel" - delete current buffer
1734 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1735 * buffer "end_bnr", then any other arguments.
1736 * addr_count == 2: ":N,N bdel" - delete buffers in range
1737 *
1738 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1739 * DOBUF_DEL (":bdel")
1740 *
1741 * Returns error message or NULL
1742 */
1743 char *
1744do_bufdel(
1745 int command,
1746 char_u *arg, // pointer to extra arguments
1747 int addr_count,
1748 int start_bnr, // first buffer number in a range
1749 int end_bnr, // buffer nr or last buffer nr in a range
1750 int forceit)
1751{
1752 int do_current = 0; // delete current buffer?
1753 int deleted = 0; // number of buffers deleted
1754 char *errormsg = NULL; // return value
1755 int bnr; // buffer number
1756 char_u *p;
1757
1758 if (addr_count == 0)
1759 {
1760 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1761 }
1762 else
1763 {
1764 if (addr_count == 2)
1765 {
1766 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001767 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001768 bnr = start_bnr;
1769 }
1770 else // addr_count == 1
1771 bnr = end_bnr;
1772
1773 for ( ;!got_int; ui_breakcheck())
1774 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001775 // Delete the current buffer last, otherwise when the
1776 // current buffer is deleted, the next buffer becomes
1777 // the current one and will be loaded, which may then
1778 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001779 if (bnr == curbuf->b_fnum)
1780 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001781 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001782 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1783 ++deleted;
1784
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001785 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001786 if (addr_count == 2)
1787 {
1788 if (++bnr > end_bnr)
1789 break;
1790 }
1791 else // addr_count == 1
1792 {
1793 arg = skipwhite(arg);
1794 if (*arg == NUL)
1795 break;
1796 if (!VIM_ISDIGIT(*arg))
1797 {
1798 p = skiptowhite_esc(arg);
1799 bnr = buflist_findpat(arg, p,
1800 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1801 FALSE, FALSE);
1802 if (bnr < 0) // failed
1803 break;
1804 arg = p;
1805 }
1806 else
1807 bnr = getdigits(&arg);
1808 }
1809 }
1810 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1811 FORWARD, do_current, forceit) == OK)
1812 ++deleted;
1813
1814 if (deleted == 0)
1815 {
1816 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001817 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001818 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001819 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001820 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001821 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001822 errormsg = (char *)IObuff;
1823 }
1824 else if (deleted >= p_report)
1825 {
1826 if (command == DOBUF_UNLOAD)
1827 smsg(NGETTEXT("%d buffer unloaded",
1828 "%d buffers unloaded", deleted), deleted);
1829 else if (command == DOBUF_DEL)
1830 smsg(NGETTEXT("%d buffer deleted",
1831 "%d buffers deleted", deleted), deleted);
1832 else
1833 smsg(NGETTEXT("%d buffer wiped out",
1834 "%d buffers wiped out", deleted), deleted);
1835 }
1836 }
1837
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001838 return errormsg;
1839}
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841/*
1842 * Set current buffer to "buf". Executes autocommands and closes current
1843 * buffer. "action" tells how to close the current buffer:
1844 * DOBUF_GOTO free or hide it
1845 * DOBUF_SPLIT nothing
1846 * DOBUF_UNLOAD unload it
1847 * DOBUF_DEL delete it
1848 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001849 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 buf_T *prevbuf;
1855 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001856 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001857#ifdef FEAT_SYN_HL
1858 long old_tw = curbuf->b_p_tw;
1859#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001860 bufref_T newbufref;
1861 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001862 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001863 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001866 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001867 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1868 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaarc667da52019-11-30 20:52:27 +01001870 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaarc667da52019-11-30 20:52:27 +01001873 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001875 set_bufref(&prevbufref, prevbuf);
1876 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001878 // Autocommands may delete the current buffer and/or the buffer we want to
1879 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001880 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001881 || (bufref_valid(&prevbufref)
1882 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001884 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001886 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001888#ifdef FEAT_SYN_HL
1889 if (prevbuf == curwin->w_buffer)
1890 reset_synblock(curwin);
1891#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001892 // autocommands may have opened a new window
1893 // with prevbuf, grr
1894 if (unload ||
1895 (last_winid != get_last_winid() &&
1896 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001897 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001898#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001899 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001901 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001903 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001904 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001905
Bram Moolenaare615db02022-01-20 21:00:54 +00001906 // Do not sync when in Insert mode and the buffer is open in
1907 // another window, might be a timer doing something in another
1908 // window.
1909 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001910 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001911 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1913 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001914 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001915 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1916 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001917 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001918 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001919 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001922 // An autocommand may have deleted "buf", already entered it (e.g., when
1923 // it did ":bunload") or aborted the script processing.
1924 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001925 valid = buf_valid(buf);
1926 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001927#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001928 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001930 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001931 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001932 // autocommands changed curbuf and we will move to another
1933 // buffer soon, so decrement curbuf->b_nwindows
1934 if (curbuf != NULL && prevbuf != curbuf)
1935 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001936 // If the buffer is not valid but curwin->w_buffer is NULL we must
1937 // enter some buffer. Using the last one is hopefully OK.
1938 if (!valid)
1939 enter_buffer(lastbuf);
1940 else
1941 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001942#ifdef FEAT_SYN_HL
1943 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001944 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001945#endif
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947}
1948
1949/*
1950 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001951 * Old curbuf must have been abandoned already! This also means "curbuf" may
1952 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001955enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001957 // when closing the current buffer stop Visual mode
1958 if (VIsual_active
1959#if defined(EXITFREE)
1960 && !entered_free_all_mem
1961#endif
1962 )
1963 end_visual_mode();
1964
Bram Moolenaar010ee962019-09-25 20:37:36 +02001965 // Get the buffer in the current window.
1966 curwin->w_buffer = buf;
1967 curbuf = buf;
1968 ++curbuf->b_nwindows;
1969
1970 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1972 if (!buf->b_help)
1973 get_winopts(buf);
1974#ifdef FEAT_FOLDING
1975 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001976 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001978 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#endif
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001982 if (curwin->w_p_diff)
1983 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#endif
1985
Bram Moolenaara971b822011-09-14 14:43:25 +02001986#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001987 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001988#endif
1989
Bram Moolenaarc667da52019-11-30 20:52:27 +01001990 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 curwin->w_cursor.lnum = 1;
1992 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001995 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
Bram Moolenaarc667da52019-11-30 20:52:27 +01001997 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001998 curwin->w_valid = 0;
1999
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02002000 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
2001 curbuf->b_last_cursor.col, TRUE);
2002
Bram Moolenaarc667da52019-11-30 20:52:27 +01002003 // Make sure the buffer is loaded.
2004 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002005 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002006 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002007 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01002008 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002009 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02002010 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002011
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002012 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015 {
Bram Moolenaareda65222019-05-16 20:29:44 +02002016 if (!msg_silent && !shortmess(SHM_FILEINFO))
2017 need_fileinfo = TRUE; // display file info after redraw
2018
2019 // check if file changed
2020 (void)buf_check_timestamp(curbuf, FALSE);
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002023#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2027 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029
Bram Moolenaarc667da52019-11-30 20:52:27 +01002030 // If autocommands did not change the cursor position, restore cursor lnum
2031 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (curwin->w_cursor.lnum == 1 && inindent(0))
2033 buflist_getfpos();
2034
Bram Moolenaarc667da52019-11-30 20:52:27 +01002035 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002037 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002038 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002039 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
Bram Moolenaar009b2592004-10-24 19:18:58 +00002041#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002042 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002043 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002044#endif
2045
Bram Moolenaarc667da52019-11-30 20:52:27 +01002046 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002047 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049#ifdef FEAT_KEYMAP
2050 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002051 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002053#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002054 // May need to set the spell language. Can only do this after the buffer
2055 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002056 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002057 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002058#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002059#ifdef FEAT_VIMINFO
2060 curbuf->b_last_used = vim_time();
2061#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002062
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002063 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064}
2065
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002066#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2067/*
2068 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002069 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002070 */
2071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002072do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002073{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002074 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002075 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002076 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002077 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002078 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002079 last_chdir_reason = "autochdir";
2080 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002081}
2082#endif
2083
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002084 static void
2085no_write_message_buf(buf_T *buf UNUSED)
2086{
2087#ifdef FEAT_TERMINAL
2088 if (term_job_running(buf->b_term))
2089 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2090 else
2091#endif
2092 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2093 buf->b_fnum);
2094}
2095
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002096 void
2097no_write_message(void)
2098{
2099#ifdef FEAT_TERMINAL
2100 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002101 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002102 else
2103#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002104 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002105}
2106
2107 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002108no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002109{
2110#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002111 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002112 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002113 else
2114#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002115 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002116}
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118/*
2119 * functions for dealing with the buffer list
2120 */
2121
2122/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002123 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2124 * only one window. That means it can be re-used.
2125 */
2126 int
2127curbuf_reusable(void)
2128{
2129 return (curbuf != NULL
2130 && curbuf->b_ffname == NULL
2131 && curbuf->b_nwindows <= 1
2132 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002133 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002134 && !curbufIsChanged());
2135}
2136
2137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 * Add a file name to the buffer list. Return a pointer to the buffer.
2139 * If the same file name already exists return a pointer to that buffer.
2140 * If it does not exist, or if fname == NULL, a new entry is created.
2141 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2142 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2143 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002144 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002145 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2146 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002147 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * This is the ONLY way to create a new buffer.
2149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002152 char_u *ffname_arg, // full path of fname or relative
2153 char_u *sfname_arg, // short fname or NULL
2154 linenr_T lnum, // preferred cursor line
2155 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002157 char_u *ffname = ffname_arg;
2158 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 buf_T *buf;
2160#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002161 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#endif
2163
Bram Moolenaar480778b2016-07-14 22:09:39 +02002164 if (top_file_num == 1)
2165 hash_init(&buf_hashtab);
2166
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002167 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002170 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 */
2172#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002173 // On Unix we can use inode numbers when the file exists. Works better
2174 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2176 st.st_dev = (dev_T)-1;
2177#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002178 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef UNIX
2180 buflist_findname_stat(ffname, &st)
2181#else
2182 buflist_findname(ffname)
2183#endif
2184 ) != NULL)
2185 {
2186 vim_free(ffname);
2187 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002188 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2189 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002190
2191 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002192 // copy the options now, if 'cpo' doesn't have 's' and not done
2193 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002194 buf_copy_options(buf, 0);
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2197 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002198 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002201 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002203 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002204 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002205 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002206 return NULL;
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 return buf;
2210 }
2211
2212 /*
2213 * If the current buffer has no name and no contents, use the current
2214 * buffer. Otherwise: Need to allocate a new buffer structure.
2215 *
2216 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002217 * (A spell file buffer is allocated in spell.c, but that's not a normal
2218 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 */
2220 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002221 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Sean Dewar00772822025-05-14 20:16:52 +02002223 bufref_T bufref;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 buf = curbuf;
Sean Dewar00772822025-05-14 20:16:52 +02002226 set_bufref(&bufref, buf);
Christian Brabandtbaa8c902025-04-19 11:14:11 +02002227 trigger_undo_ftplugin(buf, curwin);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002228 // It's like this buffer is deleted. Watch out for autocommands that
2229 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002230 buf_freeall(buf, BFA_WIPE | BFA_DEL);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002231#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002232 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002233 {
2234 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
Sean Dewar00772822025-05-14 20:16:52 +02002238 if (!bufref_valid(&bufref))
2239 buf = NULL; // buf was deleted; allocate a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 if (buf != curbuf || curbuf == NULL)
2242 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002243 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (buf == NULL)
2245 {
2246 vim_free(ffname);
2247 return NULL;
2248 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002249#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002250 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002251 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002252 if (buf->b_vars == NULL)
2253 {
2254 vim_free(ffname);
2255 vim_free(buf);
2256 return NULL;
2257 }
2258 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2259#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002260 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262
2263 if (ffname != NULL)
2264 {
2265 buf->b_ffname = ffname;
2266 buf->b_sfname = vim_strsave(sfname);
2267 }
2268
2269 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002270 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
2272 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2273 || buf->b_wininfo == NULL)
2274 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002275 if (buf->b_sfname != buf->b_ffname)
2276 VIM_CLEAR(buf->b_sfname);
2277 else
2278 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002279 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (buf != curbuf)
2281 free_buffer(buf);
2282 return NULL;
2283 }
2284
2285 if (buf == curbuf)
2286 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002287 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002288
Bram Moolenaarc667da52019-11-30 20:52:27 +01002289 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002290 buf->b_p_initialized = FALSE;
2291 buf_copy_options(buf, BCO_ENTER);
2292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002294 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 curbuf->b_kmap_state |= KEYMAP_INIT;
2296#endif
2297 }
2298 else
2299 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002300 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002302 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 buf->b_prev = NULL;
2305 firstbuf = buf;
2306 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002307 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 lastbuf->b_next = buf;
2310 buf->b_prev = lastbuf;
2311 }
2312 lastbuf = buf;
2313
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002314 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2315 {
2316 // Recycle a previously used buffer number. Used for buffers which
2317 // are normally hidden, e.g. in a popup window. Avoids that the
2318 // buffer number grows rapidly.
2319 --buf_reuse.ga_len;
2320 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002321
2322 // Move buffer to the right place in the buffer list.
2323 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2324 {
2325 buf_T *prev = buf->b_prev;
2326
2327 prev->b_next = buf->b_next;
2328 if (prev->b_next != NULL)
2329 prev->b_next->b_prev = prev;
2330 buf->b_next = prev;
2331 buf->b_prev = prev->b_prev;
2332 if (buf->b_prev != NULL)
2333 buf->b_prev->b_next = buf;
2334 prev->b_prev = buf;
2335 if (lastbuf == buf)
2336 lastbuf = prev;
2337 if (firstbuf == prev)
2338 firstbuf = buf;
2339 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002340 }
2341 else
2342 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002343 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002345 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002346 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002349 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 top_file_num = 1;
2352 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002353 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002355 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 buf_copy_options(buf, BCO_ALWAYS);
2357 }
2358
2359 buf->b_wininfo->wi_fpos.lnum = lnum;
2360 buf->b_wininfo->wi_win = curwin;
2361
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002362#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002363 hash_init(&buf->b_s.b_keywtab);
2364 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
2367 buf->b_fname = buf->b_sfname;
2368#ifdef UNIX
2369 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002370 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 else
2372 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002373 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 buf->b_dev = st.st_dev;
2375 buf->b_ino = st.st_ino;
2376 }
2377#endif
2378 buf->b_u_synced = TRUE;
2379 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002380 if (flags & BLN_DUMMY)
2381 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002383 clrallmarks(buf); // clear marks
2384 fmarks_check_names(buf); // check file marks for this file
2385 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (!(flags & BLN_DUMMY))
2387 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002388 bufref_T bufref;
2389
Bram Moolenaarc667da52019-11-30 20:52:27 +01002390 // Tricky: these autocommands may change the buffer list. They could
2391 // also split the window with re-using the one empty buffer. This may
2392 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002393 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002394 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002395 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002396 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002398 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002399 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002400 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002401 return NULL;
2402 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002403#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002404 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 return buf;
2410}
2411
2412/*
2413 * Free the memory for the options of a buffer.
2414 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2415 * 'fileencoding'.
2416 */
2417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002418free_buf_options(
2419 buf_T *buf,
2420 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 if (free_p_ff)
2423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 clear_string_option(&buf->b_p_bh);
2427 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429#ifdef FEAT_FIND_ID
2430 clear_string_option(&buf->b_p_def);
2431 clear_string_option(&buf->b_p_inc);
2432# ifdef FEAT_EVAL
2433 clear_string_option(&buf->b_p_inex);
2434# endif
2435#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002436#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 clear_string_option(&buf->b_p_inde);
2438 clear_string_option(&buf->b_p_indk);
2439#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002440#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2441 clear_string_option(&buf->b_p_bexpr);
2442#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002443#if defined(FEAT_CRYPT)
2444 clear_string_option(&buf->b_p_cm);
2445#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002446 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002447#if defined(FEAT_EVAL)
2448 clear_string_option(&buf->b_p_fex);
2449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002451# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002452 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2453 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002454 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 clear_string_option(&buf->b_p_key);
2457#endif
2458 clear_string_option(&buf->b_p_kp);
2459 clear_string_option(&buf->b_p_mps);
2460 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002461 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002463#ifdef FEAT_VARTABS
2464 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002465 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002466 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002467 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002468 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470#ifdef FEAT_KEYMAP
2471 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002472 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 ga_clear(&buf->b_kmap_ga);
2474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476#ifdef FEAT_FOLDING
2477 clear_string_option(&buf->b_p_cms);
2478#endif
2479 clear_string_option(&buf->b_p_nf);
2480#ifdef FEAT_SYN_HL
2481 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002482 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002483#endif
2484#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002485 clear_string_option(&buf->b_s.b_p_spc);
2486 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002487 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002488 buf->b_s.b_cap_prog = NULL;
2489 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002490 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 clear_string_option(&buf->b_p_cink);
2495 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002496 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002497 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002499 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 clear_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +02002501 clear_string_option(&buf->b_p_ise);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002502#ifdef FEAT_COMPL_FUNC
2503 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002504 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002505 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002506 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002507 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002508 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510#ifdef FEAT_QUICKFIX
2511 clear_string_option(&buf->b_p_gp);
2512 clear_string_option(&buf->b_p_mp);
2513 clear_string_option(&buf->b_p_efm);
2514#endif
2515 clear_string_option(&buf->b_p_ep);
2516 clear_string_option(&buf->b_p_path);
2517 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002518 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002519#ifdef FEAT_EVAL
2520 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002521 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002522 clear_string_option(&buf->b_p_ffu);
2523 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 clear_string_option(&buf->b_p_dict);
2526 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002527 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002529 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002530 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002531 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002532 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533}
2534
2535/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002536 * Get alternate file "n".
2537 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2538 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * if (options & GETF_SETMARK) call setpcmark()
2540 * if (options & GETF_ALT) we are jumping to an alternate file.
2541 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2542 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002543 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
2545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546buflist_getfile(
2547 int n,
2548 linenr_T lnum,
2549 int options,
2550 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 pos_T *fpos;
2555 colnr_T col;
2556
2557 buf = buflist_findnr(n);
2558 if (buf == NULL)
2559 {
2560 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002561 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002563 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 return FAIL;
2565 }
2566
Bram Moolenaarc667da52019-11-30 20:52:27 +01002567 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (buf == curbuf)
2569 return OK;
2570
Bram Moolenaar71223e22022-05-30 15:23:09 +01002571 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002572 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
Bram Moolenaarc667da52019-11-30 20:52:27 +01002574 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (lnum == 0)
2576 {
2577 fpos = buflist_findfpos(buf);
2578 lnum = fpos->lnum;
2579 col = fpos->col;
2580 }
2581 else
2582 col = 0;
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (options & GETF_SWITCH)
2585 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002586 // If 'switchbuf' is set jump to the window containing "buf".
2587 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002588
Bram Moolenaarc667da52019-11-30 20:52:27 +01002589 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2590 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002591 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002592 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002594 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002595 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002596 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2597 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002599 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002604 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002605 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2606 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002608 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (!p_sol && col != 0)
2610 {
2611 curwin->w_cursor.col = col;
2612 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 curwin->w_set_curswant = TRUE;
2615 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002616 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002618
2619 if (RedrawingDisabled > 0)
2620 --RedrawingDisabled;
2621 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622}
2623
2624/*
2625 * go to the last know line number for the current buffer
2626 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002628buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 pos_T *fpos;
2631
2632 fpos = buflist_findfpos(curbuf);
2633
2634 curwin->w_cursor.lnum = fpos->lnum;
2635 check_cursor_lnum();
2636
2637 if (p_sol)
2638 curwin->w_cursor.col = 0;
2639 else
2640 {
2641 curwin->w_cursor.col = fpos->col;
2642 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 curwin->w_set_curswant = TRUE;
2645 }
2646}
2647
Bram Moolenaar81695252004-12-29 20:58:21 +00002648/*
2649 * Find file in buffer list by name (it has to be for the current window).
2650 * Returns NULL if not found.
2651 */
2652 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002653buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002654{
2655 char_u *ffname;
2656 buf_T *buf = NULL;
2657
Bram Moolenaarc667da52019-11-30 20:52:27 +01002658 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002659 ffname = FullName_save(fname,
2660#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002661 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002662#else
2663 FALSE
2664#endif
2665 );
2666 if (ffname != NULL)
2667 {
2668 buf = buflist_findname(ffname);
2669 vim_free(ffname);
2670 }
2671 return buf;
2672}
Bram Moolenaar81695252004-12-29 20:58:21 +00002673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674/*
2675 * Find file in buffer list by name (it has to be for the current window).
2676 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002677 * Skips dummy buffers.
2678 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 */
2680 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002681buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002684 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 if (mch_stat((char *)ffname, &st) < 0)
2687 st.st_dev = (dev_T)-1;
2688 return buflist_findname_stat(ffname, &st);
2689}
2690
2691/*
2692 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2693 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002694 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 */
2696 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002697buflist_findname_stat(
2698 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002699 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701#endif
2702 buf_T *buf;
2703
Bram Moolenaarc667da52019-11-30 20:52:27 +01002704 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002705 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002706 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#ifdef UNIX
2708 , stp
2709#endif
2710 ))
2711 return buf;
2712 return NULL;
2713}
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715/*
2716 * Find file in buffer list by a regexp pattern.
2717 * Return fnum of the found buffer.
2718 * Return < 0 for error.
2719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002721buflist_findpat(
2722 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002723 char_u *pattern_end, // pointer to first char after pattern
2724 int unlisted, // find unlisted buffers
2725 int diffmode UNUSED, // find diff-mode buffers only
2726 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 int match = -1;
2730 int find_listed;
2731 char_u *pat;
2732 char_u *patend;
2733 int attempt;
2734 char_u *p;
2735 int toggledollar;
2736
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002737 // "%" is current file, "%%" or "#" is alternate file
2738 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2739 || (in_vim9script() && pattern_end == pattern + 2
2740 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002742 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002744 else
2745 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#ifdef FEAT_DIFF
2747 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2748 match = -1;
2749#endif
2750 }
2751
2752 /*
2753 * Try four ways of matching a listed buffer:
2754 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002755 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 * attempt == 2: with '$' at end (only match at end)
2757 * attempt == 3: with '^' at start and '$' at end (only full match)
2758 * Repeat this for finding an unlisted buffer if there was no matching
2759 * listed buffer.
2760 */
2761 else
2762 {
2763 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2764 if (pat == NULL)
2765 return -1;
2766 patend = pat + STRLEN(pat) - 1;
2767 toggledollar = (patend > pat && *patend == '$');
2768
Bram Moolenaarc667da52019-11-30 20:52:27 +01002769 // First try finding a listed buffer. If not found and "unlisted"
2770 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 find_listed = TRUE;
2772 for (;;)
2773 {
2774 for (attempt = 0; attempt <= 3; ++attempt)
2775 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002776 regmatch_T regmatch;
2777
Bram Moolenaarc667da52019-11-30 20:52:27 +01002778 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002780 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002782 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002784 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002786 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002787 {
2788 if (regmatch.regprog == NULL)
2789 {
2790 // invalid pattern, possibly after switching engine
2791 vim_free(pat);
2792 return -1;
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (buf->b_p_bl == find_listed
2795#ifdef FEAT_DIFF
2796 && (!diffmode || diff_mode_buf(buf))
2797#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002798 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002800 if (curtab_only)
2801 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002802 // Ignore the match if the buffer is not open in
2803 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002804 win_T *wp;
2805
Bram Moolenaar29323592016-07-24 22:04:11 +02002806 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002807 if (wp->w_buffer == buf)
2808 break;
2809 if (wp == NULL)
2810 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002811 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002812 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 match = -2;
2815 break;
2816 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002817 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002821 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002822 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 break;
2824 }
2825
Bram Moolenaarc667da52019-11-30 20:52:27 +01002826 // Only search for unlisted buffers if there was no match with
2827 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (!unlisted || !find_listed || match != -1)
2829 break;
2830 find_listed = FALSE;
2831 }
2832
2833 vim_free(pat);
2834 }
2835
2836 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002837 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002839 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 return match;
2841}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
Bram Moolenaar52410572019-10-27 05:12:45 +01002843#ifdef FEAT_VIMINFO
2844typedef struct {
2845 buf_T *buf;
2846 char_u *match;
2847} bufmatch_T;
2848#endif
2849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850/*
2851 * Find all buffer names that match.
2852 * For command line expansion of ":buf" and ":sbuf".
2853 * Return OK if matches found, FAIL otherwise.
2854 */
2855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002856ExpandBufnames(
2857 char_u *pat,
2858 int *num_file,
2859 char_u ***file,
2860 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002862 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 buf_T *buf;
2864 int round;
2865 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002866 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002867#ifdef FEAT_VIMINFO
2868 bufmatch_T *matches = NULL;
2869#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002870 int fuzzy;
2871 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002872 regmatch_T regmatch;
2873 int score = 0;
2874 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaarc667da52019-11-30 20:52:27 +01002876 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 *file = NULL;
2878
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002879#ifdef FEAT_DIFF
2880 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2881 return FAIL;
2882#endif
2883
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002884 fuzzy = cmdline_fuzzy_complete(pat);
2885
2886 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2887 // expression matching)
2888 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002890 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002891 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002892 int len = (int)STRLEN(pat);
2893 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002894 if (patc == NULL)
2895 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002896 STRNCPY(patc, pat + 1, len - 1);
2897 patc[len - 1] = NUL;
2898 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002899 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002900 else if (*pat == '^')
2901 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002902 else
2903 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002904 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002905 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002906
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002907 // round == 1: Count the matches.
2908 // round == 2: Build the array to keep the matches.
2909 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002910 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002911 count = 0;
2912 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002913 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002914 if (!buf->b_p_bl) // skip unlisted buffers
2915 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002916#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002917 if (options & BUF_DIFF_FILTER)
2918 // Skip buffers not suitable for
2919 // :diffget or :diffput completion.
2920 if (buf == curbuf || !diff_mode_buf(buf))
2921 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002922#endif
2923
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002924 if (!fuzzy)
2925 {
2926 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002927 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002928 // invalid pattern, possibly after recompiling
2929 if (to_free)
2930 vim_free(patc);
2931 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002932 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002933 p = buflist_match(&regmatch, buf, p_wic);
2934 }
2935 else
2936 {
2937 p = NULL;
2938 // first try matching with the short file name
2939 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2940 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002941 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002942 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002943 // next try matching with the full path file name
2944 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2945 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002948
2949 if (p == NULL)
2950 continue;
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (round == 1)
2953 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002954 ++count;
2955 continue;
2956 }
2957
2958 if (options & WILD_HOME_REPLACE)
2959 p = home_replace_save(buf, p);
2960 else
2961 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002962 if (p == NULL)
2963 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002964
2965 if (!fuzzy)
2966 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002967#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002968 if (matches != NULL)
2969 {
2970 matches[count].buf = buf;
2971 matches[count].match = p;
2972 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002973 }
2974 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002975#endif
2976 (*file)[count++] = p;
2977 }
2978 else
2979 {
2980 fuzmatch[count].idx = count;
2981 fuzmatch[count].str = p;
2982 fuzmatch[count].score = score;
2983 count++;
2984 }
2985 }
2986 if (count == 0) // no match found, break here
2987 break;
2988 if (round == 1)
2989 {
2990 if (!fuzzy)
2991 {
2992 *file = ALLOC_MULT(char_u *, count);
2993 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002994 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002995 vim_regfree(regmatch.regprog);
2996 if (to_free)
2997 vim_free(patc);
2998 return FAIL;
2999 }
3000#ifdef FEAT_VIMINFO
3001 if (options & WILD_BUFLASTUSED)
3002 matches = ALLOC_MULT(bufmatch_T, count);
3003#endif
3004 }
3005 else
3006 {
3007 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
3008 if (fuzmatch == NULL)
3009 {
3010 *num_file = 0;
3011 *file = NULL;
3012 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01003018 if (!fuzzy)
3019 {
3020 vim_regfree(regmatch.regprog);
3021 if (to_free)
3022 vim_free(patc);
3023 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003024
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003025 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01003026 {
826814741_6dff3c9c2024-12-10 17:15:14 +01003027#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003028 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01003029 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003030 int i;
3031 if (count > 1)
3032 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3033 // if the current buffer is first in the list, place it at the end
3034 if (matches[0].buf == curbuf)
3035 {
3036 for (i = 1; i < count; i++)
3037 (*file)[i-1] = matches[i].match;
3038 (*file)[count-1] = matches[0].match;
3039 }
3040 else
3041 {
3042 for (i = 0; i < count; i++)
3043 (*file)[i] = matches[i].match;
3044 }
3045 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003046 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003047#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003048 }
3049 else
3050 {
3051 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3052 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003053 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 *num_file = count;
3056 return (count == 0 ? FAIL : OK);
3057}
3058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059/*
3060 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003061 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 */
3063 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003064buflist_match(
3065 regmatch_T *rmp,
3066 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003067 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 char_u *match;
3070
Bram Moolenaarc667da52019-11-30 20:52:27 +01003071 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003072 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003073 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003074 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076 return match;
3077}
3078
3079/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003080 * Try matching the regexp in "rmp->regprog" with file name "name".
3081 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * Return "name" when there is a match, NULL when not.
3083 */
3084 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003085fname_match(
3086 regmatch_T *rmp,
3087 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003088 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 char_u *match = NULL;
3091 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003093 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003094 if (name == NULL || rmp->regprog == NULL)
3095 return NULL;
3096
3097 // Ignore case when 'fileignorecase' or the argument is set.
3098 rmp->rm_ic = p_fic || ignore_case;
3099 if (vim_regexec(rmp, name, (colnr_T)0))
3100 match = name;
3101 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003103 // Replace $(HOME) with '~' and try matching again.
3104 p = home_replace_save(NULL, name);
3105 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003107 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
3109
3110 return match;
3111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
3113/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003114 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 */
3116 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003119 char_u key[VIM_SIZEOF_INT * 2 + 1];
3120 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
3122 if (nr == 0)
3123 nr = curwin->w_alt_fnum;
John Marriottec032de2025-04-10 21:34:19 +02003124 vim_snprintf((char *)key, sizeof(key), "%x", nr);
Bram Moolenaar480778b2016-07-14 22:09:39 +02003125 hi = hash_find(&buf_hashtab, key);
3126
3127 if (!HASHITEM_EMPTY(hi))
3128 return (buf_T *)(hi->hi_key
3129 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 return NULL;
3131}
3132
3133/*
3134 * Get name of file 'n' in the buffer list.
3135 * When the file has no name an empty string is returned.
3136 * home_replace() is used to shorten the file name (used for marks).
3137 * Returns a pointer to allocated memory, of NULL when failed.
3138 */
3139 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003140buflist_nr2name(
3141 int n,
3142 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003143 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144{
3145 buf_T *buf;
3146
3147 buf = buflist_findnr(n);
3148 if (buf == NULL)
3149 return NULL;
3150 return home_replace_save(helptail ? buf : NULL,
3151 fullname ? buf->b_ffname : buf->b_fname);
3152}
3153
3154/*
3155 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3156 * When "copy_options" is TRUE save the local window option values.
3157 * When "lnum" is 0 only do the options.
3158 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003160buflist_setfpos(
3161 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003162 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003163 linenr_T lnum,
3164 colnr_T col,
3165 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
3167 wininfo_T *wip;
3168
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003169 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (wip->wi_win == win)
3171 break;
3172 if (wip == NULL)
3173 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003174 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003175 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (wip == NULL)
3177 return;
3178 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003179 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 lnum = 1;
3181 }
3182 else
3183 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003184 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (wip->wi_prev)
3186 wip->wi_prev->wi_next = wip->wi_next;
3187 else
3188 buf->b_wininfo = wip->wi_next;
3189 if (wip->wi_next)
3190 wip->wi_next->wi_prev = wip->wi_prev;
3191 if (copy_options && wip->wi_optset)
3192 {
3193 clear_winopt(&wip->wi_opt);
3194#ifdef FEAT_FOLDING
3195 deleteFoldRecurse(&wip->wi_folds);
3196#endif
3197 }
3198 }
3199 if (lnum != 0)
3200 {
3201 wip->wi_fpos.lnum = lnum;
3202 wip->wi_fpos.col = col;
3203 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003204 if (win != NULL)
3205 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003206 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003208 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3210#ifdef FEAT_FOLDING
3211 wip->wi_fold_manual = win->w_fold_manual;
3212 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3213#endif
3214 wip->wi_optset = TRUE;
3215 }
3216
Bram Moolenaarc667da52019-11-30 20:52:27 +01003217 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 wip->wi_next = buf->b_wininfo;
3219 buf->b_wininfo = wip;
3220 wip->wi_prev = NULL;
3221 if (wip->wi_next)
3222 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223}
3224
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003225#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003226/*
3227 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3228 * page. That's because a diff is local to a tab page.
3229 */
3230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003232{
3233 win_T *wp;
3234
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003235 if (!wip->wi_opt.wo_diff)
3236 return FALSE;
3237
3238 FOR_ALL_WINDOWS(wp)
3239 // return FALSE when it's a window in the current tab page, thus
3240 // the buffer was in diff mode here
3241 if (wip->wi_win == wp)
3242 return FALSE;
3243 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003244}
3245#endif
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247/*
3248 * Find info for the current window in buffer "buf".
3249 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003250 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003251 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3252 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * Returns NULL when there isn't any info.
3254 */
3255 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003256find_wininfo(
3257 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003258 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 wininfo_T *wip;
3262
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003263 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003264 if (wip->wi_win == curwin
3265#ifdef FEAT_DIFF
3266 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3267#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003268
3269 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003271
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003272 if (wip != NULL)
3273 return wip;
3274
Bram Moolenaarc667da52019-11-30 20:52:27 +01003275 // If no wininfo for curwin, use the first in the list (that doesn't have
3276 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003277 // If "need_options" is TRUE skip entries that don't have options set,
3278 // unless the window is editing "buf", so we can copy from the window
3279 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003280#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003281 if (skip_diff_buffer)
3282 {
3283 FOR_ALL_BUF_WININFO(buf, wip)
3284 if (!wininfo_other_tab_diff(wip)
3285 && (!need_options || wip->wi_optset
3286 || (wip->wi_win != NULL
3287 && wip->wi_win->w_buffer == buf)))
3288 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003289 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003290 else
3291#endif
3292 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return wip;
3294}
3295
3296/*
3297 * Reset the local window options to the values last used in this window.
3298 * If the buffer wasn't used in this window before, use the values from
3299 * the most recently used window. If the values were never set, use the
3300 * global values for the window.
3301 */
3302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 wininfo_T *wip;
3306
3307 clear_winopt(&curwin->w_onebuf_opt);
3308#ifdef FEAT_FOLDING
3309 clearFolding(curwin);
3310#endif
3311
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003312 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003313 if (wip != NULL && wip->wi_win != NULL
3314 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003316 // The buffer is currently displayed in the window: use the actual
3317 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003318 win_T *wp = wip->wi_win;
3319
3320 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3321#ifdef FEAT_FOLDING
3322 curwin->w_fold_manual = wp->w_fold_manual;
3323 curwin->w_foldinvalid = TRUE;
3324 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3325#endif
3326 }
3327 else if (wip != NULL && wip->wi_optset)
3328 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003329 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3331#ifdef FEAT_FOLDING
3332 curwin->w_fold_manual = wip->wi_fold_manual;
3333 curwin->w_foldinvalid = TRUE;
3334 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3335#endif
3336 }
3337 else
3338 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003339 if (wip != NULL)
3340 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003343 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (p_fdls >= 0)
3345 curwin->w_p_fdl = p_fdls;
3346#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003347 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348}
3349
3350/*
3351 * Find the position (lnum and col) for the buffer 'buf' for the current
3352 * window.
3353 * Returns a pointer to no_position if no position is found.
3354 */
3355 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003359 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003361 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (wip != NULL)
3363 return &(wip->wi_fpos);
3364 else
3365 return &no_position;
3366}
3367
3368/*
3369 * Find the lnum for the buffer 'buf' for the current window.
3370 */
3371 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003372buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 return buflist_findfpos(buf)->lnum;
3375}
3376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003378 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar52410572019-10-27 05:12:45 +01003383 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 int len;
3385 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003386 int ro_char;
3387 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003388#ifdef FEAT_TERMINAL
3389 int job_running;
3390 int job_none_open;
3391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar52410572019-10-27 05:12:45 +01003393#ifdef FEAT_VIMINFO
3394 garray_T buflist;
3395 buf_T **buflist_data = NULL, **p;
3396
3397 if (vim_strchr(eap->arg, 't'))
3398 {
3399 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003400 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003401 {
3402 if (ga_grow(&buflist, 1) == OK)
3403 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3404 }
3405
3406 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3407 sizeof(buf_T *), buf_compare);
3408
Bram Moolenaar3b991522019-11-06 23:26:20 +01003409 buflist_data = (buf_T **)buflist.ga_data;
3410 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003411 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003412 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003413
Bram Moolenaar3b991522019-11-06 23:26:20 +01003414 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003415 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3416 : buf->b_next)
3417#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
John Marriottec032de2025-04-10 21:34:19 +02003421 char_u *name;
3422
Bram Moolenaar0751f512018-03-29 16:37:16 +02003423#ifdef FEAT_TERMINAL
3424 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003425 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003426#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003427 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003428 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3429 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3430 || (vim_strchr(eap->arg, '+')
3431 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3432 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003433 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003434 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003435 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3436#ifdef FEAT_TERMINAL
3437 || (vim_strchr(eap->arg, 'R')
3438 && (!job_running || (job_running && job_none_open)))
3439 || (vim_strchr(eap->arg, '?')
3440 && (!job_running || (job_running && !job_none_open)))
3441 || (vim_strchr(eap->arg, 'F')
3442 && (job_running || buf->b_term == NULL))
3443#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003444 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3445 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3446 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3447 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3448 || (vim_strchr(eap->arg, '#')
3449 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 continue;
John Marriottec032de2025-04-10 21:34:19 +02003451 name = buf_spname(buf);
3452 if (name != NULL)
3453 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 else
3455 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003456 if (message_filtered(NameBuff))
3457 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003459 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3460 : (bufIsChanged(buf) ? '+' : ' ');
3461#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003462 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003463 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003464 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003465 ro_char = '?';
3466 else
3467 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003468 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3469 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003470 }
3471 else if (buf->b_term != NULL)
3472 ro_char = 'F';
3473 else
3474#endif
3475 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3476
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003477 msg_putchar('\n');
John Marriottec032de2025-04-10 21:34:19 +02003478 len = (int)vim_snprintf_safelen((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 buf->b_fnum,
3480 buf->b_p_bl ? ' ' : 'u',
3481 buf == curbuf ? '%' :
3482 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3483 buf->b_ml.ml_mfp == NULL ? ' ' :
3484 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003485 ro_char,
3486 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003487 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaarc667da52019-11-30 20:52:27 +01003489 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 i = 40 - vim_strsize(IObuff);
3491 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003493 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003494#ifdef FEAT_VIMINFO
3495 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3496 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3497 else
3498#endif
3499 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3500 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003501 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003503 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 ui_breakcheck();
3505 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003506
3507#ifdef FEAT_VIMINFO
3508 if (buflist_data)
3509 ga_clear(&buflist);
3510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513/*
3514 * Get file name and line number for file 'fnum'.
3515 * Used by DoOneCmd() for translating '%' and '#'.
3516 * Used by insert_reg() and cmdline_paste() for '#' register.
3517 * Return FAIL if not found, OK for success.
3518 */
3519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003520buflist_name_nr(
3521 int fnum,
3522 char_u **fname,
3523 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 buf_T *buf;
3526
3527 buf = buflist_findnr(fnum);
3528 if (buf == NULL || buf->b_fname == NULL)
3529 return FAIL;
3530
3531 *fname = buf->b_fname;
3532 *lnum = buflist_findlnum(buf);
3533
3534 return OK;
3535}
3536
3537/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003538 * Set the file name for "buf"' to "ffname_arg", short file name to
3539 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 * The file name with the full path is also remembered, for when :cd is used.
3541 * Returns FAIL for failure (file name already in use by other buffer)
3542 * OK otherwise.
3543 */
3544 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003545setfname(
3546 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003547 char_u *ffname_arg,
3548 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003549 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003551 char_u *ffname = ffname_arg;
3552 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003553 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003555 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
3557
3558 if (ffname == NULL || *ffname == NUL)
3559 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003560 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003561 if (buf->b_sfname != buf->b_ffname)
3562 VIM_CLEAR(buf->b_sfname);
3563 else
3564 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003565 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#ifdef UNIX
3567 st.st_dev = (dev_T)-1;
3568#endif
3569 }
3570 else
3571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003572 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3573 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return FAIL;
3575
3576 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003577 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 * - if the buffer is loaded, fail
3579 * - if the buffer is not loaded, delete it from the list
3580 */
3581#ifdef UNIX
3582 if (mch_stat((char *)ffname, &st) < 0)
3583 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003584#endif
3585 if (!(buf->b_flags & BF_DUMMY))
3586#ifdef UNIX
3587 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003589 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590#endif
3591 if (obuf != NULL && obuf != buf)
3592 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003593 win_T *win;
3594 tabpage_T *tab;
3595 int in_use = FALSE;
3596
3597 // during startup a window may use a buffer that is not loaded yet
3598 FOR_ALL_TAB_WINDOWS(tab, win)
3599 if (win->w_buffer == obuf)
3600 in_use = TRUE;
3601
3602 // it's loaded or used in a window, fail
3603 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003606 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 vim_free(ffname);
3608 return FAIL;
3609 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003610 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003611 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 sfname = vim_strsave(sfname);
3614 if (ffname == NULL || sfname == NULL)
3615 {
3616 vim_free(sfname);
3617 vim_free(ffname);
3618 return FAIL;
3619 }
3620#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003621 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003623 if (buf->b_sfname != buf->b_ffname)
3624 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 buf->b_ffname = ffname;
3627 buf->b_sfname = sfname;
3628 }
3629 buf->b_fname = buf->b_sfname;
3630#ifdef UNIX
3631 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003632 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
3634 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003635 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 buf->b_dev = st.st_dev;
3637 buf->b_ino = st.st_ino;
3638 }
3639#endif
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 buf_name_changed(buf);
3644 return OK;
3645}
3646
3647/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003648 * Crude way of changing the name of a buffer. Use with care!
3649 * The name should be relative to the current directory.
3650 */
3651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003652buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003653{
3654 buf_T *buf;
3655
3656 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003657 if (buf == NULL)
3658 return;
3659
3660 if (buf->b_sfname != buf->b_ffname)
3661 vim_free(buf->b_sfname);
3662 vim_free(buf->b_ffname);
3663 buf->b_ffname = vim_strsave(name);
3664 buf->b_sfname = NULL;
3665 // Allocate ffname and expand into full path. Also resolves .lnk
3666 // files on Win32.
3667 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3668 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003669}
3670
3671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 * Take care of what needs to be done when the name of buffer "buf" has
3673 * changed.
3674 */
3675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003676buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
3678 /*
3679 * If the file name changed, also change the name of the swapfile
3680 */
3681 if (buf->b_ml.ml_mfp != NULL)
3682 ml_setname(buf);
3683
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003684#ifdef FEAT_TERMINAL
3685 if (buf->b_term != NULL)
3686 term_clear_status_text(buf->b_term);
3687#endif
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003690 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003691 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003692 status_redraw_all(); // status lines need to be redrawn
3693 fmarks_check_names(buf); // check named file marks
3694 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695}
3696
3697/*
3698 * set alternate file name for current window
3699 *
3700 * Used by do_one_cmd(), do_write() and do_ecmd().
3701 * Return the buffer.
3702 */
3703 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003704setaltfname(
3705 char_u *ffname,
3706 char_u *sfname,
3707 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 buf_T *buf;
3710
Bram Moolenaarc667da52019-11-30 20:52:27 +01003711 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003713 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 curwin->w_alt_fnum = buf->b_fnum;
3715 return buf;
3716}
3717
3718/*
3719 * Get alternate file name for current window.
3720 * Return NULL if there isn't any, and give error message if requested.
3721 */
3722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003724 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 char_u *fname;
3727 linenr_T dummy;
3728
3729 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3730 {
3731 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003732 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 return NULL;
3734 }
3735 return fname;
3736}
3737
3738/*
3739 * Add a file name to the buflist and return its number.
3740 * Uses same flags as buflist_new(), except BLN_DUMMY.
3741 *
3742 * used by qf_init(), main() and doarglist()
3743 */
3744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003745buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 buf_T *buf;
3748
3749 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3750 if (buf != NULL)
3751 return buf->b_fnum;
3752 return 0;
3753}
3754
3755#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3756/*
3757 * Adjust slashes in file names. Called after 'shellslash' was set.
3758 */
3759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003760buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
3762 buf_T *bp;
3763
Bram Moolenaar29323592016-07-24 22:04:11 +02003764 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
3766 if (bp->b_ffname != NULL)
3767 slash_adjust(bp->b_ffname);
3768 if (bp->b_sfname != NULL)
3769 slash_adjust(bp->b_sfname);
3770 }
3771}
3772#endif
3773
3774/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003775 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 * Also save the local window option values.
3777 */
3778 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003781 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782}
3783
3784/*
3785 * Return TRUE if 'ffname' is not the same file as current file.
3786 * Fname must have a full path (expanded by mch_FullName()).
3787 */
3788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003789otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
3791 return otherfile_buf(curbuf, ffname
3792#ifdef UNIX
3793 , NULL
3794#endif
3795 );
3796}
3797
3798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003799otherfile_buf(
3800 buf_T *buf,
3801 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003803 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003805 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003807 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3809 return TRUE;
3810 if (fnamecmp(ffname, buf->b_ffname) == 0)
3811 return FALSE;
3812#ifdef UNIX
3813 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003814 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaarc667da52019-11-30 20:52:27 +01003816 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (stp == NULL)
3818 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003819 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 st.st_dev = (dev_T)-1;
3821 stp = &st;
3822 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003823 // Use dev/ino to check if the files are the same, even when the names
3824 // are different (possible with links). Still need to compare the
3825 // name above, for when the file doesn't exist yet.
3826 // Problem: The dev/ino changes when a file is deleted (and created
3827 // again) and remains the same when renamed/moved. We don't want to
3828 // mch_stat() each buffer each time, that would be too slow. Get the
3829 // dev/ino again when they appear to match, but not when they appear
3830 // to be different: Could skip a buffer when it's actually the same
3831 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (buf_same_ino(buf, stp))
3833 {
3834 buf_setino(buf);
3835 if (buf_same_ino(buf, stp))
3836 return FALSE;
3837 }
3838 }
3839#endif
3840 return TRUE;
3841}
3842
3843#if defined(UNIX) || defined(PROTO)
3844/*
3845 * Set inode and device number for a buffer.
3846 * Must always be called when b_fname is changed!.
3847 */
3848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003851 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3854 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003855 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 buf->b_dev = st.st_dev;
3857 buf->b_ino = st.st_ino;
3858 }
3859 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003860 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861}
3862
3863/*
3864 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3865 */
3866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003867buf_same_ino(
3868 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003869 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003871 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 && stp->st_dev == buf->b_dev
3873 && stp->st_ino == buf->b_ino);
3874}
3875#endif
3876
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003877/*
3878 * Print info about the current buffer.
3879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003881fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003882 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003883 int shorthelp,
3884 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886 char_u *name;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003887 char *buffer;
John Marriottec032de2025-04-10 21:34:19 +02003888 size_t bufferlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003890 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (buffer == NULL)
3892 return;
3893
Bram Moolenaarc667da52019-11-30 20:52:27 +01003894 if (fullname > 1) // 2 CTRL-G: include buffer number
John Marriottec032de2025-04-10 21:34:19 +02003895 bufferlen = vim_snprintf_safelen(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
John Marriottec032de2025-04-10 21:34:19 +02003897 buffer[bufferlen++] = '"';
3898
3899 name = buf_spname(curbuf);
3900 if (name != NULL)
3901 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3902 IOSIZE - bufferlen, "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
3904 {
3905 if (!fullname && curbuf->b_fname != NULL)
3906 name = curbuf->b_fname;
3907 else
3908 name = curbuf->b_ffname;
John Marriottec032de2025-04-10 21:34:19 +02003909 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)buffer + bufferlen,
3910 IOSIZE - (int)bufferlen, TRUE);
3911 bufferlen += STRLEN(buffer + bufferlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913
John Marriottec032de2025-04-10 21:34:19 +02003914 bufferlen += vim_snprintf_safelen(
3915 buffer + bufferlen,
3916 IOSIZE - bufferlen,
3917 "\"%s%s%s%s%s%s",
3918 curbufIsChanged() ? (shortmess(SHM_MOD)
3919 ? " [+]" : _(" [Modified]")) : " ",
3920 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
3921 ? _("[Not edited]") : "",
3922 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
3923 ? new_file_message() : "",
3924 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", curbuf->b_p_ro
3925 ? (shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")) : "",
3926 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro)
3927 ? " " : "");
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (curbuf->b_ml.ml_flags & ML_EMPTY)
John Marriottec032de2025-04-10 21:34:19 +02003930 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3931 IOSIZE - bufferlen, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003933 // Current line and column are already on the screen -- webb
John Marriottec032de2025-04-10 21:34:19 +02003934 bufferlen += vim_snprintf_safelen(
3935 buffer + bufferlen,
3936 IOSIZE - bufferlen,
3937 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--", curbuf->b_ml.ml_line_count),
3938 (long)curbuf->b_ml.ml_line_count,
3939 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 else
3941 {
John Marriottec032de2025-04-10 21:34:19 +02003942 bufferlen += vim_snprintf_safelen(
3943 buffer + bufferlen,
3944 IOSIZE - bufferlen,
3945 _("line %ld of %ld --%d%%-- col "),
3946 (long)curwin->w_cursor.lnum,
3947 (long)curbuf->b_ml.ml_line_count,
3948 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 validate_virtcol();
John Marriottec032de2025-04-10 21:34:19 +02003951 bufferlen += col_print((char_u *)buffer + bufferlen, IOSIZE - bufferlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3953 }
3954
John Marriottec032de2025-04-10 21:34:19 +02003955 (void)append_arg_number(curwin, (char_u *)buffer + bufferlen,
3956 IOSIZE - bufferlen, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
3958 if (dont_truncate)
3959 {
John Marriottec032de2025-04-10 21:34:19 +02003960 int n;
3961
Bram Moolenaarc667da52019-11-30 20:52:27 +01003962 // Temporarily set msg_scroll to avoid the message being truncated.
3963 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 msg_start();
3965 n = msg_scroll;
3966 msg_scroll = TRUE;
3967 msg(buffer);
3968 msg_scroll = n;
3969 }
3970 else
3971 {
John Marriottec032de2025-04-10 21:34:19 +02003972 char *p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003974 // Need to repeat the message after redrawing when:
3975 // - When restart_edit is set (otherwise there will be a delay
3976 // before redrawing).
3977 // - When the screen was scrolled but there is no wait-return
3978 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003979 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981
3982 vim_free(buffer);
3983}
3984
John Marriotta21240b2025-01-08 20:10:59 +01003985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003986col_print(
3987 char_u *buf,
3988 size_t buflen,
3989 int col,
3990 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 if (col == vcol)
John Marriottec032de2025-04-10 21:34:19 +02003993 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d", col);
John Marriotta21240b2025-01-08 20:10:59 +01003994
John Marriottec032de2025-04-10 21:34:19 +02003995 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998static char_u *lasttitle = NULL;
3999static char_u *lasticon = NULL;
4000
Bram Moolenaar84a93082018-06-16 22:58:15 +02004001/*
4002 * Put the file name in the title bar and icon of the window.
4003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004005maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar84a93082018-06-16 22:58:15 +02004007 char_u *title_str = NULL;
4008 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 int mustset;
4010 char_u buf[IOSIZE];
John Marriottec032de2025-04-10 21:34:19 +02004011 size_t buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (!redrawing())
4014 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004015 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 need_maketitle = TRUE;
4017 return;
4018 }
4019
4020 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02004021 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004022 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 if (p_title)
4025 {
John Marriottec032de2025-04-10 21:34:19 +02004026 int maxlen = 0;
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (p_titlelen > 0)
4029 {
4030 maxlen = p_titlelen * Columns / 100;
4031 if (maxlen < 10)
4032 maxlen = 10;
4033 }
4034
Bram Moolenaar84a93082018-06-16 22:58:15 +02004035 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (*p_titlestring != NUL)
4037 {
4038#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004039 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004040 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4041 (char_u *)"titlestring", 0,
4042 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 else
4044#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004045 title_str = p_titlestring;
John Marriottec032de2025-04-10 21:34:19 +02004046 buflen = STRLEN(title_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048 else
4049 {
John Marriottec032de2025-04-10 21:34:19 +02004050 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
John Marriottec032de2025-04-10 21:34:19 +02004052 // format: "<filename> [flags] <(path)> [argument info] <- servername>"
4053 // example:
4054 // buffer.c + (/home/vim/src) (1 of 2) - VIM
4055
4056 // reserve some space for different parts of the title.
4057 // use sizeof() to introduce 'size_t' so we don't have to
4058 // cast sizes to it.
4059#define SPACE_FOR_FNAME (sizeof(buf) - 100)
4060#define SPACE_FOR_DIR (sizeof(buf) - 20)
4061#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // at least room for " - VIM"
4062
4063 // file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (curbuf->b_fname == NULL)
John Marriottec032de2025-04-10 21:34:19 +02004065 buflen = vim_snprintf_safelen((char *)buf,
4066 SPACE_FOR_FNAME, "%s", _("[No Name]"));
Bram Moolenaar21554412017-07-24 21:44:43 +02004067#ifdef FEAT_TERMINAL
4068 else if (curbuf->b_term != NULL)
John Marriottec032de2025-04-10 21:34:19 +02004069 buflen = vim_snprintf_safelen((char *)buf,
4070 SPACE_FOR_FNAME, "%s",
4071 term_get_status_text(curbuf->b_term));
Bram Moolenaar21554412017-07-24 21:44:43 +02004072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 else
4074 {
John Marriottec032de2025-04-10 21:34:19 +02004075 buflen = vim_snprintf_safelen((char *)buf,
4076 SPACE_FOR_FNAME, "%s",
4077 ((p = transstr(gettail(curbuf->b_fname))) != NULL)
4078 ? p
4079 : (char_u *)"");
4080 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082
John Marriottec032de2025-04-10 21:34:19 +02004083 // flags
Bram Moolenaar21554412017-07-24 21:44:43 +02004084#ifdef FEAT_TERMINAL
4085 if (curbuf->b_term == NULL)
4086#endif
John Marriottec032de2025-04-10 21:34:19 +02004087 {
Bram Moolenaar21554412017-07-24 21:44:43 +02004088 switch (bufIsChanged(curbuf)
4089 + (curbuf->b_p_ro * 2)
4090 + (!curbuf->b_p_ma * 4))
4091 {
John Marriottec032de2025-04-10 21:34:19 +02004092 case 1:
4093 // file was modified
4094 buflen += vim_snprintf_safelen(
4095 (char *)buf + buflen,
4096 sizeof(buf) - buflen, " +");
4097 break;
4098 case 2:
4099 // file is readonly
4100 buflen += vim_snprintf_safelen(
4101 (char *)buf + buflen,
4102 sizeof(buf) - buflen, " =");
4103 break;
4104 case 3:
4105 // file was modified and is readonly
4106 buflen += vim_snprintf_safelen(
4107 (char *)buf + buflen,
4108 sizeof(buf) - buflen, " =+");
4109 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004110 case 4:
John Marriottec032de2025-04-10 21:34:19 +02004111 case 6:
4112 // file cannot be modified
4113 buflen += vim_snprintf_safelen(
4114 (char *)buf + buflen,
4115 sizeof(buf) - buflen, " -");
4116 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004117 case 5:
John Marriottec032de2025-04-10 21:34:19 +02004118 case 7:
4119 // file cannot be modified but was modified
4120 buflen += vim_snprintf_safelen(
4121 (char *)buf + buflen,
4122 sizeof(buf) - buflen, " -+");
4123 break;
4124 default:
4125 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004126 }
John Marriottec032de2025-04-10 21:34:19 +02004127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
John Marriottec032de2025-04-10 21:34:19 +02004129 // path (surrounded by '()')
Bram Moolenaar21554412017-07-24 21:44:43 +02004130 if (curbuf->b_fname != NULL
4131#ifdef FEAT_TERMINAL
4132 && curbuf->b_term == NULL
4133#endif
4134 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004136 // Get path of file, replace home dir with ~
John Marriottec032de2025-04-10 21:34:19 +02004137 buflen += vim_snprintf_safelen((char *)buf + buflen,
4138 sizeof(buf) - buflen, " (");
4139
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 home_replace(curbuf, curbuf->b_ffname,
John Marriottec032de2025-04-10 21:34:19 +02004141 buf + buflen, (int)(SPACE_FOR_DIR - buflen), TRUE);
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004144 // avoid "c:/name" to be reduced to "c"
John Marriottec032de2025-04-10 21:34:19 +02004145 if (SAFE_isalpha(buf[buflen]) && buf[buflen + 1] == ':')
4146 buflen += 2; // step over "c:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#endif
John Marriottec032de2025-04-10 21:34:19 +02004148
4149 // determine if we have a help or normal buffer
4150 p = gettail_sep(buf + buflen);
4151 if (p == buf + buflen)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004152 {
John Marriottec032de2025-04-10 21:34:19 +02004153 // help buffer
4154 buflen += vim_snprintf_safelen((char *)buf + buflen,
4155 SPACE_FOR_DIR - buflen, "%s)", _("help"));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 else
Bram Moolenaar2c666692012-09-05 13:30:40 +02004158 {
John Marriottec032de2025-04-10 21:34:19 +02004159 // normal buffer
4160
4161 // Translate unprintable chars and concatenate. Keep some
4162 // room for the server name. When there is no room (very long
4163 // file name) use (...).
4164 if (buflen < SPACE_FOR_DIR)
John Marriott7fb90812025-04-02 20:32:35 +02004165 {
John Marriottec032de2025-04-10 21:34:19 +02004166 // remove the file name
4167 *p = NUL;
4168
4169 buflen += vim_snprintf_safelen((char *)buf + buflen,
4170 SPACE_FOR_DIR - buflen, "%s)",
4171 ((p = transstr(buf + buflen)) != NULL)
4172 ? p
4173 : (char_u *)"");
John Marriott7fb90812025-04-02 20:32:35 +02004174 vim_free(p);
4175 }
John Marriottec032de2025-04-10 21:34:19 +02004176 else
4177 buflen += vim_snprintf_safelen((char *)buf + buflen,
4178 SPACE_FOR_ARGNR - buflen, "...)");
Bram Moolenaar2c666692012-09-05 13:30:40 +02004179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181
John Marriottec032de2025-04-10 21:34:19 +02004182 // argument info
4183 buflen += append_arg_number(curwin, buf + buflen,
4184 SPACE_FOR_ARGNR - buflen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
John Marriottec032de2025-04-10 21:34:19 +02004186 // servername
4187 buflen += vim_snprintf_safelen((char *)buf + buflen,
4188 sizeof(buf) - buflen, " - %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#if defined(FEAT_CLIENTSERVER)
John Marriottec032de2025-04-10 21:34:19 +02004190 (serverName != NULL)
4191 ? serverName :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192#endif
John Marriottec032de2025-04-10 21:34:19 +02004193 (char_u *)"VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195 if (maxlen > 0)
4196 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004197 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004198 if (vim_strsize(buf) > maxlen)
John Marriottec032de2025-04-10 21:34:19 +02004199 trunc_string(buf, buf, maxlen, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004203 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 if (p_icon)
4206 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004207 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (*p_iconstring != NUL)
4209 {
4210#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004211 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004212 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4213 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 else
4215#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004216 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 else
4219 {
John Marriottec032de2025-04-10 21:34:19 +02004220 char_u *name;
4221 int namelen;
4222
4223 name = buf_spname(curbuf);
4224 if (name == NULL)
4225 name = gettail(curbuf->b_ffname);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004226 // Truncate name at 100 bytes.
John Marriottec032de2025-04-10 21:34:19 +02004227 namelen = (int)STRLEN(name);
4228 if (namelen > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
John Marriottec032de2025-04-10 21:34:19 +02004230 namelen -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (has_mbyte)
John Marriottec032de2025-04-10 21:34:19 +02004232 namelen += (*mb_tail_off)(name, name + namelen) + 1;
4233 name += namelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
John Marriottec032de2025-04-10 21:34:19 +02004235 STRCPY(buf, name);
4236 trans_characters(buf, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 }
4238 }
4239
Bram Moolenaar84a93082018-06-16 22:58:15 +02004240 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 if (mustset)
4243 resettitle();
4244}
4245
4246/*
4247 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4248 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004249 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 */
4251 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004252value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 if ((str == NULL) != (*last == NULL)
4255 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4256 {
4257 vim_free(*last);
4258 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004261 mch_restore_title(
4262 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004267 return TRUE;
4268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 return FALSE;
4271}
4272
4273/*
4274 * Put current window title back (used after calling a shell)
4275 */
4276 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004277resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 mch_settitle(lasttitle, lasticon);
4280}
Bram Moolenaarea408852005-06-25 22:49:46 +00004281
4282# if defined(EXITFREE) || defined(PROTO)
4283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004284free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004285{
4286 vim_free(lasttitle);
4287 vim_free(lasticon);
4288}
4289# endif
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004292#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004293
4294/*
4295 * Used for building in the status line.
4296 */
4297typedef struct
4298{
4299 char_u *stl_start;
4300 int stl_minwid;
4301 int stl_maxwid;
4302 enum {
4303 Normal,
4304 Empty,
4305 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004306 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004307 Highlight,
4308 TabPage,
4309 Trunc
4310 } stl_type;
4311} stl_item_T;
4312
4313static size_t stl_items_len = 20; // Initial value, grows as needed.
4314static stl_item_T *stl_items = NULL;
4315static int *stl_groupitem = NULL;
4316static stl_hlrec_T *stl_hltab = NULL;
4317static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004318static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004321 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 * Return length of string in screen cells.
4323 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004324 * Normally works for window "wp", except when working for 'tabline' then it
4325 * is "curwin".
4326 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004328 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4330 *
4331 * If maxwidth is not zero, the string will be filled at any middle marker
4332 * or truncated if too long, fillchar is used for all whitespace.
4333 */
4334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004335build_stl_str_hl(
4336 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004337 char_u *out, // buffer to write into != NameBuff
4338 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004339 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004340 char_u *opt_name, // option name corresponding to "fmt"
4341 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004342 int fillchar,
4343 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004344 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4345 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
Bram Moolenaar10772302019-01-20 18:25:54 +01004347 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004348 colnr_T len;
John Marriottec032de2025-04-10 21:34:19 +02004349 size_t outputlen; // length of out[] used (excluding the NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 char_u *p;
4351 char_u *s;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004352 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004354 int use_sandbox;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004355 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#endif
4357 int empty_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 long l;
4359 long n;
4360 int prevchar_isflag;
4361 int prevchar_isitem;
4362 int itemisflag;
4363 int fillable;
4364 char_u *str;
4365 long num;
4366 int width;
4367 int itemcnt;
4368 int curitem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004370#ifdef FEAT_EVAL
4371 int evaldepth;
4372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 int minwid;
4374 int maxwid;
4375 int zeropad;
4376 char_u base;
4377 char_u opt;
4378#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004379 char_u buf_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004380 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004381 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004382 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004383 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004384 // TODO: find out why using called_emsg_before makes tests fail, does it
4385 // matter?
4386 // int called_emsg_before = called_emsg;
4387 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004388
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004389 // When inside update_screen() we do not want redrawing a statusline,
4390 // ruler, title, etc. to trigger another redraw, it may cause an endless
4391 // loop.
4392 if (updating_screen)
4393 redraw_not_allowed = TRUE;
4394
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004395 if (stl_items == NULL)
4396 {
4397 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4398 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004399
4400 // Allocate one more, because the last element is used to indicate the
4401 // end of the list.
4402 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4403 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004404
4405 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004406 }
4407
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004408#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004409 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4410 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4411
4412 // When the format starts with "%!" then evaluate it as an expression and
4413 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004414 if (fmt[0] == '%' && fmt[1] == '!')
4415 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004416 typval_T tv;
4417
4418 tv.v_type = VAR_NUMBER;
4419 tv.vval.v_number = wp->w_id;
4420 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4421
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004422 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004423 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004424 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004425
4426 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004427 }
4428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 if (fillchar == 0)
4431 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004432
Bram Moolenaar10772302019-01-20 18:25:54 +01004433 // The cursor in windows other than the current one isn't always
4434 // up-to-date, esp. because of autocommands and timers.
4435 lnum = wp->w_cursor.lnum;
4436 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4437 {
4438 lnum = wp->w_buffer->b_ml.ml_line_count;
4439 wp->w_cursor.lnum = lnum;
4440 }
4441
4442 // Get line & check if empty (cursorpos will show "0-1"). Note that
4443 // p will become invalid when getting another buffer line.
4444 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004445 empty_line = (*p == NUL);
4446
Bram Moolenaar10772302019-01-20 18:25:54 +01004447 // Get the byte value now, in case we need it below. This is more efficient
4448 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004449 len = ml_get_buf_len(wp->w_buffer, lnum);
4450 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004451 {
4452 // Line may have changed since checking the cursor column, or the lnum
4453 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004454 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004455 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004456 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004457 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004458 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004459 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004462#ifdef FEAT_EVAL
4463 evaldepth = 0;
4464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 p = out;
4466 curitem = 0;
4467 prevchar_isflag = TRUE;
4468 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004469 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004471 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004472 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004473 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004475 stl_item_T *new_items =
4476 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004477 if (new_items == NULL)
4478 break;
4479 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004480
4481 int *new_groupitem =
4482 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004483 if (new_groupitem == NULL)
4484 break;
4485 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004486
4487 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004488 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004489 if (new_hlrec == NULL)
4490 break;
4491 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004492 new_hlrec = vim_realloc(stl_tabtab,
4493 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004494 if (new_hlrec == NULL)
4495 break;
4496 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004497
4498 int *new_separator_locs = vim_realloc(stl_separator_locations,
4499 sizeof(int) * new_len);
4500 if (new_separator_locs == NULL)
4501 break;
John Marriottec032de2025-04-10 21:34:19 +02004502 stl_separator_locations = new_separator_locs;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004503
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004504 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004505 }
4506
zeertzjq122dea72022-07-27 15:48:45 +01004507 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 prevchar_isflag = prevchar_isitem = FALSE;
4509
4510 /*
4511 * Handle up to the next '%' or the end.
4512 */
4513 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4514 *p++ = *s++;
4515 if (*s == NUL || p + 1 >= out + outlen)
4516 break;
4517
4518 /*
4519 * Handle one '%' item.
4520 */
4521 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004522 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (*s == '%')
4525 {
4526 if (p + 1 >= out + outlen)
4527 break;
4528 *p++ = *s++;
4529 prevchar_isflag = prevchar_isitem = FALSE;
4530 continue;
4531 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004532 // STL_SEPARATE: Separation between items, filled with white space.
4533 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
4535 s++;
4536 if (groupdepth > 0)
4537 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004538 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004539 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 continue;
4541 }
4542 if (*s == STL_TRUNCMARK)
4543 {
4544 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004545 stl_items[curitem].stl_type = Trunc;
4546 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 continue;
4548 }
4549 if (*s == ')')
4550 {
John Marriottec032de2025-04-10 21:34:19 +02004551 char_u *t;
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 s++;
4554 if (groupdepth < 1)
4555 continue;
4556 groupdepth--;
4557
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004558 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 *p = NUL;
4560 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004561 if (curitem > stl_groupitem[groupdepth] + 1
4562 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
John Marriottec032de2025-04-10 21:34:19 +02004564 int group_start_userhl = 0;
4565 int group_end_userhl = 0;
4566
Bram Moolenaarc667da52019-11-30 20:52:27 +01004567 // remove group if all items are empty and highlight group
4568 // doesn't change
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004569 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004570 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004571 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004572 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004573 group_start_userhl = group_end_userhl =
4574 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004575 break;
4576 }
4577 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004578 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004579 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004580 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004582 if (stl_items[n].stl_type == Highlight)
4583 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004584 }
4585 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004587 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 p = t;
4589 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004590 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004591 {
4592 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004593 if (stl_items[n].stl_type == Highlight)
4594 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004595 // adjust the start position of TabPage to the next
4596 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004597 if (stl_items[n].stl_type == TabPage)
4598 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004602 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004604 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (has_mbyte)
4606 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004607 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004609 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004612 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614 }
4615 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004616 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4617 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004620 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004622
4623 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004624 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004625 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaarc667da52019-11-30 20:52:27 +01004627 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004628 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
LemonBoy57ff5262022-05-09 21:03:47 +01004630 // Minus one for the leading '<' added above.
4631 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004632 if (stl_items[l].stl_start < t)
4633 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004636 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004638 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004639 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (n < 0)
4641 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004642 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 n = 0 - n;
4644 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004645 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
4647 else
4648 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004649 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004650 l = (n - l) * MB_CHAR2LEN(fillchar);
4651 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004653 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004655 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4656 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004658 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 }
4661 continue;
4662 }
4663 minwid = 0;
4664 maxwid = 9999;
4665 zeropad = FALSE;
4666 l = 1;
4667 if (*s == '0')
4668 {
4669 s++;
4670 zeropad = TRUE;
4671 }
4672 if (*s == '-')
4673 {
4674 s++;
4675 l = -1;
4676 }
4677 if (VIM_ISDIGIT(*s))
4678 {
4679 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004680 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 minwid = 0;
4682 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004683 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004685 stl_items[curitem].stl_type = Highlight;
4686 stl_items[curitem].stl_start = p;
4687 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 s++;
4689 curitem++;
4690 continue;
4691 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004692 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4693 {
4694 if (*s == STL_TABCLOSENR)
4695 {
4696 if (minwid == 0)
4697 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004698 // %X ends the close label, go back to the previously
4699 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004700 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004701 if (stl_items[n].stl_type == TabPage
4702 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004703 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004704 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004705 break;
4706 }
4707 }
4708 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004709 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004710 minwid = - minwid;
4711 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004712 stl_items[curitem].stl_type = TabPage;
4713 stl_items[curitem].stl_start = p;
4714 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004715 s++;
4716 curitem++;
4717 continue;
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (*s == '.')
4720 {
4721 s++;
4722 if (VIM_ISDIGIT(*s))
4723 {
4724 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004725 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 maxwid = 50;
4727 }
4728 }
4729 minwid = (minwid > 50 ? 50 : minwid) * l;
4730 if (*s == '(')
4731 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004732 stl_groupitem[groupdepth++] = curitem;
4733 stl_items[curitem].stl_type = Group;
4734 stl_items[curitem].stl_start = p;
4735 stl_items[curitem].stl_minwid = minwid;
4736 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 s++;
4738 curitem++;
4739 continue;
4740 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004741#ifdef FEAT_EVAL
4742 // Denotes end of expanded %{} block
4743 if (*s == '}' && evaldepth > 0)
4744 {
4745 s++;
4746 evaldepth--;
4747 continue;
4748 }
4749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 if (vim_strchr(STL_ALL, *s) == NULL)
4751 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004752 if (*s == NUL) // can happen with "%0"
4753 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 s++;
4755 continue;
4756 }
4757 opt = *s++;
4758
Bram Moolenaarc667da52019-11-30 20:52:27 +01004759 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 base = 'D';
4761 itemisflag = FALSE;
4762 fillable = TRUE;
4763 num = -1;
4764 str = NULL;
4765 switch (opt)
4766 {
4767 case STL_FILEPATH:
4768 case STL_FULLPATH:
4769 case STL_FILENAME:
John Marriottec032de2025-04-10 21:34:19 +02004770 {
4771 char_u *name;
4772
Bram Moolenaarc667da52019-11-30 20:52:27 +01004773 fillable = FALSE; // don't change ' ' to fillchar
John Marriottec032de2025-04-10 21:34:19 +02004774 name = buf_spname(wp->w_buffer);
4775 if (name != NULL)
4776 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 else
4778 {
John Marriottec032de2025-04-10 21:34:19 +02004779 char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004780 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4782 }
4783 trans_characters(NameBuff, MAXPATHL);
4784 if (opt != STL_FILENAME)
4785 str = NameBuff;
4786 else
4787 str = gettail(NameBuff);
4788 break;
John Marriottec032de2025-04-10 21:34:19 +02004789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaarc667da52019-11-30 20:52:27 +01004791 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004792 {
4793#ifdef FEAT_EVAL
John Marriottec032de2025-04-10 21:34:19 +02004794 char_u *block_start = s - 1;
shadmansaleh30e3de22021-05-15 17:23:28 +02004795#endif
John Marriottec032de2025-04-10 21:34:19 +02004796 int reevaluate = (*s == '%');
4797 char_u *t;
4798 buf_T *save_curbuf;
4799 win_T *save_curwin;
shadmansaleh30e3de22021-05-15 17:23:28 +02004800
4801 if (reevaluate)
4802 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 itemisflag = TRUE;
4804 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004805 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4806 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004808 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 break;
4810 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004811 if (reevaluate)
John Marriottec032de2025-04-10 21:34:19 +02004812 p[-1] = NUL; // remove the % at the end of %{% expr %}
shadmansaleh30e3de22021-05-15 17:23:28 +02004813 else
John Marriottec032de2025-04-10 21:34:19 +02004814 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004817 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4818 "%d", curbuf->b_fnum);
4819 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
John Marriottec032de2025-04-10 21:34:19 +02004820 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "%d", curwin->w_id);
4821 set_internal_string_var((char_u *)"g:actual_curwin", buf_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004823 save_curbuf = curbuf;
4824 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004825 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 curwin = wp;
4827 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004828 // Visual mode is only valid in the current window.
4829 if (curwin != save_curwin)
4830 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004832 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004834 curwin = save_curwin;
4835 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004836 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004837 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004838 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
John Marriottec032de2025-04-10 21:34:19 +02004840 if (str != NULL && *str != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 if (*skipdigits(str) == NUL)
4843 {
4844 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004845 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 itemisflag = FALSE;
4847 }
4848 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004849
4850 // If the output of the expression needs to be evaluated
4851 // replace the %{} block with the result of evaluation
John Marriottec032de2025-04-10 21:34:19 +02004852 if (reevaluate && str != NULL && *str != NUL
shadmansaleh30e3de22021-05-15 17:23:28 +02004853 && strchr((const char *)str, '%') != NULL
4854 && evaldepth < MAX_STL_EVAL_DEPTH)
4855 {
4856 size_t parsed_usefmt = (size_t)(block_start - usefmt);
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004857 size_t str_length = strlen((const char *)str);
4858 size_t fmt_length = strlen((const char *)s);
4859 size_t new_fmt_len = parsed_usefmt
4860 + str_length + fmt_length + 3;
4861 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004862
John Marriott7fb90812025-04-02 20:32:35 +02004863 if (new_fmt != NULL)
4864 {
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004865 char_u *new_fmt_p = new_fmt;
4866
4867 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4868 + parsed_usefmt;
4869 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4870 + str_length;
4871 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4872 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4873 + fmt_length;
4874 *new_fmt_p = 0;
4875 new_fmt_p = NULL;
John Marriott7fb90812025-04-02 20:32:35 +02004876
4877 if (usefmt != fmt)
4878 vim_free(usefmt);
4879 VIM_CLEAR(str);
4880 usefmt = new_fmt;
4881 s = usefmt + parsed_usefmt;
4882 evaldepth++;
4883 continue;
4884 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#endif
4887 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 case STL_LINE:
4890 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4891 ? 0L : (long)(wp->w_cursor.lnum);
4892 break;
4893
4894 case STL_NUMLINES:
4895 num = wp->w_buffer->b_ml.ml_line_count;
4896 break;
4897
4898 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004899 num = (State & MODE_INSERT) == 0 && empty_line
4900 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 break;
4902
4903 case STL_VIRTCOL:
4904 case STL_VIRTCOL_ALT:
John Marriottec032de2025-04-10 21:34:19 +02004905 {
4906 colnr_T virtcol = wp->w_virtcol + 1;
4907
Bram Moolenaarc667da52019-11-30 20:52:27 +01004908 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004910 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4911 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 break;
4913 num = (long)virtcol;
4914 break;
John Marriottec032de2025-04-10 21:34:19 +02004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 case STL_PERCENTAGE:
John Marriottec032de2025-04-10 21:34:19 +02004918 num = calc_percentage((long)wp->w_cursor.lnum, (long)wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 break;
4920
4921 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004922 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004923 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925
Luuk van Baalba936f62022-12-15 13:15:39 +00004926 case STL_SHOWCMD:
4927 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4928 str = showcmd_buf;
4929 break;
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 case STL_ARGLISTSTAT:
4932 fillable = FALSE;
John Marriottec032de2025-04-10 21:34:19 +02004933 buf_tmp[0] = NUL;
4934 if (append_arg_number(wp, buf_tmp, sizeof(buf_tmp), FALSE) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004935 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937
4938 case STL_KEYMAP:
4939 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004940 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004941 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 break;
4943 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004944#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004945 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#else
4947 num = 0;
4948#endif
4949 break;
4950
4951 case STL_BUFNO:
4952 num = wp->w_buffer->b_fnum;
4953 break;
4954
4955 case STL_OFFSET_X:
4956 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004957 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 case STL_OFFSET:
4959#ifdef FEAT_BYTEOFF
4960 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004961 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4962 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4963 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964#endif
4965 break;
4966
4967 case STL_BYTEVAL_X:
4968 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004969 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004971 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (num == NL)
4973 num = 0;
4974 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4975 num = NL;
4976 break;
4977
4978 case STL_ROFLAG:
4979 case STL_ROFLAG_ALT:
4980 itemisflag = TRUE;
4981 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004982 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 break;
4984
4985 case STL_HELPFLAG:
4986 case STL_HELPFLAG_ALT:
4987 itemisflag = TRUE;
4988 if (wp->w_buffer->b_help)
4989 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004990 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 break;
4992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 case STL_FILETYPE:
4994 if (*wp->w_buffer->b_p_ft != NUL
4995 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4996 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004997 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004998 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004999 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001 break;
5002
5003 case STL_FILETYPE_ALT:
5004 itemisflag = TRUE;
5005 if (*wp->w_buffer->b_p_ft != NUL
5006 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
5007 {
John Marriottec032de2025-04-10 21:34:19 +02005008 char_u *t;
5009
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005010 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005011 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005012 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005014 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
Bram Moolenaar4033c552017-09-16 20:54:51 +02005018#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 case STL_PREVIEWFLAG:
5020 case STL_PREVIEWFLAG_ALT:
5021 itemisflag = TRUE;
5022 if (wp->w_p_pvw)
5023 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
5024 : _("[Preview]"));
5025 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005026
5027 case STL_QUICKFIX:
5028 if (bt_quickfix(wp->w_buffer))
5029 str = (char_u *)(wp->w_llist_ref
5030 ? _(msg_loclist)
5031 : _(msg_qflist));
5032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033#endif
5034
5035 case STL_MODIFIED:
5036 case STL_MODIFIED_ALT:
5037 itemisflag = TRUE;
5038 switch ((opt == STL_MODIFIED_ALT)
5039 + bufIsChanged(wp->w_buffer) * 2
5040 + (!wp->w_buffer->b_p_ma) * 4)
5041 {
5042 case 2: str = (char_u *)"[+]"; break;
5043 case 3: str = (char_u *)",+"; break;
5044 case 4: str = (char_u *)"[-]"; break;
5045 case 5: str = (char_u *)",-"; break;
5046 case 6: str = (char_u *)"[+-]"; break;
5047 case 7: str = (char_u *)",+-"; break;
5048 }
5049 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005050
5051 case STL_HIGHLIGHT:
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005052 {
John Marriottec032de2025-04-10 21:34:19 +02005053 char_u *t = s;
5054
5055 while (*s != '#' && *s != NUL)
5056 ++s;
5057 if (*s == '#')
5058 {
5059 stl_items[curitem].stl_type = Highlight;
5060 stl_items[curitem].stl_start = p;
5061 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
5062 curitem++;
5063 }
5064 if (*s != NUL)
5065 ++s;
5066 continue;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005070 stl_items[curitem].stl_start = p;
5071 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (str != NULL && *str)
5073 {
John Marriottec032de2025-04-10 21:34:19 +02005074 char_u *t = str;
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (itemisflag)
5077 {
5078 if ((t[0] && t[1])
5079 && ((!prevchar_isitem && *t == ',')
5080 || (prevchar_isflag && *t == ' ')))
5081 t++;
5082 prevchar_isflag = TRUE;
5083 }
5084 l = vim_strsize(t);
5085 if (l > 0)
5086 prevchar_isitem = TRUE;
5087 if (l > maxwid)
5088 {
5089 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (has_mbyte)
5091 {
5092 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005093 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 l -= byte2cells(*t++);
5097 if (p + 1 >= out + outlen)
5098 break;
5099 *p++ = '<';
5100 }
5101 if (minwid > 0)
5102 {
5103 for (; l < minwid && p + 1 < out + outlen; l++)
5104 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005105 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5107 *p++ = ' ';
5108 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005109 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111 minwid = 0;
5112 }
5113 else
5114 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005115 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005117 // Change a space by fillchar, unless fillchar is '-' and a
5118 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005119 if (fillable && *t == ' '
5120 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5121 MB_CHAR2BYTES(fillchar, p);
5122 else
5123 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005126 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 else if (num >= 0)
5129 {
John Marriottec032de2025-04-10 21:34:19 +02005130 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5131 char_u nstr[20];
5132 char_u *t = nstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005135 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 prevchar_isitem = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (opt == STL_VIRTCOL_ALT)
5138 {
5139 *t++ = '-';
5140 minwid--;
5141 }
5142 *t++ = '%';
5143 if (zeropad)
5144 *t++ = '0';
5145 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005146 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
John Marriottec032de2025-04-10 21:34:19 +02005147 *t = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 for (n = num, l = 1; n >= nbase; n /= nbase)
5150 l++;
5151 if (opt == STL_VIRTCOL_ALT)
5152 l++;
John Marriottec032de2025-04-10 21:34:19 +02005153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (l > maxwid)
5155 {
5156 l += 2;
5157 n = l - maxwid;
5158 while (l-- > maxwid)
5159 num /= nbase;
5160 *t++ = '>';
5161 *t++ = '%';
5162 *t = t[-3];
John Marriottec032de2025-04-10 21:34:19 +02005163 *++t = NUL;
5164 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5165 (char *)nstr, 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 else
John Marriottec032de2025-04-10 21:34:19 +02005168 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5169 (char *)nstr, minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005172 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005174 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5175 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5176 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (opt == STL_VIM_EXPR)
5178 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 curitem++;
5180 }
5181 *p = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005182 outputlen = (size_t)(p - out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 itemcnt = curitem;
5184
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005185#ifdef FEAT_EVAL
5186 if (usefmt != fmt)
5187 vim_free(usefmt);
5188#endif
5189
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 width = vim_strsize(out);
5191 if (maxwidth > 0 && width > maxwidth)
5192 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005193 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 l = 0;
5195 if (itemcnt == 0)
5196 s = out;
5197 else
5198 {
5199 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005200 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005202 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005203 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205 }
5206 if (l == itemcnt)
5207 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005208 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005209 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 l = 0;
5211 }
5212 }
5213
5214 if (width - vim_strsize(s) >= maxwidth)
5215 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005216 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (has_mbyte)
5218 {
5219 s = out;
5220 width = 0;
5221 for (;;)
5222 {
5223 width += ptr2cells(s);
5224 if (width >= maxwidth)
5225 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005226 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005228 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005230 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 s = out + maxwidth - 1;
5234 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005235 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 break;
5237 itemcnt = l;
5238 *s++ = '>';
John Marriottec032de2025-04-10 21:34:19 +02005239 *s = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241 else
5242 {
John Marriottec032de2025-04-10 21:34:19 +02005243 char_u *end = out + outputlen;
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (has_mbyte)
5246 {
5247 n = 0;
5248 while (width >= maxwidth)
5249 {
5250 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005251 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253 }
5254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 n = width - maxwidth + 1;
5256 p = s + n;
John Marriottec032de2025-04-10 21:34:19 +02005257 mch_memmove(s + 1, p, (size_t)(end - p) + 1); // +1 for NUL
5258 end -= (size_t)(p - (s + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 *s = '<';
5260
Bram Moolenaarc667da52019-11-30 20:52:27 +01005261 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 for (; l < itemcnt; l++)
5263 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005264 if (stl_items[l].stl_start - n >= s)
5265 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005267 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
zeertzjqd392a742023-07-01 20:24:40 +01005269
5270 // Fill up for half a double-wide character.
5271 while (++width < maxwidth)
5272 {
John Marriottec032de2025-04-10 21:34:19 +02005273 s = end;
zeertzjqd392a742023-07-01 20:24:40 +01005274 MB_CHAR2BYTES(fillchar, s);
5275 *s = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005276 end = s;
zeertzjqd392a742023-07-01 20:24:40 +01005277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 width = maxwidth;
5280 }
John Marriottec032de2025-04-10 21:34:19 +02005281 else if (width < maxwidth && outputlen + maxwidth - width + 1 < outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005283 // Find how many separators there are, which we will use when
5284 // figuring out how many groups there are.
5285 int num_separators = 0;
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005289 if (stl_items[l].stl_type == Separate)
5290 {
5291 // Create an array of the start location for each separator
5292 // mark.
5293 stl_separator_locations[num_separators] = l;
5294 num_separators++;
5295 }
5296 }
5297
5298 // If we have separated groups, then we deal with it now
5299 if (num_separators)
5300 {
5301 int standard_spaces;
5302 int final_spaces;
5303
5304 standard_spaces = (maxwidth - width) / num_separators;
5305 final_spaces = (maxwidth - width) -
5306 standard_spaces * (num_separators - 1);
5307 for (l = 0; l < num_separators; l++)
5308 {
5309 int dislocation = (l == (num_separators - 1)) ?
5310 final_spaces : standard_spaces;
5311 dislocation *= MB_CHAR2LEN(fillchar);
5312 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5313 char_u *seploc = start + dislocation;
5314 STRMOVE(seploc, start);
5315 for (s = start; s < seploc;)
5316 MB_CHAR2BYTES(fillchar, s);
5317
5318 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5319 stl_items[i].stl_start += dislocation;
5320 }
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 width = maxwidth;
5323 }
5324 }
5325
Bram Moolenaarc667da52019-11-30 20:52:27 +01005326 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005327 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005329 *hltab = stl_hltab;
5330 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 for (l = 0; l < itemcnt; l++)
5332 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005333 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005335 sp->start = stl_items[l].stl_start;
5336 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005337 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005340 sp->start = NULL;
5341 sp->userhl = 0;
5342 }
5343
Bram Moolenaarc667da52019-11-30 20:52:27 +01005344 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005345 if (tabtab != NULL)
5346 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005347 *tabtab = stl_tabtab;
5348 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005349 for (l = 0; l < itemcnt; l++)
5350 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005351 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005352 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005353 sp->start = stl_items[l].stl_start;
5354 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005355 sp++;
5356 }
5357 }
5358 sp->start = NULL;
5359 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005362 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005363
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005364 // A user function may reset KeyTyped, restore it.
5365 KeyTyped = save_KeyTyped;
5366
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005367 // Check for an error. If there is one the display will be messed up and
5368 // might loop redrawing. Avoid that by making the corresponding option
5369 // empty.
5370 // TODO: find out why using called_emsg_before makes tests fail, does it
5371 // matter?
5372 // if (called_emsg > called_emsg_before)
5373 if (did_emsg > did_emsg_before)
5374 set_string_option_direct(opt_name, -1, (char_u *)"",
5375 OPT_FREE | opt_scope, SID_ERROR);
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 return width;
5378}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005379#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381/*
John Marriottec032de2025-04-10 21:34:19 +02005382 * Get relative cursor position in window into "buf[]", in the localized
Emir SARI971cd2b2023-04-29 12:09:53 +01005383 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 */
John Marriotta21240b2025-01-08 20:10:59 +01005385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005386get_rel_pos(
5387 win_T *wp,
5388 char_u *buf,
5389 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005391 long above; // number of lines above window
5392 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaarc667da52019-11-30 20:52:27 +01005394 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005395 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 above = wp->w_topline - 1;
5397#ifdef FEAT_DIFF
5398 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005399 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005400 above = 0; // All buffer lines are displayed and there is an
5401 // indication of filler lines, that can be considered
5402 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#endif
5404 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5405 if (below <= 0)
John Marriottec032de2025-04-10 21:34:19 +02005406 return (int)vim_snprintf_safelen((char *)buf, buflen,
5407 "%s", (above == 0) ? _("All") : _("Bot"));
Emir SARI971cd2b2023-04-29 12:09:53 +01005408
John Marriottec032de2025-04-10 21:34:19 +02005409 if (above <= 0)
5410 return (int)vim_snprintf_safelen((char *)buf, buflen,
5411 "%s", _("Top"));
John Marriotta21240b2025-01-08 20:10:59 +01005412
John Marriottec032de2025-04-10 21:34:19 +02005413 // localized percentage value
5414 return (int)vim_snprintf_safelen((char *)buf, buflen,
5415 _("%2d%%"), calc_percentage(above, above + below));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
5418/*
John Marriottec032de2025-04-10 21:34:19 +02005419 * Append (file 2 of 8) to "buf[]", if editing more than one file.
5420 * Return the number of characters appended.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423append_arg_number(
5424 win_T *wp,
5425 char_u *buf,
John Marriottec032de2025-04-10 21:34:19 +02005426 size_t buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005427 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005429 if (ARGCOUNT <= 1) // nothing to do
John Marriottec032de2025-04-10 21:34:19 +02005430 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaara8490a42023-05-23 18:00:58 +01005432 char *msg;
5433 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005435 case 0: msg = _(" (%d of %d)"); break;
5436 case 1: msg = _(" ((%d) of %d)"); break;
5437 case 2: msg = _(" (file %d of %d)"); break;
5438 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005440
John Marriottec032de2025-04-10 21:34:19 +02005441 return (int)vim_snprintf_safelen((char *)buf, buflen, msg,
Bram Moolenaara8490a42023-05-23 18:00:58 +01005442 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443}
5444
5445/*
5446 * If fname is not a full path, make it a full path.
5447 * Returns pointer to allocated memory (NULL for failure).
5448 */
5449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005450fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 /*
5453 * Force expanding the path always for Unix, because symbolic links may
5454 * mess up the full path name, even though it starts with a '/'.
5455 * Also expand when there is ".." in the file name, try to remove it,
5456 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005457 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 * For MS-Windows also expand names like "longna~1" to "longname".
5459 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005460#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 return FullName_save(fname, TRUE);
5462#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005463 if (!vim_isAbsName(fname)
5464 || strstr((char *)fname, "..") != NULL
5465 || strstr((char *)fname, "//") != NULL
5466# ifdef BACKSLASH_IN_FILENAME
5467 || strstr((char *)fname, "\\\\") != NULL
5468# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005469# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 )
5473 return FullName_save(fname, FALSE);
5474
5475 fname = vim_strsave(fname);
5476
Bram Moolenaar9b942202007-10-03 12:31:33 +00005477# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005478 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005479 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 return fname;
5483#endif
5484}
5485
5486/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005487 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5488 * "*ffname" becomes a pointer to allocated memory (or NULL).
5489 * When resolving a link both "*sfname" and "*ffname" will point to the same
5490 * allocated memory.
5491 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005492 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005495fname_expand(
5496 buf_T *buf UNUSED,
5497 char_u **ffname,
5498 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005500 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005502 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005504 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506#ifdef FEAT_SHORTCUT
5507 if (!buf->b_p_bin)
5508 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005509 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005511 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005512 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005513 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
5515 vim_free(*ffname);
5516 *ffname = rfname;
5517 *sfname = rfname;
5518 }
5519 }
5520#endif
5521}
5522
5523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 * Open a window for a number of buffers.
5525 */
5526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 buf_T *buf;
5530 win_T *wp, *wpnext;
5531 int split_ret = OK;
5532 int p_ea_save;
5533 int open_wins = 0;
5534 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005535 int count; // Maximum number of windows to open.
5536 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005537 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005538 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaarc667da52019-11-30 20:52:27 +01005540 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 count = 9999;
5542 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005543 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5545 all = FALSE;
5546 else
5547 all = TRUE;
5548
Pavel Mayorove1121b12023-02-20 14:35:20 +00005549 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5550 // switching to another buffer.
5551 reset_VIsual_and_resel();
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 setpcmark();
5554
5555#ifdef FEAT_GUI
5556 need_mouse_correct = TRUE;
5557#endif
5558
5559 /*
5560 * Close superfluous windows (two windows for the same buffer).
5561 * Also close windows that are not full-width.
5562 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005563 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005564 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005565 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005567 tpnext = curtab->tp_next;
5568 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005570 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005571 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005572 || ((cmdmod.cmod_split & WSP_VERT)
5573 ? wp->w_height + wp->w_status_height < Rows - p_ch
5574 - tabline_height()
5575 : wp->w_width != Columns)
5576 || (had_tab > 0 && wp != firstwin))
5577 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005578 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005579 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005580 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005581 if (win_close(wp, FALSE) == FAIL)
5582 break;
5583 // Just in case an autocommand does something strange with
5584 // windows: start all over...
5585 wpnext = firstwin;
5586 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005587 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005588 }
5589 else
5590 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005592
Bram Moolenaarc667da52019-11-30 20:52:27 +01005593 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005594 if (had_tab == 0 || tpnext == NULL)
5595 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005596 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598
5599 /*
5600 * Go through the buffer list. When a buffer doesn't have a window yet,
5601 * open one. Otherwise move the window to the right position.
5602 * Watch out for autocommands that delete buffers or windows!
5603 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005604 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5609 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005610 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5612 continue;
5613
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005614 if (had_tab != 0)
5615 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005616 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005617 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005618 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005619 else
5620 wp = NULL;
5621 }
5622 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005623 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005624 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005625 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005626 if (wp->w_buffer == buf)
5627 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005628 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005629 if (wp != NULL)
5630 win_move_after(wp, curwin);
5631 }
5632
5633 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005635 bufref_T bufref;
5636
5637 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005638
Bram Moolenaarc667da52019-11-30 20:52:27 +01005639 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005641 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5643 ++open_wins;
5644 p_ea = p_ea_save;
5645 if (split_ret == FAIL)
5646 continue;
5647
Bram Moolenaarc667da52019-11-30 20:52:27 +01005648 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005651 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005653 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 break;
5656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (swap_exists_action == SEA_QUIT)
5658 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005659#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005660 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005661
Bram Moolenaarc667da52019-11-30 20:52:27 +01005662 // Reset the error/interrupt/exception state here so that
5663 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005664 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005665#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005666
Bram Moolenaarc667da52019-11-30 20:52:27 +01005667 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 win_close(curwin, TRUE);
5669 --open_wins;
5670 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005671 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005672
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005673#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005674 // Restore the error/interrupt/exception state if not
5675 // discarded by a new aborting error, interrupt, or uncaught
5676 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005677 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else
5681 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683
5684 ui_breakcheck();
5685 if (got_int)
5686 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005687 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 break;
5689 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005690#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005691 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005692 if (aborting())
5693 break;
5694#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005695 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005696 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005697 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005700 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703 /*
5704 * Close superfluous windows.
5705 */
5706 for (wp = lastwin; open_wins > count; )
5707 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005708 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 if (!win_valid(wp))
5711 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005712 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 wp = lastwin;
5714 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005715 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005717 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 --open_wins;
5719 wp = lastwin;
5720 }
5721 else
5722 {
5723 wp = wp->w_prev;
5724 if (wp == NULL)
5725 break;
5726 }
5727 }
5728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005731static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733/*
5734 * do_modelines() - process mode lines for the current file
5735 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005736 * "flags" can be:
5737 * OPT_WINONLY only set options local to window
5738 * OPT_NOWIN don't set options local to window
5739 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 * Returns immediately if the "ml" option isn't set.
5741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005743do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005745 linenr_T lnum;
5746 int nmlines;
5747 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
5749 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5750 return;
5751
Bram Moolenaarc667da52019-11-30 20:52:27 +01005752 // Disallow recursive entry here. Can happen when executing a modeline
5753 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (entered)
5755 return;
5756
5757 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005758 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005760 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 nmlines = 0;
5762
Hu Jialun9dcd3492021-08-28 20:42:50 +02005763 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005765 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 nmlines = 0;
5767 --entered;
5768}
5769
Bram Moolenaarc667da52019-11-30 20:52:27 +01005770#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772/*
5773 * chk_modeline() - check a single line for a mode string
5774 * Return FAIL if an error encountered.
5775 */
5776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005777chk_modeline(
5778 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005779 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 char_u *s;
John Marriottec032de2025-04-10 21:34:19 +02005782 char_u *line_end; // point to the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 char_u *e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 int prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 int retval = OK;
ichizok7e5fe382023-04-15 13:17:50 +01005786 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 prev = -1;
John Marriottec032de2025-04-10 21:34:19 +02005789 s = ml_get(lnum);
5790 line_end = s + ml_get_len(lnum);
5791 for (; *s != NUL; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 if (prev == -1 || vim_isspace(prev))
5794 {
5795 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5796 || STRNCMP(s, "vi:", (size_t)3) == 0)
5797 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005798 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005799 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
John Marriottec032de2025-04-10 21:34:19 +02005801 int vers;
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5804 e = s + 4;
5805 else
5806 e = s + 3;
5807 vers = getdigits(&e);
5808 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005809 && (s[0] != 'V'
5810 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005812 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 || (VIM_VERSION_100 < vers && s[3] == '<')
5814 || (VIM_VERSION_100 > vers && s[3] == '>')
5815 || (VIM_VERSION_100 == vers && s[3] == '=')))
5816 break;
5817 }
5818 }
5819 prev = *s;
5820 }
5821
5822 if (*s)
5823 {
John Marriottec032de2025-04-10 21:34:19 +02005824 size_t len;
5825 char_u *linecopy; // local copy of any modeline found
5826 int end;
5827
Bram Moolenaarc667da52019-11-30 20:52:27 +01005828 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 ++s;
5830 while (s[-1] != ':');
5831
John Marriottec032de2025-04-10 21:34:19 +02005832 len = (size_t)(line_end - s); // remember the line length
5833 // so we can restore 'line_end'
5834 // after the copy
5835 s = linecopy = vim_strnsave(s, len); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 if (linecopy == NULL)
5837 return FAIL;
5838
John Marriottec032de2025-04-10 21:34:19 +02005839 line_end = s + len; // restore 'line_end'
5840
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005841 // prepare for emsg()
5842 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005843 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
5845 end = FALSE;
5846 while (end == FALSE)
5847 {
5848 s = skipwhite(s);
5849 if (*s == NUL)
5850 break;
5851
5852 /*
5853 * Find end of set command: ':' or end of line.
5854 * Skip over "\:", replacing it with ":".
5855 */
5856 for (e = s; *e != ':' && *e != NUL; ++e)
5857 if (e[0] == '\\' && e[1] == ':')
John Marriottec032de2025-04-10 21:34:19 +02005858 {
5859 mch_memmove(e, e + 1, (size_t)(line_end - (e + 1)) + 1); // +1 for NUL
5860 --line_end;
5861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (*e == NUL)
5863 end = TRUE;
5864
5865 /*
5866 * If there is a "set" command, require a terminating ':' and
5867 * ignore the stuff after the ':'.
5868 * "vi:set opt opt opt: foo" -- foo not interpreted
5869 * "vi:opt opt opt: foo" -- foo interpreted
5870 * Accept "se" for compatibility with Elvis.
5871 */
5872 if (STRNCMP(s, "set ", (size_t)4) == 0
5873 || STRNCMP(s, "se ", (size_t)3) == 0)
5874 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005875 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 end = TRUE;
John Marriottec032de2025-04-10 21:34:19 +02005878 s += (*(s + 2) == ' ') ? 3 : 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005880 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaarc667da52019-11-30 20:52:27 +01005882 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005884 int secure_save = secure;
John Marriottec032de2025-04-10 21:34:19 +02005885 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005886
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005887 current_sctx.sc_version = 1;
5888#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005889 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005890 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005891 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005893
Bram Moolenaar5958f952018-11-20 04:25:21 +01005894 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005895 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005896
Bram Moolenaara3227e22006-03-08 21:32:40 +00005897 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005898
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005899 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005900 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005901 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 break;
5903 }
John Marriottec032de2025-04-10 21:34:19 +02005904 s = (e == line_end) ? e : e + 1; // advance to next part
5905 // careful not to go off the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907
ichizok7e5fe382023-04-15 13:17:50 +01005908 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005909 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 vim_free(linecopy);
5911 }
5912 return retval;
5913}
5914
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005915/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005916 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5917 */
5918 int
5919bt_normal(buf_T *buf)
5920{
5921 return buf != NULL && buf->b_p_bt[0] == NUL;
5922}
5923
5924/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005925 * Return TRUE if "buf" is the quickfix buffer.
5926 */
5927 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005928bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005929{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005930#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005931 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005932#else
5933 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005934#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005935}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005936
5937/*
5938 * Return TRUE if "buf" is a terminal buffer.
5939 */
5940 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005941bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005942{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005943#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005944 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005945#else
5946 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005947#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005948}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005949
5950/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005951 * Return TRUE if "buf" is a help buffer.
5952 */
5953 int
5954bt_help(buf_T *buf)
5955{
5956 return buf != NULL && buf->b_help;
5957}
5958
5959/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005960 * Return TRUE if "buf" is a prompt buffer.
5961 */
5962 int
5963bt_prompt(buf_T *buf)
5964{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005965 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5966}
5967
Dominique Pelle748b3082022-01-08 12:41:16 +00005968#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005969/*
5970 * Return TRUE if "buf" is a buffer for a popup window.
5971 */
5972 int
5973bt_popup(buf_T *buf)
5974{
5975 return buf != NULL && buf->b_p_bt != NULL
5976 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005977}
Dominique Pelle748b3082022-01-08 12:41:16 +00005978#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005979
5980/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005981 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005982 * buffer. This means the buffer name may not be a file name, at least not for
5983 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005984 */
5985 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005986bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005987{
5988 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5989 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005990 || buf->b_p_bt[0] == 't'
5991 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005992}
5993
Bram Moolenaarc3126192022-08-26 12:58:17 +01005994/*
5995 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5996 * buffer. This means the buffer is not to be read from a file.
5997 */
5998 static int
5999bt_nofileread(buf_T *buf)
6000{
6001 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
6002 || buf->b_p_bt[0] == 't'
6003 || buf->b_p_bt[0] == 'q'
6004 || buf->b_p_bt[0] == 'p');
6005}
6006
Dominique Pelle748b3082022-01-08 12:41:16 +00006007#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006008/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02006009 * Return TRUE if "buf" has 'buftype' set to "nofile".
6010 */
6011 int
6012bt_nofile(buf_T *buf)
6013{
6014 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
6015}
Dominique Pelle748b3082022-01-08 12:41:16 +00006016#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02006017
6018/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006019 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
6020 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006021 */
6022 int
6023bt_dontwrite(buf_T *buf)
6024{
Bram Moolenaarf2732452018-06-03 14:47:35 +02006025 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01006026 || buf->b_p_bt[0] == 't'
6027 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006028}
6029
6030 int
6031bt_dontwrite_msg(buf_T *buf)
6032{
6033 if (bt_dontwrite(buf))
6034 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00006035 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006036 return TRUE;
6037 }
6038 return FALSE;
6039}
6040
6041/*
6042 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
6043 * and 'bufhidden'.
6044 */
6045 int
6046buf_hide(buf_T *buf)
6047{
Bram Moolenaarc667da52019-11-30 20:52:27 +01006048 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006049 switch (buf->b_p_bh[0])
6050 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006051 case 'u': // "unload"
6052 case 'w': // "wipe"
6053 case 'd': return FALSE; // "delete"
6054 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006055 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006056 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006057}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059/*
6060 * Return special buffer name.
6061 * Returns NULL when the buffer has a normal file name.
6062 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006063 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
Bram Moolenaar4033c552017-09-16 20:54:51 +02006066#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006068 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006069 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006070 * Differentiate between the quickfix and location list buffers using
6071 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006072 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006073 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006074 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006075 else
6076 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02006079
Bram Moolenaarc667da52019-11-30 20:52:27 +01006080 // There is no _file_ when 'buftype' is "nofile", b_sfname
6081 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02006082 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaar21554412017-07-24 21:44:43 +02006084#ifdef FEAT_TERMINAL
6085 if (buf->b_term != NULL)
6086 return term_get_status_text(buf->b_term);
6087#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02006088 if (buf->b_fname != NULL)
6089 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01006090 if (buf == cmdwin_buf)
6091 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02006092#ifdef FEAT_JOB_CHANNEL
6093 if (bt_prompt(buf))
6094 return (char_u *)_("[Prompt]");
6095#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006096#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006097 if (bt_popup(buf))
6098 return (char_u *)_("[Popup]");
6099#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006100 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006104 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 return NULL;
6106}
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006109 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6110 */
6111 char_u *
6112buf_get_fname(buf_T *buf)
6113{
6114 if (buf->b_fname == NULL)
6115 return (char_u *)_("[No Name]");
6116 return buf->b_fname;
6117}
6118
6119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6121 */
6122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006123set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006125 if (on == curbuf->b_p_bl)
6126 return;
6127
6128 curbuf->b_p_bl = on;
6129 if (on)
6130 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6131 else
6132 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133}
6134
6135/*
6136 * Read the file for "buf" again and check if the contents changed.
6137 * Return TRUE if it changed or this could not be checked.
6138 */
6139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 buf_T *newbuf;
6143 int differ = TRUE;
6144 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 exarg_T ea;
6147
Bram Moolenaarc667da52019-11-30 20:52:27 +01006148 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6150 if (newbuf == NULL)
6151 return TRUE;
6152
Bram Moolenaarc667da52019-11-30 20:52:27 +01006153 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (prep_exarg(&ea, buf) == FAIL)
6155 {
6156 wipe_buffer(newbuf, FALSE);
6157 return TRUE;
6158 }
6159
Bram Moolenaare76062c2022-11-28 18:51:43 +00006160 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006162 if (curbuf != newbuf)
6163 {
6164 // Failed to find a window for "newbuf".
6165 wipe_buffer(newbuf, FALSE);
6166 return TRUE;
6167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006169 // We don't want to trigger autocommands now, they may have nasty
6170 // side-effects like wiping buffers
6171 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006172 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 && readfile(buf->b_ffname, buf->b_fname,
6174 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6175 &ea, READ_NEW | READ_DUMMY) == OK)
6176 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006177 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6179 {
6180 differ = FALSE;
6181 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6182 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6183 {
6184 differ = TRUE;
6185 break;
6186 }
6187 }
6188 }
6189 vim_free(ea.cmd);
6190
Bram Moolenaarc667da52019-11-30 20:52:27 +01006191 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
Bram Moolenaarc667da52019-11-30 20:52:27 +01006194 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 wipe_buffer(newbuf, FALSE);
6196
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006197 unblock_autocmds();
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 return differ;
6200}
6201
6202/*
6203 * Wipe out a buffer and decrement the last buffer number if it was used for
6204 * this buffer. Call this to wipe out a temp buffer that does not contain any
6205 * marks.
6206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006208wipe_buffer(
6209 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006210 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 if (buf->b_fnum == top_file_num - 1)
6213 --top_file_num;
6214
Bram Moolenaarc667da52019-11-30 20:52:27 +01006215 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006216 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006217
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006218 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006219
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006221 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222}