blob: b4481b2f1373be0d41e79b5b871ee9cee20a7f63 [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.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200912 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
913 {
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 {
2223 buf = curbuf;
Christian Brabandtbaa8c902025-04-19 11:14:11 +02002224 trigger_undo_ftplugin(buf, curwin);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002225 // It's like this buffer is deleted. Watch out for autocommands that
2226 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002227 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2228 if (buf != curbuf) // autocommands deleted the buffer!
2229 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002230#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002231 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002232 {
2233 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238 if (buf != curbuf || curbuf == NULL)
2239 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002240 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (buf == NULL)
2242 {
2243 vim_free(ffname);
2244 return NULL;
2245 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002246#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002247 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002248 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002249 if (buf->b_vars == NULL)
2250 {
2251 vim_free(ffname);
2252 vim_free(buf);
2253 return NULL;
2254 }
2255 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2256#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002257 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259
2260 if (ffname != NULL)
2261 {
2262 buf->b_ffname = ffname;
2263 buf->b_sfname = vim_strsave(sfname);
2264 }
2265
2266 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002267 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
2269 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2270 || buf->b_wininfo == NULL)
2271 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002272 if (buf->b_sfname != buf->b_ffname)
2273 VIM_CLEAR(buf->b_sfname);
2274 else
2275 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002276 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (buf != curbuf)
2278 free_buffer(buf);
2279 return NULL;
2280 }
2281
2282 if (buf == curbuf)
2283 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002284 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002285
Bram Moolenaarc667da52019-11-30 20:52:27 +01002286 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002287 buf->b_p_initialized = FALSE;
2288 buf_copy_options(buf, BCO_ENTER);
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002291 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 curbuf->b_kmap_state |= KEYMAP_INIT;
2293#endif
2294 }
2295 else
2296 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002297 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002299 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 buf->b_prev = NULL;
2302 firstbuf = buf;
2303 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002304 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 lastbuf->b_next = buf;
2307 buf->b_prev = lastbuf;
2308 }
2309 lastbuf = buf;
2310
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002311 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2312 {
2313 // Recycle a previously used buffer number. Used for buffers which
2314 // are normally hidden, e.g. in a popup window. Avoids that the
2315 // buffer number grows rapidly.
2316 --buf_reuse.ga_len;
2317 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002318
2319 // Move buffer to the right place in the buffer list.
2320 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2321 {
2322 buf_T *prev = buf->b_prev;
2323
2324 prev->b_next = buf->b_next;
2325 if (prev->b_next != NULL)
2326 prev->b_next->b_prev = prev;
2327 buf->b_next = prev;
2328 buf->b_prev = prev->b_prev;
2329 if (buf->b_prev != NULL)
2330 buf->b_prev->b_next = buf;
2331 prev->b_prev = buf;
2332 if (lastbuf == buf)
2333 lastbuf = prev;
2334 if (firstbuf == prev)
2335 firstbuf = buf;
2336 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002337 }
2338 else
2339 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002340 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002342 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002343 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002346 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348 top_file_num = 1;
2349 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002350 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002352 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 buf_copy_options(buf, BCO_ALWAYS);
2354 }
2355
2356 buf->b_wininfo->wi_fpos.lnum = lnum;
2357 buf->b_wininfo->wi_win = curwin;
2358
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002359#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002360 hash_init(&buf->b_s.b_keywtab);
2361 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#endif
2363
2364 buf->b_fname = buf->b_sfname;
2365#ifdef UNIX
2366 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002367 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 else
2369 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002370 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 buf->b_dev = st.st_dev;
2372 buf->b_ino = st.st_ino;
2373 }
2374#endif
2375 buf->b_u_synced = TRUE;
2376 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002377 if (flags & BLN_DUMMY)
2378 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002380 clrallmarks(buf); // clear marks
2381 fmarks_check_names(buf); // check file marks for this file
2382 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (!(flags & BLN_DUMMY))
2384 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002385 bufref_T bufref;
2386
Bram Moolenaarc667da52019-11-30 20:52:27 +01002387 // Tricky: these autocommands may change the buffer list. They could
2388 // also split the window with re-using the one empty buffer. This may
2389 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002390 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002391 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002392 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002393 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002395 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002396 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002397 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002398 return NULL;
2399 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002400#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002401 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 return buf;
2407}
2408
2409/*
2410 * Free the memory for the options of a buffer.
2411 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2412 * 'fileencoding'.
2413 */
2414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002415free_buf_options(
2416 buf_T *buf,
2417 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 if (free_p_ff)
2420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 clear_string_option(&buf->b_p_bh);
2424 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426#ifdef FEAT_FIND_ID
2427 clear_string_option(&buf->b_p_def);
2428 clear_string_option(&buf->b_p_inc);
2429# ifdef FEAT_EVAL
2430 clear_string_option(&buf->b_p_inex);
2431# endif
2432#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002433#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 clear_string_option(&buf->b_p_inde);
2435 clear_string_option(&buf->b_p_indk);
2436#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002437#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2438 clear_string_option(&buf->b_p_bexpr);
2439#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002440#if defined(FEAT_CRYPT)
2441 clear_string_option(&buf->b_p_cm);
2442#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002443 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002444#if defined(FEAT_EVAL)
2445 clear_string_option(&buf->b_p_fex);
2446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002448# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002449 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2450 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002451 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 clear_string_option(&buf->b_p_key);
2454#endif
2455 clear_string_option(&buf->b_p_kp);
2456 clear_string_option(&buf->b_p_mps);
2457 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002458 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002460#ifdef FEAT_VARTABS
2461 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002462 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002463 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002464 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002465 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifdef FEAT_KEYMAP
2468 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002469 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 ga_clear(&buf->b_kmap_ga);
2471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473#ifdef FEAT_FOLDING
2474 clear_string_option(&buf->b_p_cms);
2475#endif
2476 clear_string_option(&buf->b_p_nf);
2477#ifdef FEAT_SYN_HL
2478 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002479 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002480#endif
2481#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002482 clear_string_option(&buf->b_s.b_p_spc);
2483 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002484 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002485 buf->b_s.b_cap_prog = NULL;
2486 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002487 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 clear_string_option(&buf->b_p_cink);
2492 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002493 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002494 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002496 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 clear_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +02002498 clear_string_option(&buf->b_p_ise);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002499#ifdef FEAT_COMPL_FUNC
2500 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002501 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002502 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002503 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002504 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002505 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507#ifdef FEAT_QUICKFIX
2508 clear_string_option(&buf->b_p_gp);
2509 clear_string_option(&buf->b_p_mp);
2510 clear_string_option(&buf->b_p_efm);
2511#endif
2512 clear_string_option(&buf->b_p_ep);
2513 clear_string_option(&buf->b_p_path);
2514 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002515 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002516#ifdef FEAT_EVAL
2517 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002518 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002519 clear_string_option(&buf->b_p_ffu);
2520 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 clear_string_option(&buf->b_p_dict);
2523 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002524 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002526 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002527 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002528 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002529 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530}
2531
2532/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002533 * Get alternate file "n".
2534 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2535 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 * if (options & GETF_SETMARK) call setpcmark()
2537 * if (options & GETF_ALT) we are jumping to an alternate file.
2538 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2539 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002540 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
2542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543buflist_getfile(
2544 int n,
2545 linenr_T lnum,
2546 int options,
2547 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
2549 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 pos_T *fpos;
2552 colnr_T col;
2553
2554 buf = buflist_findnr(n);
2555 if (buf == NULL)
2556 {
2557 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002558 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002560 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return FAIL;
2562 }
2563
Bram Moolenaarc667da52019-11-30 20:52:27 +01002564 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (buf == curbuf)
2566 return OK;
2567
Bram Moolenaar71223e22022-05-30 15:23:09 +01002568 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaarc667da52019-11-30 20:52:27 +01002571 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (lnum == 0)
2573 {
2574 fpos = buflist_findfpos(buf);
2575 lnum = fpos->lnum;
2576 col = fpos->col;
2577 }
2578 else
2579 col = 0;
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (options & GETF_SWITCH)
2582 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002583 // If 'switchbuf' is set jump to the window containing "buf".
2584 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002585
Bram Moolenaarc667da52019-11-30 20:52:27 +01002586 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2587 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002588 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002589 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002591 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002592 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002593 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2594 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002596 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002601 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002602 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2603 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002605 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (!p_sol && col != 0)
2607 {
2608 curwin->w_cursor.col = col;
2609 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 curwin->w_set_curswant = TRUE;
2612 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002613 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002615
2616 if (RedrawingDisabled > 0)
2617 --RedrawingDisabled;
2618 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619}
2620
2621/*
2622 * go to the last know line number for the current buffer
2623 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002625buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 pos_T *fpos;
2628
2629 fpos = buflist_findfpos(curbuf);
2630
2631 curwin->w_cursor.lnum = fpos->lnum;
2632 check_cursor_lnum();
2633
2634 if (p_sol)
2635 curwin->w_cursor.col = 0;
2636 else
2637 {
2638 curwin->w_cursor.col = fpos->col;
2639 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 curwin->w_set_curswant = TRUE;
2642 }
2643}
2644
Bram Moolenaar81695252004-12-29 20:58:21 +00002645/*
2646 * Find file in buffer list by name (it has to be for the current window).
2647 * Returns NULL if not found.
2648 */
2649 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002650buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002651{
2652 char_u *ffname;
2653 buf_T *buf = NULL;
2654
Bram Moolenaarc667da52019-11-30 20:52:27 +01002655 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002656 ffname = FullName_save(fname,
2657#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002658 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002659#else
2660 FALSE
2661#endif
2662 );
2663 if (ffname != NULL)
2664 {
2665 buf = buflist_findname(ffname);
2666 vim_free(ffname);
2667 }
2668 return buf;
2669}
Bram Moolenaar81695252004-12-29 20:58:21 +00002670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671/*
2672 * Find file in buffer list by name (it has to be for the current window).
2673 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002674 * Skips dummy buffers.
2675 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
2677 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002681 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 if (mch_stat((char *)ffname, &st) < 0)
2684 st.st_dev = (dev_T)-1;
2685 return buflist_findname_stat(ffname, &st);
2686}
2687
2688/*
2689 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2690 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002691 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
2693 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002694buflist_findname_stat(
2695 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002696 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698#endif
2699 buf_T *buf;
2700
Bram Moolenaarc667da52019-11-30 20:52:27 +01002701 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002702 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002703 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef UNIX
2705 , stp
2706#endif
2707 ))
2708 return buf;
2709 return NULL;
2710}
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712/*
2713 * Find file in buffer list by a regexp pattern.
2714 * Return fnum of the found buffer.
2715 * Return < 0 for error.
2716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002718buflist_findpat(
2719 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002720 char_u *pattern_end, // pointer to first char after pattern
2721 int unlisted, // find unlisted buffers
2722 int diffmode UNUSED, // find diff-mode buffers only
2723 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 int match = -1;
2727 int find_listed;
2728 char_u *pat;
2729 char_u *patend;
2730 int attempt;
2731 char_u *p;
2732 int toggledollar;
2733
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002734 // "%" is current file, "%%" or "#" is alternate file
2735 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2736 || (in_vim9script() && pattern_end == pattern + 2
2737 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002739 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002741 else
2742 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#ifdef FEAT_DIFF
2744 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2745 match = -1;
2746#endif
2747 }
2748
2749 /*
2750 * Try four ways of matching a listed buffer:
2751 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002752 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 * attempt == 2: with '$' at end (only match at end)
2754 * attempt == 3: with '^' at start and '$' at end (only full match)
2755 * Repeat this for finding an unlisted buffer if there was no matching
2756 * listed buffer.
2757 */
2758 else
2759 {
2760 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2761 if (pat == NULL)
2762 return -1;
2763 patend = pat + STRLEN(pat) - 1;
2764 toggledollar = (patend > pat && *patend == '$');
2765
Bram Moolenaarc667da52019-11-30 20:52:27 +01002766 // First try finding a listed buffer. If not found and "unlisted"
2767 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 find_listed = TRUE;
2769 for (;;)
2770 {
2771 for (attempt = 0; attempt <= 3; ++attempt)
2772 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002773 regmatch_T regmatch;
2774
Bram Moolenaarc667da52019-11-30 20:52:27 +01002775 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002777 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002779 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002781 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002783 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002784 {
2785 if (regmatch.regprog == NULL)
2786 {
2787 // invalid pattern, possibly after switching engine
2788 vim_free(pat);
2789 return -1;
2790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (buf->b_p_bl == find_listed
2792#ifdef FEAT_DIFF
2793 && (!diffmode || diff_mode_buf(buf))
2794#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002795 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002797 if (curtab_only)
2798 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002799 // Ignore the match if the buffer is not open in
2800 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002801 win_T *wp;
2802
Bram Moolenaar29323592016-07-24 22:04:11 +02002803 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002804 if (wp->w_buffer == buf)
2805 break;
2806 if (wp == NULL)
2807 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002808 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002809 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
2811 match = -2;
2812 break;
2813 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002814 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002818 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002819 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 break;
2821 }
2822
Bram Moolenaarc667da52019-11-30 20:52:27 +01002823 // Only search for unlisted buffers if there was no match with
2824 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (!unlisted || !find_listed || match != -1)
2826 break;
2827 find_listed = FALSE;
2828 }
2829
2830 vim_free(pat);
2831 }
2832
2833 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002834 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002836 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 return match;
2838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
Bram Moolenaar52410572019-10-27 05:12:45 +01002840#ifdef FEAT_VIMINFO
2841typedef struct {
2842 buf_T *buf;
2843 char_u *match;
2844} bufmatch_T;
2845#endif
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847/*
2848 * Find all buffer names that match.
2849 * For command line expansion of ":buf" and ":sbuf".
2850 * Return OK if matches found, FAIL otherwise.
2851 */
2852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002853ExpandBufnames(
2854 char_u *pat,
2855 int *num_file,
2856 char_u ***file,
2857 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002859 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 buf_T *buf;
2861 int round;
2862 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002863 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002864#ifdef FEAT_VIMINFO
2865 bufmatch_T *matches = NULL;
2866#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002867 int fuzzy;
2868 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002869 regmatch_T regmatch;
2870 int score = 0;
2871 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaarc667da52019-11-30 20:52:27 +01002873 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 *file = NULL;
2875
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002876#ifdef FEAT_DIFF
2877 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2878 return FAIL;
2879#endif
2880
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002881 fuzzy = cmdline_fuzzy_complete(pat);
2882
2883 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2884 // expression matching)
2885 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002887 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002888 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002889 int len = (int)STRLEN(pat);
2890 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002891 if (patc == NULL)
2892 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002893 STRNCPY(patc, pat + 1, len - 1);
2894 patc[len - 1] = NUL;
2895 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002896 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002897 else if (*pat == '^')
2898 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002899 else
2900 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002901 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002902 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002903
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002904 // round == 1: Count the matches.
2905 // round == 2: Build the array to keep the matches.
2906 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002907 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002908 count = 0;
2909 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002910 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002911 if (!buf->b_p_bl) // skip unlisted buffers
2912 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002913#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002914 if (options & BUF_DIFF_FILTER)
2915 // Skip buffers not suitable for
2916 // :diffget or :diffput completion.
2917 if (buf == curbuf || !diff_mode_buf(buf))
2918 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002919#endif
2920
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002921 if (!fuzzy)
2922 {
2923 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002924 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002925 // invalid pattern, possibly after recompiling
2926 if (to_free)
2927 vim_free(patc);
2928 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002929 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002930 p = buflist_match(&regmatch, buf, p_wic);
2931 }
2932 else
2933 {
2934 p = NULL;
2935 // first try matching with the short file name
2936 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2937 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002938 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002939 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002940 // next try matching with the full path file name
2941 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2942 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002945
2946 if (p == NULL)
2947 continue;
2948
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (round == 1)
2950 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002951 ++count;
2952 continue;
2953 }
2954
2955 if (options & WILD_HOME_REPLACE)
2956 p = home_replace_save(buf, p);
2957 else
2958 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002959 if (p == NULL)
2960 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002961
2962 if (!fuzzy)
2963 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002964#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002965 if (matches != NULL)
2966 {
2967 matches[count].buf = buf;
2968 matches[count].match = p;
2969 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002970 }
2971 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002972#endif
2973 (*file)[count++] = p;
2974 }
2975 else
2976 {
2977 fuzmatch[count].idx = count;
2978 fuzmatch[count].str = p;
2979 fuzmatch[count].score = score;
2980 count++;
2981 }
2982 }
2983 if (count == 0) // no match found, break here
2984 break;
2985 if (round == 1)
2986 {
2987 if (!fuzzy)
2988 {
2989 *file = ALLOC_MULT(char_u *, count);
2990 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002991 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002992 vim_regfree(regmatch.regprog);
2993 if (to_free)
2994 vim_free(patc);
2995 return FAIL;
2996 }
2997#ifdef FEAT_VIMINFO
2998 if (options & WILD_BUFLASTUSED)
2999 matches = ALLOC_MULT(bufmatch_T, count);
3000#endif
3001 }
3002 else
3003 {
3004 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
3005 if (fuzmatch == NULL)
3006 {
3007 *num_file = 0;
3008 *file = NULL;
3009 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01003015 if (!fuzzy)
3016 {
3017 vim_regfree(regmatch.regprog);
3018 if (to_free)
3019 vim_free(patc);
3020 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003021
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003022 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01003023 {
826814741_6dff3c9c2024-12-10 17:15:14 +01003024#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003025 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01003026 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003027 int i;
3028 if (count > 1)
3029 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3030 // if the current buffer is first in the list, place it at the end
3031 if (matches[0].buf == curbuf)
3032 {
3033 for (i = 1; i < count; i++)
3034 (*file)[i-1] = matches[i].match;
3035 (*file)[count-1] = matches[0].match;
3036 }
3037 else
3038 {
3039 for (i = 0; i < count; i++)
3040 (*file)[i] = matches[i].match;
3041 }
3042 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003043 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003044#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003045 }
3046 else
3047 {
3048 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3049 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003050 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 *num_file = count;
3053 return (count == 0 ? FAIL : OK);
3054}
3055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056/*
3057 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003058 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
3060 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003061buflist_match(
3062 regmatch_T *rmp,
3063 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003064 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 char_u *match;
3067
Bram Moolenaarc667da52019-11-30 20:52:27 +01003068 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003069 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003070 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003071 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073 return match;
3074}
3075
3076/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003077 * Try matching the regexp in "rmp->regprog" with file name "name".
3078 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 * Return "name" when there is a match, NULL when not.
3080 */
3081 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082fname_match(
3083 regmatch_T *rmp,
3084 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003085 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 char_u *match = NULL;
3088 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003090 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003091 if (name == NULL || rmp->regprog == NULL)
3092 return NULL;
3093
3094 // Ignore case when 'fileignorecase' or the argument is set.
3095 rmp->rm_ic = p_fic || ignore_case;
3096 if (vim_regexec(rmp, name, (colnr_T)0))
3097 match = name;
3098 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003100 // Replace $(HOME) with '~' and try matching again.
3101 p = home_replace_save(NULL, name);
3102 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003104 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106
3107 return match;
3108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003111 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 */
3113 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003114buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003116 char_u key[VIM_SIZEOF_INT * 2 + 1];
3117 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 if (nr == 0)
3120 nr = curwin->w_alt_fnum;
John Marriottec032de2025-04-10 21:34:19 +02003121 vim_snprintf((char *)key, sizeof(key), "%x", nr);
Bram Moolenaar480778b2016-07-14 22:09:39 +02003122 hi = hash_find(&buf_hashtab, key);
3123
3124 if (!HASHITEM_EMPTY(hi))
3125 return (buf_T *)(hi->hi_key
3126 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return NULL;
3128}
3129
3130/*
3131 * Get name of file 'n' in the buffer list.
3132 * When the file has no name an empty string is returned.
3133 * home_replace() is used to shorten the file name (used for marks).
3134 * Returns a pointer to allocated memory, of NULL when failed.
3135 */
3136 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003137buflist_nr2name(
3138 int n,
3139 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003140 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 buf_T *buf;
3143
3144 buf = buflist_findnr(n);
3145 if (buf == NULL)
3146 return NULL;
3147 return home_replace_save(helptail ? buf : NULL,
3148 fullname ? buf->b_ffname : buf->b_fname);
3149}
3150
3151/*
3152 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3153 * When "copy_options" is TRUE save the local window option values.
3154 * When "lnum" is 0 only do the options.
3155 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003157buflist_setfpos(
3158 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003159 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003160 linenr_T lnum,
3161 colnr_T col,
3162 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 wininfo_T *wip;
3165
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003166 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (wip->wi_win == win)
3168 break;
3169 if (wip == NULL)
3170 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003171 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003172 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (wip == NULL)
3174 return;
3175 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003176 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 lnum = 1;
3178 }
3179 else
3180 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003181 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (wip->wi_prev)
3183 wip->wi_prev->wi_next = wip->wi_next;
3184 else
3185 buf->b_wininfo = wip->wi_next;
3186 if (wip->wi_next)
3187 wip->wi_next->wi_prev = wip->wi_prev;
3188 if (copy_options && wip->wi_optset)
3189 {
3190 clear_winopt(&wip->wi_opt);
3191#ifdef FEAT_FOLDING
3192 deleteFoldRecurse(&wip->wi_folds);
3193#endif
3194 }
3195 }
3196 if (lnum != 0)
3197 {
3198 wip->wi_fpos.lnum = lnum;
3199 wip->wi_fpos.col = col;
3200 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003201 if (win != NULL)
3202 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003203 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003205 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3207#ifdef FEAT_FOLDING
3208 wip->wi_fold_manual = win->w_fold_manual;
3209 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3210#endif
3211 wip->wi_optset = TRUE;
3212 }
3213
Bram Moolenaarc667da52019-11-30 20:52:27 +01003214 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 wip->wi_next = buf->b_wininfo;
3216 buf->b_wininfo = wip;
3217 wip->wi_prev = NULL;
3218 if (wip->wi_next)
3219 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003222#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003223/*
3224 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3225 * page. That's because a diff is local to a tab page.
3226 */
3227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003228wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003229{
3230 win_T *wp;
3231
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003232 if (!wip->wi_opt.wo_diff)
3233 return FALSE;
3234
3235 FOR_ALL_WINDOWS(wp)
3236 // return FALSE when it's a window in the current tab page, thus
3237 // the buffer was in diff mode here
3238 if (wip->wi_win == wp)
3239 return FALSE;
3240 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003241}
3242#endif
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244/*
3245 * Find info for the current window in buffer "buf".
3246 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003247 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003248 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3249 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * Returns NULL when there isn't any info.
3251 */
3252 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253find_wininfo(
3254 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003255 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003256 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 wininfo_T *wip;
3259
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003260 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003261 if (wip->wi_win == curwin
3262#ifdef FEAT_DIFF
3263 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3264#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003265
3266 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003268
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003269 if (wip != NULL)
3270 return wip;
3271
Bram Moolenaarc667da52019-11-30 20:52:27 +01003272 // If no wininfo for curwin, use the first in the list (that doesn't have
3273 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003274 // If "need_options" is TRUE skip entries that don't have options set,
3275 // unless the window is editing "buf", so we can copy from the window
3276 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003277#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003278 if (skip_diff_buffer)
3279 {
3280 FOR_ALL_BUF_WININFO(buf, wip)
3281 if (!wininfo_other_tab_diff(wip)
3282 && (!need_options || wip->wi_optset
3283 || (wip->wi_win != NULL
3284 && wip->wi_win->w_buffer == buf)))
3285 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003286 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003287 else
3288#endif
3289 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 return wip;
3291}
3292
3293/*
3294 * Reset the local window options to the values last used in this window.
3295 * If the buffer wasn't used in this window before, use the values from
3296 * the most recently used window. If the values were never set, use the
3297 * global values for the window.
3298 */
3299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003300get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 wininfo_T *wip;
3303
3304 clear_winopt(&curwin->w_onebuf_opt);
3305#ifdef FEAT_FOLDING
3306 clearFolding(curwin);
3307#endif
3308
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003309 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003310 if (wip != NULL && wip->wi_win != NULL
3311 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003313 // The buffer is currently displayed in the window: use the actual
3314 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003315 win_T *wp = wip->wi_win;
3316
3317 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3318#ifdef FEAT_FOLDING
3319 curwin->w_fold_manual = wp->w_fold_manual;
3320 curwin->w_foldinvalid = TRUE;
3321 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3322#endif
3323 }
3324 else if (wip != NULL && wip->wi_optset)
3325 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003326 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3328#ifdef FEAT_FOLDING
3329 curwin->w_fold_manual = wip->wi_fold_manual;
3330 curwin->w_foldinvalid = TRUE;
3331 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3332#endif
3333 }
3334 else
3335 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003336 if (wip != NULL)
3337 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003340 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 if (p_fdls >= 0)
3342 curwin->w_p_fdl = p_fdls;
3343#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003344 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
3348 * Find the position (lnum and col) for the buffer 'buf' for the current
3349 * window.
3350 * Returns a pointer to no_position if no position is found.
3351 */
3352 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003356 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003358 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (wip != NULL)
3360 return &(wip->wi_fpos);
3361 else
3362 return &no_position;
3363}
3364
3365/*
3366 * Find the lnum for the buffer 'buf' for the current window.
3367 */
3368 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003369buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 return buflist_findfpos(buf)->lnum;
3372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003375 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
Bram Moolenaar52410572019-10-27 05:12:45 +01003380 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 int len;
3382 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003383 int ro_char;
3384 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003385#ifdef FEAT_TERMINAL
3386 int job_running;
3387 int job_none_open;
3388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar52410572019-10-27 05:12:45 +01003390#ifdef FEAT_VIMINFO
3391 garray_T buflist;
3392 buf_T **buflist_data = NULL, **p;
3393
3394 if (vim_strchr(eap->arg, 't'))
3395 {
3396 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003397 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003398 {
3399 if (ga_grow(&buflist, 1) == OK)
3400 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3401 }
3402
3403 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3404 sizeof(buf_T *), buf_compare);
3405
Bram Moolenaar3b991522019-11-06 23:26:20 +01003406 buflist_data = (buf_T **)buflist.ga_data;
3407 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003408 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003409 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003410
Bram Moolenaar3b991522019-11-06 23:26:20 +01003411 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003412 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3413 : buf->b_next)
3414#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
John Marriottec032de2025-04-10 21:34:19 +02003418 char_u *name;
3419
Bram Moolenaar0751f512018-03-29 16:37:16 +02003420#ifdef FEAT_TERMINAL
3421 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003422 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003423#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003424 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003425 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3426 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3427 || (vim_strchr(eap->arg, '+')
3428 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3429 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003430 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003431 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003432 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3433#ifdef FEAT_TERMINAL
3434 || (vim_strchr(eap->arg, 'R')
3435 && (!job_running || (job_running && job_none_open)))
3436 || (vim_strchr(eap->arg, '?')
3437 && (!job_running || (job_running && !job_none_open)))
3438 || (vim_strchr(eap->arg, 'F')
3439 && (job_running || buf->b_term == NULL))
3440#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003441 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3442 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3443 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3444 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3445 || (vim_strchr(eap->arg, '#')
3446 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 continue;
John Marriottec032de2025-04-10 21:34:19 +02003448 name = buf_spname(buf);
3449 if (name != NULL)
3450 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else
3452 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003453 if (message_filtered(NameBuff))
3454 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003456 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3457 : (bufIsChanged(buf) ? '+' : ' ');
3458#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003459 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003460 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003461 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003462 ro_char = '?';
3463 else
3464 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003465 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3466 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003467 }
3468 else if (buf->b_term != NULL)
3469 ro_char = 'F';
3470 else
3471#endif
3472 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3473
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003474 msg_putchar('\n');
John Marriottec032de2025-04-10 21:34:19 +02003475 len = (int)vim_snprintf_safelen((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 buf->b_fnum,
3477 buf->b_p_bl ? ' ' : 'u',
3478 buf == curbuf ? '%' :
3479 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3480 buf->b_ml.ml_mfp == NULL ? ' ' :
3481 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003482 ro_char,
3483 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003484 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
Bram Moolenaarc667da52019-11-30 20:52:27 +01003486 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 i = 40 - vim_strsize(IObuff);
3488 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003490 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003491#ifdef FEAT_VIMINFO
3492 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3493 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3494 else
3495#endif
3496 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3497 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003498 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003500 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 ui_breakcheck();
3502 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003503
3504#ifdef FEAT_VIMINFO
3505 if (buflist_data)
3506 ga_clear(&buflist);
3507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510/*
3511 * Get file name and line number for file 'fnum'.
3512 * Used by DoOneCmd() for translating '%' and '#'.
3513 * Used by insert_reg() and cmdline_paste() for '#' register.
3514 * Return FAIL if not found, OK for success.
3515 */
3516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003517buflist_name_nr(
3518 int fnum,
3519 char_u **fname,
3520 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 buf_T *buf;
3523
3524 buf = buflist_findnr(fnum);
3525 if (buf == NULL || buf->b_fname == NULL)
3526 return FAIL;
3527
3528 *fname = buf->b_fname;
3529 *lnum = buflist_findlnum(buf);
3530
3531 return OK;
3532}
3533
3534/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003535 * Set the file name for "buf"' to "ffname_arg", short file name to
3536 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 * The file name with the full path is also remembered, for when :cd is used.
3538 * Returns FAIL for failure (file name already in use by other buffer)
3539 * OK otherwise.
3540 */
3541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003542setfname(
3543 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003544 char_u *ffname_arg,
3545 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003546 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003548 char_u *ffname = ffname_arg;
3549 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003550 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003552 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#endif
3554
3555 if (ffname == NULL || *ffname == NUL)
3556 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003557 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003558 if (buf->b_sfname != buf->b_ffname)
3559 VIM_CLEAR(buf->b_sfname);
3560 else
3561 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003562 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#ifdef UNIX
3564 st.st_dev = (dev_T)-1;
3565#endif
3566 }
3567 else
3568 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003569 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3570 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 return FAIL;
3572
3573 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003574 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 * - if the buffer is loaded, fail
3576 * - if the buffer is not loaded, delete it from the list
3577 */
3578#ifdef UNIX
3579 if (mch_stat((char *)ffname, &st) < 0)
3580 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003581#endif
3582 if (!(buf->b_flags & BF_DUMMY))
3583#ifdef UNIX
3584 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003586 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#endif
3588 if (obuf != NULL && obuf != buf)
3589 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003590 win_T *win;
3591 tabpage_T *tab;
3592 int in_use = FALSE;
3593
3594 // during startup a window may use a buffer that is not loaded yet
3595 FOR_ALL_TAB_WINDOWS(tab, win)
3596 if (win->w_buffer == obuf)
3597 in_use = TRUE;
3598
3599 // it's loaded or used in a window, fail
3600 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003603 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 vim_free(ffname);
3605 return FAIL;
3606 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003607 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003608 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
3610 sfname = vim_strsave(sfname);
3611 if (ffname == NULL || sfname == NULL)
3612 {
3613 vim_free(sfname);
3614 vim_free(ffname);
3615 return FAIL;
3616 }
3617#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003618 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003620 if (buf->b_sfname != buf->b_ffname)
3621 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 buf->b_ffname = ffname;
3624 buf->b_sfname = sfname;
3625 }
3626 buf->b_fname = buf->b_sfname;
3627#ifdef UNIX
3628 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003629 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 else
3631 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003632 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 buf->b_dev = st.st_dev;
3634 buf->b_ino = st.st_ino;
3635 }
3636#endif
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 buf_name_changed(buf);
3641 return OK;
3642}
3643
3644/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003645 * Crude way of changing the name of a buffer. Use with care!
3646 * The name should be relative to the current directory.
3647 */
3648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003649buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003650{
3651 buf_T *buf;
3652
3653 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003654 if (buf == NULL)
3655 return;
3656
3657 if (buf->b_sfname != buf->b_ffname)
3658 vim_free(buf->b_sfname);
3659 vim_free(buf->b_ffname);
3660 buf->b_ffname = vim_strsave(name);
3661 buf->b_sfname = NULL;
3662 // Allocate ffname and expand into full path. Also resolves .lnk
3663 // files on Win32.
3664 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3665 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003666}
3667
3668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 * Take care of what needs to be done when the name of buffer "buf" has
3670 * changed.
3671 */
3672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003673buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 /*
3676 * If the file name changed, also change the name of the swapfile
3677 */
3678 if (buf->b_ml.ml_mfp != NULL)
3679 ml_setname(buf);
3680
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003681#ifdef FEAT_TERMINAL
3682 if (buf->b_term != NULL)
3683 term_clear_status_text(buf->b_term);
3684#endif
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003687 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003688 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003689 status_redraw_all(); // status lines need to be redrawn
3690 fmarks_check_names(buf); // check named file marks
3691 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692}
3693
3694/*
3695 * set alternate file name for current window
3696 *
3697 * Used by do_one_cmd(), do_write() and do_ecmd().
3698 * Return the buffer.
3699 */
3700 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701setaltfname(
3702 char_u *ffname,
3703 char_u *sfname,
3704 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 buf_T *buf;
3707
Bram Moolenaarc667da52019-11-30 20:52:27 +01003708 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003710 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 curwin->w_alt_fnum = buf->b_fnum;
3712 return buf;
3713}
3714
3715/*
3716 * Get alternate file name for current window.
3717 * Return NULL if there isn't any, and give error message if requested.
3718 */
3719 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003721 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 char_u *fname;
3724 linenr_T dummy;
3725
3726 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3727 {
3728 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003729 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return NULL;
3731 }
3732 return fname;
3733}
3734
3735/*
3736 * Add a file name to the buflist and return its number.
3737 * Uses same flags as buflist_new(), except BLN_DUMMY.
3738 *
3739 * used by qf_init(), main() and doarglist()
3740 */
3741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003742buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 buf_T *buf;
3745
3746 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3747 if (buf != NULL)
3748 return buf->b_fnum;
3749 return 0;
3750}
3751
3752#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3753/*
3754 * Adjust slashes in file names. Called after 'shellslash' was set.
3755 */
3756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003757buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 buf_T *bp;
3760
Bram Moolenaar29323592016-07-24 22:04:11 +02003761 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
3763 if (bp->b_ffname != NULL)
3764 slash_adjust(bp->b_ffname);
3765 if (bp->b_sfname != NULL)
3766 slash_adjust(bp->b_sfname);
3767 }
3768}
3769#endif
3770
3771/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003772 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 * Also save the local window option values.
3774 */
3775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003776buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003778 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779}
3780
3781/*
3782 * Return TRUE if 'ffname' is not the same file as current file.
3783 * Fname must have a full path (expanded by mch_FullName()).
3784 */
3785 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003786otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787{
3788 return otherfile_buf(curbuf, ffname
3789#ifdef UNIX
3790 , NULL
3791#endif
3792 );
3793}
3794
3795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003796otherfile_buf(
3797 buf_T *buf,
3798 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003800 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003802 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003804 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3806 return TRUE;
3807 if (fnamecmp(ffname, buf->b_ffname) == 0)
3808 return FALSE;
3809#ifdef UNIX
3810 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003811 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
Bram Moolenaarc667da52019-11-30 20:52:27 +01003813 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (stp == NULL)
3815 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003816 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 st.st_dev = (dev_T)-1;
3818 stp = &st;
3819 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003820 // Use dev/ino to check if the files are the same, even when the names
3821 // are different (possible with links). Still need to compare the
3822 // name above, for when the file doesn't exist yet.
3823 // Problem: The dev/ino changes when a file is deleted (and created
3824 // again) and remains the same when renamed/moved. We don't want to
3825 // mch_stat() each buffer each time, that would be too slow. Get the
3826 // dev/ino again when they appear to match, but not when they appear
3827 // to be different: Could skip a buffer when it's actually the same
3828 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (buf_same_ino(buf, stp))
3830 {
3831 buf_setino(buf);
3832 if (buf_same_ino(buf, stp))
3833 return FALSE;
3834 }
3835 }
3836#endif
3837 return TRUE;
3838}
3839
3840#if defined(UNIX) || defined(PROTO)
3841/*
3842 * Set inode and device number for a buffer.
3843 * Must always be called when b_fname is changed!.
3844 */
3845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003846buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003848 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
3850 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3851 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003852 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 buf->b_dev = st.st_dev;
3854 buf->b_ino = st.st_ino;
3855 }
3856 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003857 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858}
3859
3860/*
3861 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3862 */
3863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003864buf_same_ino(
3865 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003866 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003868 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 && stp->st_dev == buf->b_dev
3870 && stp->st_ino == buf->b_ino);
3871}
3872#endif
3873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003874/*
3875 * Print info about the current buffer.
3876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003878fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003879 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003880 int shorthelp,
3881 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
3883 char_u *name;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003884 char *buffer;
John Marriottec032de2025-04-10 21:34:19 +02003885 size_t bufferlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003887 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (buffer == NULL)
3889 return;
3890
Bram Moolenaarc667da52019-11-30 20:52:27 +01003891 if (fullname > 1) // 2 CTRL-G: include buffer number
John Marriottec032de2025-04-10 21:34:19 +02003892 bufferlen = vim_snprintf_safelen(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
John Marriottec032de2025-04-10 21:34:19 +02003894 buffer[bufferlen++] = '"';
3895
3896 name = buf_spname(curbuf);
3897 if (name != NULL)
3898 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3899 IOSIZE - bufferlen, "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 else
3901 {
3902 if (!fullname && curbuf->b_fname != NULL)
3903 name = curbuf->b_fname;
3904 else
3905 name = curbuf->b_ffname;
John Marriottec032de2025-04-10 21:34:19 +02003906 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)buffer + bufferlen,
3907 IOSIZE - (int)bufferlen, TRUE);
3908 bufferlen += STRLEN(buffer + bufferlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910
John Marriottec032de2025-04-10 21:34:19 +02003911 bufferlen += vim_snprintf_safelen(
3912 buffer + bufferlen,
3913 IOSIZE - bufferlen,
3914 "\"%s%s%s%s%s%s",
3915 curbufIsChanged() ? (shortmess(SHM_MOD)
3916 ? " [+]" : _(" [Modified]")) : " ",
3917 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
3918 ? _("[Not edited]") : "",
3919 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
3920 ? new_file_message() : "",
3921 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", curbuf->b_p_ro
3922 ? (shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")) : "",
3923 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro)
3924 ? " " : "");
3925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (curbuf->b_ml.ml_flags & ML_EMPTY)
John Marriottec032de2025-04-10 21:34:19 +02003927 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3928 IOSIZE - bufferlen, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003930 // Current line and column are already on the screen -- webb
John Marriottec032de2025-04-10 21:34:19 +02003931 bufferlen += vim_snprintf_safelen(
3932 buffer + bufferlen,
3933 IOSIZE - bufferlen,
3934 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--", curbuf->b_ml.ml_line_count),
3935 (long)curbuf->b_ml.ml_line_count,
3936 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 else
3938 {
John Marriottec032de2025-04-10 21:34:19 +02003939 bufferlen += vim_snprintf_safelen(
3940 buffer + bufferlen,
3941 IOSIZE - bufferlen,
3942 _("line %ld of %ld --%d%%-- col "),
3943 (long)curwin->w_cursor.lnum,
3944 (long)curbuf->b_ml.ml_line_count,
3945 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 validate_virtcol();
John Marriottec032de2025-04-10 21:34:19 +02003948 bufferlen += col_print((char_u *)buffer + bufferlen, IOSIZE - bufferlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3950 }
3951
John Marriottec032de2025-04-10 21:34:19 +02003952 (void)append_arg_number(curwin, (char_u *)buffer + bufferlen,
3953 IOSIZE - bufferlen, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 if (dont_truncate)
3956 {
John Marriottec032de2025-04-10 21:34:19 +02003957 int n;
3958
Bram Moolenaarc667da52019-11-30 20:52:27 +01003959 // Temporarily set msg_scroll to avoid the message being truncated.
3960 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 msg_start();
3962 n = msg_scroll;
3963 msg_scroll = TRUE;
3964 msg(buffer);
3965 msg_scroll = n;
3966 }
3967 else
3968 {
John Marriottec032de2025-04-10 21:34:19 +02003969 char *p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003971 // Need to repeat the message after redrawing when:
3972 // - When restart_edit is set (otherwise there will be a delay
3973 // before redrawing).
3974 // - When the screen was scrolled but there is no wait-return
3975 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003976 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978
3979 vim_free(buffer);
3980}
3981
John Marriotta21240b2025-01-08 20:10:59 +01003982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003983col_print(
3984 char_u *buf,
3985 size_t buflen,
3986 int col,
3987 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
3989 if (col == vcol)
John Marriottec032de2025-04-10 21:34:19 +02003990 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d", col);
John Marriotta21240b2025-01-08 20:10:59 +01003991
John Marriottec032de2025-04-10 21:34:19 +02003992 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993}
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995static char_u *lasttitle = NULL;
3996static char_u *lasticon = NULL;
3997
Bram Moolenaar84a93082018-06-16 22:58:15 +02003998/*
3999 * Put the file name in the title bar and icon of the window.
4000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004002maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaar84a93082018-06-16 22:58:15 +02004004 char_u *title_str = NULL;
4005 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 int mustset;
4007 char_u buf[IOSIZE];
John Marriottec032de2025-04-10 21:34:19 +02004008 size_t buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (!redrawing())
4011 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004012 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 need_maketitle = TRUE;
4014 return;
4015 }
4016
4017 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02004018 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004019 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
4021 if (p_title)
4022 {
John Marriottec032de2025-04-10 21:34:19 +02004023 int maxlen = 0;
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (p_titlelen > 0)
4026 {
4027 maxlen = p_titlelen * Columns / 100;
4028 if (maxlen < 10)
4029 maxlen = 10;
4030 }
4031
Bram Moolenaar84a93082018-06-16 22:58:15 +02004032 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (*p_titlestring != NUL)
4034 {
4035#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004036 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004037 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4038 (char_u *)"titlestring", 0,
4039 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 else
4041#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004042 title_str = p_titlestring;
John Marriottec032de2025-04-10 21:34:19 +02004043 buflen = STRLEN(title_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045 else
4046 {
John Marriottec032de2025-04-10 21:34:19 +02004047 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
John Marriottec032de2025-04-10 21:34:19 +02004049 // format: "<filename> [flags] <(path)> [argument info] <- servername>"
4050 // example:
4051 // buffer.c + (/home/vim/src) (1 of 2) - VIM
4052
4053 // reserve some space for different parts of the title.
4054 // use sizeof() to introduce 'size_t' so we don't have to
4055 // cast sizes to it.
4056#define SPACE_FOR_FNAME (sizeof(buf) - 100)
4057#define SPACE_FOR_DIR (sizeof(buf) - 20)
4058#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // at least room for " - VIM"
4059
4060 // file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if (curbuf->b_fname == NULL)
John Marriottec032de2025-04-10 21:34:19 +02004062 buflen = vim_snprintf_safelen((char *)buf,
4063 SPACE_FOR_FNAME, "%s", _("[No Name]"));
Bram Moolenaar21554412017-07-24 21:44:43 +02004064#ifdef FEAT_TERMINAL
4065 else if (curbuf->b_term != NULL)
John Marriottec032de2025-04-10 21:34:19 +02004066 buflen = vim_snprintf_safelen((char *)buf,
4067 SPACE_FOR_FNAME, "%s",
4068 term_get_status_text(curbuf->b_term));
Bram Moolenaar21554412017-07-24 21:44:43 +02004069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 else
4071 {
John Marriottec032de2025-04-10 21:34:19 +02004072 buflen = vim_snprintf_safelen((char *)buf,
4073 SPACE_FOR_FNAME, "%s",
4074 ((p = transstr(gettail(curbuf->b_fname))) != NULL)
4075 ? p
4076 : (char_u *)"");
4077 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079
John Marriottec032de2025-04-10 21:34:19 +02004080 // flags
Bram Moolenaar21554412017-07-24 21:44:43 +02004081#ifdef FEAT_TERMINAL
4082 if (curbuf->b_term == NULL)
4083#endif
John Marriottec032de2025-04-10 21:34:19 +02004084 {
Bram Moolenaar21554412017-07-24 21:44:43 +02004085 switch (bufIsChanged(curbuf)
4086 + (curbuf->b_p_ro * 2)
4087 + (!curbuf->b_p_ma * 4))
4088 {
John Marriottec032de2025-04-10 21:34:19 +02004089 case 1:
4090 // file was modified
4091 buflen += vim_snprintf_safelen(
4092 (char *)buf + buflen,
4093 sizeof(buf) - buflen, " +");
4094 break;
4095 case 2:
4096 // file is readonly
4097 buflen += vim_snprintf_safelen(
4098 (char *)buf + buflen,
4099 sizeof(buf) - buflen, " =");
4100 break;
4101 case 3:
4102 // file was modified and is readonly
4103 buflen += vim_snprintf_safelen(
4104 (char *)buf + buflen,
4105 sizeof(buf) - buflen, " =+");
4106 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004107 case 4:
John Marriottec032de2025-04-10 21:34:19 +02004108 case 6:
4109 // file cannot be modified
4110 buflen += vim_snprintf_safelen(
4111 (char *)buf + buflen,
4112 sizeof(buf) - buflen, " -");
4113 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004114 case 5:
John Marriottec032de2025-04-10 21:34:19 +02004115 case 7:
4116 // file cannot be modified but was modified
4117 buflen += vim_snprintf_safelen(
4118 (char *)buf + buflen,
4119 sizeof(buf) - buflen, " -+");
4120 break;
4121 default:
4122 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004123 }
John Marriottec032de2025-04-10 21:34:19 +02004124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
John Marriottec032de2025-04-10 21:34:19 +02004126 // path (surrounded by '()')
Bram Moolenaar21554412017-07-24 21:44:43 +02004127 if (curbuf->b_fname != NULL
4128#ifdef FEAT_TERMINAL
4129 && curbuf->b_term == NULL
4130#endif
4131 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004133 // Get path of file, replace home dir with ~
John Marriottec032de2025-04-10 21:34:19 +02004134 buflen += vim_snprintf_safelen((char *)buf + buflen,
4135 sizeof(buf) - buflen, " (");
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 home_replace(curbuf, curbuf->b_ffname,
John Marriottec032de2025-04-10 21:34:19 +02004138 buf + buflen, (int)(SPACE_FOR_DIR - buflen), TRUE);
4139
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004141 // avoid "c:/name" to be reduced to "c"
John Marriottec032de2025-04-10 21:34:19 +02004142 if (SAFE_isalpha(buf[buflen]) && buf[buflen + 1] == ':')
4143 buflen += 2; // step over "c:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#endif
John Marriottec032de2025-04-10 21:34:19 +02004145
4146 // determine if we have a help or normal buffer
4147 p = gettail_sep(buf + buflen);
4148 if (p == buf + buflen)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004149 {
John Marriottec032de2025-04-10 21:34:19 +02004150 // help buffer
4151 buflen += vim_snprintf_safelen((char *)buf + buflen,
4152 SPACE_FOR_DIR - buflen, "%s)", _("help"));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 else
Bram Moolenaar2c666692012-09-05 13:30:40 +02004155 {
John Marriottec032de2025-04-10 21:34:19 +02004156 // normal buffer
4157
4158 // Translate unprintable chars and concatenate. Keep some
4159 // room for the server name. When there is no room (very long
4160 // file name) use (...).
4161 if (buflen < SPACE_FOR_DIR)
John Marriott7fb90812025-04-02 20:32:35 +02004162 {
John Marriottec032de2025-04-10 21:34:19 +02004163 // remove the file name
4164 *p = NUL;
4165
4166 buflen += vim_snprintf_safelen((char *)buf + buflen,
4167 SPACE_FOR_DIR - buflen, "%s)",
4168 ((p = transstr(buf + buflen)) != NULL)
4169 ? p
4170 : (char_u *)"");
John Marriott7fb90812025-04-02 20:32:35 +02004171 vim_free(p);
4172 }
John Marriottec032de2025-04-10 21:34:19 +02004173 else
4174 buflen += vim_snprintf_safelen((char *)buf + buflen,
4175 SPACE_FOR_ARGNR - buflen, "...)");
Bram Moolenaar2c666692012-09-05 13:30:40 +02004176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178
John Marriottec032de2025-04-10 21:34:19 +02004179 // argument info
4180 buflen += append_arg_number(curwin, buf + buflen,
4181 SPACE_FOR_ARGNR - buflen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
John Marriottec032de2025-04-10 21:34:19 +02004183 // servername
4184 buflen += vim_snprintf_safelen((char *)buf + buflen,
4185 sizeof(buf) - buflen, " - %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186#if defined(FEAT_CLIENTSERVER)
John Marriottec032de2025-04-10 21:34:19 +02004187 (serverName != NULL)
4188 ? serverName :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#endif
John Marriottec032de2025-04-10 21:34:19 +02004190 (char_u *)"VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 if (maxlen > 0)
4193 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004194 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004195 if (vim_strsize(buf) > maxlen)
John Marriottec032de2025-04-10 21:34:19 +02004196 trunc_string(buf, buf, maxlen, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004200 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 if (p_icon)
4203 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004204 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (*p_iconstring != NUL)
4206 {
4207#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004208 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004209 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4210 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 else
4212#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004213 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 else
4216 {
John Marriottec032de2025-04-10 21:34:19 +02004217 char_u *name;
4218 int namelen;
4219
4220 name = buf_spname(curbuf);
4221 if (name == NULL)
4222 name = gettail(curbuf->b_ffname);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004223 // Truncate name at 100 bytes.
John Marriottec032de2025-04-10 21:34:19 +02004224 namelen = (int)STRLEN(name);
4225 if (namelen > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
John Marriottec032de2025-04-10 21:34:19 +02004227 namelen -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (has_mbyte)
John Marriottec032de2025-04-10 21:34:19 +02004229 namelen += (*mb_tail_off)(name, name + namelen) + 1;
4230 name += namelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
John Marriottec032de2025-04-10 21:34:19 +02004232 STRCPY(buf, name);
4233 trans_characters(buf, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 }
4236
Bram Moolenaar84a93082018-06-16 22:58:15 +02004237 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 if (mustset)
4240 resettitle();
4241}
4242
4243/*
4244 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4245 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004246 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 */
4248 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004249value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
4251 if ((str == NULL) != (*last == NULL)
4252 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4253 {
4254 vim_free(*last);
4255 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004258 mch_restore_title(
4259 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004264 return TRUE;
4265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 return FALSE;
4268}
4269
4270/*
4271 * Put current window title back (used after calling a shell)
4272 */
4273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004274resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275{
4276 mch_settitle(lasttitle, lasticon);
4277}
Bram Moolenaarea408852005-06-25 22:49:46 +00004278
4279# if defined(EXITFREE) || defined(PROTO)
4280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004281free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004282{
4283 vim_free(lasttitle);
4284 vim_free(lasticon);
4285}
4286# endif
4287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004289#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004290
4291/*
4292 * Used for building in the status line.
4293 */
4294typedef struct
4295{
4296 char_u *stl_start;
4297 int stl_minwid;
4298 int stl_maxwid;
4299 enum {
4300 Normal,
4301 Empty,
4302 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004303 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004304 Highlight,
4305 TabPage,
4306 Trunc
4307 } stl_type;
4308} stl_item_T;
4309
4310static size_t stl_items_len = 20; // Initial value, grows as needed.
4311static stl_item_T *stl_items = NULL;
4312static int *stl_groupitem = NULL;
4313static stl_hlrec_T *stl_hltab = NULL;
4314static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004315static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004318 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 * Return length of string in screen cells.
4320 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004321 * Normally works for window "wp", except when working for 'tabline' then it
4322 * is "curwin".
4323 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004325 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4327 *
4328 * If maxwidth is not zero, the string will be filled at any middle marker
4329 * or truncated if too long, fillchar is used for all whitespace.
4330 */
4331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004332build_stl_str_hl(
4333 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004334 char_u *out, // buffer to write into != NameBuff
4335 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004336 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004337 char_u *opt_name, // option name corresponding to "fmt"
4338 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004339 int fillchar,
4340 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004341 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4342 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
Bram Moolenaar10772302019-01-20 18:25:54 +01004344 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004345 colnr_T len;
John Marriottec032de2025-04-10 21:34:19 +02004346 size_t outputlen; // length of out[] used (excluding the NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 char_u *p;
4348 char_u *s;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004349 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004351 int use_sandbox;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004352 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#endif
4354 int empty_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 long l;
4356 long n;
4357 int prevchar_isflag;
4358 int prevchar_isitem;
4359 int itemisflag;
4360 int fillable;
4361 char_u *str;
4362 long num;
4363 int width;
4364 int itemcnt;
4365 int curitem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004367#ifdef FEAT_EVAL
4368 int evaldepth;
4369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 int minwid;
4371 int maxwid;
4372 int zeropad;
4373 char_u base;
4374 char_u opt;
4375#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004376 char_u buf_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004377 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004378 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004379 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004380 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004381 // TODO: find out why using called_emsg_before makes tests fail, does it
4382 // matter?
4383 // int called_emsg_before = called_emsg;
4384 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004385
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004386 // When inside update_screen() we do not want redrawing a statusline,
4387 // ruler, title, etc. to trigger another redraw, it may cause an endless
4388 // loop.
4389 if (updating_screen)
4390 redraw_not_allowed = TRUE;
4391
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004392 if (stl_items == NULL)
4393 {
4394 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4395 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004396
4397 // Allocate one more, because the last element is used to indicate the
4398 // end of the list.
4399 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4400 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004401
4402 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004403 }
4404
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004405#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004406 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4407 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4408
4409 // When the format starts with "%!" then evaluate it as an expression and
4410 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004411 if (fmt[0] == '%' && fmt[1] == '!')
4412 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004413 typval_T tv;
4414
4415 tv.v_type = VAR_NUMBER;
4416 tv.vval.v_number = wp->w_id;
4417 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4418
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004419 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004420 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004421 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004422
4423 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004424 }
4425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 if (fillchar == 0)
4428 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004429
Bram Moolenaar10772302019-01-20 18:25:54 +01004430 // The cursor in windows other than the current one isn't always
4431 // up-to-date, esp. because of autocommands and timers.
4432 lnum = wp->w_cursor.lnum;
4433 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4434 {
4435 lnum = wp->w_buffer->b_ml.ml_line_count;
4436 wp->w_cursor.lnum = lnum;
4437 }
4438
4439 // Get line & check if empty (cursorpos will show "0-1"). Note that
4440 // p will become invalid when getting another buffer line.
4441 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004442 empty_line = (*p == NUL);
4443
Bram Moolenaar10772302019-01-20 18:25:54 +01004444 // Get the byte value now, in case we need it below. This is more efficient
4445 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004446 len = ml_get_buf_len(wp->w_buffer, lnum);
4447 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004448 {
4449 // Line may have changed since checking the cursor column, or the lnum
4450 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004451 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004452 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004453 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004454 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004455 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004456 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004459#ifdef FEAT_EVAL
4460 evaldepth = 0;
4461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 p = out;
4463 curitem = 0;
4464 prevchar_isflag = TRUE;
4465 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004466 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004468 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004469 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004470 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004471
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004472 stl_item_T *new_items =
4473 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474 if (new_items == NULL)
4475 break;
4476 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004477
4478 int *new_groupitem =
4479 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004480 if (new_groupitem == NULL)
4481 break;
4482 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004483
4484 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004485 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004486 if (new_hlrec == NULL)
4487 break;
4488 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004489 new_hlrec = vim_realloc(stl_tabtab,
4490 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004491 if (new_hlrec == NULL)
4492 break;
4493 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004494
4495 int *new_separator_locs = vim_realloc(stl_separator_locations,
4496 sizeof(int) * new_len);
4497 if (new_separator_locs == NULL)
4498 break;
John Marriottec032de2025-04-10 21:34:19 +02004499 stl_separator_locations = new_separator_locs;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004500
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004501 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004502 }
4503
zeertzjq122dea72022-07-27 15:48:45 +01004504 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 prevchar_isflag = prevchar_isitem = FALSE;
4506
4507 /*
4508 * Handle up to the next '%' or the end.
4509 */
4510 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4511 *p++ = *s++;
4512 if (*s == NUL || p + 1 >= out + outlen)
4513 break;
4514
4515 /*
4516 * Handle one '%' item.
4517 */
4518 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004519 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (*s == '%')
4522 {
4523 if (p + 1 >= out + outlen)
4524 break;
4525 *p++ = *s++;
4526 prevchar_isflag = prevchar_isitem = FALSE;
4527 continue;
4528 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004529 // STL_SEPARATE: Separation between items, filled with white space.
4530 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
4532 s++;
4533 if (groupdepth > 0)
4534 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004535 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004536 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 continue;
4538 }
4539 if (*s == STL_TRUNCMARK)
4540 {
4541 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004542 stl_items[curitem].stl_type = Trunc;
4543 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 continue;
4545 }
4546 if (*s == ')')
4547 {
John Marriottec032de2025-04-10 21:34:19 +02004548 char_u *t;
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 s++;
4551 if (groupdepth < 1)
4552 continue;
4553 groupdepth--;
4554
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004555 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 *p = NUL;
4557 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004558 if (curitem > stl_groupitem[groupdepth] + 1
4559 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
John Marriottec032de2025-04-10 21:34:19 +02004561 int group_start_userhl = 0;
4562 int group_end_userhl = 0;
4563
Bram Moolenaarc667da52019-11-30 20:52:27 +01004564 // remove group if all items are empty and highlight group
4565 // doesn't change
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004566 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004567 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004568 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004569 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004570 group_start_userhl = group_end_userhl =
4571 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004572 break;
4573 }
4574 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004575 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004576 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004577 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004579 if (stl_items[n].stl_type == Highlight)
4580 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004581 }
4582 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004584 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 p = t;
4586 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004587 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004588 {
4589 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004590 if (stl_items[n].stl_type == Highlight)
4591 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004592 // adjust the start position of TabPage to the next
4593 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004594 if (stl_items[n].stl_type == TabPage)
4595 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004599 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004601 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (has_mbyte)
4603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004604 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004606 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004609 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 }
4612 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004613 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4614 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004617 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004619
4620 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004621 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004622 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
Bram Moolenaarc667da52019-11-30 20:52:27 +01004624 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004625 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
LemonBoy57ff5262022-05-09 21:03:47 +01004627 // Minus one for the leading '<' added above.
4628 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004629 if (stl_items[l].stl_start < t)
4630 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004633 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004635 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004636 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (n < 0)
4638 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004639 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 n = 0 - n;
4641 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004642 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 else
4645 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004646 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004647 l = (n - l) * MB_CHAR2LEN(fillchar);
4648 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004650 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004652 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4653 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004655 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657 }
4658 continue;
4659 }
4660 minwid = 0;
4661 maxwid = 9999;
4662 zeropad = FALSE;
4663 l = 1;
4664 if (*s == '0')
4665 {
4666 s++;
4667 zeropad = TRUE;
4668 }
4669 if (*s == '-')
4670 {
4671 s++;
4672 l = -1;
4673 }
4674 if (VIM_ISDIGIT(*s))
4675 {
4676 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004677 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 minwid = 0;
4679 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004680 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004682 stl_items[curitem].stl_type = Highlight;
4683 stl_items[curitem].stl_start = p;
4684 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 s++;
4686 curitem++;
4687 continue;
4688 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004689 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4690 {
4691 if (*s == STL_TABCLOSENR)
4692 {
4693 if (minwid == 0)
4694 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004695 // %X ends the close label, go back to the previously
4696 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004697 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004698 if (stl_items[n].stl_type == TabPage
4699 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004700 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004701 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004702 break;
4703 }
4704 }
4705 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004706 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004707 minwid = - minwid;
4708 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004709 stl_items[curitem].stl_type = TabPage;
4710 stl_items[curitem].stl_start = p;
4711 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004712 s++;
4713 curitem++;
4714 continue;
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (*s == '.')
4717 {
4718 s++;
4719 if (VIM_ISDIGIT(*s))
4720 {
4721 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004722 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 maxwid = 50;
4724 }
4725 }
4726 minwid = (minwid > 50 ? 50 : minwid) * l;
4727 if (*s == '(')
4728 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004729 stl_groupitem[groupdepth++] = curitem;
4730 stl_items[curitem].stl_type = Group;
4731 stl_items[curitem].stl_start = p;
4732 stl_items[curitem].stl_minwid = minwid;
4733 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 s++;
4735 curitem++;
4736 continue;
4737 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004738#ifdef FEAT_EVAL
4739 // Denotes end of expanded %{} block
4740 if (*s == '}' && evaldepth > 0)
4741 {
4742 s++;
4743 evaldepth--;
4744 continue;
4745 }
4746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (vim_strchr(STL_ALL, *s) == NULL)
4748 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004749 if (*s == NUL) // can happen with "%0"
4750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 s++;
4752 continue;
4753 }
4754 opt = *s++;
4755
Bram Moolenaarc667da52019-11-30 20:52:27 +01004756 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 base = 'D';
4758 itemisflag = FALSE;
4759 fillable = TRUE;
4760 num = -1;
4761 str = NULL;
4762 switch (opt)
4763 {
4764 case STL_FILEPATH:
4765 case STL_FULLPATH:
4766 case STL_FILENAME:
John Marriottec032de2025-04-10 21:34:19 +02004767 {
4768 char_u *name;
4769
Bram Moolenaarc667da52019-11-30 20:52:27 +01004770 fillable = FALSE; // don't change ' ' to fillchar
John Marriottec032de2025-04-10 21:34:19 +02004771 name = buf_spname(wp->w_buffer);
4772 if (name != NULL)
4773 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 else
4775 {
John Marriottec032de2025-04-10 21:34:19 +02004776 char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004777 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4779 }
4780 trans_characters(NameBuff, MAXPATHL);
4781 if (opt != STL_FILENAME)
4782 str = NameBuff;
4783 else
4784 str = gettail(NameBuff);
4785 break;
John Marriottec032de2025-04-10 21:34:19 +02004786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaarc667da52019-11-30 20:52:27 +01004788 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004789 {
4790#ifdef FEAT_EVAL
John Marriottec032de2025-04-10 21:34:19 +02004791 char_u *block_start = s - 1;
shadmansaleh30e3de22021-05-15 17:23:28 +02004792#endif
John Marriottec032de2025-04-10 21:34:19 +02004793 int reevaluate = (*s == '%');
4794 char_u *t;
4795 buf_T *save_curbuf;
4796 win_T *save_curwin;
shadmansaleh30e3de22021-05-15 17:23:28 +02004797
4798 if (reevaluate)
4799 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 itemisflag = TRUE;
4801 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004802 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4803 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004805 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 break;
4807 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004808 if (reevaluate)
John Marriottec032de2025-04-10 21:34:19 +02004809 p[-1] = NUL; // remove the % at the end of %{% expr %}
shadmansaleh30e3de22021-05-15 17:23:28 +02004810 else
John Marriottec032de2025-04-10 21:34:19 +02004811 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004814 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4815 "%d", curbuf->b_fnum);
4816 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
John Marriottec032de2025-04-10 21:34:19 +02004817 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "%d", curwin->w_id);
4818 set_internal_string_var((char_u *)"g:actual_curwin", buf_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004820 save_curbuf = curbuf;
4821 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004822 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 curwin = wp;
4824 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004825 // Visual mode is only valid in the current window.
4826 if (curwin != save_curwin)
4827 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004829 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004831 curwin = save_curwin;
4832 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004833 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004834 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004835 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
John Marriottec032de2025-04-10 21:34:19 +02004837 if (str != NULL && *str != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 if (*skipdigits(str) == NUL)
4840 {
4841 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004842 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 itemisflag = FALSE;
4844 }
4845 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004846
4847 // If the output of the expression needs to be evaluated
4848 // replace the %{} block with the result of evaluation
John Marriottec032de2025-04-10 21:34:19 +02004849 if (reevaluate && str != NULL && *str != NUL
shadmansaleh30e3de22021-05-15 17:23:28 +02004850 && strchr((const char *)str, '%') != NULL
4851 && evaldepth < MAX_STL_EVAL_DEPTH)
4852 {
4853 size_t parsed_usefmt = (size_t)(block_start - usefmt);
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004854 size_t str_length = strlen((const char *)str);
4855 size_t fmt_length = strlen((const char *)s);
4856 size_t new_fmt_len = parsed_usefmt
4857 + str_length + fmt_length + 3;
4858 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004859
John Marriott7fb90812025-04-02 20:32:35 +02004860 if (new_fmt != NULL)
4861 {
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004862 char_u *new_fmt_p = new_fmt;
4863
4864 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4865 + parsed_usefmt;
4866 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4867 + str_length;
4868 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4869 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4870 + fmt_length;
4871 *new_fmt_p = 0;
4872 new_fmt_p = NULL;
John Marriott7fb90812025-04-02 20:32:35 +02004873
4874 if (usefmt != fmt)
4875 vim_free(usefmt);
4876 VIM_CLEAR(str);
4877 usefmt = new_fmt;
4878 s = usefmt + parsed_usefmt;
4879 evaldepth++;
4880 continue;
4881 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#endif
4884 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 case STL_LINE:
4887 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4888 ? 0L : (long)(wp->w_cursor.lnum);
4889 break;
4890
4891 case STL_NUMLINES:
4892 num = wp->w_buffer->b_ml.ml_line_count;
4893 break;
4894
4895 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004896 num = (State & MODE_INSERT) == 0 && empty_line
4897 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 break;
4899
4900 case STL_VIRTCOL:
4901 case STL_VIRTCOL_ALT:
John Marriottec032de2025-04-10 21:34:19 +02004902 {
4903 colnr_T virtcol = wp->w_virtcol + 1;
4904
Bram Moolenaarc667da52019-11-30 20:52:27 +01004905 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004907 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4908 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910 num = (long)virtcol;
4911 break;
John Marriottec032de2025-04-10 21:34:19 +02004912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 case STL_PERCENTAGE:
John Marriottec032de2025-04-10 21:34:19 +02004915 num = calc_percentage((long)wp->w_cursor.lnum, (long)wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917
4918 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004919 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004920 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 break;
4922
Luuk van Baalba936f62022-12-15 13:15:39 +00004923 case STL_SHOWCMD:
4924 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4925 str = showcmd_buf;
4926 break;
4927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 case STL_ARGLISTSTAT:
4929 fillable = FALSE;
John Marriottec032de2025-04-10 21:34:19 +02004930 buf_tmp[0] = NUL;
4931 if (append_arg_number(wp, buf_tmp, sizeof(buf_tmp), FALSE) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004932 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 break;
4934
4935 case STL_KEYMAP:
4936 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004937 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004938 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 break;
4940 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004941#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004942 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#else
4944 num = 0;
4945#endif
4946 break;
4947
4948 case STL_BUFNO:
4949 num = wp->w_buffer->b_fnum;
4950 break;
4951
4952 case STL_OFFSET_X:
4953 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004954 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 case STL_OFFSET:
4956#ifdef FEAT_BYTEOFF
4957 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004958 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4959 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4960 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#endif
4962 break;
4963
4964 case STL_BYTEVAL_X:
4965 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004966 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004968 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (num == NL)
4970 num = 0;
4971 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4972 num = NL;
4973 break;
4974
4975 case STL_ROFLAG:
4976 case STL_ROFLAG_ALT:
4977 itemisflag = TRUE;
4978 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004979 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981
4982 case STL_HELPFLAG:
4983 case STL_HELPFLAG_ALT:
4984 itemisflag = TRUE;
4985 if (wp->w_buffer->b_help)
4986 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004987 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 case STL_FILETYPE:
4991 if (*wp->w_buffer->b_p_ft != NUL
4992 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4993 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004994 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004995 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004996 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 break;
4999
5000 case STL_FILETYPE_ALT:
5001 itemisflag = TRUE;
5002 if (*wp->w_buffer->b_p_ft != NUL
5003 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
5004 {
John Marriottec032de2025-04-10 21:34:19 +02005005 char_u *t;
5006
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005007 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005008 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005009 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005011 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
Bram Moolenaar4033c552017-09-16 20:54:51 +02005015#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 case STL_PREVIEWFLAG:
5017 case STL_PREVIEWFLAG_ALT:
5018 itemisflag = TRUE;
5019 if (wp->w_p_pvw)
5020 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
5021 : _("[Preview]"));
5022 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005023
5024 case STL_QUICKFIX:
5025 if (bt_quickfix(wp->w_buffer))
5026 str = (char_u *)(wp->w_llist_ref
5027 ? _(msg_loclist)
5028 : _(msg_qflist));
5029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#endif
5031
5032 case STL_MODIFIED:
5033 case STL_MODIFIED_ALT:
5034 itemisflag = TRUE;
5035 switch ((opt == STL_MODIFIED_ALT)
5036 + bufIsChanged(wp->w_buffer) * 2
5037 + (!wp->w_buffer->b_p_ma) * 4)
5038 {
5039 case 2: str = (char_u *)"[+]"; break;
5040 case 3: str = (char_u *)",+"; break;
5041 case 4: str = (char_u *)"[-]"; break;
5042 case 5: str = (char_u *)",-"; break;
5043 case 6: str = (char_u *)"[+-]"; break;
5044 case 7: str = (char_u *)",+-"; break;
5045 }
5046 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005047
5048 case STL_HIGHLIGHT:
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005049 {
John Marriottec032de2025-04-10 21:34:19 +02005050 char_u *t = s;
5051
5052 while (*s != '#' && *s != NUL)
5053 ++s;
5054 if (*s == '#')
5055 {
5056 stl_items[curitem].stl_type = Highlight;
5057 stl_items[curitem].stl_start = p;
5058 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
5059 curitem++;
5060 }
5061 if (*s != NUL)
5062 ++s;
5063 continue;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005067 stl_items[curitem].stl_start = p;
5068 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (str != NULL && *str)
5070 {
John Marriottec032de2025-04-10 21:34:19 +02005071 char_u *t = str;
5072
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (itemisflag)
5074 {
5075 if ((t[0] && t[1])
5076 && ((!prevchar_isitem && *t == ',')
5077 || (prevchar_isflag && *t == ' ')))
5078 t++;
5079 prevchar_isflag = TRUE;
5080 }
5081 l = vim_strsize(t);
5082 if (l > 0)
5083 prevchar_isitem = TRUE;
5084 if (l > maxwid)
5085 {
5086 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (has_mbyte)
5088 {
5089 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005090 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 l -= byte2cells(*t++);
5094 if (p + 1 >= out + outlen)
5095 break;
5096 *p++ = '<';
5097 }
5098 if (minwid > 0)
5099 {
5100 for (; l < minwid && p + 1 < out + outlen; l++)
5101 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005102 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5104 *p++ = ' ';
5105 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005106 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 minwid = 0;
5109 }
5110 else
5111 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005112 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005114 // Change a space by fillchar, unless fillchar is '-' and a
5115 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005116 if (fillable && *t == ' '
5117 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5118 MB_CHAR2BYTES(fillchar, p);
5119 else
5120 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005123 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 else if (num >= 0)
5126 {
John Marriottec032de2025-04-10 21:34:19 +02005127 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5128 char_u nstr[20];
5129 char_u *t = nstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
5131 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005132 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 prevchar_isitem = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (opt == STL_VIRTCOL_ALT)
5135 {
5136 *t++ = '-';
5137 minwid--;
5138 }
5139 *t++ = '%';
5140 if (zeropad)
5141 *t++ = '0';
5142 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005143 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
John Marriottec032de2025-04-10 21:34:19 +02005144 *t = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 for (n = num, l = 1; n >= nbase; n /= nbase)
5147 l++;
5148 if (opt == STL_VIRTCOL_ALT)
5149 l++;
John Marriottec032de2025-04-10 21:34:19 +02005150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (l > maxwid)
5152 {
5153 l += 2;
5154 n = l - maxwid;
5155 while (l-- > maxwid)
5156 num /= nbase;
5157 *t++ = '>';
5158 *t++ = '%';
5159 *t = t[-3];
John Marriottec032de2025-04-10 21:34:19 +02005160 *++t = NUL;
5161 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5162 (char *)nstr, 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 else
John Marriottec032de2025-04-10 21:34:19 +02005165 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5166 (char *)nstr, minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005169 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005171 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5172 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5173 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (opt == STL_VIM_EXPR)
5175 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 curitem++;
5177 }
5178 *p = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005179 outputlen = (size_t)(p - out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 itemcnt = curitem;
5181
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005182#ifdef FEAT_EVAL
5183 if (usefmt != fmt)
5184 vim_free(usefmt);
5185#endif
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 width = vim_strsize(out);
5188 if (maxwidth > 0 && width > maxwidth)
5189 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005190 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 l = 0;
5192 if (itemcnt == 0)
5193 s = out;
5194 else
5195 {
5196 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005197 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005199 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005200 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202 }
5203 if (l == itemcnt)
5204 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005205 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005206 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 l = 0;
5208 }
5209 }
5210
5211 if (width - vim_strsize(s) >= maxwidth)
5212 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005213 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (has_mbyte)
5215 {
5216 s = out;
5217 width = 0;
5218 for (;;)
5219 {
5220 width += ptr2cells(s);
5221 if (width >= maxwidth)
5222 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005223 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005225 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005227 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 s = out + maxwidth - 1;
5231 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005232 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234 itemcnt = l;
5235 *s++ = '>';
John Marriottec032de2025-04-10 21:34:19 +02005236 *s = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 else
5239 {
John Marriottec032de2025-04-10 21:34:19 +02005240 char_u *end = out + outputlen;
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 if (has_mbyte)
5243 {
5244 n = 0;
5245 while (width >= maxwidth)
5246 {
5247 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005248 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 }
5251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 n = width - maxwidth + 1;
5253 p = s + n;
John Marriottec032de2025-04-10 21:34:19 +02005254 mch_memmove(s + 1, p, (size_t)(end - p) + 1); // +1 for NUL
5255 end -= (size_t)(p - (s + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 *s = '<';
5257
Bram Moolenaarc667da52019-11-30 20:52:27 +01005258 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 for (; l < itemcnt; l++)
5260 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005261 if (stl_items[l].stl_start - n >= s)
5262 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005264 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
zeertzjqd392a742023-07-01 20:24:40 +01005266
5267 // Fill up for half a double-wide character.
5268 while (++width < maxwidth)
5269 {
John Marriottec032de2025-04-10 21:34:19 +02005270 s = end;
zeertzjqd392a742023-07-01 20:24:40 +01005271 MB_CHAR2BYTES(fillchar, s);
5272 *s = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005273 end = s;
zeertzjqd392a742023-07-01 20:24:40 +01005274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 width = maxwidth;
5277 }
John Marriottec032de2025-04-10 21:34:19 +02005278 else if (width < maxwidth && outputlen + maxwidth - width + 1 < outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005280 // Find how many separators there are, which we will use when
5281 // figuring out how many groups there are.
5282 int num_separators = 0;
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005286 if (stl_items[l].stl_type == Separate)
5287 {
5288 // Create an array of the start location for each separator
5289 // mark.
5290 stl_separator_locations[num_separators] = l;
5291 num_separators++;
5292 }
5293 }
5294
5295 // If we have separated groups, then we deal with it now
5296 if (num_separators)
5297 {
5298 int standard_spaces;
5299 int final_spaces;
5300
5301 standard_spaces = (maxwidth - width) / num_separators;
5302 final_spaces = (maxwidth - width) -
5303 standard_spaces * (num_separators - 1);
5304 for (l = 0; l < num_separators; l++)
5305 {
5306 int dislocation = (l == (num_separators - 1)) ?
5307 final_spaces : standard_spaces;
5308 dislocation *= MB_CHAR2LEN(fillchar);
5309 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5310 char_u *seploc = start + dislocation;
5311 STRMOVE(seploc, start);
5312 for (s = start; s < seploc;)
5313 MB_CHAR2BYTES(fillchar, s);
5314
5315 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5316 stl_items[i].stl_start += dislocation;
5317 }
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 width = maxwidth;
5320 }
5321 }
5322
Bram Moolenaarc667da52019-11-30 20:52:27 +01005323 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005324 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005326 *hltab = stl_hltab;
5327 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 for (l = 0; l < itemcnt; l++)
5329 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005330 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005332 sp->start = stl_items[l].stl_start;
5333 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005334 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005337 sp->start = NULL;
5338 sp->userhl = 0;
5339 }
5340
Bram Moolenaarc667da52019-11-30 20:52:27 +01005341 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005342 if (tabtab != NULL)
5343 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005344 *tabtab = stl_tabtab;
5345 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005346 for (l = 0; l < itemcnt; l++)
5347 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005348 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005349 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005350 sp->start = stl_items[l].stl_start;
5351 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005352 sp++;
5353 }
5354 }
5355 sp->start = NULL;
5356 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005359 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005360
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005361 // A user function may reset KeyTyped, restore it.
5362 KeyTyped = save_KeyTyped;
5363
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005364 // Check for an error. If there is one the display will be messed up and
5365 // might loop redrawing. Avoid that by making the corresponding option
5366 // empty.
5367 // TODO: find out why using called_emsg_before makes tests fail, does it
5368 // matter?
5369 // if (called_emsg > called_emsg_before)
5370 if (did_emsg > did_emsg_before)
5371 set_string_option_direct(opt_name, -1, (char_u *)"",
5372 OPT_FREE | opt_scope, SID_ERROR);
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return width;
5375}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005376#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378/*
John Marriottec032de2025-04-10 21:34:19 +02005379 * Get relative cursor position in window into "buf[]", in the localized
Emir SARI971cd2b2023-04-29 12:09:53 +01005380 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 */
John Marriotta21240b2025-01-08 20:10:59 +01005382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005383get_rel_pos(
5384 win_T *wp,
5385 char_u *buf,
5386 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005388 long above; // number of lines above window
5389 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaarc667da52019-11-30 20:52:27 +01005391 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005392 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 above = wp->w_topline - 1;
5394#ifdef FEAT_DIFF
5395 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005396 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005397 above = 0; // All buffer lines are displayed and there is an
5398 // indication of filler lines, that can be considered
5399 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#endif
5401 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5402 if (below <= 0)
John Marriottec032de2025-04-10 21:34:19 +02005403 return (int)vim_snprintf_safelen((char *)buf, buflen,
5404 "%s", (above == 0) ? _("All") : _("Bot"));
Emir SARI971cd2b2023-04-29 12:09:53 +01005405
John Marriottec032de2025-04-10 21:34:19 +02005406 if (above <= 0)
5407 return (int)vim_snprintf_safelen((char *)buf, buflen,
5408 "%s", _("Top"));
John Marriotta21240b2025-01-08 20:10:59 +01005409
John Marriottec032de2025-04-10 21:34:19 +02005410 // localized percentage value
5411 return (int)vim_snprintf_safelen((char *)buf, buflen,
5412 _("%2d%%"), calc_percentage(above, above + below));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
John Marriottec032de2025-04-10 21:34:19 +02005416 * Append (file 2 of 8) to "buf[]", if editing more than one file.
5417 * Return the number of characters appended.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005420append_arg_number(
5421 win_T *wp,
5422 char_u *buf,
John Marriottec032de2025-04-10 21:34:19 +02005423 size_t buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005424 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005426 if (ARGCOUNT <= 1) // nothing to do
John Marriottec032de2025-04-10 21:34:19 +02005427 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
Bram Moolenaara8490a42023-05-23 18:00:58 +01005429 char *msg;
5430 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005432 case 0: msg = _(" (%d of %d)"); break;
5433 case 1: msg = _(" ((%d) of %d)"); break;
5434 case 2: msg = _(" (file %d of %d)"); break;
5435 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005437
John Marriottec032de2025-04-10 21:34:19 +02005438 return (int)vim_snprintf_safelen((char *)buf, buflen, msg,
Bram Moolenaara8490a42023-05-23 18:00:58 +01005439 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440}
5441
5442/*
5443 * If fname is not a full path, make it a full path.
5444 * Returns pointer to allocated memory (NULL for failure).
5445 */
5446 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005447fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 /*
5450 * Force expanding the path always for Unix, because symbolic links may
5451 * mess up the full path name, even though it starts with a '/'.
5452 * Also expand when there is ".." in the file name, try to remove it,
5453 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005454 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 * For MS-Windows also expand names like "longna~1" to "longname".
5456 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005457#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 return FullName_save(fname, TRUE);
5459#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005460 if (!vim_isAbsName(fname)
5461 || strstr((char *)fname, "..") != NULL
5462 || strstr((char *)fname, "//") != NULL
5463# ifdef BACKSLASH_IN_FILENAME
5464 || strstr((char *)fname, "\\\\") != NULL
5465# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005466# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 )
5470 return FullName_save(fname, FALSE);
5471
5472 fname = vim_strsave(fname);
5473
Bram Moolenaar9b942202007-10-03 12:31:33 +00005474# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005475 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005476 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479 return fname;
5480#endif
5481}
5482
5483/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005484 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5485 * "*ffname" becomes a pointer to allocated memory (or NULL).
5486 * When resolving a link both "*sfname" and "*ffname" will point to the same
5487 * allocated memory.
5488 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005489 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005492fname_expand(
5493 buf_T *buf UNUSED,
5494 char_u **ffname,
5495 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005497 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005499 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005501 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503#ifdef FEAT_SHORTCUT
5504 if (!buf->b_p_bin)
5505 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005506 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005508 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005509 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005510 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
5512 vim_free(*ffname);
5513 *ffname = rfname;
5514 *sfname = rfname;
5515 }
5516 }
5517#endif
5518}
5519
5520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 * Open a window for a number of buffers.
5522 */
5523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005524ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 buf_T *buf;
5527 win_T *wp, *wpnext;
5528 int split_ret = OK;
5529 int p_ea_save;
5530 int open_wins = 0;
5531 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005532 int count; // Maximum number of windows to open.
5533 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005534 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005535 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaarc667da52019-11-30 20:52:27 +01005537 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 count = 9999;
5539 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005540 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5542 all = FALSE;
5543 else
5544 all = TRUE;
5545
Pavel Mayorove1121b12023-02-20 14:35:20 +00005546 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5547 // switching to another buffer.
5548 reset_VIsual_and_resel();
5549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 setpcmark();
5551
5552#ifdef FEAT_GUI
5553 need_mouse_correct = TRUE;
5554#endif
5555
5556 /*
5557 * Close superfluous windows (two windows for the same buffer).
5558 * Also close windows that are not full-width.
5559 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005560 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005561 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005562 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005564 tpnext = curtab->tp_next;
5565 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005567 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005568 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005569 || ((cmdmod.cmod_split & WSP_VERT)
5570 ? wp->w_height + wp->w_status_height < Rows - p_ch
5571 - tabline_height()
5572 : wp->w_width != Columns)
5573 || (had_tab > 0 && wp != firstwin))
5574 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005575 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005576 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005577 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005578 if (win_close(wp, FALSE) == FAIL)
5579 break;
5580 // Just in case an autocommand does something strange with
5581 // windows: start all over...
5582 wpnext = firstwin;
5583 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005584 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005585 }
5586 else
5587 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005589
Bram Moolenaarc667da52019-11-30 20:52:27 +01005590 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005591 if (had_tab == 0 || tpnext == NULL)
5592 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005593 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595
5596 /*
5597 * Go through the buffer list. When a buffer doesn't have a window yet,
5598 * open one. Otherwise move the window to the right position.
5599 * Watch out for autocommands that delete buffers or windows!
5600 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005601 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5606 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005607 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5609 continue;
5610
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005611 if (had_tab != 0)
5612 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005613 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005614 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005615 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005616 else
5617 wp = NULL;
5618 }
5619 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005620 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005621 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005622 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005623 if (wp->w_buffer == buf)
5624 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005625 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005626 if (wp != NULL)
5627 win_move_after(wp, curwin);
5628 }
5629
5630 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005632 bufref_T bufref;
5633
5634 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005635
Bram Moolenaarc667da52019-11-30 20:52:27 +01005636 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005638 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5640 ++open_wins;
5641 p_ea = p_ea_save;
5642 if (split_ret == FAIL)
5643 continue;
5644
Bram Moolenaarc667da52019-11-30 20:52:27 +01005645 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005648 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005650 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 break;
5653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (swap_exists_action == SEA_QUIT)
5655 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005656#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005657 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005658
Bram Moolenaarc667da52019-11-30 20:52:27 +01005659 // Reset the error/interrupt/exception state here so that
5660 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005661 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005662#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005663
Bram Moolenaarc667da52019-11-30 20:52:27 +01005664 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 win_close(curwin, TRUE);
5666 --open_wins;
5667 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005668 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005669
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005670#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005671 // Restore the error/interrupt/exception state if not
5672 // discarded by a new aborting error, interrupt, or uncaught
5673 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005674 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else
5678 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680
5681 ui_breakcheck();
5682 if (got_int)
5683 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005684 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 break;
5686 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005687#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005688 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005689 if (aborting())
5690 break;
5691#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005692 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005693 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005694 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005697 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 /*
5701 * Close superfluous windows.
5702 */
5703 for (wp = lastwin; open_wins > count; )
5704 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005705 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (!win_valid(wp))
5708 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005709 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 wp = lastwin;
5711 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005712 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005714 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 --open_wins;
5716 wp = lastwin;
5717 }
5718 else
5719 {
5720 wp = wp->w_prev;
5721 if (wp == NULL)
5722 break;
5723 }
5724 }
5725}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005728static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730/*
5731 * do_modelines() - process mode lines for the current file
5732 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005733 * "flags" can be:
5734 * OPT_WINONLY only set options local to window
5735 * OPT_NOWIN don't set options local to window
5736 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 * Returns immediately if the "ml" option isn't set.
5738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005742 linenr_T lnum;
5743 int nmlines;
5744 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5747 return;
5748
Bram Moolenaarc667da52019-11-30 20:52:27 +01005749 // Disallow recursive entry here. Can happen when executing a modeline
5750 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (entered)
5752 return;
5753
5754 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005755 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005757 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 nmlines = 0;
5759
Hu Jialun9dcd3492021-08-28 20:42:50 +02005760 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005762 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 nmlines = 0;
5764 --entered;
5765}
5766
Bram Moolenaarc667da52019-11-30 20:52:27 +01005767#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769/*
5770 * chk_modeline() - check a single line for a mode string
5771 * Return FAIL if an error encountered.
5772 */
5773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774chk_modeline(
5775 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005776 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 char_u *s;
John Marriottec032de2025-04-10 21:34:19 +02005779 char_u *line_end; // point to the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 char_u *e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 int prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 int retval = OK;
ichizok7e5fe382023-04-15 13:17:50 +01005783 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 prev = -1;
John Marriottec032de2025-04-10 21:34:19 +02005786 s = ml_get(lnum);
5787 line_end = s + ml_get_len(lnum);
5788 for (; *s != NUL; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 if (prev == -1 || vim_isspace(prev))
5791 {
5792 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5793 || STRNCMP(s, "vi:", (size_t)3) == 0)
5794 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005795 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005796 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
John Marriottec032de2025-04-10 21:34:19 +02005798 int vers;
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5801 e = s + 4;
5802 else
5803 e = s + 3;
5804 vers = getdigits(&e);
5805 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005806 && (s[0] != 'V'
5807 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005809 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 || (VIM_VERSION_100 < vers && s[3] == '<')
5811 || (VIM_VERSION_100 > vers && s[3] == '>')
5812 || (VIM_VERSION_100 == vers && s[3] == '=')))
5813 break;
5814 }
5815 }
5816 prev = *s;
5817 }
5818
5819 if (*s)
5820 {
John Marriottec032de2025-04-10 21:34:19 +02005821 size_t len;
5822 char_u *linecopy; // local copy of any modeline found
5823 int end;
5824
Bram Moolenaarc667da52019-11-30 20:52:27 +01005825 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 ++s;
5827 while (s[-1] != ':');
5828
John Marriottec032de2025-04-10 21:34:19 +02005829 len = (size_t)(line_end - s); // remember the line length
5830 // so we can restore 'line_end'
5831 // after the copy
5832 s = linecopy = vim_strnsave(s, len); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (linecopy == NULL)
5834 return FAIL;
5835
John Marriottec032de2025-04-10 21:34:19 +02005836 line_end = s + len; // restore 'line_end'
5837
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005838 // prepare for emsg()
5839 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005840 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 end = FALSE;
5843 while (end == FALSE)
5844 {
5845 s = skipwhite(s);
5846 if (*s == NUL)
5847 break;
5848
5849 /*
5850 * Find end of set command: ':' or end of line.
5851 * Skip over "\:", replacing it with ":".
5852 */
5853 for (e = s; *e != ':' && *e != NUL; ++e)
5854 if (e[0] == '\\' && e[1] == ':')
John Marriottec032de2025-04-10 21:34:19 +02005855 {
5856 mch_memmove(e, e + 1, (size_t)(line_end - (e + 1)) + 1); // +1 for NUL
5857 --line_end;
5858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (*e == NUL)
5860 end = TRUE;
5861
5862 /*
5863 * If there is a "set" command, require a terminating ':' and
5864 * ignore the stuff after the ':'.
5865 * "vi:set opt opt opt: foo" -- foo not interpreted
5866 * "vi:opt opt opt: foo" -- foo interpreted
5867 * Accept "se" for compatibility with Elvis.
5868 */
5869 if (STRNCMP(s, "set ", (size_t)4) == 0
5870 || STRNCMP(s, "se ", (size_t)3) == 0)
5871 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005872 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 break;
5874 end = TRUE;
John Marriottec032de2025-04-10 21:34:19 +02005875 s += (*(s + 2) == ' ') ? 3 : 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005877 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
Bram Moolenaarc667da52019-11-30 20:52:27 +01005879 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005881 int secure_save = secure;
John Marriottec032de2025-04-10 21:34:19 +02005882 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005883
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005884 current_sctx.sc_version = 1;
5885#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005886 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005887 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005888 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005890
Bram Moolenaar5958f952018-11-20 04:25:21 +01005891 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005892 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005893
Bram Moolenaara3227e22006-03-08 21:32:40 +00005894 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005895
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005896 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005897 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005898 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 break;
5900 }
John Marriottec032de2025-04-10 21:34:19 +02005901 s = (e == line_end) ? e : e + 1; // advance to next part
5902 // careful not to go off the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
5904
ichizok7e5fe382023-04-15 13:17:50 +01005905 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005906 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 vim_free(linecopy);
5908 }
5909 return retval;
5910}
5911
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005912/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005913 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5914 */
5915 int
5916bt_normal(buf_T *buf)
5917{
5918 return buf != NULL && buf->b_p_bt[0] == NUL;
5919}
5920
5921/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005922 * Return TRUE if "buf" is the quickfix buffer.
5923 */
5924 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005925bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005926{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005927#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005928 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005929#else
5930 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005931#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005932}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005933
5934/*
5935 * Return TRUE if "buf" is a terminal buffer.
5936 */
5937 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005938bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005939{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005940#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005941 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005942#else
5943 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005944#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005945}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005946
5947/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005948 * Return TRUE if "buf" is a help buffer.
5949 */
5950 int
5951bt_help(buf_T *buf)
5952{
5953 return buf != NULL && buf->b_help;
5954}
5955
5956/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005957 * Return TRUE if "buf" is a prompt buffer.
5958 */
5959 int
5960bt_prompt(buf_T *buf)
5961{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005962 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5963}
5964
Dominique Pelle748b3082022-01-08 12:41:16 +00005965#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005966/*
5967 * Return TRUE if "buf" is a buffer for a popup window.
5968 */
5969 int
5970bt_popup(buf_T *buf)
5971{
5972 return buf != NULL && buf->b_p_bt != NULL
5973 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005974}
Dominique Pelle748b3082022-01-08 12:41:16 +00005975#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005976
5977/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005978 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005979 * buffer. This means the buffer name may not be a file name, at least not for
5980 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005981 */
5982 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005983bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005984{
5985 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5986 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005987 || buf->b_p_bt[0] == 't'
5988 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005989}
5990
Bram Moolenaarc3126192022-08-26 12:58:17 +01005991/*
5992 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5993 * buffer. This means the buffer is not to be read from a file.
5994 */
5995 static int
5996bt_nofileread(buf_T *buf)
5997{
5998 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5999 || buf->b_p_bt[0] == 't'
6000 || buf->b_p_bt[0] == 'q'
6001 || buf->b_p_bt[0] == 'p');
6002}
6003
Dominique Pelle748b3082022-01-08 12:41:16 +00006004#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006005/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02006006 * Return TRUE if "buf" has 'buftype' set to "nofile".
6007 */
6008 int
6009bt_nofile(buf_T *buf)
6010{
6011 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
6012}
Dominique Pelle748b3082022-01-08 12:41:16 +00006013#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02006014
6015/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006016 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
6017 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006018 */
6019 int
6020bt_dontwrite(buf_T *buf)
6021{
Bram Moolenaarf2732452018-06-03 14:47:35 +02006022 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01006023 || buf->b_p_bt[0] == 't'
6024 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006025}
6026
6027 int
6028bt_dontwrite_msg(buf_T *buf)
6029{
6030 if (bt_dontwrite(buf))
6031 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00006032 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006033 return TRUE;
6034 }
6035 return FALSE;
6036}
6037
6038/*
6039 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
6040 * and 'bufhidden'.
6041 */
6042 int
6043buf_hide(buf_T *buf)
6044{
Bram Moolenaarc667da52019-11-30 20:52:27 +01006045 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006046 switch (buf->b_p_bh[0])
6047 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006048 case 'u': // "unload"
6049 case 'w': // "wipe"
6050 case 'd': return FALSE; // "delete"
6051 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006052 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006053 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056/*
6057 * Return special buffer name.
6058 * Returns NULL when the buffer has a normal file name.
6059 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006060 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
Bram Moolenaar4033c552017-09-16 20:54:51 +02006063#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006065 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006066 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006067 * Differentiate between the quickfix and location list buffers using
6068 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006069 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006070 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006071 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006072 else
6073 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02006076
Bram Moolenaarc667da52019-11-30 20:52:27 +01006077 // There is no _file_ when 'buftype' is "nofile", b_sfname
6078 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02006079 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
Bram Moolenaar21554412017-07-24 21:44:43 +02006081#ifdef FEAT_TERMINAL
6082 if (buf->b_term != NULL)
6083 return term_get_status_text(buf->b_term);
6084#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02006085 if (buf->b_fname != NULL)
6086 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01006087 if (buf == cmdwin_buf)
6088 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02006089#ifdef FEAT_JOB_CHANNEL
6090 if (bt_prompt(buf))
6091 return (char_u *)_("[Prompt]");
6092#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006093#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006094 if (bt_popup(buf))
6095 return (char_u *)_("[Popup]");
6096#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006097 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006101 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 return NULL;
6103}
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006106 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6107 */
6108 char_u *
6109buf_get_fname(buf_T *buf)
6110{
6111 if (buf->b_fname == NULL)
6112 return (char_u *)_("[No Name]");
6113 return buf->b_fname;
6114}
6115
6116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6118 */
6119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006122 if (on == curbuf->b_p_bl)
6123 return;
6124
6125 curbuf->b_p_bl = on;
6126 if (on)
6127 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6128 else
6129 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130}
6131
6132/*
6133 * Read the file for "buf" again and check if the contents changed.
6134 * Return TRUE if it changed or this could not be checked.
6135 */
6136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006137buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 buf_T *newbuf;
6140 int differ = TRUE;
6141 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 exarg_T ea;
6144
Bram Moolenaarc667da52019-11-30 20:52:27 +01006145 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6147 if (newbuf == NULL)
6148 return TRUE;
6149
Bram Moolenaarc667da52019-11-30 20:52:27 +01006150 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (prep_exarg(&ea, buf) == FAIL)
6152 {
6153 wipe_buffer(newbuf, FALSE);
6154 return TRUE;
6155 }
6156
Bram Moolenaare76062c2022-11-28 18:51:43 +00006157 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006159 if (curbuf != newbuf)
6160 {
6161 // Failed to find a window for "newbuf".
6162 wipe_buffer(newbuf, FALSE);
6163 return TRUE;
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006166 // We don't want to trigger autocommands now, they may have nasty
6167 // side-effects like wiping buffers
6168 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006169 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 && readfile(buf->b_ffname, buf->b_fname,
6171 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6172 &ea, READ_NEW | READ_DUMMY) == OK)
6173 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006174 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6176 {
6177 differ = FALSE;
6178 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6179 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6180 {
6181 differ = TRUE;
6182 break;
6183 }
6184 }
6185 }
6186 vim_free(ea.cmd);
6187
Bram Moolenaarc667da52019-11-30 20:52:27 +01006188 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaarc667da52019-11-30 20:52:27 +01006191 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 wipe_buffer(newbuf, FALSE);
6193
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006194 unblock_autocmds();
6195
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 return differ;
6197}
6198
6199/*
6200 * Wipe out a buffer and decrement the last buffer number if it was used for
6201 * this buffer. Call this to wipe out a temp buffer that does not contain any
6202 * marks.
6203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006205wipe_buffer(
6206 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006207 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 if (buf->b_fnum == top_file_num - 1)
6210 --top_file_num;
6211
Bram Moolenaarc667da52019-11-30 20:52:27 +01006212 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006213 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006214
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006215 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006218 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219}