blob: fe19269a492c3d25d0af1ffd00f731abcda4ca8f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
shadmansaleh30e3de22021-05-15 17:23:28 +020030
31#ifdef FEAT_EVAL
32// Determines how deeply nested %{} blocks will be evaluated in statusline.
33# define MAX_STL_EVAL_DEPTH 100
34#endif
35
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020036static void enter_buffer(buf_T *buf);
37static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020041static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
42static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
43static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +020047static int value_changed(char_u *str, char_u **last);
John Marriottec032de2025-04-10 21:34:19 +020048static int append_arg_number(win_T *wp, char_u *buf, size_t buflen, int add_file);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static void free_buffer(buf_T *);
50static void free_buffer_stuff(buf_T *buf, int free_options);
Bram Moolenaarc3126192022-08-26 12:58:17 +010051static int bt_nofileread(buf_T *buf);
Yee Cheng Chin15b314f2022-10-09 18:53:32 +010052static void no_write_message_buf(buf_T *buf);
LemonBoy893eeeb2024-07-10 20:20:48 +020053static int do_buffer_ext(int action, int start, int dir, int count, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55#ifdef UNIX
56# define dev_T dev_t
57#else
58# define dev_T unsigned
59#endif
60
Bram Moolenaar00d253e2020-04-06 22:13:01 +020061#define FOR_ALL_BUFS_FROM_LAST(buf) \
62 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
63
Bram Moolenaar4033c552017-09-16 20:54:51 +020064#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020065static char *msg_loclist = N_("[Location List]");
66static char *msg_qflist = N_("[Quickfix List]");
67#endif
68
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020069// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070static int buf_free_count = 0;
71
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020072static int top_file_num = 1; // highest file number
73static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
74
Christian Brabandtbaa8c902025-04-19 11:14:11 +020075 static void
76trigger_undo_ftplugin(buf_T *buf, win_T *win)
77{
78 window_layout_lock();
79 buf->b_locked++;
80 win->w_locked = TRUE;
81 // b:undo_ftplugin may be set, undo it
82 do_cmdline_cmd((char_u*)"if exists('b:undo_ftplugin') | :legacy :exe \
83 b:undo_ftplugin | endif");
84 buf->b_locked--;
85 win->w_locked = FALSE;
86 window_layout_unlock();
87}
88
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020089/*
John Marriottec032de2025-04-10 21:34:19 +020090 * Calculate the percentage that `part` is of the `whole`.
91 */
92 static int
93calc_percentage(long part, long whole)
94{
95 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
96 // causes an overflow, thus for large numbers divide instead.
97 return (part > 1000000L)
98 ? (int)(part / (whole / 100L))
99 : (int)((part * 100L) / whole);
100}
101
102/*
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200103 * Return the highest possible buffer number.
104 */
105 int
106get_highest_fnum(void)
107{
108 return top_file_num - 1;
109}
110
Bram Moolenaarc024b462019-06-08 18:07:21 +0200111/*
112 * Read data from buffer for retrying.
113 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 static int
115read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100116 int read_stdin, // read file from stdin, otherwise fifo
117 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
118 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200119{
120 int retval = OK;
121 linenr_T line_count;
122
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100123 // Read from the buffer which the text is already filled in and append at
124 // the end. This makes it possible to retry when 'fileformat' or
125 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 line_count = curbuf->b_ml.ml_line_count;
127 retval = readfile(
128 read_stdin ? NULL : curbuf->b_ffname,
129 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100130 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200131 flags | READ_BUFFER);
132 if (retval == OK)
133 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100134 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200135 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200136 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137 }
138 else
139 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100140 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200141 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200142 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200143 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100144 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200145 curwin->w_cursor.lnum = 1;
146 curwin->w_cursor.col = 0;
147
148 if (read_stdin)
149 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100150 // Set or reset 'modified' before executing autocommands, so that
151 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100152 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200153 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100154 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200155 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200156
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100157 if (retval == OK)
158 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100159#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100160 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100161 curbuf, &retval);
162#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100163 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200164#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100165 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200166 }
167 return retval;
168}
169
Dominique Pelle748b3082022-01-08 12:41:16 +0000170#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200172 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
173 */
174 void
175buffer_ensure_loaded(buf_T *buf)
176{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000177 if (buf->b_ml.ml_mfp != NULL)
178 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200179
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000180 aco_save_T aco;
181
182 // Make sure the buffer is in a window. If not then skip it.
183 aucmd_prepbuf(&aco, buf);
184 if (curbuf == buf)
185 {
186 if (swap_exists_action != SEA_READONLY)
187 swap_exists_action = SEA_NONE;
188 open_buffer(FALSE, NULL, 0);
189 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200190 }
191}
Dominique Pelle748b3082022-01-08 12:41:16 +0000192#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200193
194/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200195 * Open current buffer, that is: open the memfile and read the file into
196 * memory.
197 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100200open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100201 int read_stdin, // read file from stdin
202 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100203 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100205 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200207 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100208#ifdef FEAT_SYN_HL
209 long old_tw = curbuf->b_p_tw;
210#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200211 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100213 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
214 // When re-entering the same buffer, it should not change, because the
215 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (readonlymode && curbuf->b_ffname != NULL
217 && (curbuf->b_flags & BF_NEVERLOADED))
218 curbuf->b_p_ro = TRUE;
219
Bram Moolenaar4770d092006-01-12 23:22:24 +0000220 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100222 // There MUST be a memfile, otherwise we can't do anything
223 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100224 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200225 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (curbuf->b_ml.ml_mfp != NULL)
227 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100228 // If there is no memfile at all, exit.
229 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (curbuf == NULL)
231 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000232 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200233
234 // Don't try to do any saving, with "curbuf" NULL almost nothing
235 // will work.
236 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 getout(2);
238 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200239
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000240 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100242#ifdef FEAT_SYN_HL
243 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +0200244 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 return FAIL;
247 }
248
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100249 // Do not sync this buffer yet, may first want to read the file.
250 if (curbuf->b_ml.ml_mfp != NULL)
251 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
252
Bram Moolenaarc667da52019-11-30 20:52:27 +0100253 // The autocommands in readfile() may change the buffer, but only AFTER
254 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200255 set_bufref(&old_curbuf, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +0200256 curbuf->b_modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
Bram Moolenaarc667da52019-11-30 20:52:27 +0100258 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100259 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100261 // A buffer without an actual file should not use the buffer name to read a
262 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100263 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100264 flags |= READ_NOFILE;
265
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100266 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (curbuf->b_ffname != NULL
268#ifdef FEAT_NETBEANS_INTG
269 && netbeansReadFile
270#endif
271 )
272 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100273 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200274#ifdef UNIX
275 int save_bin = curbuf->b_p_bin;
276 int perm;
277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifdef FEAT_NETBEANS_INTG
279 int oldFire = netbeansFireChanges;
280
281 netbeansFireChanges = 0;
282#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200283#ifdef UNIX
284 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200285 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200286 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200287# ifdef OPEN_CHR_FILES
288 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
289# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200290 ))
291 read_fifo = TRUE;
292 if (read_fifo)
293 curbuf->b_p_bin = TRUE;
294#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100295 if (shortmess(SHM_FILEINFO))
296 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200298 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200299 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
300#ifdef UNIX
301 if (read_fifo)
302 {
303 curbuf->b_p_bin = save_bin;
304 if (retval == OK)
Christian Brabandtf1c31342025-02-23 09:36:56 +0100305 // don't add READ_FIFO here, otherwise we won't be able to
306 // detect the encoding
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200307 retval = read_buffer(FALSE, eap, flags);
308 }
309#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100310 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_NETBEANS_INTG
312 netbeansFireChanges = oldFire;
313#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100314 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200315 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 fix_help_buffer();
317 }
318 else if (read_stdin)
319 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200320 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100322 // First read the text in binary mode into the buffer.
323 // Then read from that same buffer and append at the end. This makes
324 // it possible to retry when 'fileformat' or 'fileencoding' was
325 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 curbuf->b_p_bin = TRUE;
327 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200328 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
329 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 curbuf->b_p_bin = save_bin;
331 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200332 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
334
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100335 // Can now sync this buffer in ml_sync_all().
336 if (curbuf->b_ml.ml_mfp != NULL
337 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
338 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
339
Bram Moolenaarc667da52019-11-30 20:52:27 +0100340 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100344 parse_cino(curbuf);
345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100347 // Set/reset the Changed flag first, autocmds may change the buffer.
348 // Apply the automatic commands, before processing the modelines.
349 // So the modelines have priority over autocommands.
350 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100351 // When reading stdin, the buffer contents always needs writing, so set
352 // the changed flag. Unless in readonly mode: "ls | gview -".
353 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000354 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200355 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100356#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000359 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100361 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200362 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100363 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
Bram Moolenaarc667da52019-11-30 20:52:27 +0100365 // Set last_changedtick to avoid triggering a TextChanged autocommand right
366 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100367 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100368 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100369 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100370
Bram Moolenaarc667da52019-11-30 20:52:27 +0100371 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#ifdef FEAT_EVAL
373 if (aborting())
374#else
375 if (got_int)
376#endif
377 curbuf->b_flags |= BF_READERR;
378
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000379#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100380 // Need to update automatic folding. Do this before the autocommands,
381 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000382 foldUpdateAll(curwin);
383#endif
384
Bram Moolenaarc667da52019-11-30 20:52:27 +0100385 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (!(curwin->w_valid & VALID_TOPLINE))
387 {
388 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100389#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100393#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100395#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#endif
398
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000399 if (retval != OK)
400 return retval;
401
402 // The autocommands may have changed the current buffer. Apply the
403 // modelines to the correct buffer, if it still exists and is loaded.
404 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000406 aco_save_T aco;
407
408 // Go to the buffer that was opened, make sure it is in a window.
409 // If not then skip it.
410 aucmd_prepbuf(&aco, old_curbuf.br_buf);
411 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000413 do_modelines(0);
414 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000416 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100417#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000418 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
419 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100420#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000421 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
422 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000425 // restore curwin/curbuf and a few other things
426 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 return retval;
431}
432
433/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200434 * Store "buf" in "bufref" and set the free count.
435 */
436 void
437set_bufref(bufref_T *bufref, buf_T *buf)
438{
439 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200440 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200441 bufref->br_buf_free_count = buf_free_count;
442}
443
444/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200445 * Return TRUE if "bufref->br_buf" points to the same buffer as when
446 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200447 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200448 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
449 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200450 */
451 int
452bufref_valid(bufref_T *bufref)
453{
454 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200455 ? TRUE : buf_valid(bufref->br_buf)
456 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200457}
458
459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200461 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 */
463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100464buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 buf_T *bp;
467
Bram Moolenaarc667da52019-11-30 20:52:27 +0100468 // Assume that we more often have a recent buffer, start with the last
469 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200470 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (bp == buf)
472 return TRUE;
473 return FALSE;
474}
475
476/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200477 * A hash table used to quickly lookup a buffer by its number.
478 */
479static hashtab_T buf_hashtab;
480
481 static void
482buf_hashtab_add(buf_T *buf)
483{
John Marriottec032de2025-04-10 21:34:19 +0200484 vim_snprintf((char *)buf->b_key, sizeof(buf->b_key), "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000485 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000486 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200487}
488
489 static void
490buf_hashtab_remove(buf_T *buf)
491{
492 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
493
494 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000495 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200496}
497
Bram Moolenaar94f01952018-09-01 15:30:03 +0200498/*
499 * Return TRUE when buffer "buf" can be unloaded.
500 * Give an error message and return FALSE when the buffer is locked or the
501 * screen is being redrawn and the buffer is in a window.
502 */
503 static int
504can_unload_buffer(buf_T *buf)
505{
506 int can_unload = !buf->b_locked;
507
508 if (can_unload && updating_screen)
509 {
510 win_T *wp;
511
512 FOR_ALL_WINDOWS(wp)
513 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200514 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200515 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200516 break;
517 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200518 }
519 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100520 {
521 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
522
523 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
524 fname != NULL ? fname : (char_u *)"[No Name]");
525 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200526 return can_unload;
527}
Bram Moolenaara997b452018-04-17 23:24:06 +0200528
Bram Moolenaar480778b2016-07-14 22:09:39 +0200529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Close the link to a buffer.
531 * "action" is used when there is no longer a window for the buffer.
532 * It can be:
533 * 0 buffer becomes hidden
534 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100535 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200537 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 * When doing all but the first one on the current buffer, the caller should
539 * get a new buffer very soon!
540 *
541 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100542 *
543 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
544 * cause there to be only one window with this buffer. e.g. when ":quit" is
545 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100546 *
547 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100548 *
549 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100552close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100553 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554 buf_T *buf,
555 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100556 int abort_if_last,
557 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100560 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200561 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100562 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200563 win_T *the_curwin = curwin;
564 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200566 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
567 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaarcee52202020-03-11 14:19:58 +0100569 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100570
571 // Force unloading or deleting when 'bufhidden' says so.
572 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
573 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100574 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200575 {
576 del_buf = TRUE;
577 unload_buf = TRUE;
578 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100579 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200580 {
581 del_buf = TRUE;
582 unload_buf = TRUE;
583 wipe_buf = TRUE;
584 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100585 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200586 unload_buf = TRUE;
587
Bram Moolenaar94053a52017-08-01 21:44:33 +0200588#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100589 // depending on how we get here b_nwindows may already be zero
590 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200591 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100592 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200593 if (term_job_running(buf->b_term))
594 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200595 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200596 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200597 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100598 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200599
Bram Moolenaarc667da52019-11-30 20:52:27 +0100600 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200601 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100602
603 // A terminal buffer is wiped out when job has finished.
604 del_buf = TRUE;
605 unload_buf = TRUE;
606 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200607 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200608 else
609 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100610 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200611 del_buf = FALSE;
612 unload_buf = FALSE;
613 }
614 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100615 else if (buf->b_p_bh[0] == 'h' && !del_buf)
616 {
617 // Hide a terminal buffer.
618 unload_buf = FALSE;
619 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200620 else
621 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100622 if (del_buf || unload_buf)
623 {
624 // A terminal buffer is wiped out if the job has finished.
625 // We only do this when there's an intention to unload the
626 // buffer. This way, :hide and other similar commands won't
627 // wipe the buffer.
628 del_buf = TRUE;
629 unload_buf = TRUE;
630 wipe_buf = TRUE;
631 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200632 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100633 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200634 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // Disallow deleting the buffer when it is locked (already being closed or
638 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200639 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100640 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641
Bram Moolenaarc667da52019-11-30 20:52:27 +0100642 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200643 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100645 // Set b_last_cursor when closing the last window for the buffer.
646 // Remember the last cursor position and window options of the buffer.
647 // This used to be only for the current window, but then options like
648 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (buf->b_nwindows == 1)
650 set_last_cursor(win);
651 buflist_setfpos(buf, win,
652 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
653 win->w_cursor.col, TRUE);
654 }
655
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200656 set_bufref(&bufref, buf);
657
Bram Moolenaarc667da52019-11-30 20:52:27 +0100658 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (buf->b_nwindows == 1)
660 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200661 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100662 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200663 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
664 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200665 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100666 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200668aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000669 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100670 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100671 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200672 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100673 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200674 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100675 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200676 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
Bram Moolenaarc667da52019-11-30 20:52:27 +0100678 // When the buffer becomes hidden, but is not unloaded, trigger
679 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (!unload_buf)
681 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200682 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100683 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200684 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
685 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200686 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100687 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200688 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200689 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100690 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200691 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100692 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200693 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100695#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100696 // autocmds may abort script processing
697 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100698 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200701
Bram Moolenaarc667da52019-11-30 20:52:27 +0100702 // If the buffer was in curwin and the window has changed, go back to that
703 // window, if it still exists. This avoids that ":edit x" triggering a
704 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200705 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
706 {
707 block_autocmds();
708 goto_tabpage_win(the_curtab, the_curwin);
709 unblock_autocmds();
710 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaarc667da52019-11-30 20:52:27 +0100714 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (buf->b_nwindows > 0)
716 --buf->b_nwindows;
717
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100718#ifdef FEAT_DIFF
719 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100720 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100721#endif
722
Bram Moolenaarc667da52019-11-30 20:52:27 +0100723 // Return when a window is displaying the buffer or when it's not
724 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100726 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaarc667da52019-11-30 20:52:27 +0100728 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (buf->b_ffname == NULL)
730 del_buf = TRUE;
731
Bram Moolenaarc667da52019-11-30 20:52:27 +0100732 // When closing the current buffer stop Visual mode before freeing
733 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200734 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200735#if defined(EXITFREE)
736 && !entered_free_all_mem
737#endif
738 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200739 end_visual_mode();
740
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100741 // Free all things allocated for this buffer.
742 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
743 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100744 // Remember if we are closing the current buffer. Restore the number of
745 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 is_curbuf = (buf == curbuf);
747 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100749 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100750 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100751 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaarc667da52019-11-30 20:52:27 +0100753 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200754 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100755 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100756#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100757 // autocmds may abort script processing
758 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100759 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100762 // It's possible that autocommands change curbuf to the one being deleted.
763 // This might cause the previous curbuf to be deleted unexpectedly. But
764 // in some cases it's OK to delete the curbuf, because a new one is
765 // obtained anyway. Therefore only return if curbuf changed to the
766 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100768 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200769
Bram Moolenaar4033c552017-09-16 20:54:51 +0200770 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100771 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200772
Bram Moolenaarc667da52019-11-30 20:52:27 +0100773 // Autocommands may have opened or closed windows for this buffer.
774 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200775 if (buf->b_nwindows > 0)
776 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 /*
779 * Remove the buffer from the list.
780 */
781 if (wipe_buf)
782 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200783 tabpage_T *tp;
784 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200785
Bram Moolenaar347538f2022-03-26 16:28:06 +0000786 // Do not wipe out the buffer if it is used in a window.
787 if (buf->b_nwindows > 0)
788 return FALSE;
789
zeertzjq2e7d89b2024-07-10 19:36:36 +0200790 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200791 mark_forget_file(wp, buf->b_fnum);
792
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200793 if (action == DOBUF_WIPE_REUSE)
794 {
795 // we can re-use this buffer number, store it
796 if (buf_reuse.ga_itemsize == 0)
797 ga_init2(&buf_reuse, sizeof(int), 50);
798 if (ga_grow(&buf_reuse, 1) == OK)
799 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
800 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200801 if (buf->b_sfname != buf->b_ffname)
802 VIM_CLEAR(buf->b_sfname);
803 else
804 buf->b_sfname = NULL;
805 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (buf->b_prev == NULL)
807 firstbuf = buf->b_next;
808 else
809 buf->b_prev->b_next = buf->b_next;
810 if (buf->b_next == NULL)
811 lastbuf = buf->b_prev;
812 else
813 buf->b_next->b_prev = buf->b_prev;
814 free_buffer(buf);
815 }
816 else
817 {
818 if (del_buf)
819 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100820 // Free all internal variables and reset option values, to make
821 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 free_buffer_stuff(buf, TRUE);
823
Bram Moolenaarc667da52019-11-30 20:52:27 +0100824 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
826
Bram Moolenaarc667da52019-11-30 20:52:27 +0100827 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 buf->b_p_initialized = FALSE;
829 }
830 buf_clear_file(buf);
831 if (del_buf)
832 buf->b_p_bl = FALSE;
833 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100834 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100835 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836}
837
838/*
839 * Make buffer not contain a file.
840 */
841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100842buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200845 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100847 buf->b_p_eof = FALSE;
848 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 buf->b_p_eol = TRUE;
850 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000852 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100854 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#ifdef FEAT_NETBEANS_INTG
856 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858}
859
860/*
861 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200862 * the file. Careful: get here with "curwin" NULL when exiting.
863 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100864 * BFA_DEL buffer is going to be deleted
865 * BFA_WIPE buffer is going to be wiped out
866 * BFA_KEEP_UNDO do not free undo information
867 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100870buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200873 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200874 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200875 win_T *the_curwin = curwin;
876 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
Bram Moolenaarc667da52019-11-30 20:52:27 +0100878 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200879 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100880 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200881 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200882 if (buf->b_ml.ml_mfp != NULL)
883 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200884 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
885 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200886 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200888 return;
889 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200890 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200892 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
893 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200894 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100895 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return;
897 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200898 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200900 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
901 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200902 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100903 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 return;
905 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200906 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100907 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200908
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 // If the buffer was in curwin and the window has changed, go back to that
910 // window, if it still exists. This avoids that ":edit x" triggering a
911 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Sean Dewar31be82e2025-05-15 19:59:37 +0200912 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
Bram Moolenaar5a497892016-09-03 16:29:04 +0200913 {
914 block_autocmds();
915 goto_tabpage_win(the_curtab, the_curwin);
916 unblock_autocmds();
917 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200918
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100919#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100920 // autocmds may abort script processing
921 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100925 // It's possible that autocommands change curbuf to the one being deleted.
926 // This might cause curbuf to be deleted unexpectedly. But in some cases
927 // it's OK to delete the curbuf, because a new one is obtained anyway.
928 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (buf == curbuf && !is_curbuf)
930 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100932 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200934#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100935 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200936 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100937 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200938#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000939
940#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100941 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000942 {
943 win_T *win;
944 tabpage_T *tp;
945
946 FOR_ALL_TAB_WINDOWS(tp, win)
947 if (win->w_buffer == buf)
948 clearFolding(win);
949 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000950#endif
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#ifdef FEAT_TCL
953 tcl_buffer_free(buf);
954#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100955 ml_close(buf, TRUE); // close and delete the memline/memfile
956 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200957 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100958 // free the memory allocated for undo
959 // and reset all undo information
960 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100962 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100964#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100965 clear_buf_prop_types(buf);
966#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100967 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Free a buffer structure and the things it contains related to the buffer
972 * itself (not the file, that must have been done already).
973 */
974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100975free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200977 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200979#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100980 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000981 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200982 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200983 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200984#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200985#ifdef FEAT_LUA
986 lua_buffer_free(buf);
987#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000988#ifdef FEAT_MZSCHEME
989 mzscheme_buffer_free(buf);
990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_PERL
992 perl_buf_free(buf);
993#endif
994#ifdef FEAT_PYTHON
995 python_buffer_free(buf);
996#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997#ifdef FEAT_PYTHON3
998 python3_buffer_free(buf);
999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef FEAT_RUBY
1001 ruby_buffer_free(buf);
1002#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001003#ifdef FEAT_JOB_CHANNEL
1004 channel_buffer_free(buf);
1005#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001006#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001007 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001008#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001009#ifdef FEAT_JOB_CHANNEL
1010 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001011 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +02001012 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +02001013#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +02001014
1015 buf_hashtab_remove(buf);
1016
Bram Moolenaar9280e3f2016-07-14 23:03:19 +02001017 aubuflocal_remove(buf);
1018
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001019 if (autocmd_busy)
1020 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001021 // Do not free the buffer structure while autocommands are executing,
1022 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001023 buf->b_next = au_pending_free_buf;
1024 au_pending_free_buf = buf;
1025 }
1026 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001027 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001028 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001029 if (curbuf == buf)
1030 curbuf = NULL; // make clear it's not to be used
1031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001035 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001036 */
1037 static void
1038init_changedtick(buf_T *buf)
1039{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001040 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001041
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001042 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1043 di->di_tv.v_type = VAR_NUMBER;
1044 di->di_tv.v_lock = VAR_FIXED;
1045 di->di_tv.vval.v_number = 0;
1046
Bram Moolenaar92769c32017-02-25 15:41:37 +01001047#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001048 STRCPY(buf->b_ct_di.di_key, "changedtick");
1049 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001050#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001051}
1052
1053/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001054 * Free the b_wininfo list for buffer "buf".
1055 */
1056 static void
1057clear_wininfo(buf_T *buf)
1058{
1059 wininfo_T *wip;
1060
1061 while (buf->b_wininfo != NULL)
1062 {
1063 wip = buf->b_wininfo;
1064 buf->b_wininfo = wip->wi_next;
1065 free_wininfo(wip);
1066 }
1067}
1068
1069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1071 */
1072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073free_buffer_stuff(
1074 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001075 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 if (free_options)
1078 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001079 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001081#ifdef FEAT_SPELL
1082 ga_clear(&buf->b_s.b_langp);
1083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001086 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001087 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001088
Bram Moolenaarc667da52019-11-30 20:52:27 +01001089 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001090 hash_init(&buf->b_vars->dv_hashtab);
1091 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001092 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001093 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001096 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001098 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001100#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001101 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001102#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001103#ifdef FEAT_PROP_POPUP
1104 ga_clear_strings(&buf->b_textprop_text);
1105#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001106 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1107 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001108 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109}
1110
1111/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001112 * Free one wininfo_T.
1113 */
1114 void
1115free_wininfo(wininfo_T *wip)
1116{
1117 if (wip->wi_optset)
1118 {
1119 clear_winopt(&wip->wi_opt);
1120#ifdef FEAT_FOLDING
1121 deleteFoldRecurse(&wip->wi_folds);
1122#endif
1123 }
1124 vim_free(wip);
1125}
1126
1127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * Go to another buffer. Handles the result of the ATTENTION dialog.
1129 */
1130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001131goto_buffer(
1132 exarg_T *eap,
1133 int start,
1134 int dir,
1135 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001137 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001138 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001139 int skip_help_buf;
1140
1141 switch (eap->cmdidx)
1142 {
1143 case CMD_bnext:
1144 case CMD_sbnext:
1145 case CMD_bNext:
1146 case CMD_bprevious:
1147 case CMD_sbNext:
1148 case CMD_sbprevious:
1149 skip_help_buf = TRUE;
1150 break;
1151 default:
1152 skip_help_buf = FALSE;
1153 break;
1154 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001155
1156 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar188639d2022-04-04 16:57:21 +01001158 if (swap_exists_action == SEA_NONE)
1159 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001160 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1161 (eap->forceit ? DOBUF_FORCEIT : 0) |
1162 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1164 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001165#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001166 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001167
Bram Moolenaarc667da52019-11-30 20:52:27 +01001168 // Reset the error/interrupt/exception state here so that
1169 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001170 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001171#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001172
Bram Moolenaarc667da52019-11-30 20:52:27 +01001173 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001175 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001176 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001177
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001178#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001179 // Restore the error/interrupt/exception state if not discarded by a
1180 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001181 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001185 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188/*
1189 * Handle the situation of swap_exists_action being set.
1190 * It is allowed for "old_curbuf" to be NULL or invalid.
1191 */
1192 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001193handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001195#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001196 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001197#endif
1198#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001199 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001200#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001201 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 if (swap_exists_action == SEA_QUIT)
1204 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001205#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001206 // Reset the error/interrupt/exception state here so that
1207 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001208 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001209#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001210
Bram Moolenaarc667da52019-11-30 20:52:27 +01001211 // User selected Quit at ATTENTION prompt. Go back to previous
1212 // buffer. If that buffer is gone or the same as the current one,
1213 // open a new, empty buffer.
1214 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001215 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001216 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001217 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1218 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001219 {
1220 // Block autocommands here because curwin->w_buffer is NULL.
1221 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001222 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001223 unblock_autocmds();
1224 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001225 else
1226 buf = old_curbuf->br_buf;
1227 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001228 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001229 int old_msg_silent = msg_silent;
1230
1231 if (shortmess(SHM_FILEINFO))
1232 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001233 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001234 // restore msg_silent, so that the command line will be shown
1235 msg_silent = old_msg_silent;
1236
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001237#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001238 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001239 check_colorcolumn(NULL, curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001240#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001241 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001242 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001243
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001244#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001245 // Restore the error/interrupt/exception state if not discarded by a
1246 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001247 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 }
1250 else if (swap_exists_action == SEA_RECOVER)
1251 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001252#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001253 // Reset the error/interrupt/exception state here so that
1254 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001255 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001256#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001257
Bram Moolenaarc667da52019-11-30 20:52:27 +01001258 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001260 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001261 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001263 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001264
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001265#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001266 // Restore the error/interrupt/exception state if not discarded by a
1267 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001268 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 swap_exists_action = SEA_NONE;
1272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275 * Make the current buffer empty.
1276 * Used when it is wiped out and it's the last buffer.
1277 */
1278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279empty_curbuf(
1280 int close_others,
1281 int forceit,
1282 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001283{
1284 int retval;
1285 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001286 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001287
1288 if (action == DOBUF_UNLOAD)
1289 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001290 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001291 return FAIL;
1292 }
1293
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001294 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001295 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001296 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001297 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001298
1299 setpcmark();
1300 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1301 forceit ? ECMD_FORCEIT : 0, curwin);
1302
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001303 // do_ecmd() may create a new buffer, then we have to delete
1304 // the old one. But do_ecmd() may have done that already, check
1305 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001306 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001307 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001308 if (!close_others)
1309 need_fileinfo = FALSE;
1310 return retval;
1311}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
1314 * Implementation of the commands for the buffer list.
1315 *
1316 * action == DOBUF_GOTO go to specified buffer
1317 * action == DOBUF_SPLIT split window and go to specified buffer
1318 * action == DOBUF_UNLOAD unload specified buffer(s)
1319 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1320 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001321 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 *
1323 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1324 * start == DOBUF_FIRST go to "count" buffer from first buffer
1325 * start == DOBUF_LAST go to "count" buffer from last buffer
1326 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1327 *
1328 * Return FAIL or OK.
1329 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001330 static int
1331do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332 int action,
1333 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001334 int dir, // FORWARD or BACKWARD
1335 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001336 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 buf_T *buf;
1339 buf_T *bp;
1340 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001341 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 switch (start)
1344 {
1345 case DOBUF_FIRST: buf = firstbuf; break;
1346 case DOBUF_LAST: buf = lastbuf; break;
1347 default: buf = curbuf; break;
1348 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001349 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 while (count-- > 0)
1352 {
1353 do
1354 {
1355 buf = buf->b_next;
1356 if (buf == NULL)
1357 buf = firstbuf;
1358 }
1359 while (buf != curbuf && !bufIsChanged(buf));
1360 }
1361 if (!bufIsChanged(buf))
1362 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001363 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 return FAIL;
1365 }
1366 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001367 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369 while (buf != NULL && buf->b_fnum != count)
1370 buf = buf->b_next;
1371 }
1372 else
1373 {
1374 bp = NULL;
1375 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1376 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001377 // remember the buffer where we start, we come back there when all
1378 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (bp == NULL)
1380 bp = buf;
1381 if (dir == FORWARD)
1382 {
1383 buf = buf->b_next;
1384 if (buf == NULL)
1385 buf = firstbuf;
1386 }
1387 else
1388 {
1389 buf = buf->b_prev;
1390 if (buf == NULL)
1391 buf = lastbuf;
1392 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001393 // Don't count unlisted buffers.
1394 // Avoid non-help buffers if the starting point was a non-help buffer and
1395 // vice-versa.
1396 if (unload || (buf->b_p_bl
1397 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
1399 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001400 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 }
1402 if (bp == buf)
1403 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001404 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001405 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 return FAIL;
1407 }
1408 }
1409 }
1410
Bram Moolenaarc667da52019-11-30 20:52:27 +01001411 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 if (start == DOBUF_FIRST)
1414 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001415 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001417 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001420 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001422 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 return FAIL;
1424 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001425#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001426 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001427 return OK;
1428#endif
Sean Dewar6cb1c822025-05-03 18:37:27 +02001429 if (action == DOBUF_GOTO && buf != curbuf)
1430 {
1431 if (!check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) != 0))
1432 // disallow navigating to another buffer when 'winfixbuf' is applied
1433 return FAIL;
1434 if (buf->b_locked_split)
1435 {
1436 // disallow navigating to a closing buffer, which like splitting,
1437 // can result in more windows displaying it
1438 emsg(_(e_cannot_switch_to_a_closing_buffer));
1439 return FAIL;
1440 }
1441 }
Colin Kennedy21570352024-03-03 16:16:47 +01001442
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001443 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1444 && (buf->b_flags & BF_DUMMY))
1445 {
1446 // disallow navigating to the dummy buffer
1447 semsg(_(e_buffer_nr_does_not_exist), count);
1448 return FAIL;
1449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451#ifdef FEAT_GUI
1452 need_mouse_correct = TRUE;
1453#endif
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001456 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 */
1458 if (unload)
1459 {
1460 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001461 bufref_T bufref;
1462
Bram Moolenaar94f01952018-09-01 15:30:03 +02001463 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001464 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001465
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001466 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaarc667da52019-11-30 20:52:27 +01001468 // When unloading or deleting a buffer that's already unloaded and
1469 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001470 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1471 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 return FAIL;
1473
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001474 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001476#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001477 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001479# ifdef FEAT_TERMINAL
1480 if (term_job_running(buf->b_term))
1481 {
1482 if (term_confirm_stop(buf) == FAIL)
1483 return FAIL;
1484 }
1485 else
1486# endif
1487 {
1488 dialog_changed(buf, FALSE);
1489 if (!bufref_valid(&bufref))
1490 // Autocommand deleted buffer, oops! It's not changed
1491 // now.
1492 return FAIL;
1493 // If it's still changed fail silently, the dialog already
1494 // mentioned why it fails.
1495 if (bufIsChanged(buf))
1496 return FAIL;
1497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001499 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001502 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 return FAIL;
1504 }
1505 }
1506
Bram Moolenaarc667da52019-11-30 20:52:27 +01001507 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001508 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001509 end_visual_mode();
1510
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001511 // If deleting the last (listed) buffer, make it empty.
1512 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001513 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 if (bp->b_p_bl && bp != buf)
1515 break;
1516 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001517 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001519 // If the deleted buffer is the current one, close the current window
1520 // (unless it's the only window). Repeat this so long as we end up in
1521 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001522 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001523 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001524 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001525 {
1526 if (win_close(curwin, FALSE) == FAIL)
1527 break;
1528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001530 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (buf != curbuf)
1532 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001533 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001534 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001535 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 return OK;
1537 }
1538
1539 /*
1540 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001541 * There should be another, otherwise it would have been handled
1542 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001543 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * Then prefer the buffer we most recently visited.
1545 * Else try to find one that is loaded, after the current buffer,
1546 * then before the current buffer.
1547 * Finally use any buffer.
1548 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001549 buf = NULL; // selected buffer
1550 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001551 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1552 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001553 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 int jumpidx;
1556
1557 jumpidx = curwin->w_jumplistidx - 1;
1558 if (jumpidx < 0)
1559 jumpidx = curwin->w_jumplistlen - 1;
1560
1561 forward = jumpidx;
1562 while (jumpidx != curwin->w_jumplistidx)
1563 {
1564 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1565 if (buf != NULL)
1566 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001567 // Skip current and unlisted bufs. Also skip a quickfix
1568 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001569 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001570 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 else if (buf->b_ml.ml_mfp == NULL)
1572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001573 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if (bp == NULL)
1575 bp = buf;
1576 buf = NULL;
1577 }
1578 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001579 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001581 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1583 break;
1584 if (--jumpidx < 0)
1585 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001586 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 break;
1588 }
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaarc667da52019-11-30 20:52:27 +01001591 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 forward = TRUE;
1594 buf = curbuf->b_next;
1595 for (;;)
1596 {
1597 if (buf == NULL)
1598 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001599 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 break;
1601 buf = curbuf->b_prev;
1602 forward = FALSE;
1603 continue;
1604 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001605 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001606 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001607 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001609 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001611 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 bp = buf;
1613 }
1614 if (forward)
1615 buf = buf->b_next;
1616 else
1617 buf = buf->b_prev;
1618 }
1619 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001620 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001622 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001624 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001625 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 break;
1627 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001628 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 if (curbuf->b_next != NULL)
1631 buf = curbuf->b_next;
1632 else
1633 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001634 if (bt_quickfix(buf))
1635 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 }
1638
Bram Moolenaara02471e2014-01-10 16:43:14 +01001639 if (buf == NULL)
1640 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001641 // Autocommands must have wiped out all other buffers. Only option
1642 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001643 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001644 }
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001647 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001649 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001651 // If 'switchbuf' is set jump to the window containing "buf".
1652 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001653 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 return FAIL;
1657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaarc667da52019-11-30 20:52:27 +01001659 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (buf == curbuf)
1661 return OK;
1662
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001663 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001664 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001667 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001669# ifdef FEAT_TERMINAL
1670 if (term_job_running(curbuf->b_term))
1671 {
1672 if (term_confirm_stop(curbuf) == FAIL)
1673 return FAIL;
1674 // Manually kill the terminal here because this command will
1675 // hide it otherwise.
1676 free_terminal(curbuf);
1677 }
1678 else
1679# endif
1680 {
1681 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001682
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001683 set_bufref(&bufref, buf);
1684 dialog_changed(curbuf, FALSE);
1685 if (!bufref_valid(&bufref))
1686 // Autocommand deleted buffer, oops!
1687 return FAIL;
1688
1689 if (bufIsChanged(curbuf))
1690 {
1691 no_write_message();
1692 return FAIL;
1693 }
1694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001699 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return FAIL;
1701 }
1702 }
1703
Bram Moolenaarc667da52019-11-30 20:52:27 +01001704 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 set_curbuf(buf, action);
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001708 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001710#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001711 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 return FAIL;
1713#endif
1714
1715 return OK;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001718 int
1719do_buffer(
1720 int action,
1721 int start,
1722 int dir, // FORWARD or BACKWARD
1723 int count, // buffer number or number of buffers
1724 int forceit) // TRUE when using !
1725{
1726 return do_buffer_ext(action, start, dir, count,
1727 forceit ? DOBUF_FORCEIT : 0);
1728}
1729
1730/*
1731 * do_bufdel() - delete or unload buffer(s)
1732 *
1733 * addr_count == 0: ":bdel" - delete current buffer
1734 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1735 * buffer "end_bnr", then any other arguments.
1736 * addr_count == 2: ":N,N bdel" - delete buffers in range
1737 *
1738 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1739 * DOBUF_DEL (":bdel")
1740 *
1741 * Returns error message or NULL
1742 */
1743 char *
1744do_bufdel(
1745 int command,
1746 char_u *arg, // pointer to extra arguments
1747 int addr_count,
1748 int start_bnr, // first buffer number in a range
1749 int end_bnr, // buffer nr or last buffer nr in a range
1750 int forceit)
1751{
1752 int do_current = 0; // delete current buffer?
1753 int deleted = 0; // number of buffers deleted
1754 char *errormsg = NULL; // return value
1755 int bnr; // buffer number
1756 char_u *p;
1757
1758 if (addr_count == 0)
1759 {
1760 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1761 }
1762 else
1763 {
1764 if (addr_count == 2)
1765 {
1766 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001767 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001768 bnr = start_bnr;
1769 }
1770 else // addr_count == 1
1771 bnr = end_bnr;
1772
1773 for ( ;!got_int; ui_breakcheck())
1774 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001775 // Delete the current buffer last, otherwise when the
1776 // current buffer is deleted, the next buffer becomes
1777 // the current one and will be loaded, which may then
1778 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001779 if (bnr == curbuf->b_fnum)
1780 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001781 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001782 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1783 ++deleted;
1784
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001785 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001786 if (addr_count == 2)
1787 {
1788 if (++bnr > end_bnr)
1789 break;
1790 }
1791 else // addr_count == 1
1792 {
1793 arg = skipwhite(arg);
1794 if (*arg == NUL)
1795 break;
1796 if (!VIM_ISDIGIT(*arg))
1797 {
1798 p = skiptowhite_esc(arg);
1799 bnr = buflist_findpat(arg, p,
1800 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1801 FALSE, FALSE);
1802 if (bnr < 0) // failed
1803 break;
1804 arg = p;
1805 }
1806 else
1807 bnr = getdigits(&arg);
1808 }
1809 }
1810 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1811 FORWARD, do_current, forceit) == OK)
1812 ++deleted;
1813
1814 if (deleted == 0)
1815 {
1816 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001817 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001818 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001819 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001820 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001821 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001822 errormsg = (char *)IObuff;
1823 }
1824 else if (deleted >= p_report)
1825 {
1826 if (command == DOBUF_UNLOAD)
1827 smsg(NGETTEXT("%d buffer unloaded",
1828 "%d buffers unloaded", deleted), deleted);
1829 else if (command == DOBUF_DEL)
1830 smsg(NGETTEXT("%d buffer deleted",
1831 "%d buffers deleted", deleted), deleted);
1832 else
1833 smsg(NGETTEXT("%d buffer wiped out",
1834 "%d buffers wiped out", deleted), deleted);
1835 }
1836 }
1837
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001838 return errormsg;
1839}
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841/*
1842 * Set current buffer to "buf". Executes autocommands and closes current
1843 * buffer. "action" tells how to close the current buffer:
1844 * DOBUF_GOTO free or hide it
1845 * DOBUF_SPLIT nothing
1846 * DOBUF_UNLOAD unload it
1847 * DOBUF_DEL delete it
1848 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001849 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 buf_T *prevbuf;
1855 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001856 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001857#ifdef FEAT_SYN_HL
1858 long old_tw = curbuf->b_p_tw;
1859#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001860 bufref_T newbufref;
1861 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001862 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001863 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001866 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001867 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1868 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaarc667da52019-11-30 20:52:27 +01001870 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaarc667da52019-11-30 20:52:27 +01001873 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001875 set_bufref(&prevbufref, prevbuf);
1876 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001878 // Autocommands may delete the current buffer and/or the buffer we want to
1879 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001880 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001881 || (bufref_valid(&prevbufref)
1882 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001884 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001886 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001888#ifdef FEAT_SYN_HL
1889 if (prevbuf == curwin->w_buffer)
1890 reset_synblock(curwin);
1891#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001892 // autocommands may have opened a new window
1893 // with prevbuf, grr
1894 if (unload ||
1895 (last_winid != get_last_winid() &&
1896 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001897 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001898#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001899 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001901 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001903 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001904 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001905
Bram Moolenaare615db02022-01-20 21:00:54 +00001906 // Do not sync when in Insert mode and the buffer is open in
1907 // another window, might be a timer doing something in another
1908 // window.
1909 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001910 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001911 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1913 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001914 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001915 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1916 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001917 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001918 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001919 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001922 // An autocommand may have deleted "buf", already entered it (e.g., when
1923 // it did ":bunload") or aborted the script processing.
1924 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001925 valid = buf_valid(buf);
1926 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001927#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001928 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001930 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001931 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001932 // autocommands changed curbuf and we will move to another
1933 // buffer soon, so decrement curbuf->b_nwindows
1934 if (curbuf != NULL && prevbuf != curbuf)
1935 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001936 // If the buffer is not valid but curwin->w_buffer is NULL we must
1937 // enter some buffer. Using the last one is hopefully OK.
1938 if (!valid)
1939 enter_buffer(lastbuf);
1940 else
1941 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001942#ifdef FEAT_SYN_HL
1943 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001944 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001945#endif
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947}
1948
1949/*
1950 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001951 * Old curbuf must have been abandoned already! This also means "curbuf" may
1952 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001955enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001957 // when closing the current buffer stop Visual mode
1958 if (VIsual_active
1959#if defined(EXITFREE)
1960 && !entered_free_all_mem
1961#endif
1962 )
1963 end_visual_mode();
1964
Bram Moolenaar010ee962019-09-25 20:37:36 +02001965 // Get the buffer in the current window.
1966 curwin->w_buffer = buf;
1967 curbuf = buf;
1968 ++curbuf->b_nwindows;
1969
1970 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1972 if (!buf->b_help)
1973 get_winopts(buf);
1974#ifdef FEAT_FOLDING
1975 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001976 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001978 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#endif
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001982 if (curwin->w_p_diff)
1983 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#endif
1985
Bram Moolenaara971b822011-09-14 14:43:25 +02001986#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001987 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001988#endif
1989
Bram Moolenaarc667da52019-11-30 20:52:27 +01001990 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 curwin->w_cursor.lnum = 1;
1992 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001995 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
Bram Moolenaarc667da52019-11-30 20:52:27 +01001997 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001998 curwin->w_valid = 0;
1999
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02002000 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
2001 curbuf->b_last_cursor.col, TRUE);
2002
Bram Moolenaarc667da52019-11-30 20:52:27 +01002003 // Make sure the buffer is loaded.
2004 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002005 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002006 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002007 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01002008 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002009 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02002010 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002011
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002012 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015 {
Bram Moolenaareda65222019-05-16 20:29:44 +02002016 if (!msg_silent && !shortmess(SHM_FILEINFO))
2017 need_fileinfo = TRUE; // display file info after redraw
2018
2019 // check if file changed
2020 (void)buf_check_timestamp(curbuf, FALSE);
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002023#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2027 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029
Bram Moolenaarc667da52019-11-30 20:52:27 +01002030 // If autocommands did not change the cursor position, restore cursor lnum
2031 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (curwin->w_cursor.lnum == 1 && inindent(0))
2033 buflist_getfpos();
2034
Bram Moolenaarc667da52019-11-30 20:52:27 +01002035 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002037 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002038 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002039 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
Bram Moolenaar009b2592004-10-24 19:18:58 +00002041#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002042 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002043 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002044#endif
2045
Bram Moolenaarc667da52019-11-30 20:52:27 +01002046 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002047 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049#ifdef FEAT_KEYMAP
2050 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002051 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002053#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002054 // May need to set the spell language. Can only do this after the buffer
2055 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002056 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002057 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002058#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002059#ifdef FEAT_VIMINFO
2060 curbuf->b_last_used = vim_time();
2061#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002062
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002063 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064}
2065
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002066#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2067/*
2068 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002069 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002070 */
2071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002072do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002073{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002074 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002075 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002076 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002077 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002078 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002079 last_chdir_reason = "autochdir";
2080 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002081}
2082#endif
2083
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002084 static void
2085no_write_message_buf(buf_T *buf UNUSED)
2086{
2087#ifdef FEAT_TERMINAL
2088 if (term_job_running(buf->b_term))
2089 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2090 else
2091#endif
2092 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2093 buf->b_fnum);
2094}
2095
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002096 void
2097no_write_message(void)
2098{
2099#ifdef FEAT_TERMINAL
2100 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002101 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002102 else
2103#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002104 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002105}
2106
2107 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002108no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002109{
2110#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002111 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002112 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002113 else
2114#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002115 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002116}
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118/*
2119 * functions for dealing with the buffer list
2120 */
2121
2122/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002123 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2124 * only one window. That means it can be re-used.
2125 */
2126 int
2127curbuf_reusable(void)
2128{
2129 return (curbuf != NULL
2130 && curbuf->b_ffname == NULL
2131 && curbuf->b_nwindows <= 1
2132 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002133 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002134 && !curbufIsChanged());
2135}
2136
2137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 * Add a file name to the buffer list. Return a pointer to the buffer.
2139 * If the same file name already exists return a pointer to that buffer.
2140 * If it does not exist, or if fname == NULL, a new entry is created.
2141 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2142 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2143 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002144 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002145 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2146 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002147 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * This is the ONLY way to create a new buffer.
2149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002152 char_u *ffname_arg, // full path of fname or relative
2153 char_u *sfname_arg, // short fname or NULL
2154 linenr_T lnum, // preferred cursor line
2155 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002157 char_u *ffname = ffname_arg;
2158 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 buf_T *buf;
2160#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002161 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#endif
2163
Bram Moolenaar480778b2016-07-14 22:09:39 +02002164 if (top_file_num == 1)
2165 hash_init(&buf_hashtab);
2166
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002167 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002170 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 */
2172#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002173 // On Unix we can use inode numbers when the file exists. Works better
2174 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2176 st.st_dev = (dev_T)-1;
2177#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002178 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef UNIX
2180 buflist_findname_stat(ffname, &st)
2181#else
2182 buflist_findname(ffname)
2183#endif
2184 ) != NULL)
2185 {
2186 vim_free(ffname);
2187 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002188 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2189 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002190
2191 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002192 // copy the options now, if 'cpo' doesn't have 's' and not done
2193 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002194 buf_copy_options(buf, 0);
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2197 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002198 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002199
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002201 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002203 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002204 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002205 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002206 return NULL;
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 return buf;
2210 }
2211
2212 /*
2213 * If the current buffer has no name and no contents, use the current
2214 * buffer. Otherwise: Need to allocate a new buffer structure.
2215 *
2216 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002217 * (A spell file buffer is allocated in spell.c, but that's not a normal
2218 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 */
2220 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002221 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Sean Dewar00772822025-05-14 20:16:52 +02002223 bufref_T bufref;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 buf = curbuf;
Sean Dewar00772822025-05-14 20:16:52 +02002226 set_bufref(&bufref, buf);
Christian Brabandtbaa8c902025-04-19 11:14:11 +02002227 trigger_undo_ftplugin(buf, curwin);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002228 // It's like this buffer is deleted. Watch out for autocommands that
2229 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002230 buf_freeall(buf, BFA_WIPE | BFA_DEL);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002231#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002232 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002233 {
2234 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
Sean Dewar00772822025-05-14 20:16:52 +02002238 if (!bufref_valid(&bufref))
2239 buf = NULL; // buf was deleted; allocate a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 if (buf != curbuf || curbuf == NULL)
2242 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002243 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (buf == NULL)
2245 {
2246 vim_free(ffname);
2247 return NULL;
2248 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002249#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002250 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002251 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002252 if (buf->b_vars == NULL)
2253 {
2254 vim_free(ffname);
2255 vim_free(buf);
2256 return NULL;
2257 }
2258 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2259#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002260 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262
2263 if (ffname != NULL)
2264 {
2265 buf->b_ffname = ffname;
2266 buf->b_sfname = vim_strsave(sfname);
2267 }
2268
2269 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002270 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
2272 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2273 || buf->b_wininfo == NULL)
2274 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002275 if (buf->b_sfname != buf->b_ffname)
2276 VIM_CLEAR(buf->b_sfname);
2277 else
2278 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002279 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (buf != curbuf)
2281 free_buffer(buf);
2282 return NULL;
2283 }
2284
2285 if (buf == curbuf)
2286 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002287 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002288
Bram Moolenaarc667da52019-11-30 20:52:27 +01002289 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002290 buf->b_p_initialized = FALSE;
2291 buf_copy_options(buf, BCO_ENTER);
2292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002294 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 curbuf->b_kmap_state |= KEYMAP_INIT;
2296#endif
2297 }
2298 else
2299 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002300 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002302 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 buf->b_prev = NULL;
2305 firstbuf = buf;
2306 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002307 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 lastbuf->b_next = buf;
2310 buf->b_prev = lastbuf;
2311 }
2312 lastbuf = buf;
2313
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002314 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2315 {
2316 // Recycle a previously used buffer number. Used for buffers which
2317 // are normally hidden, e.g. in a popup window. Avoids that the
2318 // buffer number grows rapidly.
2319 --buf_reuse.ga_len;
2320 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002321
2322 // Move buffer to the right place in the buffer list.
2323 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2324 {
2325 buf_T *prev = buf->b_prev;
2326
2327 prev->b_next = buf->b_next;
2328 if (prev->b_next != NULL)
2329 prev->b_next->b_prev = prev;
2330 buf->b_next = prev;
2331 buf->b_prev = prev->b_prev;
2332 if (buf->b_prev != NULL)
2333 buf->b_prev->b_next = buf;
2334 prev->b_prev = buf;
2335 if (lastbuf == buf)
2336 lastbuf = prev;
2337 if (firstbuf == prev)
2338 firstbuf = buf;
2339 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002340 }
2341 else
2342 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002343 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002345 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002346 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002349 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 top_file_num = 1;
2352 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002353 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002355 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 buf_copy_options(buf, BCO_ALWAYS);
2357 }
2358
2359 buf->b_wininfo->wi_fpos.lnum = lnum;
2360 buf->b_wininfo->wi_win = curwin;
2361
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002362#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002363 hash_init(&buf->b_s.b_keywtab);
2364 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
2367 buf->b_fname = buf->b_sfname;
2368#ifdef UNIX
2369 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002370 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 else
2372 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002373 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 buf->b_dev = st.st_dev;
2375 buf->b_ino = st.st_ino;
2376 }
2377#endif
2378 buf->b_u_synced = TRUE;
2379 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002380 if (flags & BLN_DUMMY)
2381 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002383 clrallmarks(buf); // clear marks
2384 fmarks_check_names(buf); // check file marks for this file
2385 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (!(flags & BLN_DUMMY))
2387 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002388 bufref_T bufref;
2389
Bram Moolenaarc667da52019-11-30 20:52:27 +01002390 // Tricky: these autocommands may change the buffer list. They could
2391 // also split the window with re-using the one empty buffer. This may
2392 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002393 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002394 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002395 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002396 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002398 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002399 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002400 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002401 return NULL;
2402 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002403#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002404 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 return buf;
2410}
2411
2412/*
2413 * Free the memory for the options of a buffer.
2414 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2415 * 'fileencoding'.
2416 */
2417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002418free_buf_options(
2419 buf_T *buf,
2420 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 if (free_p_ff)
2423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 clear_string_option(&buf->b_p_bh);
2427 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429#ifdef FEAT_FIND_ID
2430 clear_string_option(&buf->b_p_def);
2431 clear_string_option(&buf->b_p_inc);
2432# ifdef FEAT_EVAL
2433 clear_string_option(&buf->b_p_inex);
2434# endif
2435#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002436#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 clear_string_option(&buf->b_p_inde);
2438 clear_string_option(&buf->b_p_indk);
2439#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002440#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2441 clear_string_option(&buf->b_p_bexpr);
2442#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002443#if defined(FEAT_CRYPT)
2444 clear_string_option(&buf->b_p_cm);
2445#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002446 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002447#if defined(FEAT_EVAL)
2448 clear_string_option(&buf->b_p_fex);
2449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002451# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002452 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2453 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002454 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 clear_string_option(&buf->b_p_key);
2457#endif
2458 clear_string_option(&buf->b_p_kp);
2459 clear_string_option(&buf->b_p_mps);
2460 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002461 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002463#ifdef FEAT_VARTABS
2464 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002465 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002466 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002467 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002468 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470#ifdef FEAT_KEYMAP
2471 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002472 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 ga_clear(&buf->b_kmap_ga);
2474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476#ifdef FEAT_FOLDING
2477 clear_string_option(&buf->b_p_cms);
2478#endif
2479 clear_string_option(&buf->b_p_nf);
2480#ifdef FEAT_SYN_HL
2481 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002482 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002483#endif
2484#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002485 clear_string_option(&buf->b_s.b_p_spc);
2486 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002487 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002488 buf->b_s.b_cap_prog = NULL;
2489 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002490 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 clear_string_option(&buf->b_p_cink);
2495 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002496 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002497 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002499 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 clear_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +02002501 clear_string_option(&buf->b_p_ise);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002502#ifdef FEAT_COMPL_FUNC
2503 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002504 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002505 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002506 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002507 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002508 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510#ifdef FEAT_QUICKFIX
glepnir7b9eb632025-05-16 19:49:23 +02002511 clear_string_option(&buf->b_p_gefm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 clear_string_option(&buf->b_p_gp);
2513 clear_string_option(&buf->b_p_mp);
2514 clear_string_option(&buf->b_p_efm);
2515#endif
2516 clear_string_option(&buf->b_p_ep);
2517 clear_string_option(&buf->b_p_path);
2518 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002519 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002520#ifdef FEAT_EVAL
2521 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002522 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002523 clear_string_option(&buf->b_p_ffu);
2524 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 clear_string_option(&buf->b_p_dict);
2527 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002528 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002530 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002531 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002532 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002533 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534}
2535
2536/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002537 * Get alternate file "n".
2538 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2539 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 * if (options & GETF_SETMARK) call setpcmark()
2541 * if (options & GETF_ALT) we are jumping to an alternate file.
2542 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2543 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002544 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
2546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547buflist_getfile(
2548 int n,
2549 linenr_T lnum,
2550 int options,
2551 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 pos_T *fpos;
2556 colnr_T col;
2557
2558 buf = buflist_findnr(n);
2559 if (buf == NULL)
2560 {
2561 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002562 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002564 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 return FAIL;
2566 }
2567
Bram Moolenaarc667da52019-11-30 20:52:27 +01002568 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (buf == curbuf)
2570 return OK;
2571
Bram Moolenaar71223e22022-05-30 15:23:09 +01002572 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaarc667da52019-11-30 20:52:27 +01002575 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (lnum == 0)
2577 {
2578 fpos = buflist_findfpos(buf);
2579 lnum = fpos->lnum;
2580 col = fpos->col;
2581 }
2582 else
2583 col = 0;
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (options & GETF_SWITCH)
2586 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002587 // If 'switchbuf' is set jump to the window containing "buf".
2588 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002589
Bram Moolenaarc667da52019-11-30 20:52:27 +01002590 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2591 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002592 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002593 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002595 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002596 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002597 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2598 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002600 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002605 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002606 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2607 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002609 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (!p_sol && col != 0)
2611 {
2612 curwin->w_cursor.col = col;
2613 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 curwin->w_set_curswant = TRUE;
2616 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002617 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002619
2620 if (RedrawingDisabled > 0)
2621 --RedrawingDisabled;
2622 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623}
2624
2625/*
2626 * go to the last know line number for the current buffer
2627 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002629buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 pos_T *fpos;
2632
2633 fpos = buflist_findfpos(curbuf);
2634
2635 curwin->w_cursor.lnum = fpos->lnum;
2636 check_cursor_lnum();
2637
2638 if (p_sol)
2639 curwin->w_cursor.col = 0;
2640 else
2641 {
2642 curwin->w_cursor.col = fpos->col;
2643 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 curwin->w_set_curswant = TRUE;
2646 }
2647}
2648
Bram Moolenaar81695252004-12-29 20:58:21 +00002649/*
2650 * Find file in buffer list by name (it has to be for the current window).
2651 * Returns NULL if not found.
2652 */
2653 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002654buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002655{
2656 char_u *ffname;
2657 buf_T *buf = NULL;
2658
Bram Moolenaarc667da52019-11-30 20:52:27 +01002659 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002660 ffname = FullName_save(fname,
2661#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002662 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002663#else
2664 FALSE
2665#endif
2666 );
2667 if (ffname != NULL)
2668 {
2669 buf = buflist_findname(ffname);
2670 vim_free(ffname);
2671 }
2672 return buf;
2673}
Bram Moolenaar81695252004-12-29 20:58:21 +00002674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675/*
2676 * Find file in buffer list by name (it has to be for the current window).
2677 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002678 * Skips dummy buffers.
2679 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
2681 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002682buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002685 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 if (mch_stat((char *)ffname, &st) < 0)
2688 st.st_dev = (dev_T)-1;
2689 return buflist_findname_stat(ffname, &st);
2690}
2691
2692/*
2693 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2694 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002695 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 */
2697 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002698buflist_findname_stat(
2699 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002700 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702#endif
2703 buf_T *buf;
2704
Bram Moolenaarc667da52019-11-30 20:52:27 +01002705 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002706 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002707 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#ifdef UNIX
2709 , stp
2710#endif
2711 ))
2712 return buf;
2713 return NULL;
2714}
2715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716/*
2717 * Find file in buffer list by a regexp pattern.
2718 * Return fnum of the found buffer.
2719 * Return < 0 for error.
2720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002722buflist_findpat(
2723 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002724 char_u *pattern_end, // pointer to first char after pattern
2725 int unlisted, // find unlisted buffers
2726 int diffmode UNUSED, // find diff-mode buffers only
2727 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 int match = -1;
2731 int find_listed;
2732 char_u *pat;
2733 char_u *patend;
2734 int attempt;
2735 char_u *p;
2736 int toggledollar;
2737
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002738 // "%" is current file, "%%" or "#" is alternate file
2739 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2740 || (in_vim9script() && pattern_end == pattern + 2
2741 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002743 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002745 else
2746 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#ifdef FEAT_DIFF
2748 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2749 match = -1;
2750#endif
2751 }
2752
2753 /*
2754 * Try four ways of matching a listed buffer:
2755 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002756 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 * attempt == 2: with '$' at end (only match at end)
2758 * attempt == 3: with '^' at start and '$' at end (only full match)
2759 * Repeat this for finding an unlisted buffer if there was no matching
2760 * listed buffer.
2761 */
2762 else
2763 {
2764 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2765 if (pat == NULL)
2766 return -1;
2767 patend = pat + STRLEN(pat) - 1;
2768 toggledollar = (patend > pat && *patend == '$');
2769
Bram Moolenaarc667da52019-11-30 20:52:27 +01002770 // First try finding a listed buffer. If not found and "unlisted"
2771 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 find_listed = TRUE;
2773 for (;;)
2774 {
2775 for (attempt = 0; attempt <= 3; ++attempt)
2776 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002777 regmatch_T regmatch;
2778
Bram Moolenaarc667da52019-11-30 20:52:27 +01002779 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002781 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002783 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002785 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002787 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002788 {
2789 if (regmatch.regprog == NULL)
2790 {
2791 // invalid pattern, possibly after switching engine
2792 vim_free(pat);
2793 return -1;
2794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (buf->b_p_bl == find_listed
2796#ifdef FEAT_DIFF
2797 && (!diffmode || diff_mode_buf(buf))
2798#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002799 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002801 if (curtab_only)
2802 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002803 // Ignore the match if the buffer is not open in
2804 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002805 win_T *wp;
2806
Bram Moolenaar29323592016-07-24 22:04:11 +02002807 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002808 if (wp->w_buffer == buf)
2809 break;
2810 if (wp == NULL)
2811 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002812 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002813 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
2815 match = -2;
2816 break;
2817 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002818 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002822 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002823 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 break;
2825 }
2826
Bram Moolenaarc667da52019-11-30 20:52:27 +01002827 // Only search for unlisted buffers if there was no match with
2828 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if (!unlisted || !find_listed || match != -1)
2830 break;
2831 find_listed = FALSE;
2832 }
2833
2834 vim_free(pat);
2835 }
2836
2837 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002838 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002840 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 return match;
2842}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
Bram Moolenaar52410572019-10-27 05:12:45 +01002844#ifdef FEAT_VIMINFO
2845typedef struct {
2846 buf_T *buf;
2847 char_u *match;
2848} bufmatch_T;
2849#endif
2850
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851/*
2852 * Find all buffer names that match.
2853 * For command line expansion of ":buf" and ":sbuf".
2854 * Return OK if matches found, FAIL otherwise.
2855 */
2856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002857ExpandBufnames(
2858 char_u *pat,
2859 int *num_file,
2860 char_u ***file,
2861 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002863 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 buf_T *buf;
2865 int round;
2866 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002867 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002868#ifdef FEAT_VIMINFO
2869 bufmatch_T *matches = NULL;
2870#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002871 int fuzzy;
2872 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002873 regmatch_T regmatch;
2874 int score = 0;
2875 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaarc667da52019-11-30 20:52:27 +01002877 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 *file = NULL;
2879
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002880#ifdef FEAT_DIFF
2881 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2882 return FAIL;
2883#endif
2884
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002885 fuzzy = cmdline_fuzzy_complete(pat);
2886
2887 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2888 // expression matching)
2889 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002891 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002892 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002893 int len = (int)STRLEN(pat);
2894 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002895 if (patc == NULL)
2896 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002897 STRNCPY(patc, pat + 1, len - 1);
2898 patc[len - 1] = NUL;
2899 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002900 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002901 else if (*pat == '^')
2902 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002903 else
2904 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002905 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002906 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002907
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002908 // round == 1: Count the matches.
2909 // round == 2: Build the array to keep the matches.
2910 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002911 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002912 count = 0;
2913 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002914 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002915 if (!buf->b_p_bl) // skip unlisted buffers
2916 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002917#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002918 if (options & BUF_DIFF_FILTER)
2919 // Skip buffers not suitable for
2920 // :diffget or :diffput completion.
2921 if (buf == curbuf || !diff_mode_buf(buf))
2922 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002923#endif
2924
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002925 if (!fuzzy)
2926 {
2927 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002928 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002929 // invalid pattern, possibly after recompiling
2930 if (to_free)
2931 vim_free(patc);
2932 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002933 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002934 p = buflist_match(&regmatch, buf, p_wic);
2935 }
2936 else
2937 {
2938 p = NULL;
2939 // first try matching with the short file name
2940 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2941 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002942 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002943 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002944 // next try matching with the full path file name
2945 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2946 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002949
2950 if (p == NULL)
2951 continue;
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (round == 1)
2954 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002955 ++count;
2956 continue;
2957 }
2958
2959 if (options & WILD_HOME_REPLACE)
2960 p = home_replace_save(buf, p);
2961 else
2962 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002963 if (p == NULL)
2964 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002965
2966 if (!fuzzy)
2967 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002968#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002969 if (matches != NULL)
2970 {
2971 matches[count].buf = buf;
2972 matches[count].match = p;
2973 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002974 }
2975 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002976#endif
2977 (*file)[count++] = p;
2978 }
2979 else
2980 {
2981 fuzmatch[count].idx = count;
2982 fuzmatch[count].str = p;
2983 fuzmatch[count].score = score;
2984 count++;
2985 }
2986 }
2987 if (count == 0) // no match found, break here
2988 break;
2989 if (round == 1)
2990 {
2991 if (!fuzzy)
2992 {
2993 *file = ALLOC_MULT(char_u *, count);
2994 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002995 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002996 vim_regfree(regmatch.regprog);
2997 if (to_free)
2998 vim_free(patc);
2999 return FAIL;
3000 }
3001#ifdef FEAT_VIMINFO
3002 if (options & WILD_BUFLASTUSED)
3003 matches = ALLOC_MULT(bufmatch_T, count);
3004#endif
3005 }
3006 else
3007 {
3008 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
3009 if (fuzmatch == NULL)
3010 {
3011 *num_file = 0;
3012 *file = NULL;
3013 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01003019 if (!fuzzy)
3020 {
3021 vim_regfree(regmatch.regprog);
3022 if (to_free)
3023 vim_free(patc);
3024 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003025
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003026 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01003027 {
826814741_6dff3c9c2024-12-10 17:15:14 +01003028#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003029 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01003030 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003031 int i;
3032 if (count > 1)
3033 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3034 // if the current buffer is first in the list, place it at the end
3035 if (matches[0].buf == curbuf)
3036 {
3037 for (i = 1; i < count; i++)
3038 (*file)[i-1] = matches[i].match;
3039 (*file)[count-1] = matches[0].match;
3040 }
3041 else
3042 {
3043 for (i = 0; i < count; i++)
3044 (*file)[i] = matches[i].match;
3045 }
3046 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003047 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003048#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003049 }
3050 else
3051 {
3052 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3053 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003054 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 *num_file = count;
3057 return (count == 0 ? FAIL : OK);
3058}
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060/*
3061 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003062 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 */
3064 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003065buflist_match(
3066 regmatch_T *rmp,
3067 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003068 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 char_u *match;
3071
Bram Moolenaarc667da52019-11-30 20:52:27 +01003072 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003073 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003074 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003075 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 return match;
3078}
3079
3080/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003081 * Try matching the regexp in "rmp->regprog" with file name "name".
3082 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * Return "name" when there is a match, NULL when not.
3084 */
3085 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086fname_match(
3087 regmatch_T *rmp,
3088 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003089 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 char_u *match = NULL;
3092 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003094 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003095 if (name == NULL || rmp->regprog == NULL)
3096 return NULL;
3097
3098 // Ignore case when 'fileignorecase' or the argument is set.
3099 rmp->rm_ic = p_fic || ignore_case;
3100 if (vim_regexec(rmp, name, (colnr_T)0))
3101 match = name;
3102 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003104 // Replace $(HOME) with '~' and try matching again.
3105 p = home_replace_save(NULL, name);
3106 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003108 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110
3111 return match;
3112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003115 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 */
3117 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003118buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003120 char_u key[VIM_SIZEOF_INT * 2 + 1];
3121 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
3123 if (nr == 0)
3124 nr = curwin->w_alt_fnum;
John Marriottec032de2025-04-10 21:34:19 +02003125 vim_snprintf((char *)key, sizeof(key), "%x", nr);
Bram Moolenaar480778b2016-07-14 22:09:39 +02003126 hi = hash_find(&buf_hashtab, key);
3127
3128 if (!HASHITEM_EMPTY(hi))
3129 return (buf_T *)(hi->hi_key
3130 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 return NULL;
3132}
3133
3134/*
3135 * Get name of file 'n' in the buffer list.
3136 * When the file has no name an empty string is returned.
3137 * home_replace() is used to shorten the file name (used for marks).
3138 * Returns a pointer to allocated memory, of NULL when failed.
3139 */
3140 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003141buflist_nr2name(
3142 int n,
3143 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003144 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 buf_T *buf;
3147
3148 buf = buflist_findnr(n);
3149 if (buf == NULL)
3150 return NULL;
3151 return home_replace_save(helptail ? buf : NULL,
3152 fullname ? buf->b_ffname : buf->b_fname);
3153}
3154
3155/*
3156 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3157 * When "copy_options" is TRUE save the local window option values.
3158 * When "lnum" is 0 only do the options.
3159 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003161buflist_setfpos(
3162 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003163 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164 linenr_T lnum,
3165 colnr_T col,
3166 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168 wininfo_T *wip;
3169
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003170 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (wip->wi_win == win)
3172 break;
3173 if (wip == NULL)
3174 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003175 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003176 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (wip == NULL)
3178 return;
3179 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003180 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 lnum = 1;
3182 }
3183 else
3184 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003185 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (wip->wi_prev)
3187 wip->wi_prev->wi_next = wip->wi_next;
3188 else
3189 buf->b_wininfo = wip->wi_next;
3190 if (wip->wi_next)
3191 wip->wi_next->wi_prev = wip->wi_prev;
3192 if (copy_options && wip->wi_optset)
3193 {
3194 clear_winopt(&wip->wi_opt);
3195#ifdef FEAT_FOLDING
3196 deleteFoldRecurse(&wip->wi_folds);
3197#endif
3198 }
3199 }
3200 if (lnum != 0)
3201 {
3202 wip->wi_fpos.lnum = lnum;
3203 wip->wi_fpos.col = col;
3204 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003205 if (win != NULL)
3206 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003207 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003209 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3211#ifdef FEAT_FOLDING
3212 wip->wi_fold_manual = win->w_fold_manual;
3213 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3214#endif
3215 wip->wi_optset = TRUE;
3216 }
3217
Bram Moolenaarc667da52019-11-30 20:52:27 +01003218 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 wip->wi_next = buf->b_wininfo;
3220 buf->b_wininfo = wip;
3221 wip->wi_prev = NULL;
3222 if (wip->wi_next)
3223 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003226#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003227/*
3228 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3229 * page. That's because a diff is local to a tab page.
3230 */
3231 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003233{
3234 win_T *wp;
3235
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003236 if (!wip->wi_opt.wo_diff)
3237 return FALSE;
3238
3239 FOR_ALL_WINDOWS(wp)
3240 // return FALSE when it's a window in the current tab page, thus
3241 // the buffer was in diff mode here
3242 if (wip->wi_win == wp)
3243 return FALSE;
3244 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003245}
3246#endif
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248/*
3249 * Find info for the current window in buffer "buf".
3250 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003251 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003252 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3253 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * Returns NULL when there isn't any info.
3255 */
3256 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003257find_wininfo(
3258 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003259 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003260 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 wininfo_T *wip;
3263
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003264 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003265 if (wip->wi_win == curwin
3266#ifdef FEAT_DIFF
3267 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3268#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003269
3270 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003272
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003273 if (wip != NULL)
3274 return wip;
3275
Bram Moolenaarc667da52019-11-30 20:52:27 +01003276 // If no wininfo for curwin, use the first in the list (that doesn't have
3277 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003278 // If "need_options" is TRUE skip entries that don't have options set,
3279 // unless the window is editing "buf", so we can copy from the window
3280 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003281#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003282 if (skip_diff_buffer)
3283 {
3284 FOR_ALL_BUF_WININFO(buf, wip)
3285 if (!wininfo_other_tab_diff(wip)
3286 && (!need_options || wip->wi_optset
3287 || (wip->wi_win != NULL
3288 && wip->wi_win->w_buffer == buf)))
3289 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003290 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003291 else
3292#endif
3293 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 return wip;
3295}
3296
3297/*
3298 * Reset the local window options to the values last used in this window.
3299 * If the buffer wasn't used in this window before, use the values from
3300 * the most recently used window. If the values were never set, use the
3301 * global values for the window.
3302 */
3303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 wininfo_T *wip;
3307
3308 clear_winopt(&curwin->w_onebuf_opt);
3309#ifdef FEAT_FOLDING
3310 clearFolding(curwin);
3311#endif
3312
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003313 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003314 if (wip != NULL && wip->wi_win != NULL
3315 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003317 // The buffer is currently displayed in the window: use the actual
3318 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003319 win_T *wp = wip->wi_win;
3320
3321 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3322#ifdef FEAT_FOLDING
3323 curwin->w_fold_manual = wp->w_fold_manual;
3324 curwin->w_foldinvalid = TRUE;
3325 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3326#endif
3327 }
3328 else if (wip != NULL && wip->wi_optset)
3329 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003330 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3332#ifdef FEAT_FOLDING
3333 curwin->w_fold_manual = wip->wi_fold_manual;
3334 curwin->w_foldinvalid = TRUE;
3335 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3336#endif
3337 }
3338 else
3339 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003340 if (wip != NULL)
3341 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003344 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (p_fdls >= 0)
3346 curwin->w_p_fdl = p_fdls;
3347#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003348 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351/*
3352 * Find the position (lnum and col) for the buffer 'buf' for the current
3353 * window.
3354 * Returns a pointer to no_position if no position is found.
3355 */
3356 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003357buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003360 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003362 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (wip != NULL)
3364 return &(wip->wi_fpos);
3365 else
3366 return &no_position;
3367}
3368
3369/*
3370 * Find the lnum for the buffer 'buf' for the current window.
3371 */
3372 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003373buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 return buflist_findfpos(buf)->lnum;
3376}
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003379 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003382buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
Bram Moolenaar52410572019-10-27 05:12:45 +01003384 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 int len;
3386 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003387 int ro_char;
3388 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003389#ifdef FEAT_TERMINAL
3390 int job_running;
3391 int job_none_open;
3392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar52410572019-10-27 05:12:45 +01003394#ifdef FEAT_VIMINFO
3395 garray_T buflist;
3396 buf_T **buflist_data = NULL, **p;
3397
3398 if (vim_strchr(eap->arg, 't'))
3399 {
3400 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003401 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003402 {
3403 if (ga_grow(&buflist, 1) == OK)
3404 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3405 }
3406
3407 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3408 sizeof(buf_T *), buf_compare);
3409
Bram Moolenaar3b991522019-11-06 23:26:20 +01003410 buflist_data = (buf_T **)buflist.ga_data;
3411 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003412 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003413 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003414
Bram Moolenaar3b991522019-11-06 23:26:20 +01003415 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003416 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3417 : buf->b_next)
3418#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
John Marriottec032de2025-04-10 21:34:19 +02003422 char_u *name;
3423
Bram Moolenaar0751f512018-03-29 16:37:16 +02003424#ifdef FEAT_TERMINAL
3425 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003426 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003427#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003429 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3430 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3431 || (vim_strchr(eap->arg, '+')
3432 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3433 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003434 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003435 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003436 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3437#ifdef FEAT_TERMINAL
3438 || (vim_strchr(eap->arg, 'R')
3439 && (!job_running || (job_running && job_none_open)))
3440 || (vim_strchr(eap->arg, '?')
3441 && (!job_running || (job_running && !job_none_open)))
3442 || (vim_strchr(eap->arg, 'F')
3443 && (job_running || buf->b_term == NULL))
3444#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003445 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3446 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3447 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3448 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3449 || (vim_strchr(eap->arg, '#')
3450 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 continue;
John Marriottec032de2025-04-10 21:34:19 +02003452 name = buf_spname(buf);
3453 if (name != NULL)
3454 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
3456 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003457 if (message_filtered(NameBuff))
3458 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003460 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3461 : (bufIsChanged(buf) ? '+' : ' ');
3462#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003463 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003464 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003465 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003466 ro_char = '?';
3467 else
3468 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003469 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3470 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003471 }
3472 else if (buf->b_term != NULL)
3473 ro_char = 'F';
3474 else
3475#endif
3476 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3477
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003478 msg_putchar('\n');
John Marriottec032de2025-04-10 21:34:19 +02003479 len = (int)vim_snprintf_safelen((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 buf->b_fnum,
3481 buf->b_p_bl ? ' ' : 'u',
3482 buf == curbuf ? '%' :
3483 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3484 buf->b_ml.ml_mfp == NULL ? ' ' :
3485 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003486 ro_char,
3487 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003488 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaarc667da52019-11-30 20:52:27 +01003490 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 i = 40 - vim_strsize(IObuff);
3492 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003494 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003495#ifdef FEAT_VIMINFO
3496 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3497 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3498 else
3499#endif
3500 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3501 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003502 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003504 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 ui_breakcheck();
3506 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003507
3508#ifdef FEAT_VIMINFO
3509 if (buflist_data)
3510 ga_clear(&buflist);
3511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514/*
3515 * Get file name and line number for file 'fnum'.
3516 * Used by DoOneCmd() for translating '%' and '#'.
3517 * Used by insert_reg() and cmdline_paste() for '#' register.
3518 * Return FAIL if not found, OK for success.
3519 */
3520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003521buflist_name_nr(
3522 int fnum,
3523 char_u **fname,
3524 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 buf_T *buf;
3527
3528 buf = buflist_findnr(fnum);
3529 if (buf == NULL || buf->b_fname == NULL)
3530 return FAIL;
3531
3532 *fname = buf->b_fname;
3533 *lnum = buflist_findlnum(buf);
3534
3535 return OK;
3536}
3537
3538/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003539 * Set the file name for "buf"' to "ffname_arg", short file name to
3540 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 * The file name with the full path is also remembered, for when :cd is used.
3542 * Returns FAIL for failure (file name already in use by other buffer)
3543 * OK otherwise.
3544 */
3545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003546setfname(
3547 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003548 char_u *ffname_arg,
3549 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003550 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003552 char_u *ffname = ffname_arg;
3553 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003554 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003556 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557#endif
3558
3559 if (ffname == NULL || *ffname == NUL)
3560 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003561 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003562 if (buf->b_sfname != buf->b_ffname)
3563 VIM_CLEAR(buf->b_sfname);
3564 else
3565 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003566 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#ifdef UNIX
3568 st.st_dev = (dev_T)-1;
3569#endif
3570 }
3571 else
3572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003573 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3574 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 return FAIL;
3576
3577 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003578 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 * - if the buffer is loaded, fail
3580 * - if the buffer is not loaded, delete it from the list
3581 */
3582#ifdef UNIX
3583 if (mch_stat((char *)ffname, &st) < 0)
3584 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003585#endif
3586 if (!(buf->b_flags & BF_DUMMY))
3587#ifdef UNIX
3588 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003590 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591#endif
3592 if (obuf != NULL && obuf != buf)
3593 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003594 win_T *win;
3595 tabpage_T *tab;
3596 int in_use = FALSE;
3597
3598 // during startup a window may use a buffer that is not loaded yet
3599 FOR_ALL_TAB_WINDOWS(tab, win)
3600 if (win->w_buffer == obuf)
3601 in_use = TRUE;
3602
3603 // it's loaded or used in a window, fail
3604 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003607 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 vim_free(ffname);
3609 return FAIL;
3610 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003611 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003612 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614 sfname = vim_strsave(sfname);
3615 if (ffname == NULL || sfname == NULL)
3616 {
3617 vim_free(sfname);
3618 vim_free(ffname);
3619 return FAIL;
3620 }
3621#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003622 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003624 if (buf->b_sfname != buf->b_ffname)
3625 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 buf->b_ffname = ffname;
3628 buf->b_sfname = sfname;
3629 }
3630 buf->b_fname = buf->b_sfname;
3631#ifdef UNIX
3632 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003633 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 else
3635 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003636 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 buf->b_dev = st.st_dev;
3638 buf->b_ino = st.st_ino;
3639 }
3640#endif
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 buf_name_changed(buf);
3645 return OK;
3646}
3647
3648/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003649 * Crude way of changing the name of a buffer. Use with care!
3650 * The name should be relative to the current directory.
3651 */
3652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003653buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003654{
3655 buf_T *buf;
3656
3657 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003658 if (buf == NULL)
3659 return;
3660
3661 if (buf->b_sfname != buf->b_ffname)
3662 vim_free(buf->b_sfname);
3663 vim_free(buf->b_ffname);
3664 buf->b_ffname = vim_strsave(name);
3665 buf->b_sfname = NULL;
3666 // Allocate ffname and expand into full path. Also resolves .lnk
3667 // files on Win32.
3668 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3669 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003670}
3671
3672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 * Take care of what needs to be done when the name of buffer "buf" has
3674 * changed.
3675 */
3676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003677buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 /*
3680 * If the file name changed, also change the name of the swapfile
3681 */
3682 if (buf->b_ml.ml_mfp != NULL)
3683 ml_setname(buf);
3684
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003685#ifdef FEAT_TERMINAL
3686 if (buf->b_term != NULL)
3687 term_clear_status_text(buf->b_term);
3688#endif
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003691 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003692 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003693 status_redraw_all(); // status lines need to be redrawn
3694 fmarks_check_names(buf); // check named file marks
3695 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696}
3697
3698/*
3699 * set alternate file name for current window
3700 *
3701 * Used by do_one_cmd(), do_write() and do_ecmd().
3702 * Return the buffer.
3703 */
3704 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705setaltfname(
3706 char_u *ffname,
3707 char_u *sfname,
3708 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 buf_T *buf;
3711
Bram Moolenaarc667da52019-11-30 20:52:27 +01003712 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003714 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 curwin->w_alt_fnum = buf->b_fnum;
3716 return buf;
3717}
3718
3719/*
3720 * Get alternate file name for current window.
3721 * Return NULL if there isn't any, and give error message if requested.
3722 */
3723 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003724getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003725 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 char_u *fname;
3728 linenr_T dummy;
3729
3730 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3731 {
3732 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003733 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 return NULL;
3735 }
3736 return fname;
3737}
3738
3739/*
3740 * Add a file name to the buflist and return its number.
3741 * Uses same flags as buflist_new(), except BLN_DUMMY.
3742 *
3743 * used by qf_init(), main() and doarglist()
3744 */
3745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003746buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
3748 buf_T *buf;
3749
3750 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3751 if (buf != NULL)
3752 return buf->b_fnum;
3753 return 0;
3754}
3755
3756#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3757/*
3758 * Adjust slashes in file names. Called after 'shellslash' was set.
3759 */
3760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 buf_T *bp;
3764
Bram Moolenaar29323592016-07-24 22:04:11 +02003765 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
3767 if (bp->b_ffname != NULL)
3768 slash_adjust(bp->b_ffname);
3769 if (bp->b_sfname != NULL)
3770 slash_adjust(bp->b_sfname);
3771 }
3772}
3773#endif
3774
3775/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003776 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * Also save the local window option values.
3778 */
3779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003780buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003782 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783}
3784
3785/*
3786 * Return TRUE if 'ffname' is not the same file as current file.
3787 * Fname must have a full path (expanded by mch_FullName()).
3788 */
3789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003790otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
3792 return otherfile_buf(curbuf, ffname
3793#ifdef UNIX
3794 , NULL
3795#endif
3796 );
3797}
3798
3799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003800otherfile_buf(
3801 buf_T *buf,
3802 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003804 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003808 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3810 return TRUE;
3811 if (fnamecmp(ffname, buf->b_ffname) == 0)
3812 return FALSE;
3813#ifdef UNIX
3814 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003815 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
Bram Moolenaarc667da52019-11-30 20:52:27 +01003817 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (stp == NULL)
3819 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003820 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 st.st_dev = (dev_T)-1;
3822 stp = &st;
3823 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003824 // Use dev/ino to check if the files are the same, even when the names
3825 // are different (possible with links). Still need to compare the
3826 // name above, for when the file doesn't exist yet.
3827 // Problem: The dev/ino changes when a file is deleted (and created
3828 // again) and remains the same when renamed/moved. We don't want to
3829 // mch_stat() each buffer each time, that would be too slow. Get the
3830 // dev/ino again when they appear to match, but not when they appear
3831 // to be different: Could skip a buffer when it's actually the same
3832 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (buf_same_ino(buf, stp))
3834 {
3835 buf_setino(buf);
3836 if (buf_same_ino(buf, stp))
3837 return FALSE;
3838 }
3839 }
3840#endif
3841 return TRUE;
3842}
3843
3844#if defined(UNIX) || defined(PROTO)
3845/*
3846 * Set inode and device number for a buffer.
3847 * Must always be called when b_fname is changed!.
3848 */
3849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003850buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003852 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
3854 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3855 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003856 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 buf->b_dev = st.st_dev;
3858 buf->b_ino = st.st_ino;
3859 }
3860 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003861 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862}
3863
3864/*
3865 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3866 */
3867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003868buf_same_ino(
3869 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003870 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003872 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 && stp->st_dev == buf->b_dev
3874 && stp->st_ino == buf->b_ino);
3875}
3876#endif
3877
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003878/*
3879 * Print info about the current buffer.
3880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003882fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003883 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003884 int shorthelp,
3885 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 char_u *name;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003888 char *buffer;
John Marriottec032de2025-04-10 21:34:19 +02003889 size_t bufferlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003891 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (buffer == NULL)
3893 return;
3894
Bram Moolenaarc667da52019-11-30 20:52:27 +01003895 if (fullname > 1) // 2 CTRL-G: include buffer number
John Marriottec032de2025-04-10 21:34:19 +02003896 bufferlen = vim_snprintf_safelen(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
John Marriottec032de2025-04-10 21:34:19 +02003898 buffer[bufferlen++] = '"';
3899
3900 name = buf_spname(curbuf);
3901 if (name != NULL)
3902 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3903 IOSIZE - bufferlen, "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else
3905 {
3906 if (!fullname && curbuf->b_fname != NULL)
3907 name = curbuf->b_fname;
3908 else
3909 name = curbuf->b_ffname;
John Marriottec032de2025-04-10 21:34:19 +02003910 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)buffer + bufferlen,
3911 IOSIZE - (int)bufferlen, TRUE);
3912 bufferlen += STRLEN(buffer + bufferlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914
John Marriottec032de2025-04-10 21:34:19 +02003915 bufferlen += vim_snprintf_safelen(
3916 buffer + bufferlen,
3917 IOSIZE - bufferlen,
3918 "\"%s%s%s%s%s%s",
3919 curbufIsChanged() ? (shortmess(SHM_MOD)
3920 ? " [+]" : _(" [Modified]")) : " ",
3921 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
3922 ? _("[Not edited]") : "",
3923 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
3924 ? new_file_message() : "",
3925 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", curbuf->b_p_ro
3926 ? (shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")) : "",
3927 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro)
3928 ? " " : "");
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if (curbuf->b_ml.ml_flags & ML_EMPTY)
John Marriottec032de2025-04-10 21:34:19 +02003931 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3932 IOSIZE - bufferlen, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003934 // Current line and column are already on the screen -- webb
John Marriottec032de2025-04-10 21:34:19 +02003935 bufferlen += vim_snprintf_safelen(
3936 buffer + bufferlen,
3937 IOSIZE - bufferlen,
3938 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--", curbuf->b_ml.ml_line_count),
3939 (long)curbuf->b_ml.ml_line_count,
3940 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 else
3942 {
John Marriottec032de2025-04-10 21:34:19 +02003943 bufferlen += vim_snprintf_safelen(
3944 buffer + bufferlen,
3945 IOSIZE - bufferlen,
3946 _("line %ld of %ld --%d%%-- col "),
3947 (long)curwin->w_cursor.lnum,
3948 (long)curbuf->b_ml.ml_line_count,
3949 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
3950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 validate_virtcol();
John Marriottec032de2025-04-10 21:34:19 +02003952 bufferlen += col_print((char_u *)buffer + bufferlen, IOSIZE - bufferlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3954 }
3955
John Marriottec032de2025-04-10 21:34:19 +02003956 (void)append_arg_number(curwin, (char_u *)buffer + bufferlen,
3957 IOSIZE - bufferlen, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 if (dont_truncate)
3960 {
John Marriottec032de2025-04-10 21:34:19 +02003961 int n;
3962
Bram Moolenaarc667da52019-11-30 20:52:27 +01003963 // Temporarily set msg_scroll to avoid the message being truncated.
3964 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 msg_start();
3966 n = msg_scroll;
3967 msg_scroll = TRUE;
3968 msg(buffer);
3969 msg_scroll = n;
3970 }
3971 else
3972 {
John Marriottec032de2025-04-10 21:34:19 +02003973 char *p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003975 // Need to repeat the message after redrawing when:
3976 // - When restart_edit is set (otherwise there will be a delay
3977 // before redrawing).
3978 // - When the screen was scrolled but there is no wait-return
3979 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003980 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982
3983 vim_free(buffer);
3984}
3985
John Marriotta21240b2025-01-08 20:10:59 +01003986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003987col_print(
3988 char_u *buf,
3989 size_t buflen,
3990 int col,
3991 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 if (col == vcol)
John Marriottec032de2025-04-10 21:34:19 +02003994 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d", col);
John Marriotta21240b2025-01-08 20:10:59 +01003995
John Marriottec032de2025-04-10 21:34:19 +02003996 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999static char_u *lasttitle = NULL;
4000static char_u *lasticon = NULL;
4001
Bram Moolenaar84a93082018-06-16 22:58:15 +02004002/*
4003 * Put the file name in the title bar and icon of the window.
4004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004006maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
Bram Moolenaar84a93082018-06-16 22:58:15 +02004008 char_u *title_str = NULL;
4009 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int mustset;
4011 char_u buf[IOSIZE];
John Marriottec032de2025-04-10 21:34:19 +02004012 size_t buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014 if (!redrawing())
4015 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004016 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 need_maketitle = TRUE;
4018 return;
4019 }
4020
4021 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02004022 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004023 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 if (p_title)
4026 {
John Marriottec032de2025-04-10 21:34:19 +02004027 int maxlen = 0;
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if (p_titlelen > 0)
4030 {
4031 maxlen = p_titlelen * Columns / 100;
4032 if (maxlen < 10)
4033 maxlen = 10;
4034 }
4035
Bram Moolenaar84a93082018-06-16 22:58:15 +02004036 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (*p_titlestring != NUL)
4038 {
4039#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004040 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004041 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4042 (char_u *)"titlestring", 0,
4043 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 else
4045#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004046 title_str = p_titlestring;
John Marriottec032de2025-04-10 21:34:19 +02004047 buflen = STRLEN(title_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 else
4050 {
John Marriottec032de2025-04-10 21:34:19 +02004051 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
John Marriottec032de2025-04-10 21:34:19 +02004053 // format: "<filename> [flags] <(path)> [argument info] <- servername>"
4054 // example:
4055 // buffer.c + (/home/vim/src) (1 of 2) - VIM
4056
4057 // reserve some space for different parts of the title.
4058 // use sizeof() to introduce 'size_t' so we don't have to
4059 // cast sizes to it.
4060#define SPACE_FOR_FNAME (sizeof(buf) - 100)
4061#define SPACE_FOR_DIR (sizeof(buf) - 20)
4062#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // at least room for " - VIM"
4063
4064 // file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (curbuf->b_fname == NULL)
John Marriottec032de2025-04-10 21:34:19 +02004066 buflen = vim_snprintf_safelen((char *)buf,
4067 SPACE_FOR_FNAME, "%s", _("[No Name]"));
Bram Moolenaar21554412017-07-24 21:44:43 +02004068#ifdef FEAT_TERMINAL
4069 else if (curbuf->b_term != NULL)
John Marriottec032de2025-04-10 21:34:19 +02004070 buflen = vim_snprintf_safelen((char *)buf,
4071 SPACE_FOR_FNAME, "%s",
4072 term_get_status_text(curbuf->b_term));
Bram Moolenaar21554412017-07-24 21:44:43 +02004073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 else
4075 {
John Marriottec032de2025-04-10 21:34:19 +02004076 buflen = vim_snprintf_safelen((char *)buf,
4077 SPACE_FOR_FNAME, "%s",
4078 ((p = transstr(gettail(curbuf->b_fname))) != NULL)
4079 ? p
4080 : (char_u *)"");
4081 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083
John Marriottec032de2025-04-10 21:34:19 +02004084 // flags
Bram Moolenaar21554412017-07-24 21:44:43 +02004085#ifdef FEAT_TERMINAL
4086 if (curbuf->b_term == NULL)
4087#endif
John Marriottec032de2025-04-10 21:34:19 +02004088 {
Bram Moolenaar21554412017-07-24 21:44:43 +02004089 switch (bufIsChanged(curbuf)
4090 + (curbuf->b_p_ro * 2)
4091 + (!curbuf->b_p_ma * 4))
4092 {
John Marriottec032de2025-04-10 21:34:19 +02004093 case 1:
4094 // file was modified
4095 buflen += vim_snprintf_safelen(
4096 (char *)buf + buflen,
4097 sizeof(buf) - buflen, " +");
4098 break;
4099 case 2:
4100 // file is readonly
4101 buflen += vim_snprintf_safelen(
4102 (char *)buf + buflen,
4103 sizeof(buf) - buflen, " =");
4104 break;
4105 case 3:
4106 // file was modified and is readonly
4107 buflen += vim_snprintf_safelen(
4108 (char *)buf + buflen,
4109 sizeof(buf) - buflen, " =+");
4110 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004111 case 4:
John Marriottec032de2025-04-10 21:34:19 +02004112 case 6:
4113 // file cannot be modified
4114 buflen += vim_snprintf_safelen(
4115 (char *)buf + buflen,
4116 sizeof(buf) - buflen, " -");
4117 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004118 case 5:
John Marriottec032de2025-04-10 21:34:19 +02004119 case 7:
4120 // file cannot be modified but was modified
4121 buflen += vim_snprintf_safelen(
4122 (char *)buf + buflen,
4123 sizeof(buf) - buflen, " -+");
4124 break;
4125 default:
4126 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004127 }
John Marriottec032de2025-04-10 21:34:19 +02004128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
John Marriottec032de2025-04-10 21:34:19 +02004130 // path (surrounded by '()')
Bram Moolenaar21554412017-07-24 21:44:43 +02004131 if (curbuf->b_fname != NULL
4132#ifdef FEAT_TERMINAL
4133 && curbuf->b_term == NULL
4134#endif
4135 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004137 // Get path of file, replace home dir with ~
John Marriottec032de2025-04-10 21:34:19 +02004138 buflen += vim_snprintf_safelen((char *)buf + buflen,
4139 sizeof(buf) - buflen, " (");
4140
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 home_replace(curbuf, curbuf->b_ffname,
John Marriottec032de2025-04-10 21:34:19 +02004142 buf + buflen, (int)(SPACE_FOR_DIR - buflen), TRUE);
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004145 // avoid "c:/name" to be reduced to "c"
John Marriottec032de2025-04-10 21:34:19 +02004146 if (SAFE_isalpha(buf[buflen]) && buf[buflen + 1] == ':')
4147 buflen += 2; // step over "c:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#endif
John Marriottec032de2025-04-10 21:34:19 +02004149
4150 // determine if we have a help or normal buffer
4151 p = gettail_sep(buf + buflen);
4152 if (p == buf + buflen)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004153 {
John Marriottec032de2025-04-10 21:34:19 +02004154 // help buffer
4155 buflen += vim_snprintf_safelen((char *)buf + buflen,
4156 SPACE_FOR_DIR - buflen, "%s)", _("help"));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 else
Bram Moolenaar2c666692012-09-05 13:30:40 +02004159 {
John Marriottec032de2025-04-10 21:34:19 +02004160 // normal buffer
4161
4162 // Translate unprintable chars and concatenate. Keep some
4163 // room for the server name. When there is no room (very long
4164 // file name) use (...).
4165 if (buflen < SPACE_FOR_DIR)
John Marriott7fb90812025-04-02 20:32:35 +02004166 {
John Marriottec032de2025-04-10 21:34:19 +02004167 // remove the file name
4168 *p = NUL;
4169
4170 buflen += vim_snprintf_safelen((char *)buf + buflen,
4171 SPACE_FOR_DIR - buflen, "%s)",
4172 ((p = transstr(buf + buflen)) != NULL)
4173 ? p
4174 : (char_u *)"");
John Marriott7fb90812025-04-02 20:32:35 +02004175 vim_free(p);
4176 }
John Marriottec032de2025-04-10 21:34:19 +02004177 else
4178 buflen += vim_snprintf_safelen((char *)buf + buflen,
4179 SPACE_FOR_ARGNR - buflen, "...)");
Bram Moolenaar2c666692012-09-05 13:30:40 +02004180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182
John Marriottec032de2025-04-10 21:34:19 +02004183 // argument info
4184 buflen += append_arg_number(curwin, buf + buflen,
4185 SPACE_FOR_ARGNR - buflen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
John Marriottec032de2025-04-10 21:34:19 +02004187 // servername
4188 buflen += vim_snprintf_safelen((char *)buf + buflen,
4189 sizeof(buf) - buflen, " - %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#if defined(FEAT_CLIENTSERVER)
John Marriottec032de2025-04-10 21:34:19 +02004191 (serverName != NULL)
4192 ? serverName :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#endif
John Marriottec032de2025-04-10 21:34:19 +02004194 (char_u *)"VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 if (maxlen > 0)
4197 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004198 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004199 if (vim_strsize(buf) > maxlen)
John Marriottec032de2025-04-10 21:34:19 +02004200 trunc_string(buf, buf, maxlen, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202 }
4203 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004204 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 if (p_icon)
4207 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004208 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (*p_iconstring != NUL)
4210 {
4211#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004212 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004213 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4214 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 else
4216#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004217 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 else
4220 {
John Marriottec032de2025-04-10 21:34:19 +02004221 char_u *name;
4222 int namelen;
4223
4224 name = buf_spname(curbuf);
4225 if (name == NULL)
4226 name = gettail(curbuf->b_ffname);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004227 // Truncate name at 100 bytes.
John Marriottec032de2025-04-10 21:34:19 +02004228 namelen = (int)STRLEN(name);
4229 if (namelen > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
John Marriottec032de2025-04-10 21:34:19 +02004231 namelen -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 if (has_mbyte)
John Marriottec032de2025-04-10 21:34:19 +02004233 namelen += (*mb_tail_off)(name, name + namelen) + 1;
4234 name += namelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
John Marriottec032de2025-04-10 21:34:19 +02004236 STRCPY(buf, name);
4237 trans_characters(buf, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 }
4240
Bram Moolenaar84a93082018-06-16 22:58:15 +02004241 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 if (mustset)
4244 resettitle();
4245}
4246
4247/*
4248 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4249 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004250 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 */
4252 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004253value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 if ((str == NULL) != (*last == NULL)
4256 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4257 {
4258 vim_free(*last);
4259 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004262 mch_restore_title(
4263 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004268 return TRUE;
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 return FALSE;
4272}
4273
4274/*
4275 * Put current window title back (used after calling a shell)
4276 */
4277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004278resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279{
4280 mch_settitle(lasttitle, lasticon);
4281}
Bram Moolenaarea408852005-06-25 22:49:46 +00004282
4283# if defined(EXITFREE) || defined(PROTO)
4284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004285free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004286{
4287 vim_free(lasttitle);
4288 vim_free(lasticon);
4289}
4290# endif
4291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004293#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004294
4295/*
4296 * Used for building in the status line.
4297 */
4298typedef struct
4299{
4300 char_u *stl_start;
4301 int stl_minwid;
4302 int stl_maxwid;
4303 enum {
4304 Normal,
4305 Empty,
4306 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004307 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004308 Highlight,
4309 TabPage,
4310 Trunc
4311 } stl_type;
4312} stl_item_T;
4313
4314static size_t stl_items_len = 20; // Initial value, grows as needed.
4315static stl_item_T *stl_items = NULL;
4316static int *stl_groupitem = NULL;
4317static stl_hlrec_T *stl_hltab = NULL;
4318static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004319static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004320
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004322 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 * Return length of string in screen cells.
4324 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004325 * Normally works for window "wp", except when working for 'tabline' then it
4326 * is "curwin".
4327 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004329 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4331 *
4332 * If maxwidth is not zero, the string will be filled at any middle marker
4333 * or truncated if too long, fillchar is used for all whitespace.
4334 */
4335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004336build_stl_str_hl(
4337 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004338 char_u *out, // buffer to write into != NameBuff
4339 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004340 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004341 char_u *opt_name, // option name corresponding to "fmt"
4342 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004343 int fillchar,
4344 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004345 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4346 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
Bram Moolenaar10772302019-01-20 18:25:54 +01004348 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004349 colnr_T len;
John Marriottec032de2025-04-10 21:34:19 +02004350 size_t outputlen; // length of out[] used (excluding the NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 char_u *p;
4352 char_u *s;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004353 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004355 int use_sandbox;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004356 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357#endif
4358 int empty_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 long l;
4360 long n;
4361 int prevchar_isflag;
4362 int prevchar_isitem;
4363 int itemisflag;
4364 int fillable;
4365 char_u *str;
4366 long num;
4367 int width;
4368 int itemcnt;
4369 int curitem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004371#ifdef FEAT_EVAL
4372 int evaldepth;
4373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int minwid;
4375 int maxwid;
4376 int zeropad;
4377 char_u base;
4378 char_u opt;
4379#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004380 char_u buf_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004381 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004382 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004383 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004384 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004385 // TODO: find out why using called_emsg_before makes tests fail, does it
4386 // matter?
4387 // int called_emsg_before = called_emsg;
4388 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004389
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004390 // When inside update_screen() we do not want redrawing a statusline,
4391 // ruler, title, etc. to trigger another redraw, it may cause an endless
4392 // loop.
4393 if (updating_screen)
4394 redraw_not_allowed = TRUE;
4395
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004396 if (stl_items == NULL)
4397 {
4398 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4399 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004400
4401 // Allocate one more, because the last element is used to indicate the
4402 // end of the list.
4403 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4404 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004405
4406 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004407 }
4408
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004409#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004410 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4411 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4412
4413 // When the format starts with "%!" then evaluate it as an expression and
4414 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004415 if (fmt[0] == '%' && fmt[1] == '!')
4416 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004417 typval_T tv;
4418
4419 tv.v_type = VAR_NUMBER;
4420 tv.vval.v_number = wp->w_id;
4421 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4422
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004423 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004424 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004425 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004426
4427 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004428 }
4429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 if (fillchar == 0)
4432 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004433
Bram Moolenaar10772302019-01-20 18:25:54 +01004434 // The cursor in windows other than the current one isn't always
4435 // up-to-date, esp. because of autocommands and timers.
4436 lnum = wp->w_cursor.lnum;
4437 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4438 {
4439 lnum = wp->w_buffer->b_ml.ml_line_count;
4440 wp->w_cursor.lnum = lnum;
4441 }
4442
4443 // Get line & check if empty (cursorpos will show "0-1"). Note that
4444 // p will become invalid when getting another buffer line.
4445 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004446 empty_line = (*p == NUL);
4447
Bram Moolenaar10772302019-01-20 18:25:54 +01004448 // Get the byte value now, in case we need it below. This is more efficient
4449 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004450 len = ml_get_buf_len(wp->w_buffer, lnum);
4451 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004452 {
4453 // Line may have changed since checking the cursor column, or the lnum
4454 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004455 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004456 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004457 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004458 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004459 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004460 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004463#ifdef FEAT_EVAL
4464 evaldepth = 0;
4465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 p = out;
4467 curitem = 0;
4468 prevchar_isflag = TRUE;
4469 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004470 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004472 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004473 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004475
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004476 stl_item_T *new_items =
4477 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004478 if (new_items == NULL)
4479 break;
4480 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004481
4482 int *new_groupitem =
4483 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004484 if (new_groupitem == NULL)
4485 break;
4486 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004487
4488 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004489 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004490 if (new_hlrec == NULL)
4491 break;
4492 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004493 new_hlrec = vim_realloc(stl_tabtab,
4494 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004495 if (new_hlrec == NULL)
4496 break;
4497 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004498
4499 int *new_separator_locs = vim_realloc(stl_separator_locations,
4500 sizeof(int) * new_len);
4501 if (new_separator_locs == NULL)
4502 break;
John Marriottec032de2025-04-10 21:34:19 +02004503 stl_separator_locations = new_separator_locs;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004504
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004505 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004506 }
4507
zeertzjq122dea72022-07-27 15:48:45 +01004508 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 prevchar_isflag = prevchar_isitem = FALSE;
4510
4511 /*
4512 * Handle up to the next '%' or the end.
4513 */
4514 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4515 *p++ = *s++;
4516 if (*s == NUL || p + 1 >= out + outlen)
4517 break;
4518
4519 /*
4520 * Handle one '%' item.
4521 */
4522 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004523 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (*s == '%')
4526 {
4527 if (p + 1 >= out + outlen)
4528 break;
4529 *p++ = *s++;
4530 prevchar_isflag = prevchar_isitem = FALSE;
4531 continue;
4532 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004533 // STL_SEPARATE: Separation between items, filled with white space.
4534 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 s++;
4537 if (groupdepth > 0)
4538 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004539 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004540 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 continue;
4542 }
4543 if (*s == STL_TRUNCMARK)
4544 {
4545 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004546 stl_items[curitem].stl_type = Trunc;
4547 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 continue;
4549 }
4550 if (*s == ')')
4551 {
John Marriottec032de2025-04-10 21:34:19 +02004552 char_u *t;
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 s++;
4555 if (groupdepth < 1)
4556 continue;
4557 groupdepth--;
4558
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004559 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 *p = NUL;
4561 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004562 if (curitem > stl_groupitem[groupdepth] + 1
4563 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
John Marriottec032de2025-04-10 21:34:19 +02004565 int group_start_userhl = 0;
4566 int group_end_userhl = 0;
4567
Bram Moolenaarc667da52019-11-30 20:52:27 +01004568 // remove group if all items are empty and highlight group
4569 // doesn't change
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004570 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004571 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004572 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004573 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004574 group_start_userhl = group_end_userhl =
4575 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004576 break;
4577 }
4578 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004579 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004580 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004581 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004583 if (stl_items[n].stl_type == Highlight)
4584 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004585 }
4586 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004588 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 p = t;
4590 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004591 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004592 {
4593 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004594 if (stl_items[n].stl_type == Highlight)
4595 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004596 // adjust the start position of TabPage to the next
4597 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004598 if (stl_items[n].stl_type == TabPage)
4599 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004603 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004605 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (has_mbyte)
4607 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004608 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004610 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004613 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 }
4616 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004617 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4618 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004621 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004623
4624 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004625 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004626 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaarc667da52019-11-30 20:52:27 +01004628 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004629 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
LemonBoy57ff5262022-05-09 21:03:47 +01004631 // Minus one for the leading '<' added above.
4632 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004633 if (stl_items[l].stl_start < t)
4634 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004637 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004639 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004640 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (n < 0)
4642 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004643 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 n = 0 - n;
4645 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004646 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 else
4649 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004650 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004651 l = (n - l) * MB_CHAR2LEN(fillchar);
4652 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004654 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004656 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4657 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004659 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 }
4662 continue;
4663 }
4664 minwid = 0;
4665 maxwid = 9999;
4666 zeropad = FALSE;
4667 l = 1;
4668 if (*s == '0')
4669 {
4670 s++;
4671 zeropad = TRUE;
4672 }
4673 if (*s == '-')
4674 {
4675 s++;
4676 l = -1;
4677 }
4678 if (VIM_ISDIGIT(*s))
4679 {
4680 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004681 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 minwid = 0;
4683 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004684 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004686 stl_items[curitem].stl_type = Highlight;
4687 stl_items[curitem].stl_start = p;
4688 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 s++;
4690 curitem++;
4691 continue;
4692 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004693 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4694 {
4695 if (*s == STL_TABCLOSENR)
4696 {
4697 if (minwid == 0)
4698 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004699 // %X ends the close label, go back to the previously
4700 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004701 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004702 if (stl_items[n].stl_type == TabPage
4703 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004704 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004705 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004706 break;
4707 }
4708 }
4709 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004710 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004711 minwid = - minwid;
4712 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004713 stl_items[curitem].stl_type = TabPage;
4714 stl_items[curitem].stl_start = p;
4715 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004716 s++;
4717 curitem++;
4718 continue;
4719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (*s == '.')
4721 {
4722 s++;
4723 if (VIM_ISDIGIT(*s))
4724 {
4725 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004726 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 maxwid = 50;
4728 }
4729 }
4730 minwid = (minwid > 50 ? 50 : minwid) * l;
4731 if (*s == '(')
4732 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004733 stl_groupitem[groupdepth++] = curitem;
4734 stl_items[curitem].stl_type = Group;
4735 stl_items[curitem].stl_start = p;
4736 stl_items[curitem].stl_minwid = minwid;
4737 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 s++;
4739 curitem++;
4740 continue;
4741 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004742#ifdef FEAT_EVAL
4743 // Denotes end of expanded %{} block
4744 if (*s == '}' && evaldepth > 0)
4745 {
4746 s++;
4747 evaldepth--;
4748 continue;
4749 }
4750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (vim_strchr(STL_ALL, *s) == NULL)
4752 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004753 if (*s == NUL) // can happen with "%0"
4754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 s++;
4756 continue;
4757 }
4758 opt = *s++;
4759
Bram Moolenaarc667da52019-11-30 20:52:27 +01004760 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 base = 'D';
4762 itemisflag = FALSE;
4763 fillable = TRUE;
4764 num = -1;
4765 str = NULL;
4766 switch (opt)
4767 {
4768 case STL_FILEPATH:
4769 case STL_FULLPATH:
4770 case STL_FILENAME:
John Marriottec032de2025-04-10 21:34:19 +02004771 {
4772 char_u *name;
4773
Bram Moolenaarc667da52019-11-30 20:52:27 +01004774 fillable = FALSE; // don't change ' ' to fillchar
John Marriottec032de2025-04-10 21:34:19 +02004775 name = buf_spname(wp->w_buffer);
4776 if (name != NULL)
4777 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 else
4779 {
John Marriottec032de2025-04-10 21:34:19 +02004780 char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004781 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4783 }
4784 trans_characters(NameBuff, MAXPATHL);
4785 if (opt != STL_FILENAME)
4786 str = NameBuff;
4787 else
4788 str = gettail(NameBuff);
4789 break;
John Marriottec032de2025-04-10 21:34:19 +02004790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaarc667da52019-11-30 20:52:27 +01004792 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004793 {
4794#ifdef FEAT_EVAL
John Marriottec032de2025-04-10 21:34:19 +02004795 char_u *block_start = s - 1;
shadmansaleh30e3de22021-05-15 17:23:28 +02004796#endif
John Marriottec032de2025-04-10 21:34:19 +02004797 int reevaluate = (*s == '%');
4798 char_u *t;
4799 buf_T *save_curbuf;
4800 win_T *save_curwin;
shadmansaleh30e3de22021-05-15 17:23:28 +02004801
4802 if (reevaluate)
4803 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 itemisflag = TRUE;
4805 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004806 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4807 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004809 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 break;
4811 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004812 if (reevaluate)
John Marriottec032de2025-04-10 21:34:19 +02004813 p[-1] = NUL; // remove the % at the end of %{% expr %}
shadmansaleh30e3de22021-05-15 17:23:28 +02004814 else
John Marriottec032de2025-04-10 21:34:19 +02004815 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004818 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4819 "%d", curbuf->b_fnum);
4820 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
John Marriottec032de2025-04-10 21:34:19 +02004821 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "%d", curwin->w_id);
4822 set_internal_string_var((char_u *)"g:actual_curwin", buf_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004824 save_curbuf = curbuf;
4825 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004826 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 curwin = wp;
4828 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004829 // Visual mode is only valid in the current window.
4830 if (curwin != save_curwin)
4831 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004833 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004835 curwin = save_curwin;
4836 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004837 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004838 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004839 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
John Marriottec032de2025-04-10 21:34:19 +02004841 if (str != NULL && *str != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
4843 if (*skipdigits(str) == NUL)
4844 {
4845 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004846 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 itemisflag = FALSE;
4848 }
4849 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004850
4851 // If the output of the expression needs to be evaluated
4852 // replace the %{} block with the result of evaluation
John Marriottec032de2025-04-10 21:34:19 +02004853 if (reevaluate && str != NULL && *str != NUL
shadmansaleh30e3de22021-05-15 17:23:28 +02004854 && strchr((const char *)str, '%') != NULL
4855 && evaldepth < MAX_STL_EVAL_DEPTH)
4856 {
4857 size_t parsed_usefmt = (size_t)(block_start - usefmt);
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004858 size_t str_length = strlen((const char *)str);
4859 size_t fmt_length = strlen((const char *)s);
4860 size_t new_fmt_len = parsed_usefmt
4861 + str_length + fmt_length + 3;
4862 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004863
John Marriott7fb90812025-04-02 20:32:35 +02004864 if (new_fmt != NULL)
4865 {
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004866 char_u *new_fmt_p = new_fmt;
4867
4868 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4869 + parsed_usefmt;
4870 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4871 + str_length;
4872 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4873 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4874 + fmt_length;
4875 *new_fmt_p = 0;
4876 new_fmt_p = NULL;
John Marriott7fb90812025-04-02 20:32:35 +02004877
4878 if (usefmt != fmt)
4879 vim_free(usefmt);
4880 VIM_CLEAR(str);
4881 usefmt = new_fmt;
4882 s = usefmt + parsed_usefmt;
4883 evaldepth++;
4884 continue;
4885 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
4888 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 case STL_LINE:
4891 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4892 ? 0L : (long)(wp->w_cursor.lnum);
4893 break;
4894
4895 case STL_NUMLINES:
4896 num = wp->w_buffer->b_ml.ml_line_count;
4897 break;
4898
4899 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004900 num = (State & MODE_INSERT) == 0 && empty_line
4901 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 break;
4903
4904 case STL_VIRTCOL:
4905 case STL_VIRTCOL_ALT:
John Marriottec032de2025-04-10 21:34:19 +02004906 {
4907 colnr_T virtcol = wp->w_virtcol + 1;
4908
Bram Moolenaarc667da52019-11-30 20:52:27 +01004909 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004911 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4912 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 break;
4914 num = (long)virtcol;
4915 break;
John Marriottec032de2025-04-10 21:34:19 +02004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 case STL_PERCENTAGE:
John Marriottec032de2025-04-10 21:34:19 +02004919 num = calc_percentage((long)wp->w_cursor.lnum, (long)wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 break;
4921
4922 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004923 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004924 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 break;
4926
Luuk van Baalba936f62022-12-15 13:15:39 +00004927 case STL_SHOWCMD:
4928 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4929 str = showcmd_buf;
4930 break;
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 case STL_ARGLISTSTAT:
4933 fillable = FALSE;
John Marriottec032de2025-04-10 21:34:19 +02004934 buf_tmp[0] = NUL;
4935 if (append_arg_number(wp, buf_tmp, sizeof(buf_tmp), FALSE) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004936 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 break;
4938
4939 case STL_KEYMAP:
4940 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004941 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004942 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 break;
4944 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004945#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004946 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#else
4948 num = 0;
4949#endif
4950 break;
4951
4952 case STL_BUFNO:
4953 num = wp->w_buffer->b_fnum;
4954 break;
4955
4956 case STL_OFFSET_X:
4957 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004958 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 case STL_OFFSET:
4960#ifdef FEAT_BYTEOFF
4961 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004962 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4963 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4964 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965#endif
4966 break;
4967
4968 case STL_BYTEVAL_X:
4969 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004970 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004972 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (num == NL)
4974 num = 0;
4975 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4976 num = NL;
4977 break;
4978
4979 case STL_ROFLAG:
4980 case STL_ROFLAG_ALT:
4981 itemisflag = TRUE;
4982 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004983 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 break;
4985
4986 case STL_HELPFLAG:
4987 case STL_HELPFLAG_ALT:
4988 itemisflag = TRUE;
4989 if (wp->w_buffer->b_help)
4990 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004991 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 break;
4993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 case STL_FILETYPE:
4995 if (*wp->w_buffer->b_p_ft != NUL
4996 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4997 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004998 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004999 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005000 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 break;
5003
5004 case STL_FILETYPE_ALT:
5005 itemisflag = TRUE;
5006 if (*wp->w_buffer->b_p_ft != NUL
5007 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
5008 {
John Marriottec032de2025-04-10 21:34:19 +02005009 char_u *t;
5010
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005011 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005012 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005013 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005015 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
Bram Moolenaar4033c552017-09-16 20:54:51 +02005019#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 case STL_PREVIEWFLAG:
5021 case STL_PREVIEWFLAG_ALT:
5022 itemisflag = TRUE;
5023 if (wp->w_p_pvw)
5024 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
5025 : _("[Preview]"));
5026 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005027
5028 case STL_QUICKFIX:
5029 if (bt_quickfix(wp->w_buffer))
5030 str = (char_u *)(wp->w_llist_ref
5031 ? _(msg_loclist)
5032 : _(msg_qflist));
5033 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#endif
5035
5036 case STL_MODIFIED:
5037 case STL_MODIFIED_ALT:
5038 itemisflag = TRUE;
5039 switch ((opt == STL_MODIFIED_ALT)
5040 + bufIsChanged(wp->w_buffer) * 2
5041 + (!wp->w_buffer->b_p_ma) * 4)
5042 {
5043 case 2: str = (char_u *)"[+]"; break;
5044 case 3: str = (char_u *)",+"; break;
5045 case 4: str = (char_u *)"[-]"; break;
5046 case 5: str = (char_u *)",-"; break;
5047 case 6: str = (char_u *)"[+-]"; break;
5048 case 7: str = (char_u *)",+-"; break;
5049 }
5050 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005051
5052 case STL_HIGHLIGHT:
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005053 {
John Marriottec032de2025-04-10 21:34:19 +02005054 char_u *t = s;
5055
5056 while (*s != '#' && *s != NUL)
5057 ++s;
5058 if (*s == '#')
5059 {
5060 stl_items[curitem].stl_type = Highlight;
5061 stl_items[curitem].stl_start = p;
5062 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
5063 curitem++;
5064 }
5065 if (*s != NUL)
5066 ++s;
5067 continue;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005071 stl_items[curitem].stl_start = p;
5072 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (str != NULL && *str)
5074 {
John Marriottec032de2025-04-10 21:34:19 +02005075 char_u *t = str;
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (itemisflag)
5078 {
5079 if ((t[0] && t[1])
5080 && ((!prevchar_isitem && *t == ',')
5081 || (prevchar_isflag && *t == ' ')))
5082 t++;
5083 prevchar_isflag = TRUE;
5084 }
5085 l = vim_strsize(t);
5086 if (l > 0)
5087 prevchar_isitem = TRUE;
5088 if (l > maxwid)
5089 {
5090 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 if (has_mbyte)
5092 {
5093 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005094 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 l -= byte2cells(*t++);
5098 if (p + 1 >= out + outlen)
5099 break;
5100 *p++ = '<';
5101 }
5102 if (minwid > 0)
5103 {
5104 for (; l < minwid && p + 1 < out + outlen; l++)
5105 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005106 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5108 *p++ = ' ';
5109 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005110 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
5112 minwid = 0;
5113 }
5114 else
5115 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005116 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005118 // Change a space by fillchar, unless fillchar is '-' and a
5119 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005120 if (fillable && *t == ' '
5121 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5122 MB_CHAR2BYTES(fillchar, p);
5123 else
5124 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005127 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 else if (num >= 0)
5130 {
John Marriottec032de2025-04-10 21:34:19 +02005131 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5132 char_u nstr[20];
5133 char_u *t = nstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005136 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 prevchar_isitem = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (opt == STL_VIRTCOL_ALT)
5139 {
5140 *t++ = '-';
5141 minwid--;
5142 }
5143 *t++ = '%';
5144 if (zeropad)
5145 *t++ = '0';
5146 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005147 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
John Marriottec032de2025-04-10 21:34:19 +02005148 *t = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 for (n = num, l = 1; n >= nbase; n /= nbase)
5151 l++;
5152 if (opt == STL_VIRTCOL_ALT)
5153 l++;
John Marriottec032de2025-04-10 21:34:19 +02005154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (l > maxwid)
5156 {
5157 l += 2;
5158 n = l - maxwid;
5159 while (l-- > maxwid)
5160 num /= nbase;
5161 *t++ = '>';
5162 *t++ = '%';
5163 *t = t[-3];
John Marriottec032de2025-04-10 21:34:19 +02005164 *++t = NUL;
5165 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5166 (char *)nstr, 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 else
John Marriottec032de2025-04-10 21:34:19 +02005169 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5170 (char *)nstr, minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005173 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005175 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5176 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5177 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (opt == STL_VIM_EXPR)
5179 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 curitem++;
5181 }
5182 *p = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005183 outputlen = (size_t)(p - out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 itemcnt = curitem;
5185
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005186#ifdef FEAT_EVAL
5187 if (usefmt != fmt)
5188 vim_free(usefmt);
5189#endif
5190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 width = vim_strsize(out);
5192 if (maxwidth > 0 && width > maxwidth)
5193 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005194 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 l = 0;
5196 if (itemcnt == 0)
5197 s = out;
5198 else
5199 {
5200 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005201 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005203 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005204 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206 }
5207 if (l == itemcnt)
5208 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005209 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005210 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 l = 0;
5212 }
5213 }
5214
5215 if (width - vim_strsize(s) >= maxwidth)
5216 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005217 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (has_mbyte)
5219 {
5220 s = out;
5221 width = 0;
5222 for (;;)
5223 {
5224 width += ptr2cells(s);
5225 if (width >= maxwidth)
5226 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005227 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005229 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005231 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
5233 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 s = out + maxwidth - 1;
5235 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005236 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238 itemcnt = l;
5239 *s++ = '>';
John Marriottec032de2025-04-10 21:34:19 +02005240 *s = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 else
5243 {
John Marriottec032de2025-04-10 21:34:19 +02005244 char_u *end = out + outputlen;
5245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (has_mbyte)
5247 {
5248 n = 0;
5249 while (width >= maxwidth)
5250 {
5251 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005252 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
5254 }
5255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 n = width - maxwidth + 1;
5257 p = s + n;
John Marriottec032de2025-04-10 21:34:19 +02005258 mch_memmove(s + 1, p, (size_t)(end - p) + 1); // +1 for NUL
5259 end -= (size_t)(p - (s + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 *s = '<';
5261
Bram Moolenaarc667da52019-11-30 20:52:27 +01005262 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 for (; l < itemcnt; l++)
5264 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005265 if (stl_items[l].stl_start - n >= s)
5266 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005268 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
zeertzjqd392a742023-07-01 20:24:40 +01005270
5271 // Fill up for half a double-wide character.
5272 while (++width < maxwidth)
5273 {
John Marriottec032de2025-04-10 21:34:19 +02005274 s = end;
zeertzjqd392a742023-07-01 20:24:40 +01005275 MB_CHAR2BYTES(fillchar, s);
5276 *s = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005277 end = s;
zeertzjqd392a742023-07-01 20:24:40 +01005278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 width = maxwidth;
5281 }
John Marriottec032de2025-04-10 21:34:19 +02005282 else if (width < maxwidth && outputlen + maxwidth - width + 1 < outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005284 // Find how many separators there are, which we will use when
5285 // figuring out how many groups there are.
5286 int num_separators = 0;
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005290 if (stl_items[l].stl_type == Separate)
5291 {
5292 // Create an array of the start location for each separator
5293 // mark.
5294 stl_separator_locations[num_separators] = l;
5295 num_separators++;
5296 }
5297 }
5298
5299 // If we have separated groups, then we deal with it now
5300 if (num_separators)
5301 {
5302 int standard_spaces;
5303 int final_spaces;
5304
5305 standard_spaces = (maxwidth - width) / num_separators;
5306 final_spaces = (maxwidth - width) -
5307 standard_spaces * (num_separators - 1);
5308 for (l = 0; l < num_separators; l++)
5309 {
5310 int dislocation = (l == (num_separators - 1)) ?
5311 final_spaces : standard_spaces;
5312 dislocation *= MB_CHAR2LEN(fillchar);
5313 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5314 char_u *seploc = start + dislocation;
5315 STRMOVE(seploc, start);
5316 for (s = start; s < seploc;)
5317 MB_CHAR2BYTES(fillchar, s);
5318
5319 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5320 stl_items[i].stl_start += dislocation;
5321 }
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 width = maxwidth;
5324 }
5325 }
5326
Bram Moolenaarc667da52019-11-30 20:52:27 +01005327 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005328 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005330 *hltab = stl_hltab;
5331 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 for (l = 0; l < itemcnt; l++)
5333 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005334 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005336 sp->start = stl_items[l].stl_start;
5337 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005338 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005341 sp->start = NULL;
5342 sp->userhl = 0;
5343 }
5344
Bram Moolenaarc667da52019-11-30 20:52:27 +01005345 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005346 if (tabtab != NULL)
5347 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005348 *tabtab = stl_tabtab;
5349 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005350 for (l = 0; l < itemcnt; l++)
5351 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005352 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005353 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005354 sp->start = stl_items[l].stl_start;
5355 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005356 sp++;
5357 }
5358 }
5359 sp->start = NULL;
5360 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005363 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005364
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005365 // A user function may reset KeyTyped, restore it.
5366 KeyTyped = save_KeyTyped;
5367
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005368 // Check for an error. If there is one the display will be messed up and
5369 // might loop redrawing. Avoid that by making the corresponding option
5370 // empty.
5371 // TODO: find out why using called_emsg_before makes tests fail, does it
5372 // matter?
5373 // if (called_emsg > called_emsg_before)
5374 if (did_emsg > did_emsg_before)
5375 set_string_option_direct(opt_name, -1, (char_u *)"",
5376 OPT_FREE | opt_scope, SID_ERROR);
5377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return width;
5379}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005380#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382/*
John Marriottec032de2025-04-10 21:34:19 +02005383 * Get relative cursor position in window into "buf[]", in the localized
Emir SARI971cd2b2023-04-29 12:09:53 +01005384 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 */
John Marriotta21240b2025-01-08 20:10:59 +01005386 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005387get_rel_pos(
5388 win_T *wp,
5389 char_u *buf,
5390 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005392 long above; // number of lines above window
5393 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaarc667da52019-11-30 20:52:27 +01005395 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005396 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 above = wp->w_topline - 1;
5398#ifdef FEAT_DIFF
5399 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005400 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005401 above = 0; // All buffer lines are displayed and there is an
5402 // indication of filler lines, that can be considered
5403 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#endif
5405 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5406 if (below <= 0)
John Marriottec032de2025-04-10 21:34:19 +02005407 return (int)vim_snprintf_safelen((char *)buf, buflen,
5408 "%s", (above == 0) ? _("All") : _("Bot"));
Emir SARI971cd2b2023-04-29 12:09:53 +01005409
John Marriottec032de2025-04-10 21:34:19 +02005410 if (above <= 0)
5411 return (int)vim_snprintf_safelen((char *)buf, buflen,
5412 "%s", _("Top"));
John Marriotta21240b2025-01-08 20:10:59 +01005413
John Marriottec032de2025-04-10 21:34:19 +02005414 // localized percentage value
5415 return (int)vim_snprintf_safelen((char *)buf, buflen,
5416 _("%2d%%"), calc_percentage(above, above + below));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
5419/*
John Marriottec032de2025-04-10 21:34:19 +02005420 * Append (file 2 of 8) to "buf[]", if editing more than one file.
5421 * Return the number of characters appended.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424append_arg_number(
5425 win_T *wp,
5426 char_u *buf,
John Marriottec032de2025-04-10 21:34:19 +02005427 size_t buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005428 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005430 if (ARGCOUNT <= 1) // nothing to do
John Marriottec032de2025-04-10 21:34:19 +02005431 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
Bram Moolenaara8490a42023-05-23 18:00:58 +01005433 char *msg;
5434 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005436 case 0: msg = _(" (%d of %d)"); break;
5437 case 1: msg = _(" ((%d) of %d)"); break;
5438 case 2: msg = _(" (file %d of %d)"); break;
5439 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005441
John Marriottec032de2025-04-10 21:34:19 +02005442 return (int)vim_snprintf_safelen((char *)buf, buflen, msg,
Bram Moolenaara8490a42023-05-23 18:00:58 +01005443 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444}
5445
5446/*
5447 * If fname is not a full path, make it a full path.
5448 * Returns pointer to allocated memory (NULL for failure).
5449 */
5450 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 /*
5454 * Force expanding the path always for Unix, because symbolic links may
5455 * mess up the full path name, even though it starts with a '/'.
5456 * Also expand when there is ".." in the file name, try to remove it,
5457 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005458 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 * For MS-Windows also expand names like "longna~1" to "longname".
5460 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005461#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 return FullName_save(fname, TRUE);
5463#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005464 if (!vim_isAbsName(fname)
5465 || strstr((char *)fname, "..") != NULL
5466 || strstr((char *)fname, "//") != NULL
5467# ifdef BACKSLASH_IN_FILENAME
5468 || strstr((char *)fname, "\\\\") != NULL
5469# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005470# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 )
5474 return FullName_save(fname, FALSE);
5475
5476 fname = vim_strsave(fname);
5477
Bram Moolenaar9b942202007-10-03 12:31:33 +00005478# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005479 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005480 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 return fname;
5484#endif
5485}
5486
5487/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005488 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5489 * "*ffname" becomes a pointer to allocated memory (or NULL).
5490 * When resolving a link both "*sfname" and "*ffname" will point to the same
5491 * allocated memory.
5492 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005493 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005496fname_expand(
5497 buf_T *buf UNUSED,
5498 char_u **ffname,
5499 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005501 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005503 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005505 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507#ifdef FEAT_SHORTCUT
5508 if (!buf->b_p_bin)
5509 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005510 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005512 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005513 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005514 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
5516 vim_free(*ffname);
5517 *ffname = rfname;
5518 *sfname = rfname;
5519 }
5520 }
5521#endif
5522}
5523
5524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 * Open a window for a number of buffers.
5526 */
5527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005528ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 buf_T *buf;
5531 win_T *wp, *wpnext;
5532 int split_ret = OK;
5533 int p_ea_save;
5534 int open_wins = 0;
5535 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005536 int count; // Maximum number of windows to open.
5537 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005538 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005539 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
Bram Moolenaarc667da52019-11-30 20:52:27 +01005541 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 count = 9999;
5543 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005544 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5546 all = FALSE;
5547 else
5548 all = TRUE;
5549
Pavel Mayorove1121b12023-02-20 14:35:20 +00005550 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5551 // switching to another buffer.
5552 reset_VIsual_and_resel();
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 setpcmark();
5555
5556#ifdef FEAT_GUI
5557 need_mouse_correct = TRUE;
5558#endif
5559
5560 /*
5561 * Close superfluous windows (two windows for the same buffer).
5562 * Also close windows that are not full-width.
5563 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005564 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005565 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005566 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005568 tpnext = curtab->tp_next;
5569 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005571 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005572 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005573 || ((cmdmod.cmod_split & WSP_VERT)
5574 ? wp->w_height + wp->w_status_height < Rows - p_ch
5575 - tabline_height()
5576 : wp->w_width != Columns)
5577 || (had_tab > 0 && wp != firstwin))
5578 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005579 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005580 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005581 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005582 if (win_close(wp, FALSE) == FAIL)
5583 break;
5584 // Just in case an autocommand does something strange with
5585 // windows: start all over...
5586 wpnext = firstwin;
5587 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005588 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005589 }
5590 else
5591 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005593
Bram Moolenaarc667da52019-11-30 20:52:27 +01005594 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005595 if (had_tab == 0 || tpnext == NULL)
5596 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005597 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599
5600 /*
5601 * Go through the buffer list. When a buffer doesn't have a window yet,
5602 * open one. Otherwise move the window to the right position.
5603 * Watch out for autocommands that delete buffers or windows!
5604 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005605 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5610 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005611 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5613 continue;
5614
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005615 if (had_tab != 0)
5616 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005617 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005618 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005619 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005620 else
5621 wp = NULL;
5622 }
5623 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005624 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005625 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005626 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005627 if (wp->w_buffer == buf)
5628 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005629 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005630 if (wp != NULL)
5631 win_move_after(wp, curwin);
5632 }
5633
5634 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005636 bufref_T bufref;
5637
5638 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005639
Bram Moolenaarc667da52019-11-30 20:52:27 +01005640 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005642 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5644 ++open_wins;
5645 p_ea = p_ea_save;
5646 if (split_ret == FAIL)
5647 continue;
5648
Bram Moolenaarc667da52019-11-30 20:52:27 +01005649 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005652 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005654 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 break;
5657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 if (swap_exists_action == SEA_QUIT)
5659 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005660#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005661 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005662
Bram Moolenaarc667da52019-11-30 20:52:27 +01005663 // Reset the error/interrupt/exception state here so that
5664 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005665 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005666#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005667
Bram Moolenaarc667da52019-11-30 20:52:27 +01005668 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 win_close(curwin, TRUE);
5670 --open_wins;
5671 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005672 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005673
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005674#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005675 // Restore the error/interrupt/exception state if not
5676 // discarded by a new aborting error, interrupt, or uncaught
5677 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005678 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else
5682 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684
5685 ui_breakcheck();
5686 if (got_int)
5687 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005688 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005691#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005692 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005693 if (aborting())
5694 break;
5695#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005696 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005697 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005698 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005701 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704 /*
5705 * Close superfluous windows.
5706 */
5707 for (wp = lastwin; open_wins > count; )
5708 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005709 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (!win_valid(wp))
5712 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005713 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 wp = lastwin;
5715 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005716 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005718 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 --open_wins;
5720 wp = lastwin;
5721 }
5722 else
5723 {
5724 wp = wp->w_prev;
5725 if (wp == NULL)
5726 break;
5727 }
5728 }
5729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005732static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005733
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734/*
5735 * do_modelines() - process mode lines for the current file
5736 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005737 * "flags" can be:
5738 * OPT_WINONLY only set options local to window
5739 * OPT_NOWIN don't set options local to window
5740 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * Returns immediately if the "ml" option isn't set.
5742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005744do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005746 linenr_T lnum;
5747 int nmlines;
5748 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5751 return;
5752
Bram Moolenaarc667da52019-11-30 20:52:27 +01005753 // Disallow recursive entry here. Can happen when executing a modeline
5754 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (entered)
5756 return;
5757
5758 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005759 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005761 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 nmlines = 0;
5763
Hu Jialun9dcd3492021-08-28 20:42:50 +02005764 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005766 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 nmlines = 0;
5768 --entered;
5769}
5770
Bram Moolenaarc667da52019-11-30 20:52:27 +01005771#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
5773/*
5774 * chk_modeline() - check a single line for a mode string
5775 * Return FAIL if an error encountered.
5776 */
5777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005778chk_modeline(
5779 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005780 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 char_u *s;
John Marriottec032de2025-04-10 21:34:19 +02005783 char_u *line_end; // point to the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 char_u *e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 int prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 int retval = OK;
ichizok7e5fe382023-04-15 13:17:50 +01005787 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
5789 prev = -1;
John Marriottec032de2025-04-10 21:34:19 +02005790 s = ml_get(lnum);
5791 line_end = s + ml_get_len(lnum);
5792 for (; *s != NUL; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 if (prev == -1 || vim_isspace(prev))
5795 {
5796 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5797 || STRNCMP(s, "vi:", (size_t)3) == 0)
5798 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005799 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005800 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {
John Marriottec032de2025-04-10 21:34:19 +02005802 int vers;
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5805 e = s + 4;
5806 else
5807 e = s + 3;
5808 vers = getdigits(&e);
5809 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005810 && (s[0] != 'V'
5811 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005813 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 || (VIM_VERSION_100 < vers && s[3] == '<')
5815 || (VIM_VERSION_100 > vers && s[3] == '>')
5816 || (VIM_VERSION_100 == vers && s[3] == '=')))
5817 break;
5818 }
5819 }
5820 prev = *s;
5821 }
5822
5823 if (*s)
5824 {
John Marriottec032de2025-04-10 21:34:19 +02005825 size_t len;
5826 char_u *linecopy; // local copy of any modeline found
5827 int end;
5828
Bram Moolenaarc667da52019-11-30 20:52:27 +01005829 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 ++s;
5831 while (s[-1] != ':');
5832
John Marriottec032de2025-04-10 21:34:19 +02005833 len = (size_t)(line_end - s); // remember the line length
5834 // so we can restore 'line_end'
5835 // after the copy
5836 s = linecopy = vim_strnsave(s, len); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 if (linecopy == NULL)
5838 return FAIL;
5839
John Marriottec032de2025-04-10 21:34:19 +02005840 line_end = s + len; // restore 'line_end'
5841
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005842 // prepare for emsg()
5843 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005844 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
5846 end = FALSE;
5847 while (end == FALSE)
5848 {
5849 s = skipwhite(s);
5850 if (*s == NUL)
5851 break;
5852
5853 /*
5854 * Find end of set command: ':' or end of line.
5855 * Skip over "\:", replacing it with ":".
5856 */
5857 for (e = s; *e != ':' && *e != NUL; ++e)
5858 if (e[0] == '\\' && e[1] == ':')
John Marriottec032de2025-04-10 21:34:19 +02005859 {
5860 mch_memmove(e, e + 1, (size_t)(line_end - (e + 1)) + 1); // +1 for NUL
5861 --line_end;
5862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (*e == NUL)
5864 end = TRUE;
5865
5866 /*
5867 * If there is a "set" command, require a terminating ':' and
5868 * ignore the stuff after the ':'.
5869 * "vi:set opt opt opt: foo" -- foo not interpreted
5870 * "vi:opt opt opt: foo" -- foo interpreted
5871 * Accept "se" for compatibility with Elvis.
5872 */
5873 if (STRNCMP(s, "set ", (size_t)4) == 0
5874 || STRNCMP(s, "se ", (size_t)3) == 0)
5875 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005876 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 break;
5878 end = TRUE;
John Marriottec032de2025-04-10 21:34:19 +02005879 s += (*(s + 2) == ' ') ? 3 : 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005881 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaarc667da52019-11-30 20:52:27 +01005883 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005885 int secure_save = secure;
John Marriottec032de2025-04-10 21:34:19 +02005886 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005887
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005888 current_sctx.sc_version = 1;
5889#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005890 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005891 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005892 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005894
Bram Moolenaar5958f952018-11-20 04:25:21 +01005895 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005896 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005897
Bram Moolenaara3227e22006-03-08 21:32:40 +00005898 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005899
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005900 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005901 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005902 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 break;
5904 }
John Marriottec032de2025-04-10 21:34:19 +02005905 s = (e == line_end) ? e : e + 1; // advance to next part
5906 // careful not to go off the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908
ichizok7e5fe382023-04-15 13:17:50 +01005909 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005910 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 vim_free(linecopy);
5912 }
5913 return retval;
5914}
5915
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005916/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005917 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5918 */
5919 int
5920bt_normal(buf_T *buf)
5921{
5922 return buf != NULL && buf->b_p_bt[0] == NUL;
5923}
5924
5925/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005926 * Return TRUE if "buf" is the quickfix buffer.
5927 */
5928 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005929bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005930{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005931#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005932 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005933#else
5934 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005935#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005936}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005937
5938/*
5939 * Return TRUE if "buf" is a terminal buffer.
5940 */
5941 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005942bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005943{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005944#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005945 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005946#else
5947 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005948#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005949}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005950
5951/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005952 * Return TRUE if "buf" is a help buffer.
5953 */
5954 int
5955bt_help(buf_T *buf)
5956{
5957 return buf != NULL && buf->b_help;
5958}
5959
5960/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005961 * Return TRUE if "buf" is a prompt buffer.
5962 */
5963 int
5964bt_prompt(buf_T *buf)
5965{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005966 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5967}
5968
Dominique Pelle748b3082022-01-08 12:41:16 +00005969#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005970/*
5971 * Return TRUE if "buf" is a buffer for a popup window.
5972 */
5973 int
5974bt_popup(buf_T *buf)
5975{
5976 return buf != NULL && buf->b_p_bt != NULL
5977 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005978}
Dominique Pelle748b3082022-01-08 12:41:16 +00005979#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005980
5981/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005982 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005983 * buffer. This means the buffer name may not be a file name, at least not for
5984 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005985 */
5986 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005987bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005988{
5989 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5990 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005991 || buf->b_p_bt[0] == 't'
5992 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005993}
5994
Bram Moolenaarc3126192022-08-26 12:58:17 +01005995/*
5996 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5997 * buffer. This means the buffer is not to be read from a file.
5998 */
5999 static int
6000bt_nofileread(buf_T *buf)
6001{
6002 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
6003 || buf->b_p_bt[0] == 't'
6004 || buf->b_p_bt[0] == 'q'
6005 || buf->b_p_bt[0] == 'p');
6006}
6007
Dominique Pelle748b3082022-01-08 12:41:16 +00006008#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006009/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02006010 * Return TRUE if "buf" has 'buftype' set to "nofile".
6011 */
6012 int
6013bt_nofile(buf_T *buf)
6014{
6015 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
6016}
Dominique Pelle748b3082022-01-08 12:41:16 +00006017#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02006018
6019/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006020 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
6021 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006022 */
6023 int
6024bt_dontwrite(buf_T *buf)
6025{
Bram Moolenaarf2732452018-06-03 14:47:35 +02006026 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01006027 || buf->b_p_bt[0] == 't'
6028 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006029}
6030
6031 int
6032bt_dontwrite_msg(buf_T *buf)
6033{
6034 if (bt_dontwrite(buf))
6035 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00006036 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006037 return TRUE;
6038 }
6039 return FALSE;
6040}
6041
6042/*
6043 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
6044 * and 'bufhidden'.
6045 */
6046 int
6047buf_hide(buf_T *buf)
6048{
Bram Moolenaarc667da52019-11-30 20:52:27 +01006049 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006050 switch (buf->b_p_bh[0])
6051 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006052 case 'u': // "unload"
6053 case 'w': // "wipe"
6054 case 'd': return FALSE; // "delete"
6055 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006056 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006057 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060/*
6061 * Return special buffer name.
6062 * Returns NULL when the buffer has a normal file name.
6063 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006064 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006065buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
Bram Moolenaar4033c552017-09-16 20:54:51 +02006067#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006069 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006070 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006071 * Differentiate between the quickfix and location list buffers using
6072 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006073 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006074 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006075 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006076 else
6077 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02006080
Bram Moolenaarc667da52019-11-30 20:52:27 +01006081 // There is no _file_ when 'buftype' is "nofile", b_sfname
6082 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02006083 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {
Bram Moolenaar21554412017-07-24 21:44:43 +02006085#ifdef FEAT_TERMINAL
6086 if (buf->b_term != NULL)
6087 return term_get_status_text(buf->b_term);
6088#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02006089 if (buf->b_fname != NULL)
6090 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01006091 if (buf == cmdwin_buf)
6092 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02006093#ifdef FEAT_JOB_CHANNEL
6094 if (bt_prompt(buf))
6095 return (char_u *)_("[Prompt]");
6096#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006097#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006098 if (bt_popup(buf))
6099 return (char_u *)_("[Popup]");
6100#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006101 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006105 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 return NULL;
6107}
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006110 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6111 */
6112 char_u *
6113buf_get_fname(buf_T *buf)
6114{
6115 if (buf->b_fname == NULL)
6116 return (char_u *)_("[No Name]");
6117 return buf->b_fname;
6118}
6119
6120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6122 */
6123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006126 if (on == curbuf->b_p_bl)
6127 return;
6128
6129 curbuf->b_p_bl = on;
6130 if (on)
6131 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6132 else
6133 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134}
6135
6136/*
6137 * Read the file for "buf" again and check if the contents changed.
6138 * Return TRUE if it changed or this could not be checked.
6139 */
6140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006141buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
6143 buf_T *newbuf;
6144 int differ = TRUE;
6145 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 exarg_T ea;
6148
Bram Moolenaarc667da52019-11-30 20:52:27 +01006149 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6151 if (newbuf == NULL)
6152 return TRUE;
6153
Bram Moolenaarc667da52019-11-30 20:52:27 +01006154 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (prep_exarg(&ea, buf) == FAIL)
6156 {
6157 wipe_buffer(newbuf, FALSE);
6158 return TRUE;
6159 }
6160
Bram Moolenaare76062c2022-11-28 18:51:43 +00006161 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006163 if (curbuf != newbuf)
6164 {
6165 // Failed to find a window for "newbuf".
6166 wipe_buffer(newbuf, FALSE);
6167 return TRUE;
6168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006170 // We don't want to trigger autocommands now, they may have nasty
6171 // side-effects like wiping buffers
6172 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006173 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 && readfile(buf->b_ffname, buf->b_fname,
6175 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6176 &ea, READ_NEW | READ_DUMMY) == OK)
6177 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006178 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6180 {
6181 differ = FALSE;
6182 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6183 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6184 {
6185 differ = TRUE;
6186 break;
6187 }
6188 }
6189 }
6190 vim_free(ea.cmd);
6191
Bram Moolenaarc667da52019-11-30 20:52:27 +01006192 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaarc667da52019-11-30 20:52:27 +01006195 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 wipe_buffer(newbuf, FALSE);
6197
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006198 unblock_autocmds();
6199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 return differ;
6201}
6202
6203/*
6204 * Wipe out a buffer and decrement the last buffer number if it was used for
6205 * this buffer. Call this to wipe out a temp buffer that does not contain any
6206 * marks.
6207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209wipe_buffer(
6210 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006211 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 if (buf->b_fnum == top_file_num - 1)
6214 --top_file_num;
6215
Bram Moolenaarc667da52019-11-30 20:52:27 +01006216 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006217 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006218
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006219 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006222 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223}