blob: 0624f9dceda694b4aa1c9fc8a5763615fa7e7afd [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
Christian Brabandt51b62382024-10-06 17:31:10 +0200529 int
530buf_locked(buf_T *buf)
531{
532 return buf->b_locked || buf->b_locked_split;
533}
534
Bram Moolenaar480778b2016-07-14 22:09:39 +0200535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 * Close the link to a buffer.
537 * "action" is used when there is no longer a window for the buffer.
538 * It can be:
539 * 0 buffer becomes hidden
540 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100541 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200543 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 * When doing all but the first one on the current buffer, the caller should
545 * get a new buffer very soon!
546 *
547 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100548 *
549 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
550 * cause there to be only one window with this buffer. e.g. when ":quit" is
551 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100552 *
553 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100554 *
555 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100557 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100558close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100559 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100560 buf_T *buf,
561 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100562 int abort_if_last,
563 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100566 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200567 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100568 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200569 win_T *the_curwin = curwin;
570 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200572 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
573 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaarcee52202020-03-11 14:19:58 +0100575 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100576
577 // Force unloading or deleting when 'bufhidden' says so.
578 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
579 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100580 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200581 {
582 del_buf = TRUE;
583 unload_buf = TRUE;
584 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100585 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200586 {
587 del_buf = TRUE;
588 unload_buf = TRUE;
589 wipe_buf = TRUE;
590 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100591 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200592 unload_buf = TRUE;
593
Bram Moolenaar94053a52017-08-01 21:44:33 +0200594#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100595 // depending on how we get here b_nwindows may already be zero
596 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200597 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100598 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200599 if (term_job_running(buf->b_term))
600 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200601 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200602 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200603 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100604 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200605
Bram Moolenaarc667da52019-11-30 20:52:27 +0100606 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200607 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100608
609 // A terminal buffer is wiped out when job has finished.
610 del_buf = TRUE;
611 unload_buf = TRUE;
612 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200613 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200614 else
615 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100616 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200617 del_buf = FALSE;
618 unload_buf = FALSE;
619 }
620 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100621 else if (buf->b_p_bh[0] == 'h' && !del_buf)
622 {
623 // Hide a terminal buffer.
624 unload_buf = FALSE;
625 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200626 else
627 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100628 if (del_buf || unload_buf)
629 {
630 // A terminal buffer is wiped out if the job has finished.
631 // We only do this when there's an intention to unload the
632 // buffer. This way, :hide and other similar commands won't
633 // wipe the buffer.
634 del_buf = TRUE;
635 unload_buf = TRUE;
636 wipe_buf = TRUE;
637 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200638 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100639 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200640 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaarc667da52019-11-30 20:52:27 +0100643 // Disallow deleting the buffer when it is locked (already being closed or
644 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200645 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100646 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200647
Bram Moolenaarc667da52019-11-30 20:52:27 +0100648 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200649 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100651 // Set b_last_cursor when closing the last window for the buffer.
652 // Remember the last cursor position and window options of the buffer.
653 // This used to be only for the current window, but then options like
654 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 if (buf->b_nwindows == 1)
656 set_last_cursor(win);
657 buflist_setfpos(buf, win,
658 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
659 win->w_cursor.col, TRUE);
660 }
661
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200662 set_bufref(&bufref, buf);
663
Bram Moolenaarc667da52019-11-30 20:52:27 +0100664 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if (buf->b_nwindows == 1)
666 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200667 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100668 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200669 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
670 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200671 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100672 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100673 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200674aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000675 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100676 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100677 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200678 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100679 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200680 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100681 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200682 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // When the buffer becomes hidden, but is not unloaded, trigger
685 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (!unload_buf)
687 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200688 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100689 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200690 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
691 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200692 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200694 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200695 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100696 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200697 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100698 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200699 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100701#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100702 // autocmds may abort script processing
703 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100704 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200707
Bram Moolenaarc667da52019-11-30 20:52:27 +0100708 // If the buffer was in curwin and the window has changed, go back to that
709 // window, if it still exists. This avoids that ":edit x" triggering a
710 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200711 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
712 {
713 block_autocmds();
714 goto_tabpage_win(the_curtab, the_curwin);
715 unblock_autocmds();
716 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaarc667da52019-11-30 20:52:27 +0100720 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (buf->b_nwindows > 0)
722 --buf->b_nwindows;
723
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100724#ifdef FEAT_DIFF
725 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100726 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100727#endif
728
Bram Moolenaarc667da52019-11-30 20:52:27 +0100729 // Return when a window is displaying the buffer or when it's not
730 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100732 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Bram Moolenaarc667da52019-11-30 20:52:27 +0100734 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (buf->b_ffname == NULL)
736 del_buf = TRUE;
737
Bram Moolenaarc667da52019-11-30 20:52:27 +0100738 // When closing the current buffer stop Visual mode before freeing
739 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200740 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200741#if defined(EXITFREE)
742 && !entered_free_all_mem
743#endif
744 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200745 end_visual_mode();
746
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100747 // Free all things allocated for this buffer.
748 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
749 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100750 // Remember if we are closing the current buffer. Restore the number of
751 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 is_curbuf = (buf == curbuf);
753 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100755 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100756 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100757 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
Bram Moolenaarc667da52019-11-30 20:52:27 +0100759 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200760 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100761 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100762#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100763 // autocmds may abort script processing
764 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100765 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100768 // It's possible that autocommands change curbuf to the one being deleted.
769 // This might cause the previous curbuf to be deleted unexpectedly. But
770 // in some cases it's OK to delete the curbuf, because a new one is
771 // obtained anyway. Therefore only return if curbuf changed to the
772 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100774 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200775
Bram Moolenaar4033c552017-09-16 20:54:51 +0200776 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100777 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200778
Bram Moolenaarc667da52019-11-30 20:52:27 +0100779 // Autocommands may have opened or closed windows for this buffer.
780 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200781 if (buf->b_nwindows > 0)
782 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /*
785 * Remove the buffer from the list.
786 */
787 if (wipe_buf)
788 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200789 tabpage_T *tp;
790 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200791
Bram Moolenaar347538f2022-03-26 16:28:06 +0000792 // Do not wipe out the buffer if it is used in a window.
793 if (buf->b_nwindows > 0)
794 return FALSE;
795
zeertzjq2e7d89b2024-07-10 19:36:36 +0200796 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200797 mark_forget_file(wp, buf->b_fnum);
798
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200799 if (action == DOBUF_WIPE_REUSE)
800 {
801 // we can re-use this buffer number, store it
802 if (buf_reuse.ga_itemsize == 0)
803 ga_init2(&buf_reuse, sizeof(int), 50);
804 if (ga_grow(&buf_reuse, 1) == OK)
805 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
806 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200807 if (buf->b_sfname != buf->b_ffname)
808 VIM_CLEAR(buf->b_sfname);
809 else
810 buf->b_sfname = NULL;
811 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 if (buf->b_prev == NULL)
813 firstbuf = buf->b_next;
814 else
815 buf->b_prev->b_next = buf->b_next;
816 if (buf->b_next == NULL)
817 lastbuf = buf->b_prev;
818 else
819 buf->b_next->b_prev = buf->b_prev;
820 free_buffer(buf);
821 }
822 else
823 {
824 if (del_buf)
825 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100826 // Free all internal variables and reset option values, to make
827 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 free_buffer_stuff(buf, TRUE);
829
Bram Moolenaarc667da52019-11-30 20:52:27 +0100830 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
832
Bram Moolenaarc667da52019-11-30 20:52:27 +0100833 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 buf->b_p_initialized = FALSE;
835 }
836 buf_clear_file(buf);
837 if (del_buf)
838 buf->b_p_bl = FALSE;
839 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100840 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100841 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Make buffer not contain a file.
846 */
847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100848buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200851 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100853 buf->b_p_eof = FALSE;
854 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 buf->b_p_eol = TRUE;
856 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000858 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100860 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#ifdef FEAT_NETBEANS_INTG
862 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#endif
864}
865
866/*
867 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200868 * the file. Careful: get here with "curwin" NULL when exiting.
869 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100870 * BFA_DEL buffer is going to be deleted
871 * BFA_WIPE buffer is going to be wiped out
872 * BFA_KEEP_UNDO do not free undo information
873 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100876buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200879 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200880 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200881 win_T *the_curwin = curwin;
882 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
Bram Moolenaarc667da52019-11-30 20:52:27 +0100884 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200885 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100886 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200887 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200888 if (buf->b_ml.ml_mfp != NULL)
889 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200890 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
891 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200892 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100893 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200894 return;
895 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200896 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200898 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
899 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200900 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100901 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 return;
903 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200904 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200906 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
907 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200908 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 return;
911 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200912 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100913 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200914
Bram Moolenaarc667da52019-11-30 20:52:27 +0100915 // If the buffer was in curwin and the window has changed, go back to that
916 // window, if it still exists. This avoids that ":edit x" triggering a
917 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200918 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
919 {
920 block_autocmds();
921 goto_tabpage_win(the_curtab, the_curwin);
922 unblock_autocmds();
923 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200924
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100925#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100926 // autocmds may abort script processing
927 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100931 // It's possible that autocommands change curbuf to the one being deleted.
932 // This might cause curbuf to be deleted unexpectedly. But in some cases
933 // it's OK to delete the curbuf, because a new one is obtained anyway.
934 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 if (buf == curbuf && !is_curbuf)
936 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100938 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200940#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100941 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200942 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100943 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200944#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000945
946#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100947 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000948 {
949 win_T *win;
950 tabpage_T *tp;
951
952 FOR_ALL_TAB_WINDOWS(tp, win)
953 if (win->w_buffer == buf)
954 clearFolding(win);
955 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000956#endif
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958#ifdef FEAT_TCL
959 tcl_buffer_free(buf);
960#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100961 ml_close(buf, TRUE); // close and delete the memline/memfile
962 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200963 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100964 // free the memory allocated for undo
965 // and reset all undo information
966 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100968 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100970#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100971 clear_buf_prop_types(buf);
972#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100973 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/*
977 * Free a buffer structure and the things it contains related to the buffer
978 * itself (not the file, that must have been done already).
979 */
980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100981free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200983 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200985#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100986 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000987 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200988 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200989 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200990#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200991#ifdef FEAT_LUA
992 lua_buffer_free(buf);
993#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000994#ifdef FEAT_MZSCHEME
995 mzscheme_buffer_free(buf);
996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#ifdef FEAT_PERL
998 perl_buf_free(buf);
999#endif
1000#ifdef FEAT_PYTHON
1001 python_buffer_free(buf);
1002#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001003#ifdef FEAT_PYTHON3
1004 python3_buffer_free(buf);
1005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#ifdef FEAT_RUBY
1007 ruby_buffer_free(buf);
1008#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001009#ifdef FEAT_JOB_CHANNEL
1010 channel_buffer_free(buf);
1011#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001012#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001013 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001014#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001015#ifdef FEAT_JOB_CHANNEL
1016 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001017 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +02001018 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +02001019#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +02001020
1021 buf_hashtab_remove(buf);
1022
Bram Moolenaar9280e3f2016-07-14 23:03:19 +02001023 aubuflocal_remove(buf);
1024
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001025 if (autocmd_busy)
1026 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001027 // Do not free the buffer structure while autocommands are executing,
1028 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001029 buf->b_next = au_pending_free_buf;
1030 au_pending_free_buf = buf;
1031 }
1032 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001033 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001034 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001035 if (curbuf == buf)
1036 curbuf = NULL; // make clear it's not to be used
1037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038}
1039
1040/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001041 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001042 */
1043 static void
1044init_changedtick(buf_T *buf)
1045{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001046 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001047
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001048 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1049 di->di_tv.v_type = VAR_NUMBER;
1050 di->di_tv.v_lock = VAR_FIXED;
1051 di->di_tv.vval.v_number = 0;
1052
Bram Moolenaar92769c32017-02-25 15:41:37 +01001053#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001054 STRCPY(buf->b_ct_di.di_key, "changedtick");
1055 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001056#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001057}
1058
1059/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001060 * Free the b_wininfo list for buffer "buf".
1061 */
1062 static void
1063clear_wininfo(buf_T *buf)
1064{
1065 wininfo_T *wip;
1066
1067 while (buf->b_wininfo != NULL)
1068 {
1069 wip = buf->b_wininfo;
1070 buf->b_wininfo = wip->wi_next;
1071 free_wininfo(wip);
1072 }
1073}
1074
1075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1077 */
1078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001079free_buffer_stuff(
1080 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001081 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 if (free_options)
1084 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001085 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001087#ifdef FEAT_SPELL
1088 ga_clear(&buf->b_s.b_langp);
1089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001092 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001093 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001094
Bram Moolenaarc667da52019-11-30 20:52:27 +01001095 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001096 hash_init(&buf->b_vars->dv_hashtab);
1097 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001098 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001099 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001102 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001104 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001106#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001107 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001108#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001109#ifdef FEAT_PROP_POPUP
1110 ga_clear_strings(&buf->b_textprop_text);
1111#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001112 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1113 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001114 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
1117/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001118 * Free one wininfo_T.
1119 */
1120 void
1121free_wininfo(wininfo_T *wip)
1122{
1123 if (wip->wi_optset)
1124 {
1125 clear_winopt(&wip->wi_opt);
1126#ifdef FEAT_FOLDING
1127 deleteFoldRecurse(&wip->wi_folds);
1128#endif
1129 }
1130 vim_free(wip);
1131}
1132
1133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * Go to another buffer. Handles the result of the ATTENTION dialog.
1135 */
1136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001137goto_buffer(
1138 exarg_T *eap,
1139 int start,
1140 int dir,
1141 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001143 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001144 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001145 int skip_help_buf;
1146
1147 switch (eap->cmdidx)
1148 {
1149 case CMD_bnext:
1150 case CMD_sbnext:
1151 case CMD_bNext:
1152 case CMD_bprevious:
1153 case CMD_sbNext:
1154 case CMD_sbprevious:
1155 skip_help_buf = TRUE;
1156 break;
1157 default:
1158 skip_help_buf = FALSE;
1159 break;
1160 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001161
1162 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaar188639d2022-04-04 16:57:21 +01001164 if (swap_exists_action == SEA_NONE)
1165 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001166 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1167 (eap->forceit ? DOBUF_FORCEIT : 0) |
1168 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1170 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001171#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001172 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001173
Bram Moolenaarc667da52019-11-30 20:52:27 +01001174 // Reset the error/interrupt/exception state here so that
1175 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001176 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001177#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001178
Bram Moolenaarc667da52019-11-30 20:52:27 +01001179 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001181 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001182 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001183
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001184#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001185 // Restore the error/interrupt/exception state if not discarded by a
1186 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001187 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001191 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194/*
1195 * Handle the situation of swap_exists_action being set.
1196 * It is allowed for "old_curbuf" to be NULL or invalid.
1197 */
1198 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001199handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001201#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001202 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001203#endif
1204#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001205 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001206#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001207 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (swap_exists_action == SEA_QUIT)
1210 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001211#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001212 // Reset the error/interrupt/exception state here so that
1213 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001214 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001215#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001216
Bram Moolenaarc667da52019-11-30 20:52:27 +01001217 // User selected Quit at ATTENTION prompt. Go back to previous
1218 // buffer. If that buffer is gone or the same as the current one,
1219 // open a new, empty buffer.
1220 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001221 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001222 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001223 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1224 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001225 {
1226 // Block autocommands here because curwin->w_buffer is NULL.
1227 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001228 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001229 unblock_autocmds();
1230 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001231 else
1232 buf = old_curbuf->br_buf;
1233 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001234 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001235 int old_msg_silent = msg_silent;
1236
1237 if (shortmess(SHM_FILEINFO))
1238 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001239 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001240 // restore msg_silent, so that the command line will be shown
1241 msg_silent = old_msg_silent;
1242
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001243#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001244 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001245 check_colorcolumn(NULL, curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001246#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001247 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001248 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001249
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001250#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001251 // Restore the error/interrupt/exception state if not discarded by a
1252 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001253 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 else if (swap_exists_action == SEA_RECOVER)
1257 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001258#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001259 // Reset the error/interrupt/exception state here so that
1260 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001261 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001262#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001263
Bram Moolenaarc667da52019-11-30 20:52:27 +01001264 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001266 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001267 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001269 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001270
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001271#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001272 // Restore the error/interrupt/exception state if not discarded by a
1273 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001274 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 swap_exists_action = SEA_NONE;
1278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001281 * Make the current buffer empty.
1282 * Used when it is wiped out and it's the last buffer.
1283 */
1284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285empty_curbuf(
1286 int close_others,
1287 int forceit,
1288 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001289{
1290 int retval;
1291 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001292 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001293
1294 if (action == DOBUF_UNLOAD)
1295 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001296 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001297 return FAIL;
1298 }
1299
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001300 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001301 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001302 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001303 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001304
1305 setpcmark();
1306 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1307 forceit ? ECMD_FORCEIT : 0, curwin);
1308
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001309 // do_ecmd() may create a new buffer, then we have to delete
1310 // the old one. But do_ecmd() may have done that already, check
1311 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001312 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001313 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001314 if (!close_others)
1315 need_fileinfo = FALSE;
1316 return retval;
1317}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319/*
1320 * Implementation of the commands for the buffer list.
1321 *
1322 * action == DOBUF_GOTO go to specified buffer
1323 * action == DOBUF_SPLIT split window and go to specified buffer
1324 * action == DOBUF_UNLOAD unload specified buffer(s)
1325 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1326 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001327 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 *
1329 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1330 * start == DOBUF_FIRST go to "count" buffer from first buffer
1331 * start == DOBUF_LAST go to "count" buffer from last buffer
1332 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1333 *
1334 * Return FAIL or OK.
1335 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001336 static int
1337do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001338 int action,
1339 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001340 int dir, // FORWARD or BACKWARD
1341 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001342 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 buf_T *buf;
1345 buf_T *bp;
1346 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001347 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 switch (start)
1350 {
1351 case DOBUF_FIRST: buf = firstbuf; break;
1352 case DOBUF_LAST: buf = lastbuf; break;
1353 default: buf = curbuf; break;
1354 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001355 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 while (count-- > 0)
1358 {
1359 do
1360 {
1361 buf = buf->b_next;
1362 if (buf == NULL)
1363 buf = firstbuf;
1364 }
1365 while (buf != curbuf && !bufIsChanged(buf));
1366 }
1367 if (!bufIsChanged(buf))
1368 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001369 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 return FAIL;
1371 }
1372 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001373 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375 while (buf != NULL && buf->b_fnum != count)
1376 buf = buf->b_next;
1377 }
1378 else
1379 {
1380 bp = NULL;
1381 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1382 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001383 // remember the buffer where we start, we come back there when all
1384 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (bp == NULL)
1386 bp = buf;
1387 if (dir == FORWARD)
1388 {
1389 buf = buf->b_next;
1390 if (buf == NULL)
1391 buf = firstbuf;
1392 }
1393 else
1394 {
1395 buf = buf->b_prev;
1396 if (buf == NULL)
1397 buf = lastbuf;
1398 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001399 // Don't count unlisted buffers.
1400 // Avoid non-help buffers if the starting point was a non-help buffer and
1401 // vice-versa.
1402 if (unload || (buf->b_p_bl
1403 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001406 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408 if (bp == buf)
1409 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001410 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001411 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 return FAIL;
1413 }
1414 }
1415 }
1416
Bram Moolenaarc667da52019-11-30 20:52:27 +01001417 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
1419 if (start == DOBUF_FIRST)
1420 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001421 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001423 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001426 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001428 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 return FAIL;
1430 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001431#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001432 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001433 return OK;
1434#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001435 if (
1436 action == DOBUF_GOTO
1437 && buf != curbuf
1438 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1439 // disallow navigating to another buffer when 'winfixbuf' is applied
1440 return FAIL;
1441
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001442 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1443 && (buf->b_flags & BF_DUMMY))
1444 {
1445 // disallow navigating to the dummy buffer
1446 semsg(_(e_buffer_nr_does_not_exist), count);
1447 return FAIL;
1448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450#ifdef FEAT_GUI
1451 need_mouse_correct = TRUE;
1452#endif
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001455 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 */
1457 if (unload)
1458 {
1459 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001460 bufref_T bufref;
1461
Bram Moolenaar94f01952018-09-01 15:30:03 +02001462 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001463 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001464
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001465 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaarc667da52019-11-30 20:52:27 +01001467 // When unloading or deleting a buffer that's already unloaded and
1468 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001469 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1470 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return FAIL;
1472
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001473 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001475#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001476 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001478# ifdef FEAT_TERMINAL
1479 if (term_job_running(buf->b_term))
1480 {
1481 if (term_confirm_stop(buf) == FAIL)
1482 return FAIL;
1483 }
1484 else
1485# endif
1486 {
1487 dialog_changed(buf, FALSE);
1488 if (!bufref_valid(&bufref))
1489 // Autocommand deleted buffer, oops! It's not changed
1490 // now.
1491 return FAIL;
1492 // If it's still changed fail silently, the dialog already
1493 // mentioned why it fails.
1494 if (bufIsChanged(buf))
1495 return FAIL;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001498 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001501 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 return FAIL;
1503 }
1504 }
1505
Bram Moolenaarc667da52019-11-30 20:52:27 +01001506 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001507 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001508 end_visual_mode();
1509
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001510 // If deleting the last (listed) buffer, make it empty.
1511 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001512 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (bp->b_p_bl && bp != buf)
1514 break;
1515 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001516 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001518 // If the deleted buffer is the current one, close the current window
1519 // (unless it's the only window). Repeat this so long as we end up in
1520 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001521 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001522 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001523 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001524 {
1525 if (win_close(curwin, FALSE) == FAIL)
1526 break;
1527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001529 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (buf != curbuf)
1531 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001532 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001533 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001534 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 return OK;
1536 }
1537
1538 /*
1539 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001540 * There should be another, otherwise it would have been handled
1541 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001542 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * Then prefer the buffer we most recently visited.
1544 * Else try to find one that is loaded, after the current buffer,
1545 * then before the current buffer.
1546 * Finally use any buffer.
1547 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001548 buf = NULL; // selected buffer
1549 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001550 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1551 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001552 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 int jumpidx;
1555
1556 jumpidx = curwin->w_jumplistidx - 1;
1557 if (jumpidx < 0)
1558 jumpidx = curwin->w_jumplistlen - 1;
1559
1560 forward = jumpidx;
1561 while (jumpidx != curwin->w_jumplistidx)
1562 {
1563 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1564 if (buf != NULL)
1565 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001566 // Skip current and unlisted bufs. Also skip a quickfix
1567 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001568 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001569 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 else if (buf->b_ml.ml_mfp == NULL)
1571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001572 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (bp == NULL)
1574 bp = buf;
1575 buf = NULL;
1576 }
1577 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001578 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001580 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1582 break;
1583 if (--jumpidx < 0)
1584 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001585 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 break;
1587 }
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaarc667da52019-11-30 20:52:27 +01001590 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592 forward = TRUE;
1593 buf = curbuf->b_next;
1594 for (;;)
1595 {
1596 if (buf == NULL)
1597 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001598 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 break;
1600 buf = curbuf->b_prev;
1601 forward = FALSE;
1602 continue;
1603 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001604 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001605 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001606 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001608 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001610 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 bp = buf;
1612 }
1613 if (forward)
1614 buf = buf->b_next;
1615 else
1616 buf = buf->b_prev;
1617 }
1618 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001619 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001621 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001623 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001624 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 break;
1626 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001627 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 if (curbuf->b_next != NULL)
1630 buf = curbuf->b_next;
1631 else
1632 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001633 if (bt_quickfix(buf))
1634 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 }
1637
Bram Moolenaara02471e2014-01-10 16:43:14 +01001638 if (buf == NULL)
1639 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001640 // Autocommands must have wiped out all other buffers. Only option
1641 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001642 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001643 }
1644
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001646 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001648 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001650 // If 'switchbuf' is set jump to the window containing "buf".
1651 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001652 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 return FAIL;
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaarc667da52019-11-30 20:52:27 +01001658 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (buf == curbuf)
1660 return OK;
1661
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001662 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001663 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001666 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001668# ifdef FEAT_TERMINAL
1669 if (term_job_running(curbuf->b_term))
1670 {
1671 if (term_confirm_stop(curbuf) == FAIL)
1672 return FAIL;
1673 // Manually kill the terminal here because this command will
1674 // hide it otherwise.
1675 free_terminal(curbuf);
1676 }
1677 else
1678# endif
1679 {
1680 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001681
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001682 set_bufref(&bufref, buf);
1683 dialog_changed(curbuf, FALSE);
1684 if (!bufref_valid(&bufref))
1685 // Autocommand deleted buffer, oops!
1686 return FAIL;
1687
1688 if (bufIsChanged(curbuf))
1689 {
1690 no_write_message();
1691 return FAIL;
1692 }
1693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001698 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return FAIL;
1700 }
1701 }
1702
Bram Moolenaarc667da52019-11-30 20:52:27 +01001703 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 set_curbuf(buf, action);
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001707 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001710 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 return FAIL;
1712#endif
1713
1714 return OK;
1715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001717 int
1718do_buffer(
1719 int action,
1720 int start,
1721 int dir, // FORWARD or BACKWARD
1722 int count, // buffer number or number of buffers
1723 int forceit) // TRUE when using !
1724{
1725 return do_buffer_ext(action, start, dir, count,
1726 forceit ? DOBUF_FORCEIT : 0);
1727}
1728
1729/*
1730 * do_bufdel() - delete or unload buffer(s)
1731 *
1732 * addr_count == 0: ":bdel" - delete current buffer
1733 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1734 * buffer "end_bnr", then any other arguments.
1735 * addr_count == 2: ":N,N bdel" - delete buffers in range
1736 *
1737 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1738 * DOBUF_DEL (":bdel")
1739 *
1740 * Returns error message or NULL
1741 */
1742 char *
1743do_bufdel(
1744 int command,
1745 char_u *arg, // pointer to extra arguments
1746 int addr_count,
1747 int start_bnr, // first buffer number in a range
1748 int end_bnr, // buffer nr or last buffer nr in a range
1749 int forceit)
1750{
1751 int do_current = 0; // delete current buffer?
1752 int deleted = 0; // number of buffers deleted
1753 char *errormsg = NULL; // return value
1754 int bnr; // buffer number
1755 char_u *p;
1756
1757 if (addr_count == 0)
1758 {
1759 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1760 }
1761 else
1762 {
1763 if (addr_count == 2)
1764 {
1765 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001766 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001767 bnr = start_bnr;
1768 }
1769 else // addr_count == 1
1770 bnr = end_bnr;
1771
1772 for ( ;!got_int; ui_breakcheck())
1773 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001774 // Delete the current buffer last, otherwise when the
1775 // current buffer is deleted, the next buffer becomes
1776 // the current one and will be loaded, which may then
1777 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001778 if (bnr == curbuf->b_fnum)
1779 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001780 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001781 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1782 ++deleted;
1783
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001784 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001785 if (addr_count == 2)
1786 {
1787 if (++bnr > end_bnr)
1788 break;
1789 }
1790 else // addr_count == 1
1791 {
1792 arg = skipwhite(arg);
1793 if (*arg == NUL)
1794 break;
1795 if (!VIM_ISDIGIT(*arg))
1796 {
1797 p = skiptowhite_esc(arg);
1798 bnr = buflist_findpat(arg, p,
1799 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1800 FALSE, FALSE);
1801 if (bnr < 0) // failed
1802 break;
1803 arg = p;
1804 }
1805 else
1806 bnr = getdigits(&arg);
1807 }
1808 }
1809 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1810 FORWARD, do_current, forceit) == OK)
1811 ++deleted;
1812
1813 if (deleted == 0)
1814 {
1815 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001816 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001817 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001818 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001819 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001820 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001821 errormsg = (char *)IObuff;
1822 }
1823 else if (deleted >= p_report)
1824 {
1825 if (command == DOBUF_UNLOAD)
1826 smsg(NGETTEXT("%d buffer unloaded",
1827 "%d buffers unloaded", deleted), deleted);
1828 else if (command == DOBUF_DEL)
1829 smsg(NGETTEXT("%d buffer deleted",
1830 "%d buffers deleted", deleted), deleted);
1831 else
1832 smsg(NGETTEXT("%d buffer wiped out",
1833 "%d buffers wiped out", deleted), deleted);
1834 }
1835 }
1836
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001837 return errormsg;
1838}
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840/*
1841 * Set current buffer to "buf". Executes autocommands and closes current
1842 * buffer. "action" tells how to close the current buffer:
1843 * DOBUF_GOTO free or hide it
1844 * DOBUF_SPLIT nothing
1845 * DOBUF_UNLOAD unload it
1846 * DOBUF_DEL delete it
1847 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001848 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 */
1850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001851set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 buf_T *prevbuf;
1854 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001855 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001856#ifdef FEAT_SYN_HL
1857 long old_tw = curbuf->b_p_tw;
1858#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001859 bufref_T newbufref;
1860 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001861 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001862 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001865 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001866 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1867 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaarc667da52019-11-30 20:52:27 +01001869 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaarc667da52019-11-30 20:52:27 +01001872 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001874 set_bufref(&prevbufref, prevbuf);
1875 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001877 // Autocommands may delete the current buffer and/or the buffer we want to
1878 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001879 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001880 || (bufref_valid(&prevbufref)
1881 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001882#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001883 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001885 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001887#ifdef FEAT_SYN_HL
1888 if (prevbuf == curwin->w_buffer)
1889 reset_synblock(curwin);
1890#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001891 // autocommands may have opened a new window
1892 // with prevbuf, grr
1893 if (unload ||
1894 (last_winid != get_last_winid() &&
1895 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001896 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001897#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001898 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001900 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001902 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001903 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001904
Bram Moolenaare615db02022-01-20 21:00:54 +00001905 // Do not sync when in Insert mode and the buffer is open in
1906 // another window, might be a timer doing something in another
1907 // window.
1908 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001909 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001910 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1912 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001913 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001914 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1915 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001916 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001917 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001918 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001921 // An autocommand may have deleted "buf", already entered it (e.g., when
1922 // it did ":bunload") or aborted the script processing.
1923 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001924 valid = buf_valid(buf);
1925 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001926#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001927 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001929 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001930 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001931 // autocommands changed curbuf and we will move to another
1932 // buffer soon, so decrement curbuf->b_nwindows
1933 if (curbuf != NULL && prevbuf != curbuf)
1934 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001935 // If the buffer is not valid but curwin->w_buffer is NULL we must
1936 // enter some buffer. Using the last one is hopefully OK.
1937 if (!valid)
1938 enter_buffer(lastbuf);
1939 else
1940 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001941#ifdef FEAT_SYN_HL
1942 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001943 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001944#endif
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946}
1947
1948/*
1949 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001950 * Old curbuf must have been abandoned already! This also means "curbuf" may
1951 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001954enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001956 // when closing the current buffer stop Visual mode
1957 if (VIsual_active
1958#if defined(EXITFREE)
1959 && !entered_free_all_mem
1960#endif
1961 )
1962 end_visual_mode();
1963
Bram Moolenaar010ee962019-09-25 20:37:36 +02001964 // Get the buffer in the current window.
1965 curwin->w_buffer = buf;
1966 curbuf = buf;
1967 ++curbuf->b_nwindows;
1968
1969 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1971 if (!buf->b_help)
1972 get_winopts(buf);
1973#ifdef FEAT_FOLDING
1974 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001975 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001977 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#endif
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001981 if (curwin->w_p_diff)
1982 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983#endif
1984
Bram Moolenaara971b822011-09-14 14:43:25 +02001985#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001986 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001987#endif
1988
Bram Moolenaarc667da52019-11-30 20:52:27 +01001989 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 curwin->w_cursor.lnum = 1;
1991 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001994 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
Bram Moolenaarc667da52019-11-30 20:52:27 +01001996 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001997 curwin->w_valid = 0;
1998
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001999 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
2000 curbuf->b_last_cursor.col, TRUE);
2001
Bram Moolenaarc667da52019-11-30 20:52:27 +01002002 // Make sure the buffer is loaded.
2003 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002004 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002005 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002006 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01002007 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002008 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02002009 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002010
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002011 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 else
2014 {
Bram Moolenaareda65222019-05-16 20:29:44 +02002015 if (!msg_silent && !shortmess(SHM_FILEINFO))
2016 need_fileinfo = TRUE; // display file info after redraw
2017
2018 // check if file changed
2019 (void)buf_check_timestamp(curbuf, FALSE);
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002022#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2026 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028
Bram Moolenaarc667da52019-11-30 20:52:27 +01002029 // If autocommands did not change the cursor position, restore cursor lnum
2030 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (curwin->w_cursor.lnum == 1 && inindent(0))
2032 buflist_getfpos();
2033
Bram Moolenaarc667da52019-11-30 20:52:27 +01002034 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002036 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002037 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002038 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
Bram Moolenaar009b2592004-10-24 19:18:58 +00002040#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002041 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002042 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002043#endif
2044
Bram Moolenaarc667da52019-11-30 20:52:27 +01002045 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002046 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048#ifdef FEAT_KEYMAP
2049 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002050 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002052#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002053 // May need to set the spell language. Can only do this after the buffer
2054 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002055 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002056 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002057#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002058#ifdef FEAT_VIMINFO
2059 curbuf->b_last_used = vim_time();
2060#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002061
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002062 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063}
2064
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002065#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2066/*
2067 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002068 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002069 */
2070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002071do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002072{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002073 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002074 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002075 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002076 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002077 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002078 last_chdir_reason = "autochdir";
2079 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002080}
2081#endif
2082
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002083 static void
2084no_write_message_buf(buf_T *buf UNUSED)
2085{
2086#ifdef FEAT_TERMINAL
2087 if (term_job_running(buf->b_term))
2088 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2089 else
2090#endif
2091 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2092 buf->b_fnum);
2093}
2094
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002095 void
2096no_write_message(void)
2097{
2098#ifdef FEAT_TERMINAL
2099 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002100 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002101 else
2102#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002103 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002104}
2105
2106 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002107no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002108{
2109#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002110 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002111 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002112 else
2113#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002114 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002115}
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117/*
2118 * functions for dealing with the buffer list
2119 */
2120
2121/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002122 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2123 * only one window. That means it can be re-used.
2124 */
2125 int
2126curbuf_reusable(void)
2127{
2128 return (curbuf != NULL
2129 && curbuf->b_ffname == NULL
2130 && curbuf->b_nwindows <= 1
2131 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002132 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002133 && !curbufIsChanged());
2134}
2135
2136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 * Add a file name to the buffer list. Return a pointer to the buffer.
2138 * If the same file name already exists return a pointer to that buffer.
2139 * If it does not exist, or if fname == NULL, a new entry is created.
2140 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2141 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2142 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002143 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002144 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2145 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002146 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 * This is the ONLY way to create a new buffer.
2148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002150buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002151 char_u *ffname_arg, // full path of fname or relative
2152 char_u *sfname_arg, // short fname or NULL
2153 linenr_T lnum, // preferred cursor line
2154 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002156 char_u *ffname = ffname_arg;
2157 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 buf_T *buf;
2159#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002160 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161#endif
2162
Bram Moolenaar480778b2016-07-14 22:09:39 +02002163 if (top_file_num == 1)
2164 hash_init(&buf_hashtab);
2165
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002166 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002169 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
2171#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002172 // On Unix we can use inode numbers when the file exists. Works better
2173 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2175 st.st_dev = (dev_T)-1;
2176#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002177 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef UNIX
2179 buflist_findname_stat(ffname, &st)
2180#else
2181 buflist_findname(ffname)
2182#endif
2183 ) != NULL)
2184 {
2185 vim_free(ffname);
2186 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002187 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2188 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002189
2190 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002191 // copy the options now, if 'cpo' doesn't have 's' and not done
2192 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002193 buf_copy_options(buf, 0);
2194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2196 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002197 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002200 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002202 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002203 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002204 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002205 return NULL;
2206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
2208 return buf;
2209 }
2210
2211 /*
2212 * If the current buffer has no name and no contents, use the current
2213 * buffer. Otherwise: Need to allocate a new buffer structure.
2214 *
2215 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002216 * (A spell file buffer is allocated in spell.c, but that's not a normal
2217 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 */
2219 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002220 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
2222 buf = curbuf;
Christian Brabandtbaa8c902025-04-19 11:14:11 +02002223 trigger_undo_ftplugin(buf, curwin);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002224 // It's like this buffer is deleted. Watch out for autocommands that
2225 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002226 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2227 if (buf != curbuf) // autocommands deleted the buffer!
2228 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002229#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002230 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002231 {
2232 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237 if (buf != curbuf || curbuf == NULL)
2238 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002239 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (buf == NULL)
2241 {
2242 vim_free(ffname);
2243 return NULL;
2244 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002245#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002246 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002247 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002248 if (buf->b_vars == NULL)
2249 {
2250 vim_free(ffname);
2251 vim_free(buf);
2252 return NULL;
2253 }
2254 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2255#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002256 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 if (ffname != NULL)
2260 {
2261 buf->b_ffname = ffname;
2262 buf->b_sfname = vim_strsave(sfname);
2263 }
2264
2265 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002266 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2269 || buf->b_wininfo == NULL)
2270 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002271 if (buf->b_sfname != buf->b_ffname)
2272 VIM_CLEAR(buf->b_sfname);
2273 else
2274 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002275 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (buf != curbuf)
2277 free_buffer(buf);
2278 return NULL;
2279 }
2280
2281 if (buf == curbuf)
2282 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002283 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002284
Bram Moolenaarc667da52019-11-30 20:52:27 +01002285 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002286 buf->b_p_initialized = FALSE;
2287 buf_copy_options(buf, BCO_ENTER);
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002290 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 curbuf->b_kmap_state |= KEYMAP_INIT;
2292#endif
2293 }
2294 else
2295 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002296 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002298 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 buf->b_prev = NULL;
2301 firstbuf = buf;
2302 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002303 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 lastbuf->b_next = buf;
2306 buf->b_prev = lastbuf;
2307 }
2308 lastbuf = buf;
2309
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002310 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2311 {
2312 // Recycle a previously used buffer number. Used for buffers which
2313 // are normally hidden, e.g. in a popup window. Avoids that the
2314 // buffer number grows rapidly.
2315 --buf_reuse.ga_len;
2316 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002317
2318 // Move buffer to the right place in the buffer list.
2319 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2320 {
2321 buf_T *prev = buf->b_prev;
2322
2323 prev->b_next = buf->b_next;
2324 if (prev->b_next != NULL)
2325 prev->b_next->b_prev = prev;
2326 buf->b_next = prev;
2327 buf->b_prev = prev->b_prev;
2328 if (buf->b_prev != NULL)
2329 buf->b_prev->b_next = buf;
2330 prev->b_prev = buf;
2331 if (lastbuf == buf)
2332 lastbuf = prev;
2333 if (firstbuf == prev)
2334 firstbuf = buf;
2335 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002336 }
2337 else
2338 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002339 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002341 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002342 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002345 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347 top_file_num = 1;
2348 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002349 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002351 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 buf_copy_options(buf, BCO_ALWAYS);
2353 }
2354
2355 buf->b_wininfo->wi_fpos.lnum = lnum;
2356 buf->b_wininfo->wi_win = curwin;
2357
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002358#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002359 hash_init(&buf->b_s.b_keywtab);
2360 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361#endif
2362
2363 buf->b_fname = buf->b_sfname;
2364#ifdef UNIX
2365 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002366 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 else
2368 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002369 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 buf->b_dev = st.st_dev;
2371 buf->b_ino = st.st_ino;
2372 }
2373#endif
2374 buf->b_u_synced = TRUE;
2375 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002376 if (flags & BLN_DUMMY)
2377 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002379 clrallmarks(buf); // clear marks
2380 fmarks_check_names(buf); // check file marks for this file
2381 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (!(flags & BLN_DUMMY))
2383 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002384 bufref_T bufref;
2385
Bram Moolenaarc667da52019-11-30 20:52:27 +01002386 // Tricky: these autocommands may change the buffer list. They could
2387 // also split the window with re-using the one empty buffer. This may
2388 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002389 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002390 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002391 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002392 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002394 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002395 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002396 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002397 return NULL;
2398 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002399#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002400 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 return buf;
2406}
2407
2408/*
2409 * Free the memory for the options of a buffer.
2410 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2411 * 'fileencoding'.
2412 */
2413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414free_buf_options(
2415 buf_T *buf,
2416 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 if (free_p_ff)
2419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 clear_string_option(&buf->b_p_bh);
2423 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425#ifdef FEAT_FIND_ID
2426 clear_string_option(&buf->b_p_def);
2427 clear_string_option(&buf->b_p_inc);
2428# ifdef FEAT_EVAL
2429 clear_string_option(&buf->b_p_inex);
2430# endif
2431#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002432#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 clear_string_option(&buf->b_p_inde);
2434 clear_string_option(&buf->b_p_indk);
2435#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002436#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2437 clear_string_option(&buf->b_p_bexpr);
2438#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002439#if defined(FEAT_CRYPT)
2440 clear_string_option(&buf->b_p_cm);
2441#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002442 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002443#if defined(FEAT_EVAL)
2444 clear_string_option(&buf->b_p_fex);
2445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002447# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002448 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2449 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002450 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 clear_string_option(&buf->b_p_key);
2453#endif
2454 clear_string_option(&buf->b_p_kp);
2455 clear_string_option(&buf->b_p_mps);
2456 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002457 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002459#ifdef FEAT_VARTABS
2460 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002461 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002462 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002463 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002464 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466#ifdef FEAT_KEYMAP
2467 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002468 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 ga_clear(&buf->b_kmap_ga);
2470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#ifdef FEAT_FOLDING
2473 clear_string_option(&buf->b_p_cms);
2474#endif
2475 clear_string_option(&buf->b_p_nf);
2476#ifdef FEAT_SYN_HL
2477 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002478 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002479#endif
2480#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002481 clear_string_option(&buf->b_s.b_p_spc);
2482 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002483 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002484 buf->b_s.b_cap_prog = NULL;
2485 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002486 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 clear_string_option(&buf->b_p_cink);
2491 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002492 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002493 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002495 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002497#ifdef FEAT_COMPL_FUNC
2498 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002499 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002500 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002501 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002502 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002503 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#ifdef FEAT_QUICKFIX
2506 clear_string_option(&buf->b_p_gp);
2507 clear_string_option(&buf->b_p_mp);
2508 clear_string_option(&buf->b_p_efm);
2509#endif
2510 clear_string_option(&buf->b_p_ep);
2511 clear_string_option(&buf->b_p_path);
2512 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002513 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002514#ifdef FEAT_EVAL
2515 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002516 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002517 clear_string_option(&buf->b_p_ffu);
2518 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 clear_string_option(&buf->b_p_dict);
2521 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002522 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002524 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002525 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002526 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002527 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528}
2529
2530/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002531 * Get alternate file "n".
2532 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2533 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 * if (options & GETF_SETMARK) call setpcmark()
2535 * if (options & GETF_ALT) we are jumping to an alternate file.
2536 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2537 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002538 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 */
2540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541buflist_getfile(
2542 int n,
2543 linenr_T lnum,
2544 int options,
2545 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 pos_T *fpos;
2550 colnr_T col;
2551
2552 buf = buflist_findnr(n);
2553 if (buf == NULL)
2554 {
2555 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002556 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002558 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 return FAIL;
2560 }
2561
Bram Moolenaarc667da52019-11-30 20:52:27 +01002562 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (buf == curbuf)
2564 return OK;
2565
Bram Moolenaar71223e22022-05-30 15:23:09 +01002566 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaarc667da52019-11-30 20:52:27 +01002569 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (lnum == 0)
2571 {
2572 fpos = buflist_findfpos(buf);
2573 lnum = fpos->lnum;
2574 col = fpos->col;
2575 }
2576 else
2577 col = 0;
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (options & GETF_SWITCH)
2580 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002581 // If 'switchbuf' is set jump to the window containing "buf".
2582 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002583
Bram Moolenaarc667da52019-11-30 20:52:27 +01002584 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2585 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002586 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002587 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002589 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002590 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002591 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2592 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002594 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002599 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002600 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2601 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002603 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (!p_sol && col != 0)
2605 {
2606 curwin->w_cursor.col = col;
2607 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 curwin->w_set_curswant = TRUE;
2610 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002611 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002613
2614 if (RedrawingDisabled > 0)
2615 --RedrawingDisabled;
2616 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617}
2618
2619/*
2620 * go to the last know line number for the current buffer
2621 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 pos_T *fpos;
2626
2627 fpos = buflist_findfpos(curbuf);
2628
2629 curwin->w_cursor.lnum = fpos->lnum;
2630 check_cursor_lnum();
2631
2632 if (p_sol)
2633 curwin->w_cursor.col = 0;
2634 else
2635 {
2636 curwin->w_cursor.col = fpos->col;
2637 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 curwin->w_set_curswant = TRUE;
2640 }
2641}
2642
Bram Moolenaar81695252004-12-29 20:58:21 +00002643/*
2644 * Find file in buffer list by name (it has to be for the current window).
2645 * Returns NULL if not found.
2646 */
2647 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002648buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002649{
2650 char_u *ffname;
2651 buf_T *buf = NULL;
2652
Bram Moolenaarc667da52019-11-30 20:52:27 +01002653 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002654 ffname = FullName_save(fname,
2655#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002656 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002657#else
2658 FALSE
2659#endif
2660 );
2661 if (ffname != NULL)
2662 {
2663 buf = buflist_findname(ffname);
2664 vim_free(ffname);
2665 }
2666 return buf;
2667}
Bram Moolenaar81695252004-12-29 20:58:21 +00002668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669/*
2670 * Find file in buffer list by name (it has to be for the current window).
2671 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002672 * Skips dummy buffers.
2673 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 */
2675 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002676buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002679 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 if (mch_stat((char *)ffname, &st) < 0)
2682 st.st_dev = (dev_T)-1;
2683 return buflist_findname_stat(ffname, &st);
2684}
2685
2686/*
2687 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2688 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002689 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
2691 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002692buflist_findname_stat(
2693 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002694 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696#endif
2697 buf_T *buf;
2698
Bram Moolenaarc667da52019-11-30 20:52:27 +01002699 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002700 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002701 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#ifdef UNIX
2703 , stp
2704#endif
2705 ))
2706 return buf;
2707 return NULL;
2708}
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
2711 * Find file in buffer list by a regexp pattern.
2712 * Return fnum of the found buffer.
2713 * Return < 0 for error.
2714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002716buflist_findpat(
2717 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002718 char_u *pattern_end, // pointer to first char after pattern
2719 int unlisted, // find unlisted buffers
2720 int diffmode UNUSED, // find diff-mode buffers only
2721 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int match = -1;
2725 int find_listed;
2726 char_u *pat;
2727 char_u *patend;
2728 int attempt;
2729 char_u *p;
2730 int toggledollar;
2731
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002732 // "%" is current file, "%%" or "#" is alternate file
2733 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2734 || (in_vim9script() && pattern_end == pattern + 2
2735 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002737 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002739 else
2740 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#ifdef FEAT_DIFF
2742 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2743 match = -1;
2744#endif
2745 }
2746
2747 /*
2748 * Try four ways of matching a listed buffer:
2749 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002750 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 * attempt == 2: with '$' at end (only match at end)
2752 * attempt == 3: with '^' at start and '$' at end (only full match)
2753 * Repeat this for finding an unlisted buffer if there was no matching
2754 * listed buffer.
2755 */
2756 else
2757 {
2758 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2759 if (pat == NULL)
2760 return -1;
2761 patend = pat + STRLEN(pat) - 1;
2762 toggledollar = (patend > pat && *patend == '$');
2763
Bram Moolenaarc667da52019-11-30 20:52:27 +01002764 // First try finding a listed buffer. If not found and "unlisted"
2765 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 find_listed = TRUE;
2767 for (;;)
2768 {
2769 for (attempt = 0; attempt <= 3; ++attempt)
2770 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002771 regmatch_T regmatch;
2772
Bram Moolenaarc667da52019-11-30 20:52:27 +01002773 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002775 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002777 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002779 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002781 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002782 {
2783 if (regmatch.regprog == NULL)
2784 {
2785 // invalid pattern, possibly after switching engine
2786 vim_free(pat);
2787 return -1;
2788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if (buf->b_p_bl == find_listed
2790#ifdef FEAT_DIFF
2791 && (!diffmode || diff_mode_buf(buf))
2792#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002793 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002795 if (curtab_only)
2796 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002797 // Ignore the match if the buffer is not open in
2798 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002799 win_T *wp;
2800
Bram Moolenaar29323592016-07-24 22:04:11 +02002801 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002802 if (wp->w_buffer == buf)
2803 break;
2804 if (wp == NULL)
2805 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002806 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002807 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
2809 match = -2;
2810 break;
2811 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002812 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002816 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002817 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 break;
2819 }
2820
Bram Moolenaarc667da52019-11-30 20:52:27 +01002821 // Only search for unlisted buffers if there was no match with
2822 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (!unlisted || !find_listed || match != -1)
2824 break;
2825 find_listed = FALSE;
2826 }
2827
2828 vim_free(pat);
2829 }
2830
2831 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002832 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002834 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return match;
2836}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaar52410572019-10-27 05:12:45 +01002838#ifdef FEAT_VIMINFO
2839typedef struct {
2840 buf_T *buf;
2841 char_u *match;
2842} bufmatch_T;
2843#endif
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845/*
2846 * Find all buffer names that match.
2847 * For command line expansion of ":buf" and ":sbuf".
2848 * Return OK if matches found, FAIL otherwise.
2849 */
2850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002851ExpandBufnames(
2852 char_u *pat,
2853 int *num_file,
2854 char_u ***file,
2855 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002857 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 buf_T *buf;
2859 int round;
2860 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002861 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002862#ifdef FEAT_VIMINFO
2863 bufmatch_T *matches = NULL;
2864#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002865 int fuzzy;
2866 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002867 regmatch_T regmatch;
2868 int score = 0;
2869 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
Bram Moolenaarc667da52019-11-30 20:52:27 +01002871 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 *file = NULL;
2873
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002874#ifdef FEAT_DIFF
2875 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2876 return FAIL;
2877#endif
2878
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002879 fuzzy = cmdline_fuzzy_complete(pat);
2880
2881 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2882 // expression matching)
2883 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002885 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002886 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002887 int len = (int)STRLEN(pat);
2888 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002889 if (patc == NULL)
2890 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002891 STRNCPY(patc, pat + 1, len - 1);
2892 patc[len - 1] = NUL;
2893 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002894 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002895 else if (*pat == '^')
2896 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002897 else
2898 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002899 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002900 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002901
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002902 // round == 1: Count the matches.
2903 // round == 2: Build the array to keep the matches.
2904 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002905 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002906 count = 0;
2907 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002908 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002909 if (!buf->b_p_bl) // skip unlisted buffers
2910 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002911#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002912 if (options & BUF_DIFF_FILTER)
2913 // Skip buffers not suitable for
2914 // :diffget or :diffput completion.
2915 if (buf == curbuf || !diff_mode_buf(buf))
2916 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002917#endif
2918
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002919 if (!fuzzy)
2920 {
2921 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002922 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002923 // invalid pattern, possibly after recompiling
2924 if (to_free)
2925 vim_free(patc);
2926 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002927 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002928 p = buflist_match(&regmatch, buf, p_wic);
2929 }
2930 else
2931 {
2932 p = NULL;
2933 // first try matching with the short file name
2934 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2935 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002936 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002937 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002938 // next try matching with the full path file name
2939 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2940 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
2942 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002943
2944 if (p == NULL)
2945 continue;
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (round == 1)
2948 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002949 ++count;
2950 continue;
2951 }
2952
2953 if (options & WILD_HOME_REPLACE)
2954 p = home_replace_save(buf, p);
2955 else
2956 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002957 if (p == NULL)
2958 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002959
2960 if (!fuzzy)
2961 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002962#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002963 if (matches != NULL)
2964 {
2965 matches[count].buf = buf;
2966 matches[count].match = p;
2967 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002968 }
2969 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002970#endif
2971 (*file)[count++] = p;
2972 }
2973 else
2974 {
2975 fuzmatch[count].idx = count;
2976 fuzmatch[count].str = p;
2977 fuzmatch[count].score = score;
2978 count++;
2979 }
2980 }
2981 if (count == 0) // no match found, break here
2982 break;
2983 if (round == 1)
2984 {
2985 if (!fuzzy)
2986 {
2987 *file = ALLOC_MULT(char_u *, count);
2988 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002989 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002990 vim_regfree(regmatch.regprog);
2991 if (to_free)
2992 vim_free(patc);
2993 return FAIL;
2994 }
2995#ifdef FEAT_VIMINFO
2996 if (options & WILD_BUFLASTUSED)
2997 matches = ALLOC_MULT(bufmatch_T, count);
2998#endif
2999 }
3000 else
3001 {
3002 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
3003 if (fuzmatch == NULL)
3004 {
3005 *num_file = 0;
3006 *file = NULL;
3007 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01003013 if (!fuzzy)
3014 {
3015 vim_regfree(regmatch.regprog);
3016 if (to_free)
3017 vim_free(patc);
3018 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003019
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003020 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01003021 {
826814741_6dff3c9c2024-12-10 17:15:14 +01003022#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003023 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01003024 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003025 int i;
3026 if (count > 1)
3027 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3028 // if the current buffer is first in the list, place it at the end
3029 if (matches[0].buf == curbuf)
3030 {
3031 for (i = 1; i < count; i++)
3032 (*file)[i-1] = matches[i].match;
3033 (*file)[count-1] = matches[0].match;
3034 }
3035 else
3036 {
3037 for (i = 0; i < count; i++)
3038 (*file)[i] = matches[i].match;
3039 }
3040 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003041 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003042#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003043 }
3044 else
3045 {
3046 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3047 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003048 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 *num_file = count;
3051 return (count == 0 ? FAIL : OK);
3052}
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054/*
3055 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003056 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 */
3058 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059buflist_match(
3060 regmatch_T *rmp,
3061 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003062 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 char_u *match;
3065
Bram Moolenaarc667da52019-11-30 20:52:27 +01003066 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003067 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003068 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003069 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071 return match;
3072}
3073
3074/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003075 * Try matching the regexp in "rmp->regprog" with file name "name".
3076 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 * Return "name" when there is a match, NULL when not.
3078 */
3079 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080fname_match(
3081 regmatch_T *rmp,
3082 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003083 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
3085 char_u *match = NULL;
3086 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003088 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003089 if (name == NULL || rmp->regprog == NULL)
3090 return NULL;
3091
3092 // Ignore case when 'fileignorecase' or the argument is set.
3093 rmp->rm_ic = p_fic || ignore_case;
3094 if (vim_regexec(rmp, name, (colnr_T)0))
3095 match = name;
3096 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003098 // Replace $(HOME) with '~' and try matching again.
3099 p = home_replace_save(NULL, name);
3100 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003102 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104
3105 return match;
3106}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003109 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
3111 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003114 char_u key[VIM_SIZEOF_INT * 2 + 1];
3115 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 if (nr == 0)
3118 nr = curwin->w_alt_fnum;
John Marriottec032de2025-04-10 21:34:19 +02003119 vim_snprintf((char *)key, sizeof(key), "%x", nr);
Bram Moolenaar480778b2016-07-14 22:09:39 +02003120 hi = hash_find(&buf_hashtab, key);
3121
3122 if (!HASHITEM_EMPTY(hi))
3123 return (buf_T *)(hi->hi_key
3124 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 return NULL;
3126}
3127
3128/*
3129 * Get name of file 'n' in the buffer list.
3130 * When the file has no name an empty string is returned.
3131 * home_replace() is used to shorten the file name (used for marks).
3132 * Returns a pointer to allocated memory, of NULL when failed.
3133 */
3134 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003135buflist_nr2name(
3136 int n,
3137 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003138 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 buf_T *buf;
3141
3142 buf = buflist_findnr(n);
3143 if (buf == NULL)
3144 return NULL;
3145 return home_replace_save(helptail ? buf : NULL,
3146 fullname ? buf->b_ffname : buf->b_fname);
3147}
3148
3149/*
3150 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3151 * When "copy_options" is TRUE save the local window option values.
3152 * When "lnum" is 0 only do the options.
3153 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003155buflist_setfpos(
3156 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003157 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003158 linenr_T lnum,
3159 colnr_T col,
3160 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 wininfo_T *wip;
3163
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003164 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (wip->wi_win == win)
3166 break;
3167 if (wip == NULL)
3168 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003169 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003170 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (wip == NULL)
3172 return;
3173 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003174 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 lnum = 1;
3176 }
3177 else
3178 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003179 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 if (wip->wi_prev)
3181 wip->wi_prev->wi_next = wip->wi_next;
3182 else
3183 buf->b_wininfo = wip->wi_next;
3184 if (wip->wi_next)
3185 wip->wi_next->wi_prev = wip->wi_prev;
3186 if (copy_options && wip->wi_optset)
3187 {
3188 clear_winopt(&wip->wi_opt);
3189#ifdef FEAT_FOLDING
3190 deleteFoldRecurse(&wip->wi_folds);
3191#endif
3192 }
3193 }
3194 if (lnum != 0)
3195 {
3196 wip->wi_fpos.lnum = lnum;
3197 wip->wi_fpos.col = col;
3198 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003199 if (win != NULL)
3200 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003201 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003203 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3205#ifdef FEAT_FOLDING
3206 wip->wi_fold_manual = win->w_fold_manual;
3207 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3208#endif
3209 wip->wi_optset = TRUE;
3210 }
3211
Bram Moolenaarc667da52019-11-30 20:52:27 +01003212 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 wip->wi_next = buf->b_wininfo;
3214 buf->b_wininfo = wip;
3215 wip->wi_prev = NULL;
3216 if (wip->wi_next)
3217 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218}
3219
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003220#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003221/*
3222 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3223 * page. That's because a diff is local to a tab page.
3224 */
3225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003227{
3228 win_T *wp;
3229
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003230 if (!wip->wi_opt.wo_diff)
3231 return FALSE;
3232
3233 FOR_ALL_WINDOWS(wp)
3234 // return FALSE when it's a window in the current tab page, thus
3235 // the buffer was in diff mode here
3236 if (wip->wi_win == wp)
3237 return FALSE;
3238 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003239}
3240#endif
3241
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242/*
3243 * Find info for the current window in buffer "buf".
3244 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003245 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003246 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3247 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * Returns NULL when there isn't any info.
3249 */
3250 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003251find_wininfo(
3252 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003253 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003254 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 wininfo_T *wip;
3257
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003258 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003259 if (wip->wi_win == curwin
3260#ifdef FEAT_DIFF
3261 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3262#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003263
3264 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003266
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003267 if (wip != NULL)
3268 return wip;
3269
Bram Moolenaarc667da52019-11-30 20:52:27 +01003270 // If no wininfo for curwin, use the first in the list (that doesn't have
3271 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003272 // If "need_options" is TRUE skip entries that don't have options set,
3273 // unless the window is editing "buf", so we can copy from the window
3274 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003275#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003276 if (skip_diff_buffer)
3277 {
3278 FOR_ALL_BUF_WININFO(buf, wip)
3279 if (!wininfo_other_tab_diff(wip)
3280 && (!need_options || wip->wi_optset
3281 || (wip->wi_win != NULL
3282 && wip->wi_win->w_buffer == buf)))
3283 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003284 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003285 else
3286#endif
3287 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return wip;
3289}
3290
3291/*
3292 * Reset the local window options to the values last used in this window.
3293 * If the buffer wasn't used in this window before, use the values from
3294 * the most recently used window. If the values were never set, use the
3295 * global values for the window.
3296 */
3297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 wininfo_T *wip;
3301
3302 clear_winopt(&curwin->w_onebuf_opt);
3303#ifdef FEAT_FOLDING
3304 clearFolding(curwin);
3305#endif
3306
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003307 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003308 if (wip != NULL && wip->wi_win != NULL
3309 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003311 // The buffer is currently displayed in the window: use the actual
3312 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003313 win_T *wp = wip->wi_win;
3314
3315 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3316#ifdef FEAT_FOLDING
3317 curwin->w_fold_manual = wp->w_fold_manual;
3318 curwin->w_foldinvalid = TRUE;
3319 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3320#endif
3321 }
3322 else if (wip != NULL && wip->wi_optset)
3323 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003324 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3326#ifdef FEAT_FOLDING
3327 curwin->w_fold_manual = wip->wi_fold_manual;
3328 curwin->w_foldinvalid = TRUE;
3329 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3330#endif
3331 }
3332 else
3333 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003334 if (wip != NULL)
3335 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
3337#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003338 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (p_fdls >= 0)
3340 curwin->w_p_fdl = p_fdls;
3341#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003342 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343}
3344
3345/*
3346 * Find the position (lnum and col) for the buffer 'buf' for the current
3347 * window.
3348 * Returns a pointer to no_position if no position is found.
3349 */
3350 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003354 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003356 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (wip != NULL)
3358 return &(wip->wi_fpos);
3359 else
3360 return &no_position;
3361}
3362
3363/*
3364 * Find the lnum for the buffer 'buf' for the current window.
3365 */
3366 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 return buflist_findfpos(buf)->lnum;
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003373 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003376buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaar52410572019-10-27 05:12:45 +01003378 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 int len;
3380 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003381 int ro_char;
3382 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003383#ifdef FEAT_TERMINAL
3384 int job_running;
3385 int job_none_open;
3386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
Bram Moolenaar52410572019-10-27 05:12:45 +01003388#ifdef FEAT_VIMINFO
3389 garray_T buflist;
3390 buf_T **buflist_data = NULL, **p;
3391
3392 if (vim_strchr(eap->arg, 't'))
3393 {
3394 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003395 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003396 {
3397 if (ga_grow(&buflist, 1) == OK)
3398 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3399 }
3400
3401 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3402 sizeof(buf_T *), buf_compare);
3403
Bram Moolenaar3b991522019-11-06 23:26:20 +01003404 buflist_data = (buf_T **)buflist.ga_data;
3405 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003406 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003407 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003408
Bram Moolenaar3b991522019-11-06 23:26:20 +01003409 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003410 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3411 : buf->b_next)
3412#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
John Marriottec032de2025-04-10 21:34:19 +02003416 char_u *name;
3417
Bram Moolenaar0751f512018-03-29 16:37:16 +02003418#ifdef FEAT_TERMINAL
3419 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003420 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003421#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003422 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003423 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3424 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3425 || (vim_strchr(eap->arg, '+')
3426 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3427 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003428 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003429 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003430 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3431#ifdef FEAT_TERMINAL
3432 || (vim_strchr(eap->arg, 'R')
3433 && (!job_running || (job_running && job_none_open)))
3434 || (vim_strchr(eap->arg, '?')
3435 && (!job_running || (job_running && !job_none_open)))
3436 || (vim_strchr(eap->arg, 'F')
3437 && (job_running || buf->b_term == NULL))
3438#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003439 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3440 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3441 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3442 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3443 || (vim_strchr(eap->arg, '#')
3444 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 continue;
John Marriottec032de2025-04-10 21:34:19 +02003446 name = buf_spname(buf);
3447 if (name != NULL)
3448 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 else
3450 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003451 if (message_filtered(NameBuff))
3452 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003454 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3455 : (bufIsChanged(buf) ? '+' : ' ');
3456#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003457 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003458 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003459 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003460 ro_char = '?';
3461 else
3462 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003463 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3464 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003465 }
3466 else if (buf->b_term != NULL)
3467 ro_char = 'F';
3468 else
3469#endif
3470 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3471
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003472 msg_putchar('\n');
John Marriottec032de2025-04-10 21:34:19 +02003473 len = (int)vim_snprintf_safelen((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 buf->b_fnum,
3475 buf->b_p_bl ? ' ' : 'u',
3476 buf == curbuf ? '%' :
3477 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3478 buf->b_ml.ml_mfp == NULL ? ' ' :
3479 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003480 ro_char,
3481 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003482 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaarc667da52019-11-30 20:52:27 +01003484 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 i = 40 - vim_strsize(IObuff);
3486 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003488 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003489#ifdef FEAT_VIMINFO
3490 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3491 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3492 else
3493#endif
3494 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3495 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003496 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003498 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 ui_breakcheck();
3500 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003501
3502#ifdef FEAT_VIMINFO
3503 if (buflist_data)
3504 ga_clear(&buflist);
3505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508/*
3509 * Get file name and line number for file 'fnum'.
3510 * Used by DoOneCmd() for translating '%' and '#'.
3511 * Used by insert_reg() and cmdline_paste() for '#' register.
3512 * Return FAIL if not found, OK for success.
3513 */
3514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515buflist_name_nr(
3516 int fnum,
3517 char_u **fname,
3518 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 buf_T *buf;
3521
3522 buf = buflist_findnr(fnum);
3523 if (buf == NULL || buf->b_fname == NULL)
3524 return FAIL;
3525
3526 *fname = buf->b_fname;
3527 *lnum = buflist_findlnum(buf);
3528
3529 return OK;
3530}
3531
3532/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003533 * Set the file name for "buf"' to "ffname_arg", short file name to
3534 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 * The file name with the full path is also remembered, for when :cd is used.
3536 * Returns FAIL for failure (file name already in use by other buffer)
3537 * OK otherwise.
3538 */
3539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540setfname(
3541 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003542 char_u *ffname_arg,
3543 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003544 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003546 char_u *ffname = ffname_arg;
3547 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003548 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003550 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
3552
3553 if (ffname == NULL || *ffname == NUL)
3554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003555 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003556 if (buf->b_sfname != buf->b_ffname)
3557 VIM_CLEAR(buf->b_sfname);
3558 else
3559 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003560 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#ifdef UNIX
3562 st.st_dev = (dev_T)-1;
3563#endif
3564 }
3565 else
3566 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003567 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3568 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return FAIL;
3570
3571 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003572 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 * - if the buffer is loaded, fail
3574 * - if the buffer is not loaded, delete it from the list
3575 */
3576#ifdef UNIX
3577 if (mch_stat((char *)ffname, &st) < 0)
3578 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003579#endif
3580 if (!(buf->b_flags & BF_DUMMY))
3581#ifdef UNIX
3582 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003584 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#endif
3586 if (obuf != NULL && obuf != buf)
3587 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003588 win_T *win;
3589 tabpage_T *tab;
3590 int in_use = FALSE;
3591
3592 // during startup a window may use a buffer that is not loaded yet
3593 FOR_ALL_TAB_WINDOWS(tab, win)
3594 if (win->w_buffer == obuf)
3595 in_use = TRUE;
3596
3597 // it's loaded or used in a window, fail
3598 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003601 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 vim_free(ffname);
3603 return FAIL;
3604 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003605 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003606 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 sfname = vim_strsave(sfname);
3609 if (ffname == NULL || sfname == NULL)
3610 {
3611 vim_free(sfname);
3612 vim_free(ffname);
3613 return FAIL;
3614 }
3615#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003616 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003618 if (buf->b_sfname != buf->b_ffname)
3619 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 buf->b_ffname = ffname;
3622 buf->b_sfname = sfname;
3623 }
3624 buf->b_fname = buf->b_sfname;
3625#ifdef UNIX
3626 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003627 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 else
3629 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003630 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 buf->b_dev = st.st_dev;
3632 buf->b_ino = st.st_ino;
3633 }
3634#endif
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 buf_name_changed(buf);
3639 return OK;
3640}
3641
3642/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003643 * Crude way of changing the name of a buffer. Use with care!
3644 * The name should be relative to the current directory.
3645 */
3646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003647buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003648{
3649 buf_T *buf;
3650
3651 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003652 if (buf == NULL)
3653 return;
3654
3655 if (buf->b_sfname != buf->b_ffname)
3656 vim_free(buf->b_sfname);
3657 vim_free(buf->b_ffname);
3658 buf->b_ffname = vim_strsave(name);
3659 buf->b_sfname = NULL;
3660 // Allocate ffname and expand into full path. Also resolves .lnk
3661 // files on Win32.
3662 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3663 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003664}
3665
3666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 * Take care of what needs to be done when the name of buffer "buf" has
3668 * changed.
3669 */
3670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003671buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 /*
3674 * If the file name changed, also change the name of the swapfile
3675 */
3676 if (buf->b_ml.ml_mfp != NULL)
3677 ml_setname(buf);
3678
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003679#ifdef FEAT_TERMINAL
3680 if (buf->b_term != NULL)
3681 term_clear_status_text(buf->b_term);
3682#endif
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003685 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003686 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003687 status_redraw_all(); // status lines need to be redrawn
3688 fmarks_check_names(buf); // check named file marks
3689 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690}
3691
3692/*
3693 * set alternate file name for current window
3694 *
3695 * Used by do_one_cmd(), do_write() and do_ecmd().
3696 * Return the buffer.
3697 */
3698 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003699setaltfname(
3700 char_u *ffname,
3701 char_u *sfname,
3702 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 buf_T *buf;
3705
Bram Moolenaarc667da52019-11-30 20:52:27 +01003706 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003708 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 curwin->w_alt_fnum = buf->b_fnum;
3710 return buf;
3711}
3712
3713/*
3714 * Get alternate file name for current window.
3715 * Return NULL if there isn't any, and give error message if requested.
3716 */
3717 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003718getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003719 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
3721 char_u *fname;
3722 linenr_T dummy;
3723
3724 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3725 {
3726 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003727 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 return NULL;
3729 }
3730 return fname;
3731}
3732
3733/*
3734 * Add a file name to the buflist and return its number.
3735 * Uses same flags as buflist_new(), except BLN_DUMMY.
3736 *
3737 * used by qf_init(), main() and doarglist()
3738 */
3739 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003740buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 buf_T *buf;
3743
3744 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3745 if (buf != NULL)
3746 return buf->b_fnum;
3747 return 0;
3748}
3749
3750#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3751/*
3752 * Adjust slashes in file names. Called after 'shellslash' was set.
3753 */
3754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003755buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 buf_T *bp;
3758
Bram Moolenaar29323592016-07-24 22:04:11 +02003759 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
3761 if (bp->b_ffname != NULL)
3762 slash_adjust(bp->b_ffname);
3763 if (bp->b_sfname != NULL)
3764 slash_adjust(bp->b_sfname);
3765 }
3766}
3767#endif
3768
3769/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003770 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 * Also save the local window option values.
3772 */
3773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003776 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777}
3778
3779/*
3780 * Return TRUE if 'ffname' is not the same file as current file.
3781 * Fname must have a full path (expanded by mch_FullName()).
3782 */
3783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003784otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 return otherfile_buf(curbuf, ffname
3787#ifdef UNIX
3788 , NULL
3789#endif
3790 );
3791}
3792
3793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003794otherfile_buf(
3795 buf_T *buf,
3796 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003798 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003800 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003802 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3804 return TRUE;
3805 if (fnamecmp(ffname, buf->b_ffname) == 0)
3806 return FALSE;
3807#ifdef UNIX
3808 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003809 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaarc667da52019-11-30 20:52:27 +01003811 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (stp == NULL)
3813 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003814 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 st.st_dev = (dev_T)-1;
3816 stp = &st;
3817 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003818 // Use dev/ino to check if the files are the same, even when the names
3819 // are different (possible with links). Still need to compare the
3820 // name above, for when the file doesn't exist yet.
3821 // Problem: The dev/ino changes when a file is deleted (and created
3822 // again) and remains the same when renamed/moved. We don't want to
3823 // mch_stat() each buffer each time, that would be too slow. Get the
3824 // dev/ino again when they appear to match, but not when they appear
3825 // to be different: Could skip a buffer when it's actually the same
3826 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (buf_same_ino(buf, stp))
3828 {
3829 buf_setino(buf);
3830 if (buf_same_ino(buf, stp))
3831 return FALSE;
3832 }
3833 }
3834#endif
3835 return TRUE;
3836}
3837
3838#if defined(UNIX) || defined(PROTO)
3839/*
3840 * Set inode and device number for a buffer.
3841 * Must always be called when b_fname is changed!.
3842 */
3843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003846 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3849 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003850 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 buf->b_dev = st.st_dev;
3852 buf->b_ino = st.st_ino;
3853 }
3854 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003855 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856}
3857
3858/*
3859 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3860 */
3861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003862buf_same_ino(
3863 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003864 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003866 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 && stp->st_dev == buf->b_dev
3868 && stp->st_ino == buf->b_ino);
3869}
3870#endif
3871
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003872/*
3873 * Print info about the current buffer.
3874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003876fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003877 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003878 int shorthelp,
3879 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
3881 char_u *name;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003882 char *buffer;
John Marriottec032de2025-04-10 21:34:19 +02003883 size_t bufferlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003885 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (buffer == NULL)
3887 return;
3888
Bram Moolenaarc667da52019-11-30 20:52:27 +01003889 if (fullname > 1) // 2 CTRL-G: include buffer number
John Marriottec032de2025-04-10 21:34:19 +02003890 bufferlen = vim_snprintf_safelen(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
John Marriottec032de2025-04-10 21:34:19 +02003892 buffer[bufferlen++] = '"';
3893
3894 name = buf_spname(curbuf);
3895 if (name != NULL)
3896 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3897 IOSIZE - bufferlen, "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
3899 {
3900 if (!fullname && curbuf->b_fname != NULL)
3901 name = curbuf->b_fname;
3902 else
3903 name = curbuf->b_ffname;
John Marriottec032de2025-04-10 21:34:19 +02003904 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)buffer + bufferlen,
3905 IOSIZE - (int)bufferlen, TRUE);
3906 bufferlen += STRLEN(buffer + bufferlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908
John Marriottec032de2025-04-10 21:34:19 +02003909 bufferlen += vim_snprintf_safelen(
3910 buffer + bufferlen,
3911 IOSIZE - bufferlen,
3912 "\"%s%s%s%s%s%s",
3913 curbufIsChanged() ? (shortmess(SHM_MOD)
3914 ? " [+]" : _(" [Modified]")) : " ",
3915 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
3916 ? _("[Not edited]") : "",
3917 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
3918 ? new_file_message() : "",
3919 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", curbuf->b_p_ro
3920 ? (shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")) : "",
3921 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro)
3922 ? " " : "");
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (curbuf->b_ml.ml_flags & ML_EMPTY)
John Marriottec032de2025-04-10 21:34:19 +02003925 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3926 IOSIZE - bufferlen, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003928 // Current line and column are already on the screen -- webb
John Marriottec032de2025-04-10 21:34:19 +02003929 bufferlen += vim_snprintf_safelen(
3930 buffer + bufferlen,
3931 IOSIZE - bufferlen,
3932 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--", curbuf->b_ml.ml_line_count),
3933 (long)curbuf->b_ml.ml_line_count,
3934 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 else
3936 {
John Marriottec032de2025-04-10 21:34:19 +02003937 bufferlen += vim_snprintf_safelen(
3938 buffer + bufferlen,
3939 IOSIZE - bufferlen,
3940 _("line %ld of %ld --%d%%-- col "),
3941 (long)curwin->w_cursor.lnum,
3942 (long)curbuf->b_ml.ml_line_count,
3943 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 validate_virtcol();
John Marriottec032de2025-04-10 21:34:19 +02003946 bufferlen += col_print((char_u *)buffer + bufferlen, IOSIZE - bufferlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3948 }
3949
John Marriottec032de2025-04-10 21:34:19 +02003950 (void)append_arg_number(curwin, (char_u *)buffer + bufferlen,
3951 IOSIZE - bufferlen, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 if (dont_truncate)
3954 {
John Marriottec032de2025-04-10 21:34:19 +02003955 int n;
3956
Bram Moolenaarc667da52019-11-30 20:52:27 +01003957 // Temporarily set msg_scroll to avoid the message being truncated.
3958 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 msg_start();
3960 n = msg_scroll;
3961 msg_scroll = TRUE;
3962 msg(buffer);
3963 msg_scroll = n;
3964 }
3965 else
3966 {
John Marriottec032de2025-04-10 21:34:19 +02003967 char *p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003969 // Need to repeat the message after redrawing when:
3970 // - When restart_edit is set (otherwise there will be a delay
3971 // before redrawing).
3972 // - When the screen was scrolled but there is no wait-return
3973 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003974 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976
3977 vim_free(buffer);
3978}
3979
John Marriotta21240b2025-01-08 20:10:59 +01003980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003981col_print(
3982 char_u *buf,
3983 size_t buflen,
3984 int col,
3985 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 if (col == vcol)
John Marriottec032de2025-04-10 21:34:19 +02003988 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d", col);
John Marriotta21240b2025-01-08 20:10:59 +01003989
John Marriottec032de2025-04-10 21:34:19 +02003990 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991}
3992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993static char_u *lasttitle = NULL;
3994static char_u *lasticon = NULL;
3995
Bram Moolenaar84a93082018-06-16 22:58:15 +02003996/*
3997 * Put the file name in the title bar and icon of the window.
3998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004000maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
Bram Moolenaar84a93082018-06-16 22:58:15 +02004002 char_u *title_str = NULL;
4003 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int mustset;
4005 char_u buf[IOSIZE];
John Marriottec032de2025-04-10 21:34:19 +02004006 size_t buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 if (!redrawing())
4009 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004010 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 need_maketitle = TRUE;
4012 return;
4013 }
4014
4015 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02004016 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004017 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019 if (p_title)
4020 {
John Marriottec032de2025-04-10 21:34:19 +02004021 int maxlen = 0;
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (p_titlelen > 0)
4024 {
4025 maxlen = p_titlelen * Columns / 100;
4026 if (maxlen < 10)
4027 maxlen = 10;
4028 }
4029
Bram Moolenaar84a93082018-06-16 22:58:15 +02004030 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 if (*p_titlestring != NUL)
4032 {
4033#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004034 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004035 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4036 (char_u *)"titlestring", 0,
4037 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 else
4039#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004040 title_str = p_titlestring;
John Marriottec032de2025-04-10 21:34:19 +02004041 buflen = STRLEN(title_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043 else
4044 {
John Marriottec032de2025-04-10 21:34:19 +02004045 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
John Marriottec032de2025-04-10 21:34:19 +02004047 // format: "<filename> [flags] <(path)> [argument info] <- servername>"
4048 // example:
4049 // buffer.c + (/home/vim/src) (1 of 2) - VIM
4050
4051 // reserve some space for different parts of the title.
4052 // use sizeof() to introduce 'size_t' so we don't have to
4053 // cast sizes to it.
4054#define SPACE_FOR_FNAME (sizeof(buf) - 100)
4055#define SPACE_FOR_DIR (sizeof(buf) - 20)
4056#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // at least room for " - VIM"
4057
4058 // file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (curbuf->b_fname == NULL)
John Marriottec032de2025-04-10 21:34:19 +02004060 buflen = vim_snprintf_safelen((char *)buf,
4061 SPACE_FOR_FNAME, "%s", _("[No Name]"));
Bram Moolenaar21554412017-07-24 21:44:43 +02004062#ifdef FEAT_TERMINAL
4063 else if (curbuf->b_term != NULL)
John Marriottec032de2025-04-10 21:34:19 +02004064 buflen = vim_snprintf_safelen((char *)buf,
4065 SPACE_FOR_FNAME, "%s",
4066 term_get_status_text(curbuf->b_term));
Bram Moolenaar21554412017-07-24 21:44:43 +02004067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else
4069 {
John Marriottec032de2025-04-10 21:34:19 +02004070 buflen = vim_snprintf_safelen((char *)buf,
4071 SPACE_FOR_FNAME, "%s",
4072 ((p = transstr(gettail(curbuf->b_fname))) != NULL)
4073 ? p
4074 : (char_u *)"");
4075 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077
John Marriottec032de2025-04-10 21:34:19 +02004078 // flags
Bram Moolenaar21554412017-07-24 21:44:43 +02004079#ifdef FEAT_TERMINAL
4080 if (curbuf->b_term == NULL)
4081#endif
John Marriottec032de2025-04-10 21:34:19 +02004082 {
Bram Moolenaar21554412017-07-24 21:44:43 +02004083 switch (bufIsChanged(curbuf)
4084 + (curbuf->b_p_ro * 2)
4085 + (!curbuf->b_p_ma * 4))
4086 {
John Marriottec032de2025-04-10 21:34:19 +02004087 case 1:
4088 // file was modified
4089 buflen += vim_snprintf_safelen(
4090 (char *)buf + buflen,
4091 sizeof(buf) - buflen, " +");
4092 break;
4093 case 2:
4094 // file is readonly
4095 buflen += vim_snprintf_safelen(
4096 (char *)buf + buflen,
4097 sizeof(buf) - buflen, " =");
4098 break;
4099 case 3:
4100 // file was modified and is readonly
4101 buflen += vim_snprintf_safelen(
4102 (char *)buf + buflen,
4103 sizeof(buf) - buflen, " =+");
4104 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004105 case 4:
John Marriottec032de2025-04-10 21:34:19 +02004106 case 6:
4107 // file cannot be modified
4108 buflen += vim_snprintf_safelen(
4109 (char *)buf + buflen,
4110 sizeof(buf) - buflen, " -");
4111 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004112 case 5:
John Marriottec032de2025-04-10 21:34:19 +02004113 case 7:
4114 // file cannot be modified but was modified
4115 buflen += vim_snprintf_safelen(
4116 (char *)buf + buflen,
4117 sizeof(buf) - buflen, " -+");
4118 break;
4119 default:
4120 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004121 }
John Marriottec032de2025-04-10 21:34:19 +02004122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
John Marriottec032de2025-04-10 21:34:19 +02004124 // path (surrounded by '()')
Bram Moolenaar21554412017-07-24 21:44:43 +02004125 if (curbuf->b_fname != NULL
4126#ifdef FEAT_TERMINAL
4127 && curbuf->b_term == NULL
4128#endif
4129 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004131 // Get path of file, replace home dir with ~
John Marriottec032de2025-04-10 21:34:19 +02004132 buflen += vim_snprintf_safelen((char *)buf + buflen,
4133 sizeof(buf) - buflen, " (");
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 home_replace(curbuf, curbuf->b_ffname,
John Marriottec032de2025-04-10 21:34:19 +02004136 buf + buflen, (int)(SPACE_FOR_DIR - buflen), TRUE);
4137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004139 // avoid "c:/name" to be reduced to "c"
John Marriottec032de2025-04-10 21:34:19 +02004140 if (SAFE_isalpha(buf[buflen]) && buf[buflen + 1] == ':')
4141 buflen += 2; // step over "c:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#endif
John Marriottec032de2025-04-10 21:34:19 +02004143
4144 // determine if we have a help or normal buffer
4145 p = gettail_sep(buf + buflen);
4146 if (p == buf + buflen)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004147 {
John Marriottec032de2025-04-10 21:34:19 +02004148 // help buffer
4149 buflen += vim_snprintf_safelen((char *)buf + buflen,
4150 SPACE_FOR_DIR - buflen, "%s)", _("help"));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
Bram Moolenaar2c666692012-09-05 13:30:40 +02004153 {
John Marriottec032de2025-04-10 21:34:19 +02004154 // normal buffer
4155
4156 // Translate unprintable chars and concatenate. Keep some
4157 // room for the server name. When there is no room (very long
4158 // file name) use (...).
4159 if (buflen < SPACE_FOR_DIR)
John Marriott7fb90812025-04-02 20:32:35 +02004160 {
John Marriottec032de2025-04-10 21:34:19 +02004161 // remove the file name
4162 *p = NUL;
4163
4164 buflen += vim_snprintf_safelen((char *)buf + buflen,
4165 SPACE_FOR_DIR - buflen, "%s)",
4166 ((p = transstr(buf + buflen)) != NULL)
4167 ? p
4168 : (char_u *)"");
John Marriott7fb90812025-04-02 20:32:35 +02004169 vim_free(p);
4170 }
John Marriottec032de2025-04-10 21:34:19 +02004171 else
4172 buflen += vim_snprintf_safelen((char *)buf + buflen,
4173 SPACE_FOR_ARGNR - buflen, "...)");
Bram Moolenaar2c666692012-09-05 13:30:40 +02004174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176
John Marriottec032de2025-04-10 21:34:19 +02004177 // argument info
4178 buflen += append_arg_number(curwin, buf + buflen,
4179 SPACE_FOR_ARGNR - buflen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
John Marriottec032de2025-04-10 21:34:19 +02004181 // servername
4182 buflen += vim_snprintf_safelen((char *)buf + buflen,
4183 sizeof(buf) - buflen, " - %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#if defined(FEAT_CLIENTSERVER)
John Marriottec032de2025-04-10 21:34:19 +02004185 (serverName != NULL)
4186 ? serverName :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#endif
John Marriottec032de2025-04-10 21:34:19 +02004188 (char_u *)"VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
4190 if (maxlen > 0)
4191 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004192 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004193 if (vim_strsize(buf) > maxlen)
John Marriottec032de2025-04-10 21:34:19 +02004194 trunc_string(buf, buf, maxlen, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
4197 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004198 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
4200 if (p_icon)
4201 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004202 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (*p_iconstring != NUL)
4204 {
4205#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004206 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004207 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4208 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 else
4210#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004211 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213 else
4214 {
John Marriottec032de2025-04-10 21:34:19 +02004215 char_u *name;
4216 int namelen;
4217
4218 name = buf_spname(curbuf);
4219 if (name == NULL)
4220 name = gettail(curbuf->b_ffname);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004221 // Truncate name at 100 bytes.
John Marriottec032de2025-04-10 21:34:19 +02004222 namelen = (int)STRLEN(name);
4223 if (namelen > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
John Marriottec032de2025-04-10 21:34:19 +02004225 namelen -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (has_mbyte)
John Marriottec032de2025-04-10 21:34:19 +02004227 namelen += (*mb_tail_off)(name, name + namelen) + 1;
4228 name += namelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
John Marriottec032de2025-04-10 21:34:19 +02004230 STRCPY(buf, name);
4231 trans_characters(buf, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 }
4234
Bram Moolenaar84a93082018-06-16 22:58:15 +02004235 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
4237 if (mustset)
4238 resettitle();
4239}
4240
4241/*
4242 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4243 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004244 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 */
4246 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004247value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
4249 if ((str == NULL) != (*last == NULL)
4250 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4251 {
4252 vim_free(*last);
4253 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004256 mch_restore_title(
4257 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004262 return TRUE;
4263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 return FALSE;
4266}
4267
4268/*
4269 * Put current window title back (used after calling a shell)
4270 */
4271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004272resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
4274 mch_settitle(lasttitle, lasticon);
4275}
Bram Moolenaarea408852005-06-25 22:49:46 +00004276
4277# if defined(EXITFREE) || defined(PROTO)
4278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004279free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004280{
4281 vim_free(lasttitle);
4282 vim_free(lasticon);
4283}
4284# endif
4285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004287#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004288
4289/*
4290 * Used for building in the status line.
4291 */
4292typedef struct
4293{
4294 char_u *stl_start;
4295 int stl_minwid;
4296 int stl_maxwid;
4297 enum {
4298 Normal,
4299 Empty,
4300 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004301 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004302 Highlight,
4303 TabPage,
4304 Trunc
4305 } stl_type;
4306} stl_item_T;
4307
4308static size_t stl_items_len = 20; // Initial value, grows as needed.
4309static stl_item_T *stl_items = NULL;
4310static int *stl_groupitem = NULL;
4311static stl_hlrec_T *stl_hltab = NULL;
4312static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004313static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004316 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 * Return length of string in screen cells.
4318 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004319 * Normally works for window "wp", except when working for 'tabline' then it
4320 * is "curwin".
4321 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004323 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4325 *
4326 * If maxwidth is not zero, the string will be filled at any middle marker
4327 * or truncated if too long, fillchar is used for all whitespace.
4328 */
4329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004330build_stl_str_hl(
4331 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004332 char_u *out, // buffer to write into != NameBuff
4333 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004334 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004335 char_u *opt_name, // option name corresponding to "fmt"
4336 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004337 int fillchar,
4338 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004339 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4340 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
Bram Moolenaar10772302019-01-20 18:25:54 +01004342 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004343 colnr_T len;
John Marriottec032de2025-04-10 21:34:19 +02004344 size_t outputlen; // length of out[] used (excluding the NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 char_u *p;
4346 char_u *s;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004347 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004349 int use_sandbox;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004350 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#endif
4352 int empty_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 long l;
4354 long n;
4355 int prevchar_isflag;
4356 int prevchar_isitem;
4357 int itemisflag;
4358 int fillable;
4359 char_u *str;
4360 long num;
4361 int width;
4362 int itemcnt;
4363 int curitem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004365#ifdef FEAT_EVAL
4366 int evaldepth;
4367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 int minwid;
4369 int maxwid;
4370 int zeropad;
4371 char_u base;
4372 char_u opt;
4373#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004374 char_u buf_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004375 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004376 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004377 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004378 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004379 // TODO: find out why using called_emsg_before makes tests fail, does it
4380 // matter?
4381 // int called_emsg_before = called_emsg;
4382 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004383
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004384 // When inside update_screen() we do not want redrawing a statusline,
4385 // ruler, title, etc. to trigger another redraw, it may cause an endless
4386 // loop.
4387 if (updating_screen)
4388 redraw_not_allowed = TRUE;
4389
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004390 if (stl_items == NULL)
4391 {
4392 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4393 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004394
4395 // Allocate one more, because the last element is used to indicate the
4396 // end of the list.
4397 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4398 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004399
4400 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004401 }
4402
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004403#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004404 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4405 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4406
4407 // When the format starts with "%!" then evaluate it as an expression and
4408 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004409 if (fmt[0] == '%' && fmt[1] == '!')
4410 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004411 typval_T tv;
4412
4413 tv.v_type = VAR_NUMBER;
4414 tv.vval.v_number = wp->w_id;
4415 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4416
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004417 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004418 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004419 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004420
4421 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004422 }
4423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 if (fillchar == 0)
4426 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004427
Bram Moolenaar10772302019-01-20 18:25:54 +01004428 // The cursor in windows other than the current one isn't always
4429 // up-to-date, esp. because of autocommands and timers.
4430 lnum = wp->w_cursor.lnum;
4431 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4432 {
4433 lnum = wp->w_buffer->b_ml.ml_line_count;
4434 wp->w_cursor.lnum = lnum;
4435 }
4436
4437 // Get line & check if empty (cursorpos will show "0-1"). Note that
4438 // p will become invalid when getting another buffer line.
4439 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004440 empty_line = (*p == NUL);
4441
Bram Moolenaar10772302019-01-20 18:25:54 +01004442 // Get the byte value now, in case we need it below. This is more efficient
4443 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004444 len = ml_get_buf_len(wp->w_buffer, lnum);
4445 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004446 {
4447 // Line may have changed since checking the cursor column, or the lnum
4448 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004449 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004450 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004451 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004452 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004453 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004454 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004457#ifdef FEAT_EVAL
4458 evaldepth = 0;
4459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 p = out;
4461 curitem = 0;
4462 prevchar_isflag = TRUE;
4463 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004464 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004466 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004467 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004468 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004469
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004470 stl_item_T *new_items =
4471 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004472 if (new_items == NULL)
4473 break;
4474 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004475
4476 int *new_groupitem =
4477 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004478 if (new_groupitem == NULL)
4479 break;
4480 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004481
4482 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004483 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004484 if (new_hlrec == NULL)
4485 break;
4486 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004487 new_hlrec = vim_realloc(stl_tabtab,
4488 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004489 if (new_hlrec == NULL)
4490 break;
4491 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004492
4493 int *new_separator_locs = vim_realloc(stl_separator_locations,
4494 sizeof(int) * new_len);
4495 if (new_separator_locs == NULL)
4496 break;
John Marriottec032de2025-04-10 21:34:19 +02004497 stl_separator_locations = new_separator_locs;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004498
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004499 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004500 }
4501
zeertzjq122dea72022-07-27 15:48:45 +01004502 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 prevchar_isflag = prevchar_isitem = FALSE;
4504
4505 /*
4506 * Handle up to the next '%' or the end.
4507 */
4508 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4509 *p++ = *s++;
4510 if (*s == NUL || p + 1 >= out + outlen)
4511 break;
4512
4513 /*
4514 * Handle one '%' item.
4515 */
4516 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004517 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (*s == '%')
4520 {
4521 if (p + 1 >= out + outlen)
4522 break;
4523 *p++ = *s++;
4524 prevchar_isflag = prevchar_isitem = FALSE;
4525 continue;
4526 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004527 // STL_SEPARATE: Separation between items, filled with white space.
4528 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 s++;
4531 if (groupdepth > 0)
4532 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004533 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004534 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 continue;
4536 }
4537 if (*s == STL_TRUNCMARK)
4538 {
4539 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004540 stl_items[curitem].stl_type = Trunc;
4541 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 continue;
4543 }
4544 if (*s == ')')
4545 {
John Marriottec032de2025-04-10 21:34:19 +02004546 char_u *t;
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 s++;
4549 if (groupdepth < 1)
4550 continue;
4551 groupdepth--;
4552
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004553 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 *p = NUL;
4555 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004556 if (curitem > stl_groupitem[groupdepth] + 1
4557 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
John Marriottec032de2025-04-10 21:34:19 +02004559 int group_start_userhl = 0;
4560 int group_end_userhl = 0;
4561
Bram Moolenaarc667da52019-11-30 20:52:27 +01004562 // remove group if all items are empty and highlight group
4563 // doesn't change
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004564 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004565 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004566 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004567 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004568 group_start_userhl = group_end_userhl =
4569 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004570 break;
4571 }
4572 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004573 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004574 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004575 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004577 if (stl_items[n].stl_type == Highlight)
4578 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004579 }
4580 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004582 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 p = t;
4584 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004585 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004586 {
4587 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004588 if (stl_items[n].stl_type == Highlight)
4589 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004590 // adjust the start position of TabPage to the next
4591 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004592 if (stl_items[n].stl_type == TabPage)
4593 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004597 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004599 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (has_mbyte)
4601 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004602 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004604 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004607 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 }
4610 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004611 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4612 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
4614 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004615 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004617
4618 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004619 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004620 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaarc667da52019-11-30 20:52:27 +01004622 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004623 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
LemonBoy57ff5262022-05-09 21:03:47 +01004625 // Minus one for the leading '<' added above.
4626 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004627 if (stl_items[l].stl_start < t)
4628 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004631 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004633 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004634 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (n < 0)
4636 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004637 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 n = 0 - n;
4639 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004640 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 else
4643 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004644 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004645 l = (n - l) * MB_CHAR2LEN(fillchar);
4646 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004648 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004650 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4651 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004653 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655 }
4656 continue;
4657 }
4658 minwid = 0;
4659 maxwid = 9999;
4660 zeropad = FALSE;
4661 l = 1;
4662 if (*s == '0')
4663 {
4664 s++;
4665 zeropad = TRUE;
4666 }
4667 if (*s == '-')
4668 {
4669 s++;
4670 l = -1;
4671 }
4672 if (VIM_ISDIGIT(*s))
4673 {
4674 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004675 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 minwid = 0;
4677 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004678 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004680 stl_items[curitem].stl_type = Highlight;
4681 stl_items[curitem].stl_start = p;
4682 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 s++;
4684 curitem++;
4685 continue;
4686 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004687 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4688 {
4689 if (*s == STL_TABCLOSENR)
4690 {
4691 if (minwid == 0)
4692 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004693 // %X ends the close label, go back to the previously
4694 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004695 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004696 if (stl_items[n].stl_type == TabPage
4697 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004698 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004699 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004700 break;
4701 }
4702 }
4703 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004704 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004705 minwid = - minwid;
4706 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004707 stl_items[curitem].stl_type = TabPage;
4708 stl_items[curitem].stl_start = p;
4709 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004710 s++;
4711 curitem++;
4712 continue;
4713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (*s == '.')
4715 {
4716 s++;
4717 if (VIM_ISDIGIT(*s))
4718 {
4719 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004720 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 maxwid = 50;
4722 }
4723 }
4724 minwid = (minwid > 50 ? 50 : minwid) * l;
4725 if (*s == '(')
4726 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004727 stl_groupitem[groupdepth++] = curitem;
4728 stl_items[curitem].stl_type = Group;
4729 stl_items[curitem].stl_start = p;
4730 stl_items[curitem].stl_minwid = minwid;
4731 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 s++;
4733 curitem++;
4734 continue;
4735 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004736#ifdef FEAT_EVAL
4737 // Denotes end of expanded %{} block
4738 if (*s == '}' && evaldepth > 0)
4739 {
4740 s++;
4741 evaldepth--;
4742 continue;
4743 }
4744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (vim_strchr(STL_ALL, *s) == NULL)
4746 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004747 if (*s == NUL) // can happen with "%0"
4748 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 s++;
4750 continue;
4751 }
4752 opt = *s++;
4753
Bram Moolenaarc667da52019-11-30 20:52:27 +01004754 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 base = 'D';
4756 itemisflag = FALSE;
4757 fillable = TRUE;
4758 num = -1;
4759 str = NULL;
4760 switch (opt)
4761 {
4762 case STL_FILEPATH:
4763 case STL_FULLPATH:
4764 case STL_FILENAME:
John Marriottec032de2025-04-10 21:34:19 +02004765 {
4766 char_u *name;
4767
Bram Moolenaarc667da52019-11-30 20:52:27 +01004768 fillable = FALSE; // don't change ' ' to fillchar
John Marriottec032de2025-04-10 21:34:19 +02004769 name = buf_spname(wp->w_buffer);
4770 if (name != NULL)
4771 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 else
4773 {
John Marriottec032de2025-04-10 21:34:19 +02004774 char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004775 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4777 }
4778 trans_characters(NameBuff, MAXPATHL);
4779 if (opt != STL_FILENAME)
4780 str = NameBuff;
4781 else
4782 str = gettail(NameBuff);
4783 break;
John Marriottec032de2025-04-10 21:34:19 +02004784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaarc667da52019-11-30 20:52:27 +01004786 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004787 {
4788#ifdef FEAT_EVAL
John Marriottec032de2025-04-10 21:34:19 +02004789 char_u *block_start = s - 1;
shadmansaleh30e3de22021-05-15 17:23:28 +02004790#endif
John Marriottec032de2025-04-10 21:34:19 +02004791 int reevaluate = (*s == '%');
4792 char_u *t;
4793 buf_T *save_curbuf;
4794 win_T *save_curwin;
shadmansaleh30e3de22021-05-15 17:23:28 +02004795
4796 if (reevaluate)
4797 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 itemisflag = TRUE;
4799 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004800 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4801 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004803 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 break;
4805 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004806 if (reevaluate)
John Marriottec032de2025-04-10 21:34:19 +02004807 p[-1] = NUL; // remove the % at the end of %{% expr %}
shadmansaleh30e3de22021-05-15 17:23:28 +02004808 else
John Marriottec032de2025-04-10 21:34:19 +02004809 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004812 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4813 "%d", curbuf->b_fnum);
4814 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
John Marriottec032de2025-04-10 21:34:19 +02004815 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "%d", curwin->w_id);
4816 set_internal_string_var((char_u *)"g:actual_curwin", buf_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004818 save_curbuf = curbuf;
4819 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004820 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 curwin = wp;
4822 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004823 // Visual mode is only valid in the current window.
4824 if (curwin != save_curwin)
4825 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004827 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004829 curwin = save_curwin;
4830 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004831 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004832 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004833 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
John Marriottec032de2025-04-10 21:34:19 +02004835 if (str != NULL && *str != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
4837 if (*skipdigits(str) == NUL)
4838 {
4839 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004840 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 itemisflag = FALSE;
4842 }
4843 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004844
4845 // If the output of the expression needs to be evaluated
4846 // replace the %{} block with the result of evaluation
John Marriottec032de2025-04-10 21:34:19 +02004847 if (reevaluate && str != NULL && *str != NUL
shadmansaleh30e3de22021-05-15 17:23:28 +02004848 && strchr((const char *)str, '%') != NULL
4849 && evaldepth < MAX_STL_EVAL_DEPTH)
4850 {
4851 size_t parsed_usefmt = (size_t)(block_start - usefmt);
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004852 size_t str_length = strlen((const char *)str);
4853 size_t fmt_length = strlen((const char *)s);
4854 size_t new_fmt_len = parsed_usefmt
4855 + str_length + fmt_length + 3;
4856 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004857
John Marriott7fb90812025-04-02 20:32:35 +02004858 if (new_fmt != NULL)
4859 {
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004860 char_u *new_fmt_p = new_fmt;
4861
4862 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4863 + parsed_usefmt;
4864 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4865 + str_length;
4866 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4867 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4868 + fmt_length;
4869 *new_fmt_p = 0;
4870 new_fmt_p = NULL;
John Marriott7fb90812025-04-02 20:32:35 +02004871
4872 if (usefmt != fmt)
4873 vim_free(usefmt);
4874 VIM_CLEAR(str);
4875 usefmt = new_fmt;
4876 s = usefmt + parsed_usefmt;
4877 evaldepth++;
4878 continue;
4879 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#endif
4882 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 case STL_LINE:
4885 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4886 ? 0L : (long)(wp->w_cursor.lnum);
4887 break;
4888
4889 case STL_NUMLINES:
4890 num = wp->w_buffer->b_ml.ml_line_count;
4891 break;
4892
4893 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004894 num = (State & MODE_INSERT) == 0 && empty_line
4895 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 break;
4897
4898 case STL_VIRTCOL:
4899 case STL_VIRTCOL_ALT:
John Marriottec032de2025-04-10 21:34:19 +02004900 {
4901 colnr_T virtcol = wp->w_virtcol + 1;
4902
Bram Moolenaarc667da52019-11-30 20:52:27 +01004903 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004905 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4906 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 break;
4908 num = (long)virtcol;
4909 break;
John Marriottec032de2025-04-10 21:34:19 +02004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
4912 case STL_PERCENTAGE:
John Marriottec032de2025-04-10 21:34:19 +02004913 num = calc_percentage((long)wp->w_cursor.lnum, (long)wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 break;
4915
4916 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004917 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004918 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 break;
4920
Luuk van Baalba936f62022-12-15 13:15:39 +00004921 case STL_SHOWCMD:
4922 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4923 str = showcmd_buf;
4924 break;
4925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 case STL_ARGLISTSTAT:
4927 fillable = FALSE;
John Marriottec032de2025-04-10 21:34:19 +02004928 buf_tmp[0] = NUL;
4929 if (append_arg_number(wp, buf_tmp, sizeof(buf_tmp), FALSE) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004930 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 break;
4932
4933 case STL_KEYMAP:
4934 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004935 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004936 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 break;
4938 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004939#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004940 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#else
4942 num = 0;
4943#endif
4944 break;
4945
4946 case STL_BUFNO:
4947 num = wp->w_buffer->b_fnum;
4948 break;
4949
4950 case STL_OFFSET_X:
4951 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004952 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 case STL_OFFSET:
4954#ifdef FEAT_BYTEOFF
4955 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004956 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4957 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4958 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#endif
4960 break;
4961
4962 case STL_BYTEVAL_X:
4963 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004964 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004966 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (num == NL)
4968 num = 0;
4969 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4970 num = NL;
4971 break;
4972
4973 case STL_ROFLAG:
4974 case STL_ROFLAG_ALT:
4975 itemisflag = TRUE;
4976 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004977 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 break;
4979
4980 case STL_HELPFLAG:
4981 case STL_HELPFLAG_ALT:
4982 itemisflag = TRUE;
4983 if (wp->w_buffer->b_help)
4984 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004985 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 break;
4987
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 case STL_FILETYPE:
4989 if (*wp->w_buffer->b_p_ft != NUL
4990 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4991 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004992 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004993 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004994 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 break;
4997
4998 case STL_FILETYPE_ALT:
4999 itemisflag = TRUE;
5000 if (*wp->w_buffer->b_p_ft != NUL
5001 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
5002 {
John Marriottec032de2025-04-10 21:34:19 +02005003 char_u *t;
5004
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005005 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005006 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005007 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005009 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
Bram Moolenaar4033c552017-09-16 20:54:51 +02005013#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 case STL_PREVIEWFLAG:
5015 case STL_PREVIEWFLAG_ALT:
5016 itemisflag = TRUE;
5017 if (wp->w_p_pvw)
5018 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
5019 : _("[Preview]"));
5020 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005021
5022 case STL_QUICKFIX:
5023 if (bt_quickfix(wp->w_buffer))
5024 str = (char_u *)(wp->w_llist_ref
5025 ? _(msg_loclist)
5026 : _(msg_qflist));
5027 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028#endif
5029
5030 case STL_MODIFIED:
5031 case STL_MODIFIED_ALT:
5032 itemisflag = TRUE;
5033 switch ((opt == STL_MODIFIED_ALT)
5034 + bufIsChanged(wp->w_buffer) * 2
5035 + (!wp->w_buffer->b_p_ma) * 4)
5036 {
5037 case 2: str = (char_u *)"[+]"; break;
5038 case 3: str = (char_u *)",+"; break;
5039 case 4: str = (char_u *)"[-]"; break;
5040 case 5: str = (char_u *)",-"; break;
5041 case 6: str = (char_u *)"[+-]"; break;
5042 case 7: str = (char_u *)",+-"; break;
5043 }
5044 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005045
5046 case STL_HIGHLIGHT:
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005047 {
John Marriottec032de2025-04-10 21:34:19 +02005048 char_u *t = s;
5049
5050 while (*s != '#' && *s != NUL)
5051 ++s;
5052 if (*s == '#')
5053 {
5054 stl_items[curitem].stl_type = Highlight;
5055 stl_items[curitem].stl_start = p;
5056 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
5057 curitem++;
5058 }
5059 if (*s != NUL)
5060 ++s;
5061 continue;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005065 stl_items[curitem].stl_start = p;
5066 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (str != NULL && *str)
5068 {
John Marriottec032de2025-04-10 21:34:19 +02005069 char_u *t = str;
5070
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (itemisflag)
5072 {
5073 if ((t[0] && t[1])
5074 && ((!prevchar_isitem && *t == ',')
5075 || (prevchar_isflag && *t == ' ')))
5076 t++;
5077 prevchar_isflag = TRUE;
5078 }
5079 l = vim_strsize(t);
5080 if (l > 0)
5081 prevchar_isitem = TRUE;
5082 if (l > maxwid)
5083 {
5084 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (has_mbyte)
5086 {
5087 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005088 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 l -= byte2cells(*t++);
5092 if (p + 1 >= out + outlen)
5093 break;
5094 *p++ = '<';
5095 }
5096 if (minwid > 0)
5097 {
5098 for (; l < minwid && p + 1 < out + outlen; l++)
5099 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005100 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5102 *p++ = ' ';
5103 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005104 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106 minwid = 0;
5107 }
5108 else
5109 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005110 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005112 // Change a space by fillchar, unless fillchar is '-' and a
5113 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005114 if (fillable && *t == ' '
5115 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5116 MB_CHAR2BYTES(fillchar, p);
5117 else
5118 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005121 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123 else if (num >= 0)
5124 {
John Marriottec032de2025-04-10 21:34:19 +02005125 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5126 char_u nstr[20];
5127 char_u *t = nstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005130 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 prevchar_isitem = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (opt == STL_VIRTCOL_ALT)
5133 {
5134 *t++ = '-';
5135 minwid--;
5136 }
5137 *t++ = '%';
5138 if (zeropad)
5139 *t++ = '0';
5140 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005141 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
John Marriottec032de2025-04-10 21:34:19 +02005142 *t = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 for (n = num, l = 1; n >= nbase; n /= nbase)
5145 l++;
5146 if (opt == STL_VIRTCOL_ALT)
5147 l++;
John Marriottec032de2025-04-10 21:34:19 +02005148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 if (l > maxwid)
5150 {
5151 l += 2;
5152 n = l - maxwid;
5153 while (l-- > maxwid)
5154 num /= nbase;
5155 *t++ = '>';
5156 *t++ = '%';
5157 *t = t[-3];
John Marriottec032de2025-04-10 21:34:19 +02005158 *++t = NUL;
5159 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5160 (char *)nstr, 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162 else
John Marriottec032de2025-04-10 21:34:19 +02005163 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5164 (char *)nstr, minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005167 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005169 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5170 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5171 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (opt == STL_VIM_EXPR)
5173 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 curitem++;
5175 }
5176 *p = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005177 outputlen = (size_t)(p - out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 itemcnt = curitem;
5179
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005180#ifdef FEAT_EVAL
5181 if (usefmt != fmt)
5182 vim_free(usefmt);
5183#endif
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 width = vim_strsize(out);
5186 if (maxwidth > 0 && width > maxwidth)
5187 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005188 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 l = 0;
5190 if (itemcnt == 0)
5191 s = out;
5192 else
5193 {
5194 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005195 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005197 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005198 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 break;
5200 }
5201 if (l == itemcnt)
5202 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005203 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005204 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 l = 0;
5206 }
5207 }
5208
5209 if (width - vim_strsize(s) >= maxwidth)
5210 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005211 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (has_mbyte)
5213 {
5214 s = out;
5215 width = 0;
5216 for (;;)
5217 {
5218 width += ptr2cells(s);
5219 if (width >= maxwidth)
5220 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005221 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005223 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005225 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 s = out + maxwidth - 1;
5229 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005230 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 break;
5232 itemcnt = l;
5233 *s++ = '>';
John Marriottec032de2025-04-10 21:34:19 +02005234 *s = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236 else
5237 {
John Marriottec032de2025-04-10 21:34:19 +02005238 char_u *end = out + outputlen;
5239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (has_mbyte)
5241 {
5242 n = 0;
5243 while (width >= maxwidth)
5244 {
5245 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005246 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 }
5249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 n = width - maxwidth + 1;
5251 p = s + n;
John Marriottec032de2025-04-10 21:34:19 +02005252 mch_memmove(s + 1, p, (size_t)(end - p) + 1); // +1 for NUL
5253 end -= (size_t)(p - (s + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 *s = '<';
5255
Bram Moolenaarc667da52019-11-30 20:52:27 +01005256 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 for (; l < itemcnt; l++)
5258 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005259 if (stl_items[l].stl_start - n >= s)
5260 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005262 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
zeertzjqd392a742023-07-01 20:24:40 +01005264
5265 // Fill up for half a double-wide character.
5266 while (++width < maxwidth)
5267 {
John Marriottec032de2025-04-10 21:34:19 +02005268 s = end;
zeertzjqd392a742023-07-01 20:24:40 +01005269 MB_CHAR2BYTES(fillchar, s);
5270 *s = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005271 end = s;
zeertzjqd392a742023-07-01 20:24:40 +01005272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 width = maxwidth;
5275 }
John Marriottec032de2025-04-10 21:34:19 +02005276 else if (width < maxwidth && outputlen + maxwidth - width + 1 < outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005278 // Find how many separators there are, which we will use when
5279 // figuring out how many groups there are.
5280 int num_separators = 0;
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005284 if (stl_items[l].stl_type == Separate)
5285 {
5286 // Create an array of the start location for each separator
5287 // mark.
5288 stl_separator_locations[num_separators] = l;
5289 num_separators++;
5290 }
5291 }
5292
5293 // If we have separated groups, then we deal with it now
5294 if (num_separators)
5295 {
5296 int standard_spaces;
5297 int final_spaces;
5298
5299 standard_spaces = (maxwidth - width) / num_separators;
5300 final_spaces = (maxwidth - width) -
5301 standard_spaces * (num_separators - 1);
5302 for (l = 0; l < num_separators; l++)
5303 {
5304 int dislocation = (l == (num_separators - 1)) ?
5305 final_spaces : standard_spaces;
5306 dislocation *= MB_CHAR2LEN(fillchar);
5307 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5308 char_u *seploc = start + dislocation;
5309 STRMOVE(seploc, start);
5310 for (s = start; s < seploc;)
5311 MB_CHAR2BYTES(fillchar, s);
5312
5313 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5314 stl_items[i].stl_start += dislocation;
5315 }
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 width = maxwidth;
5318 }
5319 }
5320
Bram Moolenaarc667da52019-11-30 20:52:27 +01005321 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005322 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005324 *hltab = stl_hltab;
5325 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 for (l = 0; l < itemcnt; l++)
5327 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005328 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005330 sp->start = stl_items[l].stl_start;
5331 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005332 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005335 sp->start = NULL;
5336 sp->userhl = 0;
5337 }
5338
Bram Moolenaarc667da52019-11-30 20:52:27 +01005339 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005340 if (tabtab != NULL)
5341 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005342 *tabtab = stl_tabtab;
5343 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005344 for (l = 0; l < itemcnt; l++)
5345 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005346 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005347 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005348 sp->start = stl_items[l].stl_start;
5349 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005350 sp++;
5351 }
5352 }
5353 sp->start = NULL;
5354 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 }
5356
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005357 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005358
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005359 // A user function may reset KeyTyped, restore it.
5360 KeyTyped = save_KeyTyped;
5361
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005362 // Check for an error. If there is one the display will be messed up and
5363 // might loop redrawing. Avoid that by making the corresponding option
5364 // empty.
5365 // TODO: find out why using called_emsg_before makes tests fail, does it
5366 // matter?
5367 // if (called_emsg > called_emsg_before)
5368 if (did_emsg > did_emsg_before)
5369 set_string_option_direct(opt_name, -1, (char_u *)"",
5370 OPT_FREE | opt_scope, SID_ERROR);
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 return width;
5373}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005374#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376/*
John Marriottec032de2025-04-10 21:34:19 +02005377 * Get relative cursor position in window into "buf[]", in the localized
Emir SARI971cd2b2023-04-29 12:09:53 +01005378 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 */
John Marriotta21240b2025-01-08 20:10:59 +01005380 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005381get_rel_pos(
5382 win_T *wp,
5383 char_u *buf,
5384 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005386 long above; // number of lines above window
5387 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaarc667da52019-11-30 20:52:27 +01005389 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005390 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 above = wp->w_topline - 1;
5392#ifdef FEAT_DIFF
5393 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005394 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005395 above = 0; // All buffer lines are displayed and there is an
5396 // indication of filler lines, that can be considered
5397 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398#endif
5399 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5400 if (below <= 0)
John Marriottec032de2025-04-10 21:34:19 +02005401 return (int)vim_snprintf_safelen((char *)buf, buflen,
5402 "%s", (above == 0) ? _("All") : _("Bot"));
Emir SARI971cd2b2023-04-29 12:09:53 +01005403
John Marriottec032de2025-04-10 21:34:19 +02005404 if (above <= 0)
5405 return (int)vim_snprintf_safelen((char *)buf, buflen,
5406 "%s", _("Top"));
John Marriotta21240b2025-01-08 20:10:59 +01005407
John Marriottec032de2025-04-10 21:34:19 +02005408 // localized percentage value
5409 return (int)vim_snprintf_safelen((char *)buf, buflen,
5410 _("%2d%%"), calc_percentage(above, above + below));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413/*
John Marriottec032de2025-04-10 21:34:19 +02005414 * Append (file 2 of 8) to "buf[]", if editing more than one file.
5415 * Return the number of characters appended.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005418append_arg_number(
5419 win_T *wp,
5420 char_u *buf,
John Marriottec032de2025-04-10 21:34:19 +02005421 size_t buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005422 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005424 if (ARGCOUNT <= 1) // nothing to do
John Marriottec032de2025-04-10 21:34:19 +02005425 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
Bram Moolenaara8490a42023-05-23 18:00:58 +01005427 char *msg;
5428 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005430 case 0: msg = _(" (%d of %d)"); break;
5431 case 1: msg = _(" ((%d) of %d)"); break;
5432 case 2: msg = _(" (file %d of %d)"); break;
5433 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005435
John Marriottec032de2025-04-10 21:34:19 +02005436 return (int)vim_snprintf_safelen((char *)buf, buflen, msg,
Bram Moolenaara8490a42023-05-23 18:00:58 +01005437 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438}
5439
5440/*
5441 * If fname is not a full path, make it a full path.
5442 * Returns pointer to allocated memory (NULL for failure).
5443 */
5444 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005445fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 /*
5448 * Force expanding the path always for Unix, because symbolic links may
5449 * mess up the full path name, even though it starts with a '/'.
5450 * Also expand when there is ".." in the file name, try to remove it,
5451 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005452 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 * For MS-Windows also expand names like "longna~1" to "longname".
5454 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005455#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return FullName_save(fname, TRUE);
5457#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005458 if (!vim_isAbsName(fname)
5459 || strstr((char *)fname, "..") != NULL
5460 || strstr((char *)fname, "//") != NULL
5461# ifdef BACKSLASH_IN_FILENAME
5462 || strstr((char *)fname, "\\\\") != NULL
5463# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005464# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 )
5468 return FullName_save(fname, FALSE);
5469
5470 fname = vim_strsave(fname);
5471
Bram Moolenaar9b942202007-10-03 12:31:33 +00005472# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005473 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005474 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 return fname;
5478#endif
5479}
5480
5481/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005482 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5483 * "*ffname" becomes a pointer to allocated memory (or NULL).
5484 * When resolving a link both "*sfname" and "*ffname" will point to the same
5485 * allocated memory.
5486 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005487 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005490fname_expand(
5491 buf_T *buf UNUSED,
5492 char_u **ffname,
5493 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005495 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005497 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005499 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501#ifdef FEAT_SHORTCUT
5502 if (!buf->b_p_bin)
5503 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005504 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005506 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005507 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005508 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 vim_free(*ffname);
5511 *ffname = rfname;
5512 *sfname = rfname;
5513 }
5514 }
5515#endif
5516}
5517
5518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 * Open a window for a number of buffers.
5520 */
5521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005522ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 buf_T *buf;
5525 win_T *wp, *wpnext;
5526 int split_ret = OK;
5527 int p_ea_save;
5528 int open_wins = 0;
5529 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005530 int count; // Maximum number of windows to open.
5531 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005532 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005533 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaarc667da52019-11-30 20:52:27 +01005535 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 count = 9999;
5537 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005538 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5540 all = FALSE;
5541 else
5542 all = TRUE;
5543
Pavel Mayorove1121b12023-02-20 14:35:20 +00005544 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5545 // switching to another buffer.
5546 reset_VIsual_and_resel();
5547
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 setpcmark();
5549
5550#ifdef FEAT_GUI
5551 need_mouse_correct = TRUE;
5552#endif
5553
5554 /*
5555 * Close superfluous windows (two windows for the same buffer).
5556 * Also close windows that are not full-width.
5557 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005558 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005559 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005560 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005562 tpnext = curtab->tp_next;
5563 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005565 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005566 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005567 || ((cmdmod.cmod_split & WSP_VERT)
5568 ? wp->w_height + wp->w_status_height < Rows - p_ch
5569 - tabline_height()
5570 : wp->w_width != Columns)
5571 || (had_tab > 0 && wp != firstwin))
5572 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005573 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005574 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005575 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005576 if (win_close(wp, FALSE) == FAIL)
5577 break;
5578 // Just in case an autocommand does something strange with
5579 // windows: start all over...
5580 wpnext = firstwin;
5581 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005582 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005583 }
5584 else
5585 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005587
Bram Moolenaarc667da52019-11-30 20:52:27 +01005588 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005589 if (had_tab == 0 || tpnext == NULL)
5590 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005591 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593
5594 /*
5595 * Go through the buffer list. When a buffer doesn't have a window yet,
5596 * open one. Otherwise move the window to the right position.
5597 * Watch out for autocommands that delete buffers or windows!
5598 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005599 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5604 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005605 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5607 continue;
5608
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005609 if (had_tab != 0)
5610 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005611 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005612 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005613 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005614 else
5615 wp = NULL;
5616 }
5617 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005618 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005619 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005620 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005621 if (wp->w_buffer == buf)
5622 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005623 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005624 if (wp != NULL)
5625 win_move_after(wp, curwin);
5626 }
5627
5628 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005630 bufref_T bufref;
5631
5632 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005633
Bram Moolenaarc667da52019-11-30 20:52:27 +01005634 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005636 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5638 ++open_wins;
5639 p_ea = p_ea_save;
5640 if (split_ret == FAIL)
5641 continue;
5642
Bram Moolenaarc667da52019-11-30 20:52:27 +01005643 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005646 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005648 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 break;
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 if (swap_exists_action == SEA_QUIT)
5653 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005654#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005655 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005656
Bram Moolenaarc667da52019-11-30 20:52:27 +01005657 // Reset the error/interrupt/exception state here so that
5658 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005659 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005660#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005661
Bram Moolenaarc667da52019-11-30 20:52:27 +01005662 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 win_close(curwin, TRUE);
5664 --open_wins;
5665 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005666 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005667
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005668#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005669 // Restore the error/interrupt/exception state if not
5670 // discarded by a new aborting error, interrupt, or uncaught
5671 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005672 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else
5676 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678
5679 ui_breakcheck();
5680 if (got_int)
5681 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005682 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 break;
5684 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005685#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005686 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005687 if (aborting())
5688 break;
5689#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005690 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005691 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005692 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005695 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 /*
5699 * Close superfluous windows.
5700 */
5701 for (wp = lastwin; open_wins > count; )
5702 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005703 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 if (!win_valid(wp))
5706 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005707 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 wp = lastwin;
5709 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005710 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005712 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 --open_wins;
5714 wp = lastwin;
5715 }
5716 else
5717 {
5718 wp = wp->w_prev;
5719 if (wp == NULL)
5720 break;
5721 }
5722 }
5723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005726static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728/*
5729 * do_modelines() - process mode lines for the current file
5730 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005731 * "flags" can be:
5732 * OPT_WINONLY only set options local to window
5733 * OPT_NOWIN don't set options local to window
5734 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * Returns immediately if the "ml" option isn't set.
5736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005740 linenr_T lnum;
5741 int nmlines;
5742 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5745 return;
5746
Bram Moolenaarc667da52019-11-30 20:52:27 +01005747 // Disallow recursive entry here. Can happen when executing a modeline
5748 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (entered)
5750 return;
5751
5752 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005753 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005755 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 nmlines = 0;
5757
Hu Jialun9dcd3492021-08-28 20:42:50 +02005758 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005760 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 nmlines = 0;
5762 --entered;
5763}
5764
Bram Moolenaarc667da52019-11-30 20:52:27 +01005765#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767/*
5768 * chk_modeline() - check a single line for a mode string
5769 * Return FAIL if an error encountered.
5770 */
5771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005772chk_modeline(
5773 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005774 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 char_u *s;
John Marriottec032de2025-04-10 21:34:19 +02005777 char_u *line_end; // point to the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 char_u *e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 int prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 int retval = OK;
ichizok7e5fe382023-04-15 13:17:50 +01005781 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 prev = -1;
John Marriottec032de2025-04-10 21:34:19 +02005784 s = ml_get(lnum);
5785 line_end = s + ml_get_len(lnum);
5786 for (; *s != NUL; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 if (prev == -1 || vim_isspace(prev))
5789 {
5790 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5791 || STRNCMP(s, "vi:", (size_t)3) == 0)
5792 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005793 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005794 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
John Marriottec032de2025-04-10 21:34:19 +02005796 int vers;
5797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5799 e = s + 4;
5800 else
5801 e = s + 3;
5802 vers = getdigits(&e);
5803 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005804 && (s[0] != 'V'
5805 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005807 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 || (VIM_VERSION_100 < vers && s[3] == '<')
5809 || (VIM_VERSION_100 > vers && s[3] == '>')
5810 || (VIM_VERSION_100 == vers && s[3] == '=')))
5811 break;
5812 }
5813 }
5814 prev = *s;
5815 }
5816
5817 if (*s)
5818 {
John Marriottec032de2025-04-10 21:34:19 +02005819 size_t len;
5820 char_u *linecopy; // local copy of any modeline found
5821 int end;
5822
Bram Moolenaarc667da52019-11-30 20:52:27 +01005823 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 ++s;
5825 while (s[-1] != ':');
5826
John Marriottec032de2025-04-10 21:34:19 +02005827 len = (size_t)(line_end - s); // remember the line length
5828 // so we can restore 'line_end'
5829 // after the copy
5830 s = linecopy = vim_strnsave(s, len); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (linecopy == NULL)
5832 return FAIL;
5833
John Marriottec032de2025-04-10 21:34:19 +02005834 line_end = s + len; // restore 'line_end'
5835
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005836 // prepare for emsg()
5837 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005838 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 end = FALSE;
5841 while (end == FALSE)
5842 {
5843 s = skipwhite(s);
5844 if (*s == NUL)
5845 break;
5846
5847 /*
5848 * Find end of set command: ':' or end of line.
5849 * Skip over "\:", replacing it with ":".
5850 */
5851 for (e = s; *e != ':' && *e != NUL; ++e)
5852 if (e[0] == '\\' && e[1] == ':')
John Marriottec032de2025-04-10 21:34:19 +02005853 {
5854 mch_memmove(e, e + 1, (size_t)(line_end - (e + 1)) + 1); // +1 for NUL
5855 --line_end;
5856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 if (*e == NUL)
5858 end = TRUE;
5859
5860 /*
5861 * If there is a "set" command, require a terminating ':' and
5862 * ignore the stuff after the ':'.
5863 * "vi:set opt opt opt: foo" -- foo not interpreted
5864 * "vi:opt opt opt: foo" -- foo interpreted
5865 * Accept "se" for compatibility with Elvis.
5866 */
5867 if (STRNCMP(s, "set ", (size_t)4) == 0
5868 || STRNCMP(s, "se ", (size_t)3) == 0)
5869 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005870 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 break;
5872 end = TRUE;
John Marriottec032de2025-04-10 21:34:19 +02005873 s += (*(s + 2) == ' ') ? 3 : 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005875 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
Bram Moolenaarc667da52019-11-30 20:52:27 +01005877 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005879 int secure_save = secure;
John Marriottec032de2025-04-10 21:34:19 +02005880 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005881
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005882 current_sctx.sc_version = 1;
5883#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005884 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005885 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005886 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005888
Bram Moolenaar5958f952018-11-20 04:25:21 +01005889 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005890 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005891
Bram Moolenaara3227e22006-03-08 21:32:40 +00005892 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005893
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005894 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005895 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005896 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 break;
5898 }
John Marriottec032de2025-04-10 21:34:19 +02005899 s = (e == line_end) ? e : e + 1; // advance to next part
5900 // careful not to go off the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902
ichizok7e5fe382023-04-15 13:17:50 +01005903 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005904 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 vim_free(linecopy);
5906 }
5907 return retval;
5908}
5909
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005910/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005911 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5912 */
5913 int
5914bt_normal(buf_T *buf)
5915{
5916 return buf != NULL && buf->b_p_bt[0] == NUL;
5917}
5918
5919/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005920 * Return TRUE if "buf" is the quickfix buffer.
5921 */
5922 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005923bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005924{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005925#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005926 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005927#else
5928 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005929#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005930}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005931
5932/*
5933 * Return TRUE if "buf" is a terminal buffer.
5934 */
5935 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005936bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005937{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005938#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005939 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005940#else
5941 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005942#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005943}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005944
5945/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005946 * Return TRUE if "buf" is a help buffer.
5947 */
5948 int
5949bt_help(buf_T *buf)
5950{
5951 return buf != NULL && buf->b_help;
5952}
5953
5954/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005955 * Return TRUE if "buf" is a prompt buffer.
5956 */
5957 int
5958bt_prompt(buf_T *buf)
5959{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005960 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5961}
5962
Dominique Pelle748b3082022-01-08 12:41:16 +00005963#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005964/*
5965 * Return TRUE if "buf" is a buffer for a popup window.
5966 */
5967 int
5968bt_popup(buf_T *buf)
5969{
5970 return buf != NULL && buf->b_p_bt != NULL
5971 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005972}
Dominique Pelle748b3082022-01-08 12:41:16 +00005973#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005974
5975/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005976 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005977 * buffer. This means the buffer name may not be a file name, at least not for
5978 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005979 */
5980 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005981bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005982{
5983 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5984 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005985 || buf->b_p_bt[0] == 't'
5986 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005987}
5988
Bram Moolenaarc3126192022-08-26 12:58:17 +01005989/*
5990 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5991 * buffer. This means the buffer is not to be read from a file.
5992 */
5993 static int
5994bt_nofileread(buf_T *buf)
5995{
5996 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5997 || buf->b_p_bt[0] == 't'
5998 || buf->b_p_bt[0] == 'q'
5999 || buf->b_p_bt[0] == 'p');
6000}
6001
Dominique Pelle748b3082022-01-08 12:41:16 +00006002#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006003/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02006004 * Return TRUE if "buf" has 'buftype' set to "nofile".
6005 */
6006 int
6007bt_nofile(buf_T *buf)
6008{
6009 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
6010}
Dominique Pelle748b3082022-01-08 12:41:16 +00006011#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02006012
6013/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006014 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
6015 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006016 */
6017 int
6018bt_dontwrite(buf_T *buf)
6019{
Bram Moolenaarf2732452018-06-03 14:47:35 +02006020 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01006021 || buf->b_p_bt[0] == 't'
6022 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006023}
6024
6025 int
6026bt_dontwrite_msg(buf_T *buf)
6027{
6028 if (bt_dontwrite(buf))
6029 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00006030 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006031 return TRUE;
6032 }
6033 return FALSE;
6034}
6035
6036/*
6037 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
6038 * and 'bufhidden'.
6039 */
6040 int
6041buf_hide(buf_T *buf)
6042{
Bram Moolenaarc667da52019-11-30 20:52:27 +01006043 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006044 switch (buf->b_p_bh[0])
6045 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006046 case 'u': // "unload"
6047 case 'w': // "wipe"
6048 case 'd': return FALSE; // "delete"
6049 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006050 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006051 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054/*
6055 * Return special buffer name.
6056 * Returns NULL when the buffer has a normal file name.
6057 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
Bram Moolenaar4033c552017-09-16 20:54:51 +02006061#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006063 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006064 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006065 * Differentiate between the quickfix and location list buffers using
6066 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006067 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006068 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006069 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006070 else
6071 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02006074
Bram Moolenaarc667da52019-11-30 20:52:27 +01006075 // There is no _file_ when 'buftype' is "nofile", b_sfname
6076 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02006077 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
Bram Moolenaar21554412017-07-24 21:44:43 +02006079#ifdef FEAT_TERMINAL
6080 if (buf->b_term != NULL)
6081 return term_get_status_text(buf->b_term);
6082#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02006083 if (buf->b_fname != NULL)
6084 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01006085 if (buf == cmdwin_buf)
6086 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02006087#ifdef FEAT_JOB_CHANNEL
6088 if (bt_prompt(buf))
6089 return (char_u *)_("[Prompt]");
6090#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006091#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006092 if (bt_popup(buf))
6093 return (char_u *)_("[Popup]");
6094#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006095 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006099 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 return NULL;
6101}
6102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006104 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6105 */
6106 char_u *
6107buf_get_fname(buf_T *buf)
6108{
6109 if (buf->b_fname == NULL)
6110 return (char_u *)_("[No Name]");
6111 return buf->b_fname;
6112}
6113
6114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6116 */
6117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006120 if (on == curbuf->b_p_bl)
6121 return;
6122
6123 curbuf->b_p_bl = on;
6124 if (on)
6125 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6126 else
6127 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128}
6129
6130/*
6131 * Read the file for "buf" again and check if the contents changed.
6132 * Return TRUE if it changed or this could not be checked.
6133 */
6134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 buf_T *newbuf;
6138 int differ = TRUE;
6139 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 exarg_T ea;
6142
Bram Moolenaarc667da52019-11-30 20:52:27 +01006143 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6145 if (newbuf == NULL)
6146 return TRUE;
6147
Bram Moolenaarc667da52019-11-30 20:52:27 +01006148 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (prep_exarg(&ea, buf) == FAIL)
6150 {
6151 wipe_buffer(newbuf, FALSE);
6152 return TRUE;
6153 }
6154
Bram Moolenaare76062c2022-11-28 18:51:43 +00006155 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006157 if (curbuf != newbuf)
6158 {
6159 // Failed to find a window for "newbuf".
6160 wipe_buffer(newbuf, FALSE);
6161 return TRUE;
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006164 // We don't want to trigger autocommands now, they may have nasty
6165 // side-effects like wiping buffers
6166 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006167 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 && readfile(buf->b_ffname, buf->b_fname,
6169 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6170 &ea, READ_NEW | READ_DUMMY) == OK)
6171 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006172 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6174 {
6175 differ = FALSE;
6176 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6177 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6178 {
6179 differ = TRUE;
6180 break;
6181 }
6182 }
6183 }
6184 vim_free(ea.cmd);
6185
Bram Moolenaarc667da52019-11-30 20:52:27 +01006186 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
Bram Moolenaarc667da52019-11-30 20:52:27 +01006189 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 wipe_buffer(newbuf, FALSE);
6191
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006192 unblock_autocmds();
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 return differ;
6195}
6196
6197/*
6198 * Wipe out a buffer and decrement the last buffer number if it was used for
6199 * this buffer. Call this to wipe out a temp buffer that does not contain any
6200 * marks.
6201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203wipe_buffer(
6204 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006205 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 if (buf->b_fnum == top_file_num - 1)
6208 --top_file_num;
6209
Bram Moolenaarc667da52019-11-30 20:52:27 +01006210 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006211 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006212
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006213 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006216 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217}